blob: 62550be526c913b3fadd70911803895b307fbf5e [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) \
Calin Juravle27df7582015-04-17 19:12:31 +01001387 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001388 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001389 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001390 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001391 M(Neg, UnaryOperation) \
1392 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001393 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001394 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001395 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001396 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001397 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001398 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001399 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001400 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001401 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001402 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001403 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001404 M(Return, Instruction) \
1405 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001406 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001407 M(Shl, BinaryOperation) \
1408 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001409 M(StaticFieldGet, Instruction) \
1410 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001411 M(UnresolvedInstanceFieldGet, Instruction) \
1412 M(UnresolvedInstanceFieldSet, Instruction) \
1413 M(UnresolvedStaticFieldGet, Instruction) \
1414 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001415 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001416 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001417 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001418 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001419 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001420 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001421 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001422 M(Xor, BinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001423 M(VecReplicateScalar, VecUnaryOperation) \
Aart Bik0148de42017-09-05 09:25:01 -07001424 M(VecExtractScalar, VecUnaryOperation) \
1425 M(VecReduce, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001426 M(VecCnv, VecUnaryOperation) \
1427 M(VecNeg, VecUnaryOperation) \
Aart Bik6daebeb2017-04-03 14:35:41 -07001428 M(VecAbs, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001429 M(VecNot, VecUnaryOperation) \
1430 M(VecAdd, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001431 M(VecHalvingAdd, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001432 M(VecSub, VecBinaryOperation) \
1433 M(VecMul, VecBinaryOperation) \
1434 M(VecDiv, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001435 M(VecMin, VecBinaryOperation) \
1436 M(VecMax, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001437 M(VecAnd, VecBinaryOperation) \
1438 M(VecAndNot, VecBinaryOperation) \
1439 M(VecOr, VecBinaryOperation) \
1440 M(VecXor, VecBinaryOperation) \
1441 M(VecShl, VecBinaryOperation) \
1442 M(VecShr, VecBinaryOperation) \
1443 M(VecUShr, VecBinaryOperation) \
Aart Bik8de59162017-04-21 09:42:01 -07001444 M(VecSetScalars, VecOperation) \
Artem Serovf34dd202017-04-10 17:41:46 +01001445 M(VecMultiplyAccumulate, VecOperation) \
Aart Bikdbbac8f2017-09-01 13:06:08 -07001446 M(VecSADAccumulate, VecOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001447 M(VecLoad, VecMemoryOperation) \
1448 M(VecStore, VecMemoryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001449
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001450/*
1451 * Instructions, shared across several (not all) architectures.
1452 */
1453#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1454#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1455#else
1456#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001457 M(BitwiseNegatedRight, Instruction) \
Anton Kirilov74234da2017-01-13 14:42:47 +00001458 M(DataProcWithShifterOp, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001459 M(MultiplyAccumulate, Instruction) \
Artem Serove1811ed2017-04-27 16:50:47 +01001460 M(IntermediateAddressIndex, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001461#endif
1462
Alexandre Ramesef20f712015-06-09 10:29:30 +01001463#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1464
1465#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1466
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001467#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001468#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001469#else
1470#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1471 M(MipsComputeBaseMethodAddress, Instruction) \
Lena Djokica2901602017-09-21 13:50:52 +02001472 M(MipsPackedSwitch, Instruction) \
1473 M(IntermediateArrayAddressIndex, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001474#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001475
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001476#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1477
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001478#ifndef ART_ENABLE_CODEGEN_x86
1479#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1480#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001481#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1482 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001483 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001484 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001485 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001486#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001487
1488#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1489
1490#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1491 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001492 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001493 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1494 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001495 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001496 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001497 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1498 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1499
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001500#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1501 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001502 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001503 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001504 M(BinaryOperation, Instruction) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001505 M(Invoke, Instruction) \
1506 M(VecOperation, Instruction) \
1507 M(VecUnaryOperation, VecOperation) \
1508 M(VecBinaryOperation, VecOperation) \
1509 M(VecMemoryOperation, VecOperation)
Dave Allison20dfc792014-06-16 20:44:29 -07001510
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001511#define FOR_EACH_INSTRUCTION(M) \
1512 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1513 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1514
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001515#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001516FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1517#undef FORWARD_DECLARATION
1518
Vladimir Marko372f10e2016-05-17 16:30:10 +01001519#define DECLARE_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001520 private: \
1521 H##type& operator=(const H##type&) = delete; \
1522 public: \
Vladimir Marko372f10e2016-05-17 16:30:10 +01001523 const char* DebugName() const OVERRIDE { return #type; } \
1524 bool InstructionTypeEquals(const HInstruction* other) const OVERRIDE { \
1525 return other->Is##type(); \
1526 } \
Artem Serovcced8ba2017-07-19 18:18:09 +01001527 HInstruction* Clone(ArenaAllocator* arena) const OVERRIDE { \
1528 DCHECK(IsClonable()); \
1529 return new (arena) H##type(*this->As##type()); \
1530 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001531 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001532
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001533#define DECLARE_ABSTRACT_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001534 private: \
1535 H##type& operator=(const H##type&) = delete; \
1536 public: \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001537 bool Is##type() const { return As##type() != nullptr; } \
1538 const H##type* As##type() const { return this; } \
1539 H##type* As##type() { return this; }
1540
Artem Serovcced8ba2017-07-19 18:18:09 +01001541#define DEFAULT_COPY_CONSTRUCTOR(type) \
1542 explicit H##type(const H##type& other) = default;
1543
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001544template <typename T>
Vladimir Marko82b07402017-03-01 19:02:04 +00001545class HUseListNode : public ArenaObject<kArenaAllocUseListNode>,
1546 public IntrusiveForwardListNode<HUseListNode<T>> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001547 public:
Igor Murashkind01745e2017-04-05 16:40:31 -07001548 // Get the instruction which has this use as one of the inputs.
David Brazdiled596192015-01-23 10:39:45 +00001549 T GetUser() const { return user_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001550 // Get the position of the input record that this use corresponds to.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001551 size_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001552 // Set the position of the input record that this use corresponds to.
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001553 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001554
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001555 private:
David Brazdiled596192015-01-23 10:39:45 +00001556 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001557 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001558
1559 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001560 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001561
Vladimir Marko46817b82016-03-29 12:21:58 +01001562 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001563
1564 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1565};
1566
David Brazdiled596192015-01-23 10:39:45 +00001567template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001568using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001569
David Brazdil1abb4192015-02-17 18:33:36 +00001570// This class is used by HEnvironment and HInstruction classes to record the
1571// instructions they use and pointers to the corresponding HUseListNodes kept
1572// by the used instructions.
1573template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001574class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001575 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001576 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1577 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001578
Vladimir Marko46817b82016-03-29 12:21:58 +01001579 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1580 : HUserRecord(old_record.instruction_, before_use_node) {}
1581 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1582 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001583 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001584 }
1585
1586 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001587 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1588 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001589
1590 private:
1591 // Instruction used by the user.
1592 HInstruction* instruction_;
1593
Vladimir Marko46817b82016-03-29 12:21:58 +01001594 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1595 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001596};
1597
Vladimir Markoe9004912016-06-16 16:50:52 +01001598// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1599// This is used for HInstruction::GetInputs() to return a container wrapper providing
1600// HInstruction* values even though the underlying container has HUserRecord<>s.
1601struct HInputExtractor {
1602 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1603 return record.GetInstruction();
1604 }
1605 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1606 return record.GetInstruction();
1607 }
1608};
1609
1610using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1611using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1612
Aart Bik854a02b2015-07-14 16:07:00 -07001613/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001614 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001615 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001616 * For write/read dependences on fields/arrays, the dependence analysis uses
1617 * type disambiguation (e.g. a float field write cannot modify the value of an
1618 * integer field read) and the access type (e.g. a reference array write cannot
1619 * modify the value of a reference field read [although it may modify the
1620 * reference fetch prior to reading the field, which is represented by its own
1621 * write/read dependence]). The analysis makes conservative points-to
1622 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1623 * the same, and any reference read depends on any reference read without
1624 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001625 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001626 * The internal representation uses 38-bit and is described in the table below.
1627 * The first line indicates the side effect, and for field/array accesses the
1628 * second line indicates the type of the access (in the order of the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001629 * DataType::Type enum).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001630 * The two numbered lines below indicate the bit position in the bitfield (read
1631 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001632 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001633 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1634 * +-------------+---------+---------+--------------+---------+---------+
1635 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1636 * | 3 |333333322|222222221| 1 |111111110|000000000|
1637 * | 7 |654321098|765432109| 8 |765432109|876543210|
1638 *
1639 * Note that, to ease the implementation, 'changes' bits are least significant
1640 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001641 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001642class SideEffects : public ValueObject {
1643 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001644 SideEffects() : flags_(0) {}
1645
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001646 static SideEffects None() {
1647 return SideEffects(0);
1648 }
1649
1650 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001651 return SideEffects(kAllChangeBits | kAllDependOnBits);
1652 }
1653
1654 static SideEffects AllChanges() {
1655 return SideEffects(kAllChangeBits);
1656 }
1657
1658 static SideEffects AllDependencies() {
1659 return SideEffects(kAllDependOnBits);
1660 }
1661
1662 static SideEffects AllExceptGCDependency() {
1663 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1664 }
1665
1666 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001667 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001668 }
1669
Aart Bik34c3ba92015-07-20 14:08:59 -07001670 static SideEffects AllWrites() {
1671 return SideEffects(kAllWrites);
1672 }
1673
1674 static SideEffects AllReads() {
1675 return SideEffects(kAllReads);
1676 }
1677
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001678 static SideEffects FieldWriteOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001679 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001680 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001681 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001682 }
1683
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001684 static SideEffects ArrayWriteOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001685 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001686 }
1687
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001688 static SideEffects FieldReadOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001689 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001690 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001691 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001692 }
1693
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001694 static SideEffects ArrayReadOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001695 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001696 }
1697
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001698 static SideEffects CanTriggerGC() {
1699 return SideEffects(1ULL << kCanTriggerGCBit);
1700 }
1701
1702 static SideEffects DependsOnGC() {
1703 return SideEffects(1ULL << kDependsOnGCBit);
1704 }
1705
Aart Bik854a02b2015-07-14 16:07:00 -07001706 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001707 SideEffects Union(SideEffects other) const {
1708 return SideEffects(flags_ | other.flags_);
1709 }
1710
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001711 SideEffects Exclusion(SideEffects other) const {
1712 return SideEffects(flags_ & ~other.flags_);
1713 }
1714
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001715 void Add(SideEffects other) {
1716 flags_ |= other.flags_;
1717 }
1718
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001719 bool Includes(SideEffects other) const {
1720 return (other.flags_ & flags_) == other.flags_;
1721 }
1722
1723 bool HasSideEffects() const {
1724 return (flags_ & kAllChangeBits);
1725 }
1726
1727 bool HasDependencies() const {
1728 return (flags_ & kAllDependOnBits);
1729 }
1730
1731 // Returns true if there are no side effects or dependencies.
1732 bool DoesNothing() const {
1733 return flags_ == 0;
1734 }
1735
Aart Bik854a02b2015-07-14 16:07:00 -07001736 // Returns true if something is written.
1737 bool DoesAnyWrite() const {
1738 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001739 }
1740
Aart Bik854a02b2015-07-14 16:07:00 -07001741 // Returns true if something is read.
1742 bool DoesAnyRead() const {
1743 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001744 }
1745
Aart Bik854a02b2015-07-14 16:07:00 -07001746 // Returns true if potentially everything is written and read
1747 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001748 bool DoesAllReadWrite() const {
1749 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1750 }
1751
Aart Bik854a02b2015-07-14 16:07:00 -07001752 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001753 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001754 }
1755
Roland Levillain0d5a2812015-11-13 10:07:31 +00001756 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001757 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001758 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1759 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001760 }
1761
1762 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001763 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001764 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001765 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001766 for (int s = kLastBit; s >= 0; s--) {
1767 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1768 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1769 // This is a bit for the GC side effect.
1770 if (current_bit_is_set) {
1771 flags += "GC";
1772 }
Aart Bik854a02b2015-07-14 16:07:00 -07001773 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001774 } else {
1775 // This is a bit for the array/field analysis.
1776 // The underscore character stands for the 'can trigger GC' bit.
1777 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1778 if (current_bit_is_set) {
1779 flags += kDebug[s];
1780 }
1781 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1782 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1783 flags += "|";
1784 }
1785 }
Aart Bik854a02b2015-07-14 16:07:00 -07001786 }
1787 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001788 }
1789
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001790 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001791
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001792 private:
1793 static constexpr int kFieldArrayAnalysisBits = 9;
1794
1795 static constexpr int kFieldWriteOffset = 0;
1796 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1797 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1798 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1799
1800 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1801
1802 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1803 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1804 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1805 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1806
1807 static constexpr int kLastBit = kDependsOnGCBit;
1808 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1809
1810 // Aliases.
1811
1812 static_assert(kChangeBits == kDependOnBits,
1813 "the 'change' bits should match the 'depend on' bits.");
1814
1815 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1816 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1817 static constexpr uint64_t kAllWrites =
1818 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1819 static constexpr uint64_t kAllReads =
1820 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001821
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001822 // Translates type to bit flag. The type must correspond to a Java type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001823 static uint64_t TypeFlag(DataType::Type type, int offset) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001824 int shift;
1825 switch (type) {
1826 case DataType::Type::kReference: shift = 0; break;
1827 case DataType::Type::kBool: shift = 1; break;
1828 case DataType::Type::kInt8: shift = 2; break;
1829 case DataType::Type::kUint16: shift = 3; break;
1830 case DataType::Type::kInt16: shift = 4; break;
1831 case DataType::Type::kInt32: shift = 5; break;
1832 case DataType::Type::kInt64: shift = 6; break;
1833 case DataType::Type::kFloat32: shift = 7; break;
1834 case DataType::Type::kFloat64: shift = 8; break;
1835 default:
1836 LOG(FATAL) << "Unexpected data type " << type;
1837 UNREACHABLE();
1838 }
Aart Bik854a02b2015-07-14 16:07:00 -07001839 DCHECK_LE(kFieldWriteOffset, shift);
1840 DCHECK_LT(shift, kArrayWriteOffset);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001841 return UINT64_C(1) << (shift + offset);
Aart Bik854a02b2015-07-14 16:07:00 -07001842 }
1843
1844 // Private constructor on direct flags value.
1845 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1846
1847 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001848};
1849
David Brazdiled596192015-01-23 10:39:45 +00001850// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001851class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001852 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001853 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
Mingyao Yang01b47b02017-02-03 12:09:57 -08001854 size_t number_of_vregs,
1855 ArtMethod* method,
1856 uint32_t dex_pc,
1857 HInstruction* holder)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001858 : vregs_(number_of_vregs, allocator->Adapter(kArenaAllocEnvironmentVRegs)),
1859 locations_(allocator->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001860 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001861 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001862 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001863 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001864 }
1865
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001866 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
1867 const HEnvironment& to_copy,
1868 HInstruction* holder)
1869 : HEnvironment(allocator,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001870 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001871 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001872 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001873 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001874
Vladimir Markoec32f642017-06-02 10:51:55 +01001875 void AllocateLocations() {
1876 DCHECK(locations_.empty());
1877 locations_.resize(vregs_.size());
1878 }
1879
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001880 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001881 if (parent_ != nullptr) {
1882 parent_->SetAndCopyParentChain(allocator, parent);
1883 } else {
1884 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1885 parent_->CopyFrom(parent);
1886 if (parent->GetParent() != nullptr) {
1887 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1888 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001889 }
David Brazdiled596192015-01-23 10:39:45 +00001890 }
1891
Vladimir Marko69d310e2017-10-09 14:12:23 +01001892 void CopyFrom(ArrayRef<HInstruction* const> locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001893 void CopyFrom(HEnvironment* environment);
1894
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001895 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1896 // input to the loop phi instead. This is for inserting instructions that
1897 // require an environment (like HDeoptimization) in the loop pre-header.
1898 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001899
1900 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001901 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001902 }
1903
1904 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001905 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001906 }
1907
David Brazdil1abb4192015-02-17 18:33:36 +00001908 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001909
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001910 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001911
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001912 HEnvironment* GetParent() const { return parent_; }
1913
1914 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001915 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001916 }
1917
1918 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001919 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001920 }
1921
1922 uint32_t GetDexPc() const {
1923 return dex_pc_;
1924 }
1925
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001926 ArtMethod* GetMethod() const {
1927 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001928 }
1929
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001930 HInstruction* GetHolder() const {
1931 return holder_;
1932 }
1933
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001934
1935 bool IsFromInlinedInvoke() const {
1936 return GetParent() != nullptr;
1937 }
1938
David Brazdiled596192015-01-23 10:39:45 +00001939 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001940 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1941 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001942 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001943 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001944 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00001945
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001946 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001947 HInstruction* const holder_;
1948
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001949 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001950
1951 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1952};
1953
Vladimir Markof9f64412015-09-02 14:05:49 +01001954class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001955 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05301956#define DECLARE_KIND(type, super) k##type,
1957 enum InstructionKind {
1958 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1959 kLastInstructionKind
1960 };
1961#undef DECLARE_KIND
1962
1963 HInstruction(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001964 : previous_(nullptr),
1965 next_(nullptr),
1966 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001967 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001968 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001969 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001970 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001971 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001972 locations_(nullptr),
1973 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001974 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001975 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001976 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05301977 SetPackedField<InstructionKindField>(kind);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001978 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1979 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001980
Dave Allison20dfc792014-06-16 20:44:29 -07001981 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001982
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001983
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001984 HInstruction* GetNext() const { return next_; }
1985 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001986
Calin Juravle77520bc2015-01-12 18:45:46 +00001987 HInstruction* GetNextDisregardingMoves() const;
1988 HInstruction* GetPreviousDisregardingMoves() const;
1989
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001990 HBasicBlock* GetBlock() const { return block_; }
Vladimir Markoca6fff82017-10-03 14:49:14 +01001991 ArenaAllocator* GetAllocator() const { return block_->GetGraph()->GetAllocator(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001992 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001993 bool IsInBlock() const { return block_ != nullptr; }
1994 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001995 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1996 bool IsIrreducibleLoopHeaderPhi() const {
1997 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1998 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001999
Vladimir Marko372f10e2016-05-17 16:30:10 +01002000 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
2001
2002 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
2003 // One virtual method is enough, just const_cast<> and then re-add the const.
2004 return ArrayRef<const HUserRecord<HInstruction*>>(
2005 const_cast<HInstruction*>(this)->GetInputRecords());
2006 }
2007
Vladimir Markoe9004912016-06-16 16:50:52 +01002008 HInputsRef GetInputs() {
2009 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002010 }
2011
Vladimir Markoe9004912016-06-16 16:50:52 +01002012 HConstInputsRef GetInputs() const {
2013 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002014 }
2015
2016 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00002017 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002018
Aart Bik2767f4b2016-10-28 15:03:53 -07002019 bool HasInput(HInstruction* input) const {
2020 for (const HInstruction* i : GetInputs()) {
2021 if (i == input) {
2022 return true;
2023 }
2024 }
2025 return false;
2026 }
2027
Vladimir Marko372f10e2016-05-17 16:30:10 +01002028 void SetRawInputAt(size_t index, HInstruction* input) {
2029 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
2030 }
2031
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002032 virtual void Accept(HGraphVisitor* visitor) = 0;
2033 virtual const char* DebugName() const = 0;
2034
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002035 virtual DataType::Type GetType() const { return DataType::Type::kVoid; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002036
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002037 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002038
2039 uint32_t GetDexPc() const { return dex_pc_; }
2040
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002041 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01002042
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002043 // Can the instruction throw?
2044 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
2045 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01002046 virtual bool CanThrow() const { return false; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08002047
2048 // Does the instruction always throw an exception unconditionally?
2049 virtual bool AlwaysThrows() const { return false; }
2050
David Brazdilec16f792015-08-19 15:04:01 +01002051 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002052
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002053 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002054 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002055
Calin Juravle10e244f2015-01-26 18:54:32 +00002056 // Does not apply for all instructions, but having this at top level greatly
2057 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00002058 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002059 virtual bool CanBeNull() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002060 DCHECK_EQ(GetType(), DataType::Type::kReference) << "CanBeNull only applies to reference types";
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002061 return true;
2062 }
Calin Juravle10e244f2015-01-26 18:54:32 +00002063
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002064 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01002065 return false;
2066 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002067
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002068 virtual bool IsActualObject() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002069 return GetType() == DataType::Type::kReference;
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002070 }
2071
Calin Juravle2e768302015-07-28 14:41:11 +00002072 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00002073
Calin Juravle61d544b2015-02-23 16:46:57 +00002074 ReferenceTypeInfo GetReferenceTypeInfo() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002075 DCHECK_EQ(GetType(), DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002076 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00002077 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00002078 }
Calin Juravleacf735c2015-02-12 15:25:22 +00002079
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002080 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00002081 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002082 // Note: fixup_end remains valid across push_front().
2083 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
2084 HUseListNode<HInstruction*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002085 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HInstruction*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002086 uses_.push_front(*new_node);
2087 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002088 }
2089
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002090 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01002091 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002092 // Note: env_fixup_end remains valid across push_front().
2093 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
2094 HUseListNode<HEnvironment*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002095 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HEnvironment*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002096 env_uses_.push_front(*new_node);
2097 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002098 }
2099
David Brazdil1abb4192015-02-17 18:33:36 +00002100 void RemoveAsUserOfInput(size_t input) {
2101 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002102 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2103 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2104 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00002105 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002106
Vladimir Marko372f10e2016-05-17 16:30:10 +01002107 void RemoveAsUserOfAllInputs() {
2108 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2109 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2110 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2111 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
2112 }
2113 }
2114
David Brazdil1abb4192015-02-17 18:33:36 +00002115 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2116 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002117
Vladimir Marko46817b82016-03-29 12:21:58 +01002118 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2119 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2120 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002121 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002122 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002123 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002124
Aart Bikcc42be02016-10-20 16:14:16 -07002125 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002126 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002127 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002128 !CanThrow() &&
2129 !IsSuspendCheck() &&
2130 !IsControlFlow() &&
2131 !IsNativeDebugInfo() &&
2132 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002133 // If we added an explicit barrier then we should keep it.
Igor Murashkind01745e2017-04-05 16:40:31 -07002134 !IsMemoryBarrier() &&
2135 !IsConstructorFence();
Aart Bik482095d2016-10-10 15:39:10 -07002136 }
2137
Aart Bikcc42be02016-10-20 16:14:16 -07002138 bool IsDeadAndRemovable() const {
2139 return IsRemovable() && !HasUses();
2140 }
2141
Roland Levillain6c82d402014-10-13 16:10:27 +01002142 // Does this instruction strictly dominate `other_instruction`?
2143 // Returns false if this instruction and `other_instruction` are the same.
2144 // Aborts if this instruction and `other_instruction` are both phis.
2145 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002146
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002147 int GetId() const { return id_; }
2148 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002149
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002150 int GetSsaIndex() const { return ssa_index_; }
2151 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2152 bool HasSsaIndex() const { return ssa_index_ != -1; }
2153
2154 bool HasEnvironment() const { return environment_ != nullptr; }
2155 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002156 // Set the `environment_` field. Raw because this method does not
2157 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002158 void SetRawEnvironment(HEnvironment* environment) {
2159 DCHECK(environment_ == nullptr);
2160 DCHECK_EQ(environment->GetHolder(), this);
2161 environment_ = environment;
2162 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002163
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002164 void InsertRawEnvironment(HEnvironment* environment) {
2165 DCHECK(environment_ != nullptr);
2166 DCHECK_EQ(environment->GetHolder(), this);
2167 DCHECK(environment->GetParent() == nullptr);
2168 environment->parent_ = environment_;
2169 environment_ = environment;
2170 }
2171
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002172 void RemoveEnvironment();
2173
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002174 // Set the environment of this instruction, copying it from `environment`. While
2175 // copying, the uses lists are being updated.
2176 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002177 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002178 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002179 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002180 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002181 if (environment->GetParent() != nullptr) {
2182 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2183 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002184 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002185
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002186 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2187 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002188 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002189 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002190 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002191 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002192 if (environment->GetParent() != nullptr) {
2193 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2194 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002195 }
2196
Nicolas Geoffray39468442014-09-02 15:17:15 +01002197 // Returns the number of entries in the environment. Typically, that is the
2198 // number of dex registers in a method. It could be more in case of inlining.
2199 size_t EnvironmentSize() const;
2200
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002201 LocationSummary* GetLocations() const { return locations_; }
2202 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002203
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002204 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002205 void ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002206 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002207
Alexandre Rames188d4312015-04-09 18:30:21 +01002208 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2209 // uses of this instruction by `other` are *not* updated.
2210 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2211 ReplaceWith(other);
2212 other->ReplaceInput(this, use_index);
2213 }
2214
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002215 // Move `this` instruction before `cursor`
2216 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002217
Vladimir Markofb337ea2015-11-25 15:25:10 +00002218 // Move `this` before its first user and out of any loops. If there is no
2219 // out-of-loop user that dominates all other users, move the instruction
2220 // to the end of the out-of-loop common dominator of the user's blocks.
2221 //
2222 // This can be used only on non-throwing instructions with no side effects that
2223 // have at least one use but no environment uses.
2224 void MoveBeforeFirstUserAndOutOfLoops();
2225
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002226#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002227 bool Is##type() const; \
2228 const H##type* As##type() const; \
2229 H##type* As##type();
2230
2231 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2232#undef INSTRUCTION_TYPE_CHECK
2233
2234#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002235 bool Is##type() const { return (As##type() != nullptr); } \
2236 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002237 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002238 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002239#undef INSTRUCTION_TYPE_CHECK
2240
Artem Serovcced8ba2017-07-19 18:18:09 +01002241 // Return a clone of the instruction if it is clonable (shallow copy by default, custom copy
2242 // if a custom copy-constructor is provided for a particular type). If IsClonable() is false for
2243 // the instruction then the behaviour of this function is undefined.
2244 //
2245 // Note: It is semantically valid to create a clone of the instruction only until
2246 // prepare_for_register_allocator phase as lifetime, intervals and codegen info are not
2247 // copied.
2248 //
2249 // Note: HEnvironment and some other fields are not copied and are set to default values, see
2250 // 'explicit HInstruction(const HInstruction& other)' for details.
2251 virtual HInstruction* Clone(ArenaAllocator* arena ATTRIBUTE_UNUSED) const {
2252 LOG(FATAL) << "Cloning is not implemented for the instruction " <<
2253 DebugName() << " " << GetId();
2254 UNREACHABLE();
2255 }
2256
2257 // Return whether instruction can be cloned (copied).
2258 virtual bool IsClonable() const { return false; }
2259
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002260 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002261 // TODO: this method is used by LICM and GVN with possibly different
2262 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002263 virtual bool CanBeMoved() const { return false; }
2264
2265 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002266 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002267 return false;
2268 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002269
2270 // Returns whether any data encoded in the two instructions is equal.
2271 // This method does not look at the inputs. Both instructions must be
2272 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002273 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002274 return false;
2275 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002276
2277 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002278 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002279 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002280 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002281
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002282 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2283 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2284 // the virtual function because the __attribute__((__pure__)) doesn't really
2285 // apply the strong requirement for virtual functions, preventing optimizations.
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302286 InstructionKind GetKind() const { return GetPackedField<InstructionKindField>(); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002287
2288 virtual size_t ComputeHashCode() const {
2289 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002290 for (const HInstruction* input : GetInputs()) {
2291 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002292 }
2293 return result;
2294 }
2295
2296 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002297 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002298 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002299
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002300 size_t GetLifetimePosition() const { return lifetime_position_; }
2301 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2302 LiveInterval* GetLiveInterval() const { return live_interval_; }
2303 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2304 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2305
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002306 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2307
2308 // Returns whether the code generation of the instruction will require to have access
2309 // to the current method. Such instructions are:
2310 // (1): Instructions that require an environment, as calling the runtime requires
2311 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002312 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2313 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002314 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002315 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002316 }
2317
Vladimir Markodc151b22015-10-15 18:02:30 +01002318 // Returns whether the code generation of the instruction will require to have access
2319 // to the dex cache of the current method's declaring class via the current method.
2320 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002321
Mark Mendellc4701932015-04-10 13:18:51 -04002322 // Does this instruction have any use in an environment before
2323 // control flow hits 'other'?
2324 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2325
2326 // Remove all references to environment uses of this instruction.
2327 // The caller must ensure that this is safe to do.
2328 void RemoveEnvironmentUsers();
2329
Vladimir Markoa1de9182016-02-25 11:37:38 +00002330 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2331 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002332
David Brazdil1abb4192015-02-17 18:33:36 +00002333 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002334 // If set, the machine code for this instruction is assumed to be generated by
2335 // its users. Used by liveness analysis to compute use positions accordingly.
2336 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2337 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302338 static constexpr size_t kFieldInstructionKind = kFlagReferenceTypeIsExact + 1;
2339 static constexpr size_t kFieldInstructionKindSize =
2340 MinimumBitsToStore(static_cast<size_t>(InstructionKind::kLastInstructionKind - 1));
2341 static constexpr size_t kNumberOfGenericPackedBits =
2342 kFieldInstructionKind + kFieldInstructionKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00002343 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2344
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302345 static_assert(kNumberOfGenericPackedBits <= kMaxNumberOfPackedBits,
2346 "Too many generic packed fields");
2347
Vladimir Marko372f10e2016-05-17 16:30:10 +01002348 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2349 return GetInputRecords()[i];
2350 }
2351
2352 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2353 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2354 input_records[index] = input;
2355 }
David Brazdil1abb4192015-02-17 18:33:36 +00002356
Vladimir Markoa1de9182016-02-25 11:37:38 +00002357 uint32_t GetPackedFields() const {
2358 return packed_fields_;
2359 }
2360
2361 template <size_t flag>
2362 bool GetPackedFlag() const {
2363 return (packed_fields_ & (1u << flag)) != 0u;
2364 }
2365
2366 template <size_t flag>
2367 void SetPackedFlag(bool value = true) {
2368 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2369 }
2370
2371 template <typename BitFieldType>
2372 typename BitFieldType::value_type GetPackedField() const {
2373 return BitFieldType::Decode(packed_fields_);
2374 }
2375
2376 template <typename BitFieldType>
2377 void SetPackedField(typename BitFieldType::value_type value) {
2378 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2379 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2380 }
2381
Artem Serovcced8ba2017-07-19 18:18:09 +01002382 // Copy construction for the instruction (used for Clone function).
2383 //
2384 // Fields (e.g. lifetime, intervals and codegen info) associated with phases starting from
2385 // prepare_for_register_allocator are not copied (set to default values).
2386 //
2387 // Copy constructors must be provided for every HInstruction type; default copy constructor is
2388 // fine for most of them. However for some of the instructions a custom copy constructor must be
2389 // specified (when instruction has non-trivially copyable fields and must have a special behaviour
2390 // for copying them).
2391 explicit HInstruction(const HInstruction& other)
2392 : previous_(nullptr),
2393 next_(nullptr),
2394 block_(nullptr),
2395 dex_pc_(other.dex_pc_),
2396 id_(-1),
2397 ssa_index_(-1),
2398 packed_fields_(other.packed_fields_),
2399 environment_(nullptr),
2400 locations_(nullptr),
2401 live_interval_(nullptr),
2402 lifetime_position_(kNoLifetime),
2403 side_effects_(other.side_effects_),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302404 reference_type_handle_(other.reference_type_handle_) {
2405 }
Artem Serovcced8ba2017-07-19 18:18:09 +01002406
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002407 private:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302408 using InstructionKindField =
2409 BitField<InstructionKind, kFieldInstructionKind, kFieldInstructionKindSize>;
2410
Vladimir Marko46817b82016-03-29 12:21:58 +01002411 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2412 auto before_use_node = uses_.before_begin();
2413 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2414 HInstruction* user = use_node->GetUser();
2415 size_t input_index = use_node->GetIndex();
2416 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2417 before_use_node = use_node;
2418 }
2419 }
2420
2421 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2422 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2423 if (next != uses_.end()) {
2424 HInstruction* next_user = next->GetUser();
2425 size_t next_index = next->GetIndex();
2426 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2427 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2428 }
2429 }
2430
2431 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2432 auto before_env_use_node = env_uses_.before_begin();
2433 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2434 HEnvironment* user = env_use_node->GetUser();
2435 size_t input_index = env_use_node->GetIndex();
2436 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2437 before_env_use_node = env_use_node;
2438 }
2439 }
2440
2441 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2442 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2443 if (next != env_uses_.end()) {
2444 HEnvironment* next_user = next->GetUser();
2445 size_t next_index = next->GetIndex();
2446 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2447 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2448 }
2449 }
David Brazdil1abb4192015-02-17 18:33:36 +00002450
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002451 HInstruction* previous_;
2452 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002453 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002454 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002455
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002456 // An instruction gets an id when it is added to the graph.
2457 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002458 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002459 int id_;
2460
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002461 // When doing liveness analysis, instructions that have uses get an SSA index.
2462 int ssa_index_;
2463
Vladimir Markoa1de9182016-02-25 11:37:38 +00002464 // Packed fields.
2465 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002466
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002467 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002468 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002469
2470 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002471 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002472
Nicolas Geoffray39468442014-09-02 15:17:15 +01002473 // The environment associated with this instruction. Not null if the instruction
2474 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002475 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002476
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002477 // Set by the code generator.
2478 LocationSummary* locations_;
2479
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002480 // Set by the liveness analysis.
2481 LiveInterval* live_interval_;
2482
2483 // Set by the liveness analysis, this is the position in a linear
2484 // order of blocks where this instruction's live interval start.
2485 size_t lifetime_position_;
2486
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002487 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002488
Vladimir Markoa1de9182016-02-25 11:37:38 +00002489 // The reference handle part of the reference type info.
2490 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002491 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002492 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002493
David Brazdil1abb4192015-02-17 18:33:36 +00002494 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002495 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002496 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002497 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002498 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002499};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002500std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002501
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002502// Iterates over the instructions, while preserving the next instruction
2503// in case the current instruction gets removed from the list by the user
2504// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002505class HInstructionIterator : public ValueObject {
2506 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002507 explicit HInstructionIterator(const HInstructionList& instructions)
2508 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002509 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002510 }
2511
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002512 bool Done() const { return instruction_ == nullptr; }
2513 HInstruction* Current() const { return instruction_; }
2514 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002515 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002516 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002517 }
2518
2519 private:
2520 HInstruction* instruction_;
2521 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002522
2523 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002524};
2525
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002526// Iterates over the instructions without saving the next instruction,
2527// therefore handling changes in the graph potentially made by the user
2528// of this iterator.
2529class HInstructionIteratorHandleChanges : public ValueObject {
2530 public:
2531 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2532 : instruction_(instructions.first_instruction_) {
2533 }
2534
2535 bool Done() const { return instruction_ == nullptr; }
2536 HInstruction* Current() const { return instruction_; }
2537 void Advance() {
2538 instruction_ = instruction_->GetNext();
2539 }
2540
2541 private:
2542 HInstruction* instruction_;
2543
2544 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2545};
2546
2547
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002548class HBackwardInstructionIterator : public ValueObject {
2549 public:
2550 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2551 : instruction_(instructions.last_instruction_) {
2552 next_ = Done() ? nullptr : instruction_->GetPrevious();
2553 }
2554
2555 bool Done() const { return instruction_ == nullptr; }
2556 HInstruction* Current() const { return instruction_; }
2557 void Advance() {
2558 instruction_ = next_;
2559 next_ = Done() ? nullptr : instruction_->GetPrevious();
2560 }
2561
2562 private:
2563 HInstruction* instruction_;
2564 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002565
2566 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002567};
2568
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002569class HVariableInputSizeInstruction : public HInstruction {
2570 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002571 using HInstruction::GetInputRecords; // Keep the const version visible.
2572 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
2573 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2574 }
2575
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002576 void AddInput(HInstruction* input);
2577 void InsertInputAt(size_t index, HInstruction* input);
2578 void RemoveInputAt(size_t index);
2579
Igor Murashkind01745e2017-04-05 16:40:31 -07002580 // Removes all the inputs.
2581 // Also removes this instructions from each input's use list
2582 // (for non-environment uses only).
2583 void RemoveAllInputs();
2584
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002585 protected:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302586 HVariableInputSizeInstruction(InstructionKind inst_kind,
2587 SideEffects side_effects,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002588 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002589 ArenaAllocator* allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002590 size_t number_of_inputs,
2591 ArenaAllocKind kind)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302592 : HInstruction(inst_kind, side_effects, dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002593 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002594
Artem Serovcced8ba2017-07-19 18:18:09 +01002595 DEFAULT_COPY_CONSTRUCTOR(VariableInputSizeInstruction);
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002596
Artem Serovcced8ba2017-07-19 18:18:09 +01002597 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002598};
2599
Vladimir Markof9f64412015-09-02 14:05:49 +01002600template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002601class HTemplateInstruction: public HInstruction {
2602 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302603 HTemplateInstruction<N>(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
2604 : HInstruction(kind, side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002605 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002606
Vladimir Marko372f10e2016-05-17 16:30:10 +01002607 using HInstruction::GetInputRecords; // Keep the const version visible.
2608 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2609 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002610 }
2611
Artem Serovcced8ba2017-07-19 18:18:09 +01002612 protected:
2613 DEFAULT_COPY_CONSTRUCTOR(TemplateInstruction<N>);
2614
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002615 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002616 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002617
2618 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002619};
2620
Vladimir Markof9f64412015-09-02 14:05:49 +01002621// HTemplateInstruction specialization for N=0.
2622template<>
2623class HTemplateInstruction<0>: public HInstruction {
2624 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302625 explicit HTemplateInstruction<0>(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
2626 : HInstruction(kind, side_effects, dex_pc) {}
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002627
Vladimir Markof9f64412015-09-02 14:05:49 +01002628 virtual ~HTemplateInstruction() {}
2629
Vladimir Marko372f10e2016-05-17 16:30:10 +01002630 using HInstruction::GetInputRecords; // Keep the const version visible.
2631 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2632 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002633 }
2634
Artem Serovcced8ba2017-07-19 18:18:09 +01002635 protected:
2636 DEFAULT_COPY_CONSTRUCTOR(TemplateInstruction<0>);
2637
Vladimir Markof9f64412015-09-02 14:05:49 +01002638 private:
2639 friend class SsaBuilder;
2640};
2641
Dave Allison20dfc792014-06-16 20:44:29 -07002642template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002643class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002644 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302645 using HInstruction::InstructionKind;
2646 HExpression<N>(InstructionKind kind,
2647 DataType::Type type,
2648 SideEffects side_effects,
2649 uint32_t dex_pc)
2650 : HTemplateInstruction<N>(kind, side_effects, dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00002651 this->template SetPackedField<TypeField>(type);
2652 }
Dave Allison20dfc792014-06-16 20:44:29 -07002653 virtual ~HExpression() {}
2654
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002655 DataType::Type GetType() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00002656 return TypeField::Decode(this->GetPackedFields());
2657 }
Dave Allison20dfc792014-06-16 20:44:29 -07002658
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002659 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002660 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2661 static constexpr size_t kFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002662 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00002663 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2664 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2665 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002666 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
Artem Serovcced8ba2017-07-19 18:18:09 +01002667 DEFAULT_COPY_CONSTRUCTOR(Expression<N>);
Dave Allison20dfc792014-06-16 20:44:29 -07002668};
2669
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002670// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2671// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002672class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002673 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002674 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302675 : HTemplateInstruction(kReturnVoid, SideEffects::None(), dex_pc) {
2676 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002677
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002678 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002679
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002680 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002681
Artem Serovcced8ba2017-07-19 18:18:09 +01002682 protected:
2683 DEFAULT_COPY_CONSTRUCTOR(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002684};
2685
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002686// Represents dex's RETURN opcodes. A HReturn is a control flow
2687// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002688class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002689 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002690 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302691 : HTemplateInstruction(kReturn, SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002692 SetRawInputAt(0, value);
2693 }
2694
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002695 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002696
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002697 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002698
Artem Serovcced8ba2017-07-19 18:18:09 +01002699 protected:
2700 DEFAULT_COPY_CONSTRUCTOR(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002701};
2702
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002703class HPhi FINAL : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002704 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002705 HPhi(ArenaAllocator* allocator,
David Brazdildee58d62016-04-07 09:54:26 +00002706 uint32_t reg_number,
2707 size_t number_of_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002708 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +00002709 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002710 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302711 kPhi,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002712 SideEffects::None(),
2713 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002714 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002715 number_of_inputs,
2716 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002717 reg_number_(reg_number) {
2718 SetPackedField<TypeField>(ToPhiType(type));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002719 DCHECK_NE(GetType(), DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +00002720 // Phis are constructed live and marked dead if conflicting or unused.
2721 // Individual steps of SsaBuilder should assume that if a phi has been
2722 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2723 SetPackedFlag<kFlagIsLive>(true);
2724 SetPackedFlag<kFlagCanBeNull>(true);
2725 }
2726
Artem Serovcced8ba2017-07-19 18:18:09 +01002727 bool IsClonable() const OVERRIDE { return true; }
2728
David Brazdildee58d62016-04-07 09:54:26 +00002729 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002730 static DataType::Type ToPhiType(DataType::Type type) {
2731 return DataType::Kind(type);
David Brazdildee58d62016-04-07 09:54:26 +00002732 }
2733
2734 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2735
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002736 DataType::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2737 void SetType(DataType::Type new_type) {
David Brazdildee58d62016-04-07 09:54:26 +00002738 // Make sure that only valid type changes occur. The following are allowed:
2739 // (1) int -> float/ref (primitive type propagation),
2740 // (2) long -> double (primitive type propagation).
2741 DCHECK(GetType() == new_type ||
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002742 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kFloat32) ||
2743 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kReference) ||
2744 (GetType() == DataType::Type::kInt64 && new_type == DataType::Type::kFloat64));
David Brazdildee58d62016-04-07 09:54:26 +00002745 SetPackedField<TypeField>(new_type);
2746 }
2747
2748 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2749 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2750
2751 uint32_t GetRegNumber() const { return reg_number_; }
2752
2753 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2754 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2755 bool IsDead() const { return !IsLive(); }
2756 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2757
Vladimir Markoe9004912016-06-16 16:50:52 +01002758 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002759 return other != nullptr
2760 && other->IsPhi()
2761 && other->AsPhi()->GetBlock() == GetBlock()
2762 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2763 }
2764
Nicolas Geoffrayf57c1ae2017-06-28 17:40:18 +01002765 bool HasEquivalentPhi() const {
2766 if (GetPrevious() != nullptr && GetPrevious()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2767 return true;
2768 }
2769 if (GetNext() != nullptr && GetNext()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2770 return true;
2771 }
2772 return false;
2773 }
2774
David Brazdildee58d62016-04-07 09:54:26 +00002775 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2776 // An equivalent phi is a phi having the same dex register and type.
2777 // It assumes that phis with the same dex register are adjacent.
2778 HPhi* GetNextEquivalentPhiWithSameType() {
2779 HInstruction* next = GetNext();
2780 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2781 if (next->GetType() == GetType()) {
2782 return next->AsPhi();
2783 }
2784 next = next->GetNext();
2785 }
2786 return nullptr;
2787 }
2788
2789 DECLARE_INSTRUCTION(Phi);
2790
Artem Serovcced8ba2017-07-19 18:18:09 +01002791 protected:
2792 DEFAULT_COPY_CONSTRUCTOR(Phi);
2793
David Brazdildee58d62016-04-07 09:54:26 +00002794 private:
2795 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2796 static constexpr size_t kFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002797 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
David Brazdildee58d62016-04-07 09:54:26 +00002798 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2799 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2800 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2801 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002802 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
David Brazdildee58d62016-04-07 09:54:26 +00002803
David Brazdildee58d62016-04-07 09:54:26 +00002804 const uint32_t reg_number_;
David Brazdildee58d62016-04-07 09:54:26 +00002805};
2806
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002807// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002808// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002809// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002810class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002811 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302812 explicit HExit(uint32_t dex_pc = kNoDexPc)
2813 : HTemplateInstruction(kExit, SideEffects::None(), dex_pc) {
2814 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002815
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002816 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002817
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002818 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002819
Artem Serovcced8ba2017-07-19 18:18:09 +01002820 protected:
2821 DEFAULT_COPY_CONSTRUCTOR(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002822};
2823
2824// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002825class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002826 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302827 explicit HGoto(uint32_t dex_pc = kNoDexPc)
2828 : HTemplateInstruction(kGoto, SideEffects::None(), dex_pc) {
2829 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002830
Artem Serovcced8ba2017-07-19 18:18:09 +01002831 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002832 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002833
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002834 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002835 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002836 }
2837
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002838 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002839
Artem Serovcced8ba2017-07-19 18:18:09 +01002840 protected:
2841 DEFAULT_COPY_CONSTRUCTOR(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002842};
2843
Roland Levillain9867bc72015-08-05 10:21:34 +01002844class HConstant : public HExpression<0> {
2845 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302846 explicit HConstant(InstructionKind kind, DataType::Type type, uint32_t dex_pc = kNoDexPc)
2847 : HExpression(kind, type, SideEffects::None(), dex_pc) {
2848 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002849
2850 bool CanBeMoved() const OVERRIDE { return true; }
2851
Roland Levillain1a653882016-03-18 18:05:57 +00002852 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002853 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002854 // Is this constant 0 in the arithmetic sense?
2855 virtual bool IsArithmeticZero() const { return false; }
2856 // Is this constant a 0-bit pattern?
2857 virtual bool IsZeroBitPattern() const { return false; }
2858 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002859 virtual bool IsOne() const { return false; }
2860
David Brazdil77a48ae2015-09-15 12:34:04 +00002861 virtual uint64_t GetValueAsUint64() const = 0;
2862
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002863 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002864
Artem Serovcced8ba2017-07-19 18:18:09 +01002865 protected:
2866 DEFAULT_COPY_CONSTRUCTOR(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002867};
2868
Vladimir Markofcb503c2016-05-18 12:48:17 +01002869class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002870 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002871 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002872 return true;
2873 }
2874
David Brazdil77a48ae2015-09-15 12:34:04 +00002875 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2876
Roland Levillain9867bc72015-08-05 10:21:34 +01002877 size_t ComputeHashCode() const OVERRIDE { return 0; }
2878
Roland Levillain1a653882016-03-18 18:05:57 +00002879 // The null constant representation is a 0-bit pattern.
2880 virtual bool IsZeroBitPattern() const { return true; }
2881
Roland Levillain9867bc72015-08-05 10:21:34 +01002882 DECLARE_INSTRUCTION(NullConstant);
2883
Artem Serovcced8ba2017-07-19 18:18:09 +01002884 protected:
2885 DEFAULT_COPY_CONSTRUCTOR(NullConstant);
2886
Roland Levillain9867bc72015-08-05 10:21:34 +01002887 private:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002888 explicit HNullConstant(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302889 : HConstant(kNullConstant, DataType::Type::kReference, dex_pc) {
2890 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002891
2892 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01002893};
2894
2895// Constants of the type int. Those can be from Dex instructions, or
2896// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002897class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002898 public:
2899 int32_t GetValue() const { return value_; }
2900
David Brazdil9f389d42015-10-01 14:32:56 +01002901 uint64_t GetValueAsUint64() const OVERRIDE {
2902 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2903 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002904
Vladimir Marko372f10e2016-05-17 16:30:10 +01002905 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002906 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002907 return other->AsIntConstant()->value_ == value_;
2908 }
2909
2910 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2911
2912 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002913 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2914 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002915 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2916
Roland Levillain1a653882016-03-18 18:05:57 +00002917 // Integer constants are used to encode Boolean values as well,
2918 // where 1 means true and 0 means false.
2919 bool IsTrue() const { return GetValue() == 1; }
2920 bool IsFalse() const { return GetValue() == 0; }
2921
Roland Levillain9867bc72015-08-05 10:21:34 +01002922 DECLARE_INSTRUCTION(IntConstant);
2923
Artem Serovcced8ba2017-07-19 18:18:09 +01002924 protected:
2925 DEFAULT_COPY_CONSTRUCTOR(IntConstant);
2926
Roland Levillain9867bc72015-08-05 10:21:34 +01002927 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002928 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302929 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc), value_(value) {
2930 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002931 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302932 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc),
2933 value_(value ? 1 : 0) {
2934 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002935
2936 const int32_t value_;
2937
2938 friend class HGraph;
2939 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2940 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Roland Levillain9867bc72015-08-05 10:21:34 +01002941};
2942
Vladimir Markofcb503c2016-05-18 12:48:17 +01002943class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002944 public:
2945 int64_t GetValue() const { return value_; }
2946
David Brazdil77a48ae2015-09-15 12:34:04 +00002947 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2948
Vladimir Marko372f10e2016-05-17 16:30:10 +01002949 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002950 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002951 return other->AsLongConstant()->value_ == value_;
2952 }
2953
2954 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2955
2956 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002957 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2958 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002959 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2960
2961 DECLARE_INSTRUCTION(LongConstant);
2962
Artem Serovcced8ba2017-07-19 18:18:09 +01002963 protected:
2964 DEFAULT_COPY_CONSTRUCTOR(LongConstant);
2965
Roland Levillain9867bc72015-08-05 10:21:34 +01002966 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002967 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302968 : HConstant(kLongConstant, DataType::Type::kInt64, dex_pc),
2969 value_(value) {
2970 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002971
2972 const int64_t value_;
2973
2974 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01002975};
Dave Allison20dfc792014-06-16 20:44:29 -07002976
Vladimir Markofcb503c2016-05-18 12:48:17 +01002977class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002978 public:
2979 float GetValue() const { return value_; }
2980
2981 uint64_t GetValueAsUint64() const OVERRIDE {
2982 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2983 }
2984
Vladimir Marko372f10e2016-05-17 16:30:10 +01002985 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002986 DCHECK(other->IsFloatConstant()) << other->DebugName();
2987 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2988 }
2989
2990 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2991
2992 bool IsMinusOne() const OVERRIDE {
2993 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2994 }
Roland Levillain1a653882016-03-18 18:05:57 +00002995 bool IsArithmeticZero() const OVERRIDE {
2996 return std::fpclassify(value_) == FP_ZERO;
2997 }
2998 bool IsArithmeticPositiveZero() const {
2999 return IsArithmeticZero() && !std::signbit(value_);
3000 }
3001 bool IsArithmeticNegativeZero() const {
3002 return IsArithmeticZero() && std::signbit(value_);
3003 }
3004 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003005 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00003006 }
3007 bool IsOne() const OVERRIDE {
3008 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3009 }
3010 bool IsNaN() const {
3011 return std::isnan(value_);
3012 }
3013
3014 DECLARE_INSTRUCTION(FloatConstant);
3015
Artem Serovcced8ba2017-07-19 18:18:09 +01003016 protected:
3017 DEFAULT_COPY_CONSTRUCTOR(FloatConstant);
3018
Roland Levillain31dd3d62016-02-16 12:21:02 +00003019 private:
3020 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303021 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3022 value_(value) {
3023 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003024 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303025 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3026 value_(bit_cast<float, int32_t>(value)) {
3027 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003028
3029 const float value_;
3030
3031 // Only the SsaBuilder and HGraph can create floating-point constants.
3032 friend class SsaBuilder;
3033 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003034};
3035
Vladimir Markofcb503c2016-05-18 12:48:17 +01003036class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003037 public:
3038 double GetValue() const { return value_; }
3039
3040 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
3041
Vladimir Marko372f10e2016-05-17 16:30:10 +01003042 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003043 DCHECK(other->IsDoubleConstant()) << other->DebugName();
3044 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
3045 }
3046
3047 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
3048
3049 bool IsMinusOne() const OVERRIDE {
3050 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
3051 }
Roland Levillain1a653882016-03-18 18:05:57 +00003052 bool IsArithmeticZero() const OVERRIDE {
3053 return std::fpclassify(value_) == FP_ZERO;
3054 }
3055 bool IsArithmeticPositiveZero() const {
3056 return IsArithmeticZero() && !std::signbit(value_);
3057 }
3058 bool IsArithmeticNegativeZero() const {
3059 return IsArithmeticZero() && std::signbit(value_);
3060 }
3061 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003062 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00003063 }
3064 bool IsOne() const OVERRIDE {
3065 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3066 }
3067 bool IsNaN() const {
3068 return std::isnan(value_);
3069 }
3070
3071 DECLARE_INSTRUCTION(DoubleConstant);
3072
Artem Serovcced8ba2017-07-19 18:18:09 +01003073 protected:
3074 DEFAULT_COPY_CONSTRUCTOR(DoubleConstant);
3075
Roland Levillain31dd3d62016-02-16 12:21:02 +00003076 private:
3077 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303078 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3079 value_(value) {
3080 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003081 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303082 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3083 value_(bit_cast<double, int64_t>(value)) {
3084 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003085
3086 const double value_;
3087
3088 // Only the SsaBuilder and HGraph can create floating-point constants.
3089 friend class SsaBuilder;
3090 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003091};
3092
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003093// Conditional branch. A block ending with an HIf instruction must have
3094// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003095class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003096 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003097 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303098 : HTemplateInstruction(kIf, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003099 SetRawInputAt(0, input);
3100 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003101
Artem Serovcced8ba2017-07-19 18:18:09 +01003102 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003103 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003104
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003105 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003106 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003107 }
3108
3109 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003110 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003111 }
3112
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003113 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003114
Artem Serovcced8ba2017-07-19 18:18:09 +01003115 protected:
3116 DEFAULT_COPY_CONSTRUCTOR(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003117};
3118
David Brazdilfc6a86a2015-06-26 10:33:45 +00003119
3120// Abstract instruction which marks the beginning and/or end of a try block and
3121// links it to the respective exception handlers. Behaves the same as a Goto in
3122// non-exceptional control flow.
3123// Normal-flow successor is stored at index zero, exception handlers under
3124// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003125class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003126 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003127 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01003128 kEntry,
3129 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003130 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01003131 };
3132
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003133 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303134 : HTemplateInstruction(kTryBoundary, SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003135 SetPackedField<BoundaryKindField>(kind);
3136 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003137
3138 bool IsControlFlow() const OVERRIDE { return true; }
3139
3140 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01003141 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003142
David Brazdild26a4112015-11-10 11:07:31 +00003143 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
3144 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
3145 }
3146
David Brazdilfc6a86a2015-06-26 10:33:45 +00003147 // Returns whether `handler` is among its exception handlers (non-zero index
3148 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01003149 bool HasExceptionHandler(const HBasicBlock& handler) const {
3150 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00003151 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00003152 }
3153
3154 // If not present already, adds `handler` to its block's list of exception
3155 // handlers.
3156 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01003157 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003158 GetBlock()->AddSuccessor(handler);
3159 }
3160 }
3161
Vladimir Markoa1de9182016-02-25 11:37:38 +00003162 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
3163 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003164
David Brazdilffee3d32015-07-06 11:48:53 +01003165 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
3166
David Brazdilfc6a86a2015-06-26 10:33:45 +00003167 DECLARE_INSTRUCTION(TryBoundary);
3168
Artem Serovcced8ba2017-07-19 18:18:09 +01003169 protected:
3170 DEFAULT_COPY_CONSTRUCTOR(TryBoundary);
3171
David Brazdilfc6a86a2015-06-26 10:33:45 +00003172 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003173 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
3174 static constexpr size_t kFieldBoundaryKindSize =
3175 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
3176 static constexpr size_t kNumberOfTryBoundaryPackedBits =
3177 kFieldBoundaryKind + kFieldBoundaryKindSize;
3178 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
3179 "Too many packed fields.");
3180 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00003181};
3182
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003183// Deoptimize to interpreter, upon checking a condition.
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003184class HDeoptimize FINAL : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003185 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003186 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
3187 // across.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003188 HDeoptimize(ArenaAllocator* allocator,
3189 HInstruction* cond,
3190 DeoptimizationKind kind,
3191 uint32_t dex_pc)
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003192 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303193 kDeoptimize,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003194 SideEffects::All(),
3195 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003196 allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003197 /* number_of_inputs */ 1,
3198 kArenaAllocMisc) {
3199 SetPackedFlag<kFieldCanBeMoved>(false);
3200 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003201 SetRawInputAt(0, cond);
3202 }
3203
Artem Serovcced8ba2017-07-19 18:18:09 +01003204 bool IsClonable() const OVERRIDE { return true; }
3205
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003206 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
3207 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
3208 // instead of `guard`.
3209 // We set CanTriggerGC to prevent any intermediate address to be live
3210 // at the point of the `HDeoptimize`.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003211 HDeoptimize(ArenaAllocator* allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003212 HInstruction* cond,
3213 HInstruction* guard,
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003214 DeoptimizationKind kind,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003215 uint32_t dex_pc)
3216 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303217 kDeoptimize,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003218 SideEffects::CanTriggerGC(),
3219 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003220 allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003221 /* number_of_inputs */ 2,
3222 kArenaAllocMisc) {
3223 SetPackedFlag<kFieldCanBeMoved>(true);
3224 SetPackedField<DeoptimizeKindField>(kind);
3225 SetRawInputAt(0, cond);
3226 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003227 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003228
3229 bool CanBeMoved() const OVERRIDE { return GetPackedFlag<kFieldCanBeMoved>(); }
3230
3231 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
3232 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
3233 }
3234
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003235 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003236
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003237 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003238
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003239 DeoptimizationKind GetDeoptimizationKind() const { return GetPackedField<DeoptimizeKindField>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003240
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003241 DataType::Type GetType() const OVERRIDE {
3242 return GuardsAnInput() ? GuardedInput()->GetType() : DataType::Type::kVoid;
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003243 }
3244
3245 bool GuardsAnInput() const {
3246 return InputCount() == 2;
3247 }
3248
3249 HInstruction* GuardedInput() const {
3250 DCHECK(GuardsAnInput());
3251 return InputAt(1);
3252 }
3253
3254 void RemoveGuard() {
3255 RemoveInputAt(1);
3256 }
3257
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003258 DECLARE_INSTRUCTION(Deoptimize);
3259
Artem Serovcced8ba2017-07-19 18:18:09 +01003260 protected:
3261 DEFAULT_COPY_CONSTRUCTOR(Deoptimize);
3262
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003263 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003264 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3265 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3266 static constexpr size_t kFieldDeoptimizeKindSize =
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003267 MinimumBitsToStore(static_cast<size_t>(DeoptimizationKind::kLast));
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003268 static constexpr size_t kNumberOfDeoptimizePackedBits =
3269 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3270 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3271 "Too many packed fields.");
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003272 using DeoptimizeKindField =
3273 BitField<DeoptimizationKind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003274};
3275
Mingyao Yang063fc772016-08-02 11:02:54 -07003276// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3277// The compiled code checks this flag value in a guard before devirtualized call and
3278// if it's true, starts to do deoptimization.
3279// It has a 4-byte slot on stack.
3280// TODO: allocate a register for this flag.
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003281class HShouldDeoptimizeFlag FINAL : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003282 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003283 // CHA guards are only optimized in a separate pass and it has no side effects
3284 // with regard to other passes.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003285 HShouldDeoptimizeFlag(ArenaAllocator* allocator, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303286 : HVariableInputSizeInstruction(kShouldDeoptimizeFlag,
3287 SideEffects::None(),
3288 dex_pc,
3289 allocator,
3290 0,
3291 kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003292 }
3293
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003294 DataType::Type GetType() const OVERRIDE { return DataType::Type::kInt32; }
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003295
3296 // We do all CHA guard elimination/motion in a single pass, after which there is no
3297 // further guard elimination/motion since a guard might have been used for justification
3298 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3299 // to avoid other optimizations trying to move it.
Mingyao Yang063fc772016-08-02 11:02:54 -07003300 bool CanBeMoved() const OVERRIDE { return false; }
3301
3302 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3303
Artem Serovcced8ba2017-07-19 18:18:09 +01003304 protected:
3305 DEFAULT_COPY_CONSTRUCTOR(ShouldDeoptimizeFlag);
Mingyao Yang063fc772016-08-02 11:02:54 -07003306};
3307
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003308// Represents the ArtMethod that was passed as a first argument to
3309// the method. It is used by instructions that depend on it, like
3310// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003311class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003312 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003313 explicit HCurrentMethod(DataType::Type type, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303314 : HExpression(kCurrentMethod, type, SideEffects::None(), dex_pc) {
3315 }
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003316
3317 DECLARE_INSTRUCTION(CurrentMethod);
3318
Artem Serovcced8ba2017-07-19 18:18:09 +01003319 protected:
3320 DEFAULT_COPY_CONSTRUCTOR(CurrentMethod);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003321};
3322
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003323// Fetches an ArtMethod from the virtual table or the interface method table
3324// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003325class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003326 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003327 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003328 kVTable,
3329 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003330 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003331 };
3332 HClassTableGet(HInstruction* cls,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003333 DataType::Type type,
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003334 TableKind kind,
3335 size_t index,
3336 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303337 : HExpression(kClassTableGet, type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003338 index_(index) {
3339 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003340 SetRawInputAt(0, cls);
3341 }
3342
Artem Serovcced8ba2017-07-19 18:18:09 +01003343 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003344 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003345 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003346 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003347 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003348 }
3349
Vladimir Markoa1de9182016-02-25 11:37:38 +00003350 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003351 size_t GetIndex() const { return index_; }
3352
3353 DECLARE_INSTRUCTION(ClassTableGet);
3354
Artem Serovcced8ba2017-07-19 18:18:09 +01003355 protected:
3356 DEFAULT_COPY_CONSTRUCTOR(ClassTableGet);
3357
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003358 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003359 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
3360 static constexpr size_t kFieldTableKindSize =
3361 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3362 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3363 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3364 "Too many packed fields.");
3365 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3366
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003367 // The index of the ArtMethod in the table.
3368 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003369};
3370
Mark Mendellfe57faa2015-09-18 09:26:15 -04003371// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3372// have one successor for each entry in the switch table, and the final successor
3373// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003374class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003375 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003376 HPackedSwitch(int32_t start_value,
3377 uint32_t num_entries,
3378 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003379 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303380 : HTemplateInstruction(kPackedSwitch, SideEffects::None(), dex_pc),
Mark Mendellfe57faa2015-09-18 09:26:15 -04003381 start_value_(start_value),
3382 num_entries_(num_entries) {
3383 SetRawInputAt(0, input);
3384 }
3385
Artem Serovcced8ba2017-07-19 18:18:09 +01003386 bool IsClonable() const OVERRIDE { return true; }
3387
Mark Mendellfe57faa2015-09-18 09:26:15 -04003388 bool IsControlFlow() const OVERRIDE { return true; }
3389
3390 int32_t GetStartValue() const { return start_value_; }
3391
Vladimir Marko211c2112015-09-24 16:52:33 +01003392 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003393
3394 HBasicBlock* GetDefaultBlock() const {
3395 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003396 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003397 }
3398 DECLARE_INSTRUCTION(PackedSwitch);
3399
Artem Serovcced8ba2017-07-19 18:18:09 +01003400 protected:
3401 DEFAULT_COPY_CONSTRUCTOR(PackedSwitch);
3402
Mark Mendellfe57faa2015-09-18 09:26:15 -04003403 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003404 const int32_t start_value_;
3405 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003406};
3407
Roland Levillain88cb1752014-10-20 16:36:47 +01003408class HUnaryOperation : public HExpression<1> {
3409 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303410 HUnaryOperation(InstructionKind kind,
3411 DataType::Type result_type,
3412 HInstruction* input,
3413 uint32_t dex_pc = kNoDexPc)
3414 : HExpression(kind, result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003415 SetRawInputAt(0, input);
3416 }
3417
Artem Serovcced8ba2017-07-19 18:18:09 +01003418 // All of the UnaryOperation instructions are clonable.
3419 bool IsClonable() const OVERRIDE { return true; }
3420
Roland Levillain88cb1752014-10-20 16:36:47 +01003421 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003422 DataType::Type GetResultType() const { return GetType(); }
Roland Levillain88cb1752014-10-20 16:36:47 +01003423
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003424 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003425 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003426 return true;
3427 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003428
Roland Levillain31dd3d62016-02-16 12:21:02 +00003429 // Try to statically evaluate `this` and return a HConstant
3430 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003431 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003432 HConstant* TryStaticEvaluation() const;
3433
3434 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003435 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3436 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003437 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3438 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003439
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003440 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003441
Artem Serovcced8ba2017-07-19 18:18:09 +01003442 protected:
3443 DEFAULT_COPY_CONSTRUCTOR(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003444};
3445
Dave Allison20dfc792014-06-16 20:44:29 -07003446class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003447 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303448 HBinaryOperation(InstructionKind kind,
3449 DataType::Type result_type,
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003450 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003451 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003452 SideEffects side_effects = SideEffects::None(),
3453 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303454 : HExpression(kind, result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003455 SetRawInputAt(0, left);
3456 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003457 }
3458
Artem Serovcced8ba2017-07-19 18:18:09 +01003459 // All of the BinaryOperation instructions are clonable.
3460 bool IsClonable() const OVERRIDE { return true; }
3461
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003462 HInstruction* GetLeft() const { return InputAt(0); }
3463 HInstruction* GetRight() const { return InputAt(1); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003464 DataType::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003465
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003466 virtual bool IsCommutative() const { return false; }
3467
3468 // Put constant on the right.
3469 // Returns whether order is changed.
3470 bool OrderInputsWithConstantOnTheRight() {
3471 HInstruction* left = InputAt(0);
3472 HInstruction* right = InputAt(1);
3473 if (left->IsConstant() && !right->IsConstant()) {
3474 ReplaceInput(right, 0);
3475 ReplaceInput(left, 1);
3476 return true;
3477 }
3478 return false;
3479 }
3480
3481 // Order inputs by instruction id, but favor constant on the right side.
3482 // This helps GVN for commutative ops.
3483 void OrderInputs() {
3484 DCHECK(IsCommutative());
3485 HInstruction* left = InputAt(0);
3486 HInstruction* right = InputAt(1);
3487 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3488 return;
3489 }
3490 if (OrderInputsWithConstantOnTheRight()) {
3491 return;
3492 }
3493 // Order according to instruction id.
3494 if (left->GetId() > right->GetId()) {
3495 ReplaceInput(right, 0);
3496 ReplaceInput(left, 1);
3497 }
3498 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003499
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003500 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003501 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003502 return true;
3503 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003504
Roland Levillain31dd3d62016-02-16 12:21:02 +00003505 // Try to statically evaluate `this` and return a HConstant
3506 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003507 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003508 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003509
3510 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003511 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3512 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003513 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3514 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003515 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003516 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3517 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003518 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3519 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003520 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3521 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003522 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003523 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3524 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003525
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003526 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003527 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003528 HConstant* GetConstantRight() const;
3529
3530 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003531 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003532 HInstruction* GetLeastConstantLeft() const;
3533
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003534 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003535
Artem Serovcced8ba2017-07-19 18:18:09 +01003536 protected:
3537 DEFAULT_COPY_CONSTRUCTOR(BinaryOperation);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003538};
3539
Mark Mendellc4701932015-04-10 13:18:51 -04003540// The comparison bias applies for floating point operations and indicates how NaN
3541// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003542enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003543 kNoBias, // bias is not applicable (i.e. for long operation)
3544 kGtBias, // return 1 for NaN comparisons
3545 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003546 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003547};
3548
Roland Levillain31dd3d62016-02-16 12:21:02 +00003549std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3550
Dave Allison20dfc792014-06-16 20:44:29 -07003551class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003552 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303553 HCondition(InstructionKind kind,
3554 HInstruction* first,
3555 HInstruction* second,
3556 uint32_t dex_pc = kNoDexPc)
3557 : HBinaryOperation(kind,
3558 DataType::Type::kBool,
3559 first,
3560 second,
3561 SideEffects::None(),
3562 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003563 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3564 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003565
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003566 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003567 // `instruction`, and disregard moves in between.
3568 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003569
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003570 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003571
3572 virtual IfCondition GetCondition() const = 0;
3573
Mark Mendellc4701932015-04-10 13:18:51 -04003574 virtual IfCondition GetOppositeCondition() const = 0;
3575
Vladimir Markoa1de9182016-02-25 11:37:38 +00003576 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003577 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3578
Vladimir Markoa1de9182016-02-25 11:37:38 +00003579 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3580 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003581
Vladimir Marko372f10e2016-05-17 16:30:10 +01003582 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003583 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003584 }
3585
Roland Levillain4fa13f62015-07-06 18:11:54 +01003586 bool IsFPConditionTrueIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003587 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003588 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003589 if (if_cond == kCondNE) {
3590 return true;
3591 } else if (if_cond == kCondEQ) {
3592 return false;
3593 }
3594 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003595 }
3596
3597 bool IsFPConditionFalseIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003598 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003599 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003600 if (if_cond == kCondEQ) {
3601 return true;
3602 } else if (if_cond == kCondNE) {
3603 return false;
3604 }
3605 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003606 }
3607
Roland Levillain31dd3d62016-02-16 12:21:02 +00003608 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003609 // Needed if we merge a HCompare into a HCondition.
3610 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3611 static constexpr size_t kFieldComparisonBiasSize =
3612 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3613 static constexpr size_t kNumberOfConditionPackedBits =
3614 kFieldComparisonBias + kFieldComparisonBiasSize;
3615 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3616 using ComparisonBiasField =
3617 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3618
Roland Levillain31dd3d62016-02-16 12:21:02 +00003619 template <typename T>
3620 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3621
3622 template <typename T>
3623 int32_t CompareFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003624 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003625 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3626 // Handle the bias.
3627 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3628 }
3629
3630 // Return an integer constant containing the result of a condition evaluated at compile time.
3631 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3632 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3633 }
3634
Artem Serovcced8ba2017-07-19 18:18:09 +01003635 DEFAULT_COPY_CONSTRUCTOR(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003636};
3637
3638// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003639class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003640 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003641 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303642 : HCondition(kEqual, first, second, dex_pc) {
3643 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003644
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003645 bool IsCommutative() const OVERRIDE { return true; }
3646
Vladimir Marko9e23df52015-11-10 17:14:35 +00003647 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3648 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003649 return MakeConstantCondition(true, GetDexPc());
3650 }
3651 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3652 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3653 }
3654 // In the following Evaluate methods, a HCompare instruction has
3655 // been merged into this HEqual instruction; evaluate it as
3656 // `Compare(x, y) == 0`.
3657 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3658 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3659 GetDexPc());
3660 }
3661 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3662 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3663 }
3664 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3665 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003666 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003667
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003668 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003669
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003670 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003671 return kCondEQ;
3672 }
3673
Mark Mendellc4701932015-04-10 13:18:51 -04003674 IfCondition GetOppositeCondition() const OVERRIDE {
3675 return kCondNE;
3676 }
3677
Artem Serovcced8ba2017-07-19 18:18:09 +01003678 protected:
3679 DEFAULT_COPY_CONSTRUCTOR(Equal);
3680
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003681 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003682 template <typename T> static bool Compute(T x, T y) { return x == y; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003683};
3684
Vladimir Markofcb503c2016-05-18 12:48:17 +01003685class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003686 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303687 HNotEqual(HInstruction* first, HInstruction* second,
3688 uint32_t dex_pc = kNoDexPc)
3689 : HCondition(kNotEqual, first, second, dex_pc) {
3690 }
Dave Allison20dfc792014-06-16 20:44:29 -07003691
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003692 bool IsCommutative() const OVERRIDE { return true; }
3693
Vladimir Marko9e23df52015-11-10 17:14:35 +00003694 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3695 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003696 return MakeConstantCondition(false, GetDexPc());
3697 }
3698 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3699 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3700 }
3701 // In the following Evaluate methods, a HCompare instruction has
3702 // been merged into this HNotEqual instruction; evaluate it as
3703 // `Compare(x, y) != 0`.
3704 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3705 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3706 }
3707 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3708 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3709 }
3710 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3711 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003712 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003713
Dave Allison20dfc792014-06-16 20:44:29 -07003714 DECLARE_INSTRUCTION(NotEqual);
3715
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003716 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003717 return kCondNE;
3718 }
3719
Mark Mendellc4701932015-04-10 13:18:51 -04003720 IfCondition GetOppositeCondition() const OVERRIDE {
3721 return kCondEQ;
3722 }
3723
Artem Serovcced8ba2017-07-19 18:18:09 +01003724 protected:
3725 DEFAULT_COPY_CONSTRUCTOR(NotEqual);
3726
Dave Allison20dfc792014-06-16 20:44:29 -07003727 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003728 template <typename T> static bool Compute(T x, T y) { return x != y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003729};
3730
Vladimir Markofcb503c2016-05-18 12:48:17 +01003731class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003732 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303733 HLessThan(HInstruction* first, HInstruction* second,
3734 uint32_t dex_pc = kNoDexPc)
3735 : HCondition(kLessThan, first, second, dex_pc) {
3736 }
Dave Allison20dfc792014-06-16 20:44:29 -07003737
Roland Levillain9867bc72015-08-05 10:21:34 +01003738 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003739 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003740 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003741 // In the following Evaluate methods, a HCompare instruction has
3742 // been merged into this HLessThan instruction; evaluate it as
3743 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003744 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003745 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3746 }
3747 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3748 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3749 }
3750 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3751 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003752 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003753
Dave Allison20dfc792014-06-16 20:44:29 -07003754 DECLARE_INSTRUCTION(LessThan);
3755
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003756 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003757 return kCondLT;
3758 }
3759
Mark Mendellc4701932015-04-10 13:18:51 -04003760 IfCondition GetOppositeCondition() const OVERRIDE {
3761 return kCondGE;
3762 }
3763
Artem Serovcced8ba2017-07-19 18:18:09 +01003764 protected:
3765 DEFAULT_COPY_CONSTRUCTOR(LessThan);
3766
Dave Allison20dfc792014-06-16 20:44:29 -07003767 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003768 template <typename T> static bool Compute(T x, T y) { return x < y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003769};
3770
Vladimir Markofcb503c2016-05-18 12:48:17 +01003771class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003772 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303773 HLessThanOrEqual(HInstruction* first, HInstruction* second,
3774 uint32_t dex_pc = kNoDexPc)
3775 : HCondition(kLessThanOrEqual, first, second, dex_pc) {
3776 }
Dave Allison20dfc792014-06-16 20:44:29 -07003777
Roland Levillain9867bc72015-08-05 10:21:34 +01003778 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003779 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003780 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003781 // In the following Evaluate methods, a HCompare instruction has
3782 // been merged into this HLessThanOrEqual instruction; evaluate it as
3783 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003784 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003785 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3786 }
3787 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3788 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3789 }
3790 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3791 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003792 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003793
Dave Allison20dfc792014-06-16 20:44:29 -07003794 DECLARE_INSTRUCTION(LessThanOrEqual);
3795
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003796 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003797 return kCondLE;
3798 }
3799
Mark Mendellc4701932015-04-10 13:18:51 -04003800 IfCondition GetOppositeCondition() const OVERRIDE {
3801 return kCondGT;
3802 }
3803
Artem Serovcced8ba2017-07-19 18:18:09 +01003804 protected:
3805 DEFAULT_COPY_CONSTRUCTOR(LessThanOrEqual);
3806
Dave Allison20dfc792014-06-16 20:44:29 -07003807 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003808 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003809};
3810
Vladimir Markofcb503c2016-05-18 12:48:17 +01003811class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003812 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003813 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303814 : HCondition(kGreaterThan, first, second, dex_pc) {
3815 }
Dave Allison20dfc792014-06-16 20:44:29 -07003816
Roland Levillain9867bc72015-08-05 10:21:34 +01003817 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003818 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003819 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003820 // In the following Evaluate methods, a HCompare instruction has
3821 // been merged into this HGreaterThan instruction; evaluate it as
3822 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003823 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003824 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3825 }
3826 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3827 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3828 }
3829 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3830 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003831 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003832
Dave Allison20dfc792014-06-16 20:44:29 -07003833 DECLARE_INSTRUCTION(GreaterThan);
3834
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003835 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003836 return kCondGT;
3837 }
3838
Mark Mendellc4701932015-04-10 13:18:51 -04003839 IfCondition GetOppositeCondition() const OVERRIDE {
3840 return kCondLE;
3841 }
3842
Artem Serovcced8ba2017-07-19 18:18:09 +01003843 protected:
3844 DEFAULT_COPY_CONSTRUCTOR(GreaterThan);
3845
Dave Allison20dfc792014-06-16 20:44:29 -07003846 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003847 template <typename T> static bool Compute(T x, T y) { return x > y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003848};
3849
Vladimir Markofcb503c2016-05-18 12:48:17 +01003850class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003851 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003852 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303853 : HCondition(kGreaterThanOrEqual, first, second, dex_pc) {
3854 }
Dave Allison20dfc792014-06-16 20:44:29 -07003855
Roland Levillain9867bc72015-08-05 10:21:34 +01003856 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003857 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003858 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003859 // In the following Evaluate methods, a HCompare instruction has
3860 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3861 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003862 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003863 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3864 }
3865 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3866 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3867 }
3868 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3869 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003870 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003871
Dave Allison20dfc792014-06-16 20:44:29 -07003872 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3873
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003874 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003875 return kCondGE;
3876 }
3877
Mark Mendellc4701932015-04-10 13:18:51 -04003878 IfCondition GetOppositeCondition() const OVERRIDE {
3879 return kCondLT;
3880 }
3881
Artem Serovcced8ba2017-07-19 18:18:09 +01003882 protected:
3883 DEFAULT_COPY_CONSTRUCTOR(GreaterThanOrEqual);
3884
Dave Allison20dfc792014-06-16 20:44:29 -07003885 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003886 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003887};
3888
Vladimir Markofcb503c2016-05-18 12:48:17 +01003889class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003890 public:
3891 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303892 : HCondition(kBelow, first, second, dex_pc) {
3893 }
Aart Bike9f37602015-10-09 11:15:55 -07003894
3895 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003896 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003897 }
3898 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003899 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3900 }
3901 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3902 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3903 LOG(FATAL) << DebugName() << " is not defined for float values";
3904 UNREACHABLE();
3905 }
3906 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3907 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3908 LOG(FATAL) << DebugName() << " is not defined for double values";
3909 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003910 }
3911
3912 DECLARE_INSTRUCTION(Below);
3913
3914 IfCondition GetCondition() const OVERRIDE {
3915 return kCondB;
3916 }
3917
3918 IfCondition GetOppositeCondition() const OVERRIDE {
3919 return kCondAE;
3920 }
3921
Artem Serovcced8ba2017-07-19 18:18:09 +01003922 protected:
3923 DEFAULT_COPY_CONSTRUCTOR(Below);
3924
Aart Bike9f37602015-10-09 11:15:55 -07003925 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003926 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003927 return MakeUnsigned(x) < MakeUnsigned(y);
3928 }
Aart Bike9f37602015-10-09 11:15:55 -07003929};
3930
Vladimir Markofcb503c2016-05-18 12:48:17 +01003931class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003932 public:
3933 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303934 : HCondition(kBelowOrEqual, first, second, dex_pc) {
3935 }
Aart Bike9f37602015-10-09 11:15:55 -07003936
3937 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003938 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003939 }
3940 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003941 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3942 }
3943 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3944 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3945 LOG(FATAL) << DebugName() << " is not defined for float values";
3946 UNREACHABLE();
3947 }
3948 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3949 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3950 LOG(FATAL) << DebugName() << " is not defined for double values";
3951 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003952 }
3953
3954 DECLARE_INSTRUCTION(BelowOrEqual);
3955
3956 IfCondition GetCondition() const OVERRIDE {
3957 return kCondBE;
3958 }
3959
3960 IfCondition GetOppositeCondition() const OVERRIDE {
3961 return kCondA;
3962 }
3963
Artem Serovcced8ba2017-07-19 18:18:09 +01003964 protected:
3965 DEFAULT_COPY_CONSTRUCTOR(BelowOrEqual);
3966
Aart Bike9f37602015-10-09 11:15:55 -07003967 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003968 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003969 return MakeUnsigned(x) <= MakeUnsigned(y);
3970 }
Aart Bike9f37602015-10-09 11:15:55 -07003971};
3972
Vladimir Markofcb503c2016-05-18 12:48:17 +01003973class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003974 public:
3975 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303976 : HCondition(kAbove, first, second, dex_pc) {
3977 }
Aart Bike9f37602015-10-09 11:15:55 -07003978
3979 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003980 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003981 }
3982 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003983 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3984 }
3985 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3986 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3987 LOG(FATAL) << DebugName() << " is not defined for float values";
3988 UNREACHABLE();
3989 }
3990 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3991 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3992 LOG(FATAL) << DebugName() << " is not defined for double values";
3993 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003994 }
3995
3996 DECLARE_INSTRUCTION(Above);
3997
3998 IfCondition GetCondition() const OVERRIDE {
3999 return kCondA;
4000 }
4001
4002 IfCondition GetOppositeCondition() const OVERRIDE {
4003 return kCondBE;
4004 }
4005
Artem Serovcced8ba2017-07-19 18:18:09 +01004006 protected:
4007 DEFAULT_COPY_CONSTRUCTOR(Above);
4008
Aart Bike9f37602015-10-09 11:15:55 -07004009 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004010 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004011 return MakeUnsigned(x) > MakeUnsigned(y);
4012 }
Aart Bike9f37602015-10-09 11:15:55 -07004013};
4014
Vladimir Markofcb503c2016-05-18 12:48:17 +01004015class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004016 public:
4017 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304018 : HCondition(kAboveOrEqual, first, second, dex_pc) {
4019 }
Aart Bike9f37602015-10-09 11:15:55 -07004020
4021 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004022 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004023 }
4024 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004025 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4026 }
4027 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4028 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4029 LOG(FATAL) << DebugName() << " is not defined for float values";
4030 UNREACHABLE();
4031 }
4032 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4033 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4034 LOG(FATAL) << DebugName() << " is not defined for double values";
4035 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004036 }
4037
4038 DECLARE_INSTRUCTION(AboveOrEqual);
4039
4040 IfCondition GetCondition() const OVERRIDE {
4041 return kCondAE;
4042 }
4043
4044 IfCondition GetOppositeCondition() const OVERRIDE {
4045 return kCondB;
4046 }
4047
Artem Serovcced8ba2017-07-19 18:18:09 +01004048 protected:
4049 DEFAULT_COPY_CONSTRUCTOR(AboveOrEqual);
4050
Aart Bike9f37602015-10-09 11:15:55 -07004051 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004052 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004053 return MakeUnsigned(x) >= MakeUnsigned(y);
4054 }
Aart Bike9f37602015-10-09 11:15:55 -07004055};
Dave Allison20dfc792014-06-16 20:44:29 -07004056
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004057// Instruction to check how two inputs compare to each other.
4058// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01004059class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004060 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00004061 // Note that `comparison_type` is the type of comparison performed
4062 // between the comparison's inputs, not the type of the instantiated
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004063 // HCompare instruction (which is always DataType::Type::kInt).
4064 HCompare(DataType::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004065 HInstruction* first,
4066 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04004067 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004068 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304069 : HBinaryOperation(kCompare,
4070 DataType::Type::kInt32,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004071 first,
4072 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00004073 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004074 dex_pc) {
4075 SetPackedField<ComparisonBiasField>(bias);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004076 DCHECK_EQ(comparison_type, DataType::Kind(first->GetType()));
4077 DCHECK_EQ(comparison_type, DataType::Kind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004078 }
4079
Roland Levillain9867bc72015-08-05 10:21:34 +01004080 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004081 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
4082
4083 template <typename T>
4084 int32_t ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004085 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004086 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
4087 // Handle the bias.
4088 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
4089 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004090
Roland Levillain9867bc72015-08-05 10:21:34 +01004091 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004092 // Note that there is no "cmp-int" Dex instruction so we shouldn't
4093 // reach this code path when processing a freshly built HIR
4094 // graph. However HCompare integer instructions can be synthesized
4095 // by the instruction simplifier to implement IntegerCompare and
4096 // IntegerSignum intrinsics, so we have to handle this case.
4097 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004098 }
4099 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004100 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4101 }
4102 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4103 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4104 }
4105 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4106 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01004107 }
4108
Vladimir Marko372f10e2016-05-17 16:30:10 +01004109 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004110 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00004111 }
4112
Vladimir Markoa1de9182016-02-25 11:37:38 +00004113 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04004114
Roland Levillain31dd3d62016-02-16 12:21:02 +00004115 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00004116 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00004117 bool IsGtBias() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004118 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004119 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00004120 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004121
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004122 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type type ATTRIBUTE_UNUSED) {
Roland Levillain1693a1f2016-03-15 14:57:31 +00004123 // Comparisons do not require a runtime call in any back end.
4124 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004125 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004126
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004127 DECLARE_INSTRUCTION(Compare);
4128
Roland Levillain31dd3d62016-02-16 12:21:02 +00004129 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004130 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
4131 static constexpr size_t kFieldComparisonBiasSize =
4132 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
4133 static constexpr size_t kNumberOfComparePackedBits =
4134 kFieldComparisonBias + kFieldComparisonBiasSize;
4135 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4136 using ComparisonBiasField =
4137 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
4138
Roland Levillain31dd3d62016-02-16 12:21:02 +00004139 // Return an integer constant containing the result of a comparison evaluated at compile time.
4140 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
4141 DCHECK(value == -1 || value == 0 || value == 1) << value;
4142 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
4143 }
4144
Artem Serovcced8ba2017-07-19 18:18:09 +01004145 DEFAULT_COPY_CONSTRUCTOR(Compare);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004146};
4147
Nicolas Geoffray2b615ba2017-01-06 14:40:07 +00004148class HNewInstance FINAL : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00004149 public:
4150 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00004151 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004152 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00004153 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00004154 bool finalizable,
4155 QuickEntrypointEnum entrypoint)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304156 : HExpression(kNewInstance,
4157 DataType::Type::kReference,
4158 SideEffects::CanTriggerGC(),
4159 dex_pc),
David Brazdil6de19382016-01-08 17:37:10 +00004160 type_index_(type_index),
4161 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00004162 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004163 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00004164 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00004165 }
4166
Artem Serovcced8ba2017-07-19 18:18:09 +01004167 bool IsClonable() const OVERRIDE { return true; }
4168
Andreas Gampea5b09a62016-11-17 15:21:22 -08004169 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00004170 const DexFile& GetDexFile() const { return dex_file_; }
4171
4172 // Calls runtime so needs an environment.
4173 bool NeedsEnvironment() const OVERRIDE { return true; }
4174
Mingyao Yang062157f2016-03-02 10:15:36 -08004175 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
4176 bool CanThrow() const OVERRIDE { return true; }
4177
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004178 bool NeedsChecks() const {
4179 return entrypoint_ == kQuickAllocObjectWithChecks;
4180 }
David Brazdil6de19382016-01-08 17:37:10 +00004181
Vladimir Markoa1de9182016-02-25 11:37:38 +00004182 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00004183
4184 bool CanBeNull() const OVERRIDE { return false; }
4185
4186 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4187
4188 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
4189 entrypoint_ = entrypoint;
4190 }
4191
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004192 HLoadClass* GetLoadClass() const {
4193 HInstruction* input = InputAt(0);
4194 if (input->IsClinitCheck()) {
4195 input = input->InputAt(0);
4196 }
4197 DCHECK(input->IsLoadClass());
4198 return input->AsLoadClass();
4199 }
4200
David Brazdil6de19382016-01-08 17:37:10 +00004201 bool IsStringAlloc() const;
4202
4203 DECLARE_INSTRUCTION(NewInstance);
4204
Artem Serovcced8ba2017-07-19 18:18:09 +01004205 protected:
4206 DEFAULT_COPY_CONSTRUCTOR(NewInstance);
4207
David Brazdil6de19382016-01-08 17:37:10 +00004208 private:
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004209 static constexpr size_t kFlagFinalizable = kNumberOfExpressionPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004210 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
4211 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
4212 "Too many packed fields.");
4213
Andreas Gampea5b09a62016-11-17 15:21:22 -08004214 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00004215 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00004216 QuickEntrypointEnum entrypoint_;
David Brazdil6de19382016-01-08 17:37:10 +00004217};
4218
Agi Csaki05f20562015-08-19 14:58:14 -07004219enum IntrinsicNeedsEnvironmentOrCache {
4220 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
4221 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07004222};
4223
Aart Bik5d75afe2015-12-14 11:57:01 -08004224enum IntrinsicSideEffects {
4225 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
4226 kReadSideEffects, // Intrinsic may read heap memory.
4227 kWriteSideEffects, // Intrinsic may write heap memory.
4228 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
4229};
4230
4231enum IntrinsicExceptions {
4232 kNoThrow, // Intrinsic does not throw any exceptions.
4233 kCanThrow // Intrinsic may throw exceptions.
4234};
4235
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004236class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004237 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004238 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004239
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01004240 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004241 SetRawInputAt(index, argument);
4242 }
4243
Roland Levillain3e3d7332015-04-28 11:00:54 +01004244 // Return the number of arguments. This number can be lower than
4245 // the number of inputs returned by InputCount(), as some invoke
4246 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
4247 // inputs at the end of their list of inputs.
4248 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
4249
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004250 DataType::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004251
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004252 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4253
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004254 InvokeType GetInvokeType() const {
4255 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004256 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004257
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01004258 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004259 return intrinsic_;
4260 }
4261
Aart Bik5d75afe2015-12-14 11:57:01 -08004262 void SetIntrinsic(Intrinsics intrinsic,
4263 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
4264 IntrinsicSideEffects side_effects,
4265 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004266
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004267 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00004268 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004269 }
4270
Aart Bikff7d89c2016-11-07 08:49:28 -08004271 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4272
Vladimir Markoa1de9182016-02-25 11:37:38 +00004273 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004274
Aart Bika8b8e9b2018-01-09 11:01:02 -08004275 void SetAlwaysThrows(bool always_throws) { SetPackedFlag<kFlagAlwaysThrows>(always_throws); }
4276
4277 bool AlwaysThrows() const OVERRIDE { return GetPackedFlag<kFlagAlwaysThrows>(); }
4278
Aart Bik71bf7b42016-11-16 10:17:46 -08004279 bool CanBeMoved() const OVERRIDE { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004280
Vladimir Marko372f10e2016-05-17 16:30:10 +01004281 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08004282 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4283 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004284
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004285 uint32_t* GetIntrinsicOptimizations() {
4286 return &intrinsic_optimizations_;
4287 }
4288
4289 const uint32_t* GetIntrinsicOptimizations() const {
4290 return &intrinsic_optimizations_;
4291 }
4292
4293 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4294
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004295 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004296 void SetResolvedMethod(ArtMethod* method) { resolved_method_ = method; }
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004297
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004298 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004299
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004300 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004301 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4302 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004303 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4304 static constexpr size_t kFieldReturnType =
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004305 kFieldInvokeType + kFieldInvokeTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004306 static constexpr size_t kFieldReturnTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004307 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00004308 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
Aart Bika8b8e9b2018-01-09 11:01:02 -08004309 static constexpr size_t kFlagAlwaysThrows = kFlagCanThrow + 1;
4310 static constexpr size_t kNumberOfInvokePackedBits = kFlagAlwaysThrows + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004311 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004312 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004313 using ReturnTypeField = BitField<DataType::Type, kFieldReturnType, kFieldReturnTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004314
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304315 HInvoke(InstructionKind kind,
4316 ArenaAllocator* allocator,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004317 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004318 uint32_t number_of_other_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004319 DataType::Type return_type,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004320 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004321 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004322 ArtMethod* resolved_method,
4323 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004324 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304325 kind,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004326 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4327 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004328 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004329 number_of_arguments + number_of_other_inputs,
4330 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004331 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004332 resolved_method_(resolved_method),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004333 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07004334 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004335 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004336 SetPackedField<ReturnTypeField>(return_type);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004337 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004338 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004339 }
4340
Artem Serovcced8ba2017-07-19 18:18:09 +01004341 DEFAULT_COPY_CONSTRUCTOR(Invoke);
4342
Roland Levillain3e3d7332015-04-28 11:00:54 +01004343 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004344 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004345 const uint32_t dex_method_index_;
4346 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004347
4348 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4349 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004350};
4351
Vladimir Markofcb503c2016-05-18 12:48:17 +01004352class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004353 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004354 HInvokeUnresolved(ArenaAllocator* allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004355 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004356 DataType::Type return_type,
Calin Juravle175dc732015-08-25 15:42:32 +01004357 uint32_t dex_pc,
4358 uint32_t dex_method_index,
4359 InvokeType invoke_type)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304360 : HInvoke(kInvokeUnresolved,
4361 allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004362 number_of_arguments,
4363 0u /* number_of_other_inputs */,
4364 return_type,
4365 dex_pc,
4366 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004367 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01004368 invoke_type) {
4369 }
4370
Artem Serovcced8ba2017-07-19 18:18:09 +01004371 bool IsClonable() const OVERRIDE { return true; }
4372
Calin Juravle175dc732015-08-25 15:42:32 +01004373 DECLARE_INSTRUCTION(InvokeUnresolved);
4374
Artem Serovcced8ba2017-07-19 18:18:09 +01004375 protected:
4376 DEFAULT_COPY_CONSTRUCTOR(InvokeUnresolved);
Calin Juravle175dc732015-08-25 15:42:32 +01004377};
4378
Orion Hodsonac141392017-01-13 11:53:47 +00004379class HInvokePolymorphic FINAL : public HInvoke {
4380 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004381 HInvokePolymorphic(ArenaAllocator* allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004382 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004383 DataType::Type return_type,
Orion Hodsonac141392017-01-13 11:53:47 +00004384 uint32_t dex_pc,
4385 uint32_t dex_method_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304386 : HInvoke(kInvokePolymorphic,
4387 allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004388 number_of_arguments,
4389 0u /* number_of_other_inputs */,
4390 return_type,
4391 dex_pc,
4392 dex_method_index,
4393 nullptr,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304394 kVirtual) {
4395 }
Orion Hodsonac141392017-01-13 11:53:47 +00004396
Artem Serovcced8ba2017-07-19 18:18:09 +01004397 bool IsClonable() const OVERRIDE { return true; }
4398
Orion Hodsonac141392017-01-13 11:53:47 +00004399 DECLARE_INSTRUCTION(InvokePolymorphic);
4400
Artem Serovcced8ba2017-07-19 18:18:09 +01004401 protected:
4402 DEFAULT_COPY_CONSTRUCTOR(InvokePolymorphic);
Orion Hodsonac141392017-01-13 11:53:47 +00004403};
4404
Vladimir Markofcb503c2016-05-18 12:48:17 +01004405class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004406 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004407 // Requirements of this method call regarding the class
4408 // initialization (clinit) check of its declaring class.
4409 enum class ClinitCheckRequirement {
4410 kNone, // Class already initialized.
4411 kExplicit, // Static call having explicit clinit check as last input.
4412 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004413 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004414 };
4415
Vladimir Marko58155012015-08-19 12:49:41 +00004416 // Determines how to load the target ArtMethod*.
4417 enum class MethodLoadKind {
4418 // Use a String init ArtMethod* loaded from Thread entrypoints.
4419 kStringInit,
4420
4421 // Use the method's own ArtMethod* loaded by the register allocator.
4422 kRecursive,
4423
Vladimir Marko65979462017-05-19 17:25:12 +01004424 // Use PC-relative boot image ArtMethod* address that will be known at link time.
4425 // Used for boot image methods referenced by boot image code.
4426 kBootImageLinkTimePcRelative,
4427
Vladimir Marko58155012015-08-19 12:49:41 +00004428 // Use ArtMethod* at a known address, embed the direct address in the code.
4429 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
4430 kDirectAddress,
4431
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004432 // Load from an entry in the .bss section using a PC-relative load.
4433 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
4434 kBssEntry,
Vladimir Marko58155012015-08-19 12:49:41 +00004435
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004436 // Make a runtime call to resolve and call the method. This is the last-resort-kind
4437 // used when other kinds are unimplemented on a particular architecture.
4438 kRuntimeCall,
Vladimir Marko58155012015-08-19 12:49:41 +00004439 };
4440
4441 // Determines the location of the code pointer.
4442 enum class CodePtrLocation {
4443 // Recursive call, use local PC-relative call instruction.
4444 kCallSelf,
4445
Vladimir Marko58155012015-08-19 12:49:41 +00004446 // Use code pointer from the ArtMethod*.
4447 // Used when we don't know the target code. This is also the last-resort-kind used when
4448 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4449 kCallArtMethod,
4450 };
4451
4452 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004453 MethodLoadKind method_load_kind;
4454 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004455 // The method load data holds
4456 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4457 // Note that there are multiple string init methods, each having its own offset.
4458 // - the method address for kDirectAddress
Vladimir Markodc151b22015-10-15 18:02:30 +01004459 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004460 };
4461
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004462 HInvokeStaticOrDirect(ArenaAllocator* allocator,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004463 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004464 DataType::Type return_type,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004465 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004466 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004467 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004468 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004469 InvokeType invoke_type,
4470 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004471 ClinitCheckRequirement clinit_check_requirement)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304472 : HInvoke(kInvokeStaticOrDirect,
4473 allocator,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004474 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004475 // There is potentially one extra argument for the HCurrentMethod node, and
4476 // potentially one other if the clinit check is explicit, and potentially
4477 // one other if the method is a string factory.
4478 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004479 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004480 return_type,
4481 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004482 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004483 resolved_method,
4484 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004485 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004486 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004487 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4488 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004489
Artem Serovcced8ba2017-07-19 18:18:09 +01004490 bool IsClonable() const OVERRIDE { return true; }
4491
Vladimir Markodc151b22015-10-15 18:02:30 +01004492 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004493 bool had_current_method_input = HasCurrentMethodInput();
4494 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4495
4496 // Using the current method is the default and once we find a better
4497 // method load kind, we should not go back to using the current method.
4498 DCHECK(had_current_method_input || !needs_current_method_input);
4499
4500 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004501 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4502 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004503 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004504 dispatch_info_ = dispatch_info;
4505 }
4506
Aart Bik6daebeb2017-04-03 14:35:41 -07004507 DispatchInfo GetDispatchInfo() const {
4508 return dispatch_info_;
4509 }
4510
Vladimir Markoc53c0792015-11-19 15:48:33 +00004511 void AddSpecialInput(HInstruction* input) {
4512 // We allow only one special input.
4513 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4514 DCHECK(InputCount() == GetSpecialInputIndex() ||
4515 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4516 InsertInputAt(GetSpecialInputIndex(), input);
4517 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004518
Vladimir Marko372f10e2016-05-17 16:30:10 +01004519 using HInstruction::GetInputRecords; // Keep the const version visible.
4520 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
4521 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4522 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4523 DCHECK(!input_records.empty());
4524 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4525 HInstruction* last_input = input_records.back().GetInstruction();
4526 // Note: `last_input` may be null during arguments setup.
4527 if (last_input != nullptr) {
4528 // `last_input` is the last input of a static invoke marked as having
4529 // an explicit clinit check. It must either be:
4530 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4531 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4532 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4533 }
4534 }
4535 return input_records;
4536 }
4537
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004538 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004539 // We access the method via the dex cache so we can't do an implicit null check.
4540 // TODO: for intrinsics we can generate implicit null checks.
4541 return false;
4542 }
4543
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004544 bool CanBeNull() const OVERRIDE {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004545 return GetPackedField<ReturnTypeField>() == DataType::Type::kReference && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004546 }
4547
Vladimir Markoc53c0792015-11-19 15:48:33 +00004548 // Get the index of the special input, if any.
4549 //
David Brazdil6de19382016-01-08 17:37:10 +00004550 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4551 // method pointer; otherwise there may be one platform-specific special input,
4552 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004553 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004554 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004555
Vladimir Marko58155012015-08-19 12:49:41 +00004556 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4557 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4558 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004559 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004560 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004561 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko65979462017-05-19 17:25:12 +01004562 bool HasPcRelativeMethodLoadKind() const {
4563 return GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004564 GetMethodLoadKind() == MethodLoadKind::kBssEntry;
Vladimir Marko65979462017-05-19 17:25:12 +01004565 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004566 bool HasCurrentMethodInput() const {
4567 // This function can be called only after the invoke has been fully initialized by the builder.
4568 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004569 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004570 return true;
4571 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004572 DCHECK(InputCount() == GetSpecialInputIndex() ||
4573 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004574 return false;
4575 }
4576 }
Vladimir Marko58155012015-08-19 12:49:41 +00004577
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004578 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004579 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004580 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004581 }
4582
4583 uint64_t GetMethodAddress() const {
4584 DCHECK(HasMethodAddress());
4585 return dispatch_info_.method_load_data;
4586 }
4587
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004588 const DexFile& GetDexFileForPcRelativeDexCache() const;
4589
Vladimir Markoa1de9182016-02-25 11:37:38 +00004590 ClinitCheckRequirement GetClinitCheckRequirement() const {
4591 return GetPackedField<ClinitCheckRequirementField>();
4592 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004593
Roland Levillain4c0eb422015-04-24 16:43:49 +01004594 // Is this instruction a call to a static method?
4595 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004596 return GetInvokeType() == kStatic;
4597 }
4598
4599 MethodReference GetTargetMethod() const {
4600 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004601 }
4602
Vladimir Markofbb184a2015-11-13 14:47:00 +00004603 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4604 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4605 // instruction; only relevant for static calls with explicit clinit check.
4606 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004607 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004608 size_t last_input_index = inputs_.size() - 1u;
4609 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004610 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004611 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004612 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004613 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004614 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004615 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004616 }
4617
4618 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004619 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004620 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004621 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004622 }
4623
4624 // Is this a call to a static method whose declaring class has an
4625 // implicit intialization check requirement?
4626 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004627 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004628 }
4629
Vladimir Markob554b5a2015-11-06 12:57:55 +00004630 // Does this method load kind need the current method as an input?
4631 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004632 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kRuntimeCall;
Vladimir Markob554b5a2015-11-06 12:57:55 +00004633 }
4634
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004635 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004636
Artem Serovcced8ba2017-07-19 18:18:09 +01004637 protected:
4638 DEFAULT_COPY_CONSTRUCTOR(InvokeStaticOrDirect);
4639
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004640 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004641 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004642 static constexpr size_t kFieldClinitCheckRequirementSize =
4643 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4644 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4645 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4646 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4647 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004648 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4649 kFieldClinitCheckRequirement,
4650 kFieldClinitCheckRequirementSize>;
4651
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004652 // Cached values of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffrayea179f42018-02-08 22:30:18 +00004653 const MethodReference target_method_;
Vladimir Marko58155012015-08-19 12:49:41 +00004654 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004655};
Vladimir Markof64242a2015-12-01 14:58:23 +00004656std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004657std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004658
Vladimir Markofcb503c2016-05-18 12:48:17 +01004659class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004660 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004661 HInvokeVirtual(ArenaAllocator* allocator,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004662 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004663 DataType::Type return_type,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004664 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004665 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004666 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004667 uint32_t vtable_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304668 : HInvoke(kInvokeVirtual,
4669 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004670 number_of_arguments,
4671 0u,
4672 return_type,
4673 dex_pc,
4674 dex_method_index,
4675 resolved_method,
4676 kVirtual),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304677 vtable_index_(vtable_index) {
4678 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004679
Artem Serovcced8ba2017-07-19 18:18:09 +01004680 bool IsClonable() const OVERRIDE { return true; }
4681
Aart Bik71bf7b42016-11-16 10:17:46 -08004682 bool CanBeNull() const OVERRIDE {
4683 switch (GetIntrinsic()) {
4684 case Intrinsics::kThreadCurrentThread:
4685 case Intrinsics::kStringBufferAppend:
4686 case Intrinsics::kStringBufferToString:
4687 case Intrinsics::kStringBuilderAppend:
4688 case Intrinsics::kStringBuilderToString:
4689 return false;
4690 default:
4691 return HInvoke::CanBeNull();
4692 }
4693 }
4694
Calin Juravle641547a2015-04-21 22:08:51 +01004695 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004696 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004697 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004698 }
4699
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004700 uint32_t GetVTableIndex() const { return vtable_index_; }
4701
4702 DECLARE_INSTRUCTION(InvokeVirtual);
4703
Artem Serovcced8ba2017-07-19 18:18:09 +01004704 protected:
4705 DEFAULT_COPY_CONSTRUCTOR(InvokeVirtual);
4706
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004707 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004708 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004709 const uint32_t vtable_index_;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004710};
4711
Vladimir Markofcb503c2016-05-18 12:48:17 +01004712class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004713 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004714 HInvokeInterface(ArenaAllocator* allocator,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004715 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004716 DataType::Type return_type,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004717 uint32_t dex_pc,
4718 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004719 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004720 uint32_t imt_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304721 : HInvoke(kInvokeInterface,
4722 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004723 number_of_arguments,
4724 0u,
4725 return_type,
4726 dex_pc,
4727 dex_method_index,
4728 resolved_method,
4729 kInterface),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304730 imt_index_(imt_index) {
4731 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004732
Artem Serovcced8ba2017-07-19 18:18:09 +01004733 bool IsClonable() const OVERRIDE { return true; }
4734
Calin Juravle641547a2015-04-21 22:08:51 +01004735 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004736 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004737 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004738 }
4739
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004740 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
4741 // The assembly stub currently needs it.
4742 return true;
4743 }
4744
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004745 uint32_t GetImtIndex() const { return imt_index_; }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004746
4747 DECLARE_INSTRUCTION(InvokeInterface);
4748
Artem Serovcced8ba2017-07-19 18:18:09 +01004749 protected:
4750 DEFAULT_COPY_CONSTRUCTOR(InvokeInterface);
4751
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004752 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004753 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004754 const uint32_t imt_index_;
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004755};
4756
Vladimir Markofcb503c2016-05-18 12:48:17 +01004757class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004758 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004759 HNeg(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304760 : HUnaryOperation(kNeg, result_type, input, dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004761 DCHECK_EQ(result_type, DataType::Kind(input->GetType()));
Roland Levillain937e6cd2016-03-22 11:54:37 +00004762 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004763
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004764 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004765
4766 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004767 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004768 }
4769 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004770 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004771 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004772 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4773 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4774 }
4775 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4776 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4777 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004778
Roland Levillain88cb1752014-10-20 16:36:47 +01004779 DECLARE_INSTRUCTION(Neg);
4780
Artem Serovcced8ba2017-07-19 18:18:09 +01004781 protected:
4782 DEFAULT_COPY_CONSTRUCTOR(Neg);
Roland Levillain88cb1752014-10-20 16:36:47 +01004783};
4784
Vladimir Markofcb503c2016-05-18 12:48:17 +01004785class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004786 public:
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004787 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304788 : HExpression(kNewArray, DataType::Type::kReference, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004789 SetRawInputAt(0, cls);
4790 SetRawInputAt(1, length);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004791 }
4792
Artem Serovcced8ba2017-07-19 18:18:09 +01004793 bool IsClonable() const OVERRIDE { return true; }
4794
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004795 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004796 bool NeedsEnvironment() const OVERRIDE { return true; }
4797
Mingyao Yang0c365e62015-03-31 15:09:29 -07004798 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4799 bool CanThrow() const OVERRIDE { return true; }
4800
Calin Juravle10e244f2015-01-26 18:54:32 +00004801 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004802
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004803 HLoadClass* GetLoadClass() const {
4804 DCHECK(InputAt(0)->IsLoadClass());
4805 return InputAt(0)->AsLoadClass();
4806 }
4807
4808 HInstruction* GetLength() const {
4809 return InputAt(1);
4810 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004811
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004812 DECLARE_INSTRUCTION(NewArray);
4813
Artem Serovcced8ba2017-07-19 18:18:09 +01004814 protected:
4815 DEFAULT_COPY_CONSTRUCTOR(NewArray);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004816};
4817
Vladimir Markofcb503c2016-05-18 12:48:17 +01004818class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004819 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004820 HAdd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004821 HInstruction* left,
4822 HInstruction* right,
4823 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304824 : HBinaryOperation(kAdd, result_type, left, right, SideEffects::None(), dex_pc) {
4825 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004826
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004827 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004828
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004829 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004830
4831 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004832 return GetBlock()->GetGraph()->GetIntConstant(
4833 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004834 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004835 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004836 return GetBlock()->GetGraph()->GetLongConstant(
4837 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004838 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004839 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4840 return GetBlock()->GetGraph()->GetFloatConstant(
4841 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4842 }
4843 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4844 return GetBlock()->GetGraph()->GetDoubleConstant(
4845 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4846 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004847
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004848 DECLARE_INSTRUCTION(Add);
4849
Artem Serovcced8ba2017-07-19 18:18:09 +01004850 protected:
4851 DEFAULT_COPY_CONSTRUCTOR(Add);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004852};
4853
Vladimir Markofcb503c2016-05-18 12:48:17 +01004854class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004855 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004856 HSub(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004857 HInstruction* left,
4858 HInstruction* right,
4859 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304860 : HBinaryOperation(kSub, result_type, left, right, SideEffects::None(), dex_pc) {
4861 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004862
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004863 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004864
4865 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004866 return GetBlock()->GetGraph()->GetIntConstant(
4867 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004868 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004869 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004870 return GetBlock()->GetGraph()->GetLongConstant(
4871 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004872 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004873 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4874 return GetBlock()->GetGraph()->GetFloatConstant(
4875 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4876 }
4877 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4878 return GetBlock()->GetGraph()->GetDoubleConstant(
4879 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4880 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004881
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004882 DECLARE_INSTRUCTION(Sub);
4883
Artem Serovcced8ba2017-07-19 18:18:09 +01004884 protected:
4885 DEFAULT_COPY_CONSTRUCTOR(Sub);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004886};
4887
Vladimir Markofcb503c2016-05-18 12:48:17 +01004888class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004889 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004890 HMul(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004891 HInstruction* left,
4892 HInstruction* right,
4893 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304894 : HBinaryOperation(kMul, result_type, left, right, SideEffects::None(), dex_pc) {
4895 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004896
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004897 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004898
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004899 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004900
4901 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004902 return GetBlock()->GetGraph()->GetIntConstant(
4903 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004904 }
4905 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004906 return GetBlock()->GetGraph()->GetLongConstant(
4907 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004908 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004909 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4910 return GetBlock()->GetGraph()->GetFloatConstant(
4911 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4912 }
4913 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4914 return GetBlock()->GetGraph()->GetDoubleConstant(
4915 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4916 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004917
4918 DECLARE_INSTRUCTION(Mul);
4919
Artem Serovcced8ba2017-07-19 18:18:09 +01004920 protected:
4921 DEFAULT_COPY_CONSTRUCTOR(Mul);
Calin Juravle34bacdf2014-10-07 20:23:36 +01004922};
4923
Vladimir Markofcb503c2016-05-18 12:48:17 +01004924class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004925 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004926 HDiv(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004927 HInstruction* left,
4928 HInstruction* right,
4929 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304930 : HBinaryOperation(kDiv, result_type, left, right, SideEffects::None(), dex_pc) {
4931 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004932
Roland Levillain9867bc72015-08-05 10:21:34 +01004933 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004934 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004935 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004936 // Our graph structure ensures we never have 0 for `y` during
4937 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004938 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004939 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004940 return (y == -1) ? -x : x / y;
4941 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004942
Roland Levillain31dd3d62016-02-16 12:21:02 +00004943 template <typename T>
4944 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004945 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004946 return x / y;
4947 }
4948
Roland Levillain9867bc72015-08-05 10:21:34 +01004949 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004950 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004951 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004952 }
4953 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004954 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004955 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4956 }
4957 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4958 return GetBlock()->GetGraph()->GetFloatConstant(
4959 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4960 }
4961 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4962 return GetBlock()->GetGraph()->GetDoubleConstant(
4963 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004964 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004965
4966 DECLARE_INSTRUCTION(Div);
4967
Artem Serovcced8ba2017-07-19 18:18:09 +01004968 protected:
4969 DEFAULT_COPY_CONSTRUCTOR(Div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00004970};
4971
Vladimir Markofcb503c2016-05-18 12:48:17 +01004972class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004973 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004974 HRem(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004975 HInstruction* left,
4976 HInstruction* right,
4977 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304978 : HBinaryOperation(kRem, result_type, left, right, SideEffects::None(), dex_pc) {
4979 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004980
Roland Levillain9867bc72015-08-05 10:21:34 +01004981 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004982 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004983 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004984 // Our graph structure ensures we never have 0 for `y` during
4985 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004986 DCHECK_NE(y, 0);
4987 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4988 return (y == -1) ? 0 : x % y;
4989 }
4990
Roland Levillain31dd3d62016-02-16 12:21:02 +00004991 template <typename T>
4992 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004993 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004994 return std::fmod(x, y);
4995 }
4996
Roland Levillain9867bc72015-08-05 10:21:34 +01004997 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004998 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004999 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005000 }
5001 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005002 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005003 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005004 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005005 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
5006 return GetBlock()->GetGraph()->GetFloatConstant(
5007 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5008 }
5009 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
5010 return GetBlock()->GetGraph()->GetDoubleConstant(
5011 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5012 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005013
5014 DECLARE_INSTRUCTION(Rem);
5015
Artem Serovcced8ba2017-07-19 18:18:09 +01005016 protected:
5017 DEFAULT_COPY_CONSTRUCTOR(Rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00005018};
5019
Aart Bik3dad3412018-02-28 12:01:46 -08005020class HAbs FINAL : public HUnaryOperation {
5021 public:
5022 HAbs(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
5023 : HUnaryOperation(kAbs, result_type, input, dex_pc) {}
5024
5025 // Evaluation for integral values.
5026 template <typename T> static T ComputeIntegral(T x) {
5027 return x < 0 ? -x : x;
5028 }
5029
5030 // Evaluation for floating-point values.
5031 // Note, as a "quality of implementation", rather than pure "spec compliance",
5032 // we require that Math.abs() clears the sign bit (but changes nothing else)
5033 // for all floating-point numbers, including NaN (signaling NaN may become quiet though).
5034 // http://b/30758343
5035 template <typename T, typename S> static T ComputeFP(T x) {
5036 S bits = bit_cast<S, T>(x);
5037 return bit_cast<T, S>(bits & std::numeric_limits<S>::max());
5038 }
5039
5040 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
5041 return GetBlock()->GetGraph()->GetIntConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5042 }
5043 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
5044 return GetBlock()->GetGraph()->GetLongConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5045 }
5046 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
5047 return GetBlock()->GetGraph()->GetFloatConstant(
5048 ComputeFP<float, int32_t>(x->GetValue()), GetDexPc());
5049 }
5050 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
5051 return GetBlock()->GetGraph()->GetDoubleConstant(
5052 ComputeFP<double, int64_t>(x->GetValue()), GetDexPc());
5053 }
5054
5055 DECLARE_INSTRUCTION(Abs);
5056
5057 protected:
5058 DEFAULT_COPY_CONSTRUCTOR(Abs);
5059};
5060
Vladimir Markofcb503c2016-05-18 12:48:17 +01005061class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00005062 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00005063 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
5064 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00005065 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305066 : HExpression(kDivZeroCheck, value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00005067 SetRawInputAt(0, value);
5068 }
5069
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005070 DataType::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
Serguei Katkov8c0676c2015-08-03 13:55:33 +06005071
Calin Juravled0d48522014-11-04 16:40:20 +00005072 bool CanBeMoved() const OVERRIDE { return true; }
5073
Vladimir Marko372f10e2016-05-17 16:30:10 +01005074 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00005075 return true;
5076 }
5077
5078 bool NeedsEnvironment() const OVERRIDE { return true; }
5079 bool CanThrow() const OVERRIDE { return true; }
5080
Calin Juravled0d48522014-11-04 16:40:20 +00005081 DECLARE_INSTRUCTION(DivZeroCheck);
5082
Artem Serovcced8ba2017-07-19 18:18:09 +01005083 protected:
5084 DEFAULT_COPY_CONSTRUCTOR(DivZeroCheck);
Calin Juravled0d48522014-11-04 16:40:20 +00005085};
5086
Vladimir Markofcb503c2016-05-18 12:48:17 +01005087class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005088 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005089 HShl(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005090 HInstruction* value,
5091 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005092 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305093 : HBinaryOperation(kShl, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005094 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5095 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005096 }
5097
Roland Levillain5b5b9312016-03-22 14:57:31 +00005098 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005099 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005100 return value << (distance & max_shift_distance);
5101 }
5102
5103 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005104 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005105 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005106 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005107 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005108 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005109 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005110 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005111 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5112 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5113 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5114 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005115 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005116 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5117 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005118 LOG(FATAL) << DebugName() << " is not defined for float values";
5119 UNREACHABLE();
5120 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005121 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5122 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005123 LOG(FATAL) << DebugName() << " is not defined for double values";
5124 UNREACHABLE();
5125 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005126
5127 DECLARE_INSTRUCTION(Shl);
5128
Artem Serovcced8ba2017-07-19 18:18:09 +01005129 protected:
5130 DEFAULT_COPY_CONSTRUCTOR(Shl);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005131};
5132
Vladimir Markofcb503c2016-05-18 12:48:17 +01005133class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005134 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005135 HShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005136 HInstruction* value,
5137 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005138 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305139 : HBinaryOperation(kShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005140 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5141 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005142 }
5143
Roland Levillain5b5b9312016-03-22 14:57:31 +00005144 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005145 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005146 return value >> (distance & max_shift_distance);
5147 }
5148
5149 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005150 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005151 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005152 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005153 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005154 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005155 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005156 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005157 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5158 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5159 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5160 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005161 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005162 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5163 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005164 LOG(FATAL) << DebugName() << " is not defined for float values";
5165 UNREACHABLE();
5166 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005167 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5168 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005169 LOG(FATAL) << DebugName() << " is not defined for double values";
5170 UNREACHABLE();
5171 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005172
5173 DECLARE_INSTRUCTION(Shr);
5174
Artem Serovcced8ba2017-07-19 18:18:09 +01005175 protected:
5176 DEFAULT_COPY_CONSTRUCTOR(Shr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005177};
5178
Vladimir Markofcb503c2016-05-18 12:48:17 +01005179class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005180 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005181 HUShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005182 HInstruction* value,
5183 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005184 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305185 : HBinaryOperation(kUShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005186 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5187 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00005188 }
5189
Roland Levillain5b5b9312016-03-22 14:57:31 +00005190 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005191 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005192 typedef typename std::make_unsigned<T>::type V;
5193 V ux = static_cast<V>(value);
5194 return static_cast<T>(ux >> (distance & max_shift_distance));
5195 }
5196
5197 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005198 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005199 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005200 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005201 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005202 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005203 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005204 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005205 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5206 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5207 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5208 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00005209 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005210 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5211 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005212 LOG(FATAL) << DebugName() << " is not defined for float values";
5213 UNREACHABLE();
5214 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005215 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5216 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005217 LOG(FATAL) << DebugName() << " is not defined for double values";
5218 UNREACHABLE();
5219 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005220
5221 DECLARE_INSTRUCTION(UShr);
5222
Artem Serovcced8ba2017-07-19 18:18:09 +01005223 protected:
5224 DEFAULT_COPY_CONSTRUCTOR(UShr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005225};
5226
Vladimir Markofcb503c2016-05-18 12:48:17 +01005227class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005228 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005229 HAnd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005230 HInstruction* left,
5231 HInstruction* right,
5232 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305233 : HBinaryOperation(kAnd, result_type, left, right, SideEffects::None(), dex_pc) {
5234 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005235
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005236 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005237
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005238 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005239
5240 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005241 return GetBlock()->GetGraph()->GetIntConstant(
5242 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005243 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005244 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005245 return GetBlock()->GetGraph()->GetLongConstant(
5246 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005247 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005248 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5249 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5250 LOG(FATAL) << DebugName() << " is not defined for float values";
5251 UNREACHABLE();
5252 }
5253 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5254 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5255 LOG(FATAL) << DebugName() << " is not defined for double values";
5256 UNREACHABLE();
5257 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005258
5259 DECLARE_INSTRUCTION(And);
5260
Artem Serovcced8ba2017-07-19 18:18:09 +01005261 protected:
5262 DEFAULT_COPY_CONSTRUCTOR(And);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005263};
5264
Vladimir Markofcb503c2016-05-18 12:48:17 +01005265class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005266 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005267 HOr(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005268 HInstruction* left,
5269 HInstruction* right,
5270 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305271 : HBinaryOperation(kOr, result_type, left, right, SideEffects::None(), dex_pc) {
5272 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005273
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005274 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005275
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005276 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005277
5278 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005279 return GetBlock()->GetGraph()->GetIntConstant(
5280 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005281 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005282 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005283 return GetBlock()->GetGraph()->GetLongConstant(
5284 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005285 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005286 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5287 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5288 LOG(FATAL) << DebugName() << " is not defined for float values";
5289 UNREACHABLE();
5290 }
5291 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5292 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5293 LOG(FATAL) << DebugName() << " is not defined for double values";
5294 UNREACHABLE();
5295 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005296
5297 DECLARE_INSTRUCTION(Or);
5298
Artem Serovcced8ba2017-07-19 18:18:09 +01005299 protected:
5300 DEFAULT_COPY_CONSTRUCTOR(Or);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005301};
5302
Vladimir Markofcb503c2016-05-18 12:48:17 +01005303class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005304 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005305 HXor(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005306 HInstruction* left,
5307 HInstruction* right,
5308 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305309 : HBinaryOperation(kXor, result_type, left, right, SideEffects::None(), dex_pc) {
5310 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005311
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005312 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005313
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005314 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005315
5316 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005317 return GetBlock()->GetGraph()->GetIntConstant(
5318 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005319 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005320 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005321 return GetBlock()->GetGraph()->GetLongConstant(
5322 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005323 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005324 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5325 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5326 LOG(FATAL) << DebugName() << " is not defined for float values";
5327 UNREACHABLE();
5328 }
5329 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5330 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5331 LOG(FATAL) << DebugName() << " is not defined for double values";
5332 UNREACHABLE();
5333 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005334
5335 DECLARE_INSTRUCTION(Xor);
5336
Artem Serovcced8ba2017-07-19 18:18:09 +01005337 protected:
5338 DEFAULT_COPY_CONSTRUCTOR(Xor);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005339};
5340
Vladimir Markofcb503c2016-05-18 12:48:17 +01005341class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005342 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005343 HRor(DataType::Type result_type, HInstruction* value, HInstruction* distance)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305344 : HBinaryOperation(kRor, result_type, value, distance) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005345 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5346 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00005347 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005348
Roland Levillain5b5b9312016-03-22 14:57:31 +00005349 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005350 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005351 typedef typename std::make_unsigned<T>::type V;
5352 V ux = static_cast<V>(value);
5353 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005354 return static_cast<T>(ux);
5355 } else {
5356 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005357 return static_cast<T>(ux >> (distance & max_shift_value)) |
5358 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005359 }
5360 }
5361
Roland Levillain5b5b9312016-03-22 14:57:31 +00005362 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005363 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005364 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005365 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005366 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005367 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005368 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005369 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005370 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5371 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5372 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5373 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005374 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005375 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5376 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005377 LOG(FATAL) << DebugName() << " is not defined for float values";
5378 UNREACHABLE();
5379 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005380 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5381 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005382 LOG(FATAL) << DebugName() << " is not defined for double values";
5383 UNREACHABLE();
5384 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005385
5386 DECLARE_INSTRUCTION(Ror);
5387
Artem Serovcced8ba2017-07-19 18:18:09 +01005388 protected:
5389 DEFAULT_COPY_CONSTRUCTOR(Ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005390};
5391
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005392// The value of a parameter in this method. Its location depends on
5393// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01005394class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005395 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005396 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005397 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005398 uint8_t index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005399 DataType::Type parameter_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005400 bool is_this = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305401 : HExpression(kParameterValue, parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005402 dex_file_(dex_file),
5403 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005404 index_(index) {
5405 SetPackedFlag<kFlagIsThis>(is_this);
5406 SetPackedFlag<kFlagCanBeNull>(!is_this);
5407 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005408
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005409 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005410 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005411 uint8_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07005412 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005413
Vladimir Markoa1de9182016-02-25 11:37:38 +00005414 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
5415 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005416
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005417 DECLARE_INSTRUCTION(ParameterValue);
5418
Artem Serovcced8ba2017-07-19 18:18:09 +01005419 protected:
5420 DEFAULT_COPY_CONSTRUCTOR(ParameterValue);
5421
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005422 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005423 // Whether or not the parameter value corresponds to 'this' argument.
5424 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
5425 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5426 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5427 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5428 "Too many packed fields.");
5429
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005430 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005431 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005432 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005433 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005434 const uint8_t index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005435};
5436
Vladimir Markofcb503c2016-05-18 12:48:17 +01005437class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005438 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005439 HNot(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305440 : HUnaryOperation(kNot, result_type, input, dex_pc) {
5441 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005442
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005443 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005444 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005445 return true;
5446 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005447
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005448 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005449
5450 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005451 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005452 }
5453 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005454 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005455 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005456 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5457 LOG(FATAL) << DebugName() << " is not defined for float values";
5458 UNREACHABLE();
5459 }
5460 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5461 LOG(FATAL) << DebugName() << " is not defined for double values";
5462 UNREACHABLE();
5463 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005464
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005465 DECLARE_INSTRUCTION(Not);
5466
Artem Serovcced8ba2017-07-19 18:18:09 +01005467 protected:
5468 DEFAULT_COPY_CONSTRUCTOR(Not);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005469};
5470
Vladimir Markofcb503c2016-05-18 12:48:17 +01005471class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005472 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005473 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305474 : HUnaryOperation(kBooleanNot, DataType::Type::kBool, input, dex_pc) {
5475 }
David Brazdil66d126e2015-04-03 16:02:44 +01005476
5477 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005478 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01005479 return true;
5480 }
5481
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005482 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005483 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005484 return !x;
5485 }
5486
Roland Levillain9867bc72015-08-05 10:21:34 +01005487 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005488 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005489 }
5490 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5491 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005492 UNREACHABLE();
5493 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005494 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5495 LOG(FATAL) << DebugName() << " is not defined for float values";
5496 UNREACHABLE();
5497 }
5498 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5499 LOG(FATAL) << DebugName() << " is not defined for double values";
5500 UNREACHABLE();
5501 }
David Brazdil66d126e2015-04-03 16:02:44 +01005502
5503 DECLARE_INSTRUCTION(BooleanNot);
5504
Artem Serovcced8ba2017-07-19 18:18:09 +01005505 protected:
5506 DEFAULT_COPY_CONSTRUCTOR(BooleanNot);
David Brazdil66d126e2015-04-03 16:02:44 +01005507};
5508
Vladimir Markofcb503c2016-05-18 12:48:17 +01005509class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005510 public:
5511 // Instantiate a type conversion of `input` to `result_type`.
Vladimir Markoc8fb2112017-10-03 11:37:52 +01005512 HTypeConversion(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305513 : HExpression(kTypeConversion, result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005514 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005515 // Invariant: We should never generate a conversion to a Boolean value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005516 DCHECK_NE(DataType::Type::kBool, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005517 }
5518
5519 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005520 DataType::Type GetInputType() const { return GetInput()->GetType(); }
5521 DataType::Type GetResultType() const { return GetType(); }
Roland Levillaindff1f282014-11-05 14:15:05 +00005522
5523 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005524 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5525 return true;
5526 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005527
Mark Mendelle82549b2015-05-06 10:55:34 -04005528 // Try to statically evaluate the conversion and return a HConstant
5529 // containing the result. If the input cannot be converted, return nullptr.
5530 HConstant* TryStaticEvaluation() const;
5531
Roland Levillaindff1f282014-11-05 14:15:05 +00005532 DECLARE_INSTRUCTION(TypeConversion);
5533
Artem Serovcced8ba2017-07-19 18:18:09 +01005534 protected:
5535 DEFAULT_COPY_CONSTRUCTOR(TypeConversion);
Roland Levillaindff1f282014-11-05 14:15:05 +00005536};
5537
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005538static constexpr uint32_t kNoRegNumber = -1;
5539
Vladimir Markofcb503c2016-05-18 12:48:17 +01005540class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005541 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005542 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
5543 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005544 HNullCheck(HInstruction* value, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305545 : HExpression(kNullCheck, value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005546 SetRawInputAt(0, value);
5547 }
5548
Artem Serovcced8ba2017-07-19 18:18:09 +01005549 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00005550 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005551 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005552 return true;
5553 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005554
Calin Juravle10e244f2015-01-26 18:54:32 +00005555 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005556
Calin Juravle10e244f2015-01-26 18:54:32 +00005557 bool CanThrow() const OVERRIDE { return true; }
5558
5559 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005560
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005561 DECLARE_INSTRUCTION(NullCheck);
5562
Artem Serovcced8ba2017-07-19 18:18:09 +01005563 protected:
5564 DEFAULT_COPY_CONSTRUCTOR(NullCheck);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005565};
5566
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005567// Embeds an ArtField and all the information required by the compiler. We cache
5568// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005569class FieldInfo : public ValueObject {
5570 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005571 FieldInfo(ArtField* field,
5572 MemberOffset field_offset,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005573 DataType::Type field_type,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005574 bool is_volatile,
5575 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005576 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005577 const DexFile& dex_file)
5578 : field_(field),
5579 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005580 field_type_(field_type),
5581 is_volatile_(is_volatile),
5582 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005583 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005584 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005585
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005586 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005587 MemberOffset GetFieldOffset() const { return field_offset_; }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005588 DataType::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005589 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005590 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005591 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005592 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005593
5594 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005595 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005596 const MemberOffset field_offset_;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005597 const DataType::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005598 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005599 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005600 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005601 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005602};
5603
Vladimir Markofcb503c2016-05-18 12:48:17 +01005604class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005605 public:
5606 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005607 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005608 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005609 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005610 bool is_volatile,
5611 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005612 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005613 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005614 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305615 : HExpression(kInstanceFieldGet,
5616 field_type,
5617 SideEffects::FieldReadOfType(field_type, is_volatile),
5618 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005619 field_info_(field,
5620 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005621 field_type,
5622 is_volatile,
5623 field_idx,
5624 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005625 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005626 SetRawInputAt(0, value);
5627 }
5628
Artem Serovcced8ba2017-07-19 18:18:09 +01005629 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005630 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005631
Vladimir Marko372f10e2016-05-17 16:30:10 +01005632 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5633 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005634 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005635 }
5636
Calin Juravle641547a2015-04-21 22:08:51 +01005637 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005638 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005639 }
5640
5641 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005642 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5643 }
5644
Calin Juravle52c48962014-12-16 17:02:57 +00005645 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005646 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005647 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005648 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005649
Vladimir Marko61b92282017-10-11 13:23:17 +01005650 void SetType(DataType::Type new_type) {
5651 DCHECK(DataType::IsIntegralType(GetType()));
5652 DCHECK(DataType::IsIntegralType(new_type));
5653 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5654 SetPackedField<TypeField>(new_type);
5655 }
5656
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005657 DECLARE_INSTRUCTION(InstanceFieldGet);
5658
Artem Serovcced8ba2017-07-19 18:18:09 +01005659 protected:
5660 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldGet);
5661
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005662 private:
5663 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005664};
5665
Vladimir Markofcb503c2016-05-18 12:48:17 +01005666class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005667 public:
5668 HInstanceFieldSet(HInstruction* object,
5669 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005670 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005671 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005672 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005673 bool is_volatile,
5674 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005675 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005676 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005677 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305678 : HTemplateInstruction(kInstanceFieldSet,
5679 SideEffects::FieldWriteOfType(field_type, is_volatile),
5680 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005681 field_info_(field,
5682 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005683 field_type,
5684 is_volatile,
5685 field_idx,
5686 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005687 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005688 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005689 SetRawInputAt(0, object);
5690 SetRawInputAt(1, value);
5691 }
5692
Artem Serovcced8ba2017-07-19 18:18:09 +01005693 bool IsClonable() const OVERRIDE { return true; }
5694
Calin Juravle641547a2015-04-21 22:08:51 +01005695 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005696 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005697 }
5698
Calin Juravle52c48962014-12-16 17:02:57 +00005699 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005700 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005701 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005702 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005703 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005704 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5705 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005706
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005707 DECLARE_INSTRUCTION(InstanceFieldSet);
5708
Artem Serovcced8ba2017-07-19 18:18:09 +01005709 protected:
5710 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldSet);
5711
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005712 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005713 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5714 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5715 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5716 "Too many packed fields.");
5717
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005718 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005719};
5720
Vladimir Markofcb503c2016-05-18 12:48:17 +01005721class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005722 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005723 HArrayGet(HInstruction* array,
5724 HInstruction* index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005725 DataType::Type type,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005726 uint32_t dex_pc)
5727 : HArrayGet(array,
5728 index,
5729 type,
5730 SideEffects::ArrayReadOfType(type),
5731 dex_pc,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305732 /* is_string_char_at */ false) {
5733 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005734
5735 HArrayGet(HInstruction* array,
5736 HInstruction* index,
5737 DataType::Type type,
5738 SideEffects side_effects,
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005739 uint32_t dex_pc,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005740 bool is_string_char_at)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305741 : HExpression(kArrayGet, type, side_effects, dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005742 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005743 SetRawInputAt(0, array);
5744 SetRawInputAt(1, index);
5745 }
5746
Artem Serovcced8ba2017-07-19 18:18:09 +01005747 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005748 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005749 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005750 return true;
5751 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005752 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005753 // TODO: We can be smarter here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01005754 // Currently, unless the array is the result of NewArray, the array access is always
5755 // preceded by some form of null NullCheck necessary for the bounds check, usually
5756 // implicit null check on the ArrayLength input to BoundsCheck or Deoptimize for
5757 // dynamic BCE. There are cases when these could be removed to produce better code.
5758 // If we ever add optimizations to do so we should allow an implicit check here
5759 // (as long as the address falls in the first page).
5760 //
5761 // As an example of such fancy optimization, we could eliminate BoundsCheck for
5762 // a = cond ? new int[1] : null;
5763 // a[0]; // The Phi does not need bounds check for either input.
Calin Juravle77520bc2015-01-12 18:45:46 +00005764 return false;
5765 }
5766
David Brazdil4833f5a2015-12-16 10:37:39 +00005767 bool IsEquivalentOf(HArrayGet* other) const {
5768 bool result = (GetDexPc() == other->GetDexPc());
5769 if (kIsDebugBuild && result) {
5770 DCHECK_EQ(GetBlock(), other->GetBlock());
5771 DCHECK_EQ(GetArray(), other->GetArray());
5772 DCHECK_EQ(GetIndex(), other->GetIndex());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005773 if (DataType::IsIntOrLongType(GetType())) {
5774 DCHECK(DataType::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005775 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005776 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
5777 DCHECK(DataType::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005778 }
5779 }
5780 return result;
5781 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005782
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005783 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5784
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005785 HInstruction* GetArray() const { return InputAt(0); }
5786 HInstruction* GetIndex() const { return InputAt(1); }
5787
Vladimir Marko61b92282017-10-11 13:23:17 +01005788 void SetType(DataType::Type new_type) {
5789 DCHECK(DataType::IsIntegralType(GetType()));
5790 DCHECK(DataType::IsIntegralType(new_type));
5791 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5792 SetPackedField<TypeField>(new_type);
5793 }
5794
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005795 DECLARE_INSTRUCTION(ArrayGet);
5796
Artem Serovcced8ba2017-07-19 18:18:09 +01005797 protected:
5798 DEFAULT_COPY_CONSTRUCTOR(ArrayGet);
5799
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005800 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005801 // We treat a String as an array, creating the HArrayGet from String.charAt()
5802 // intrinsic in the instruction simplifier. We can always determine whether
5803 // a particular HArrayGet is actually a String.charAt() by looking at the type
5804 // of the input but that requires holding the mutator lock, so we prefer to use
5805 // a flag, so that code generators don't need to do the locking.
5806 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
5807 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5808 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5809 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005810};
5811
Vladimir Markofcb503c2016-05-18 12:48:17 +01005812class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005813 public:
5814 HArraySet(HInstruction* array,
5815 HInstruction* index,
5816 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005817 DataType::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005818 uint32_t dex_pc)
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005819 : HArraySet(array,
5820 index,
5821 value,
5822 expected_component_type,
5823 // Make a best guess for side effects now, may be refined during SSA building.
5824 ComputeSideEffects(GetComponentType(value->GetType(), expected_component_type)),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305825 dex_pc) {
5826 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005827
5828 HArraySet(HInstruction* array,
5829 HInstruction* index,
5830 HInstruction* value,
5831 DataType::Type expected_component_type,
5832 SideEffects side_effects,
5833 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305834 : HTemplateInstruction(kArraySet, side_effects, dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005835 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005836 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005837 SetPackedFlag<kFlagValueCanBeNull>(true);
5838 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005839 SetRawInputAt(0, array);
5840 SetRawInputAt(1, index);
5841 SetRawInputAt(2, value);
5842 }
5843
Artem Serovcced8ba2017-07-19 18:18:09 +01005844 bool IsClonable() const OVERRIDE { return true; }
5845
Calin Juravle77520bc2015-01-12 18:45:46 +00005846 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005847 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005848 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005849 }
5850
Mingyao Yang81014cb2015-06-02 03:16:27 -07005851 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005852 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005853
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005854 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005855 // TODO: Same as for ArrayGet.
5856 return false;
5857 }
5858
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005859 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005860 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005861 }
5862
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005863 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005864 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005865 }
5866
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005867 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005868 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005869 }
5870
Vladimir Markoa1de9182016-02-25 11:37:38 +00005871 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5872 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5873 bool StaticTypeOfArrayIsObjectArray() const {
5874 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5875 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005876
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005877 HInstruction* GetArray() const { return InputAt(0); }
5878 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005879 HInstruction* GetValue() const { return InputAt(2); }
5880
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005881 DataType::Type GetComponentType() const {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005882 return GetComponentType(GetValue()->GetType(), GetRawExpectedComponentType());
5883 }
5884
5885 static DataType::Type GetComponentType(DataType::Type value_type,
5886 DataType::Type expected_component_type) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005887 // The Dex format does not type floating point index operations. Since the
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005888 // `expected_component_type` comes from SSA building and can therefore not
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005889 // be correct, we also check what is the value type. If it is a floating
5890 // point type, we must use that type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005891 return ((value_type == DataType::Type::kFloat32) || (value_type == DataType::Type::kFloat64))
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005892 ? value_type
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005893 : expected_component_type;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005894 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005895
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005896 DataType::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005897 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005898 }
5899
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005900 static SideEffects ComputeSideEffects(DataType::Type type) {
5901 return SideEffects::ArrayWriteOfType(type).Union(SideEffectsForArchRuntimeCalls(type));
Aart Bik18b36ab2016-04-13 16:41:35 -07005902 }
5903
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005904 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type value_type) {
5905 return (value_type == DataType::Type::kReference) ? SideEffects::CanTriggerGC()
5906 : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005907 }
5908
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005909 DECLARE_INSTRUCTION(ArraySet);
5910
Artem Serovcced8ba2017-07-19 18:18:09 +01005911 protected:
5912 DEFAULT_COPY_CONSTRUCTOR(ArraySet);
5913
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005914 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005915 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5916 static constexpr size_t kFieldExpectedComponentTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005917 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00005918 static constexpr size_t kFlagNeedsTypeCheck =
5919 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5920 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005921 // Cached information for the reference_type_info_ so that codegen
5922 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005923 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5924 static constexpr size_t kNumberOfArraySetPackedBits =
5925 kFlagStaticTypeOfArrayIsObjectArray + 1;
5926 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5927 using ExpectedComponentTypeField =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005928 BitField<DataType::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005929};
5930
Vladimir Markofcb503c2016-05-18 12:48:17 +01005931class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005932 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005933 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305934 : HExpression(kArrayLength, DataType::Type::kInt32, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005935 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005936 // Note that arrays do not change length, so the instruction does not
5937 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005938 SetRawInputAt(0, array);
5939 }
5940
Artem Serovcced8ba2017-07-19 18:18:09 +01005941 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle77520bc2015-01-12 18:45:46 +00005942 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005943 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005944 return true;
5945 }
Calin Juravle641547a2015-04-21 22:08:51 +01005946 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5947 return obj == InputAt(0);
5948 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005949
Vladimir Markodce016e2016-04-28 13:10:02 +01005950 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5951
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005952 DECLARE_INSTRUCTION(ArrayLength);
5953
Artem Serovcced8ba2017-07-19 18:18:09 +01005954 protected:
5955 DEFAULT_COPY_CONSTRUCTOR(ArrayLength);
5956
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005957 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005958 // We treat a String as an array, creating the HArrayLength from String.length()
5959 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5960 // determine whether a particular HArrayLength is actually a String.length() by
5961 // looking at the type of the input but that requires holding the mutator lock, so
5962 // we prefer to use a flag, so that code generators don't need to do the locking.
5963 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5964 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5965 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5966 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005967};
5968
Vladimir Markofcb503c2016-05-18 12:48:17 +01005969class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005970 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005971 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5972 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005973 HBoundsCheck(HInstruction* index,
5974 HInstruction* length,
5975 uint32_t dex_pc,
Vladimir Marko0259c242017-12-04 11:27:47 +00005976 bool is_string_char_at = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305977 : HExpression(kBoundsCheck, index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005978 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(index->GetType()));
Vladimir Marko0259c242017-12-04 11:27:47 +00005979 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005980 SetRawInputAt(0, index);
5981 SetRawInputAt(1, length);
5982 }
5983
Artem Serovcced8ba2017-07-19 18:18:09 +01005984 bool IsClonable() const OVERRIDE { return true; }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005985 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005986 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005987 return true;
5988 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005989
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005990 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005991
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005992 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005993
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005994 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005995
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005996 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005997
5998 DECLARE_INSTRUCTION(BoundsCheck);
5999
Artem Serovcced8ba2017-07-19 18:18:09 +01006000 protected:
6001 DEFAULT_COPY_CONSTRUCTOR(BoundsCheck);
6002
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006003 private:
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00006004 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006005};
6006
Vladimir Markofcb503c2016-05-18 12:48:17 +01006007class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006008 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00006009 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306010 : HTemplateInstruction(kSuspendCheck, SideEffects::CanTriggerGC(), dex_pc),
6011 slow_path_(nullptr) {
6012 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006013
Artem Serovcced8ba2017-07-19 18:18:09 +01006014 bool IsClonable() const OVERRIDE { return true; }
6015
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006016 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006017 return true;
6018 }
6019
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006020 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
6021 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006022
6023 DECLARE_INSTRUCTION(SuspendCheck);
6024
Artem Serovcced8ba2017-07-19 18:18:09 +01006025 protected:
6026 DEFAULT_COPY_CONSTRUCTOR(SuspendCheck);
6027
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006028 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006029 // Only used for code generation, in order to share the same slow path between back edges
6030 // of a same loop.
6031 SlowPathCode* slow_path_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006032};
6033
David Srbecky0cf44932015-12-09 14:09:59 +00006034// Pseudo-instruction which provides the native debugger with mapping information.
6035// It ensures that we can generate line number and local variables at this point.
6036class HNativeDebugInfo : public HTemplateInstruction<0> {
6037 public:
6038 explicit HNativeDebugInfo(uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306039 : HTemplateInstruction<0>(kNativeDebugInfo, SideEffects::None(), dex_pc) {
6040 }
David Srbecky0cf44932015-12-09 14:09:59 +00006041
6042 bool NeedsEnvironment() const OVERRIDE {
6043 return true;
6044 }
6045
6046 DECLARE_INSTRUCTION(NativeDebugInfo);
6047
Artem Serovcced8ba2017-07-19 18:18:09 +01006048 protected:
6049 DEFAULT_COPY_CONSTRUCTOR(NativeDebugInfo);
David Srbecky0cf44932015-12-09 14:09:59 +00006050};
6051
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006052/**
6053 * Instruction to load a Class object.
6054 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006055class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006056 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006057 // Determines how to load the Class.
6058 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006059 // We cannot load this class. See HSharpening::SharpenLoadClass.
6060 kInvalid = -1,
6061
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006062 // Use the Class* from the method's own ArtMethod*.
6063 kReferrersClass,
6064
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006065 // Use PC-relative boot image Class* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006066 // Used for boot image classes referenced by boot image code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006067 kBootImageLinkTimePcRelative,
6068
6069 // Use a known boot image Class* address, embedded in the code by the codegen.
6070 // Used for boot image classes referenced by apps in AOT- and JIT-compiled code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006071 kBootImageAddress,
6072
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006073 // Use a PC-relative load from a boot image ClassTable mmapped into the .bss
6074 // of the oat file.
6075 kBootImageClassTable,
6076
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006077 // Load from an entry in the .bss section using a PC-relative load.
6078 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
6079 kBssEntry,
6080
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006081 // Load from the root table associated with the JIT compiled method.
6082 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006083
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006084 // Load using a simple runtime call. This is the fall-back load kind when
6085 // the codegen is unable to use another appropriate kind.
6086 kRuntimeCall,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006087
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006088 kLast = kRuntimeCall
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006089 };
6090
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006091 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08006092 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006093 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006094 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006095 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01006096 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00006097 bool needs_access_check)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306098 : HInstruction(kLoadClass, SideEffectsForArchRuntimeCalls(), dex_pc),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006099 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006100 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006101 dex_file_(dex_file),
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006102 klass_(klass),
6103 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01006104 // Referrers class should not need access check. We never inline unverified
6105 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006106 DCHECK(!is_referrers_class || !needs_access_check);
6107
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006108 SetPackedField<LoadKindField>(
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006109 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kRuntimeCall);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006110 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00006111 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006112 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006113 }
6114
Artem Serovcced8ba2017-07-19 18:18:09 +01006115 bool IsClonable() const OVERRIDE { return true; }
6116
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006117 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006118
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006119 LoadKind GetLoadKind() const {
6120 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006121 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006122
6123 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006124
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006125 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006126
Andreas Gampea5b09a62016-11-17 15:21:22 -08006127 size_t ComputeHashCode() const OVERRIDE { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006128
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01006129 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006130
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006131 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006132 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006133 }
6134
Calin Juravle0ba218d2015-05-19 18:46:01 +01006135 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00006136 // The entrypoint the code generator is going to call does not do
6137 // clinit of the class.
6138 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00006139 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006140 }
6141
6142 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00006143 return NeedsAccessCheck() ||
6144 MustGenerateClinitCheck() ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006145 GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006146 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01006147 }
6148
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006149 bool CanThrow() const OVERRIDE {
Vladimir Marko41559982017-01-06 14:04:23 +00006150 return NeedsAccessCheck() ||
6151 MustGenerateClinitCheck() ||
6152 // If the class is in the boot image, the lookup in the runtime call cannot throw.
6153 // This keeps CanThrow() consistent between non-PIC (using kBootImageAddress) and
6154 // PIC and subsequently avoids a DCE behavior dependency on the PIC option.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006155 ((GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006156 GetLoadKind() == LoadKind::kBssEntry) &&
6157 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006158 }
6159
Calin Juravleacf735c2015-02-12 15:25:22 +00006160 ReferenceTypeInfo GetLoadedClassRTI() {
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006161 return loaded_class_rti_;
Calin Juravleacf735c2015-02-12 15:25:22 +00006162 }
6163
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006164 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
6165 // Make sure we only set exact types (the loaded class should never be merged).
6166 DCHECK(rti.IsExact());
6167 loaded_class_rti_ = rti;
Calin Juravleacf735c2015-02-12 15:25:22 +00006168 }
6169
Andreas Gampea5b09a62016-11-17 15:21:22 -08006170 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006171 const DexFile& GetDexFile() const { return dex_file_; }
6172
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006173 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006174 return GetLoadKind() == LoadKind::kRuntimeCall;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006175 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00006176
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006177 static SideEffects SideEffectsForArchRuntimeCalls() {
6178 return SideEffects::CanTriggerGC();
6179 }
6180
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006181 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006182 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006183 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006184 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006185
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006186 void MarkInBootImage() {
6187 SetPackedFlag<kFlagIsInBootImage>(true);
6188 }
6189
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006190 void AddSpecialInput(HInstruction* special_input);
6191
6192 using HInstruction::GetInputRecords; // Keep the const version visible.
6193 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
6194 return ArrayRef<HUserRecord<HInstruction*>>(
6195 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6196 }
6197
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006198 DataType::Type GetType() const OVERRIDE {
6199 return DataType::Type::kReference;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006200 }
6201
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006202 Handle<mirror::Class> GetClass() const {
6203 return klass_;
6204 }
6205
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006206 DECLARE_INSTRUCTION(LoadClass);
6207
Artem Serovcced8ba2017-07-19 18:18:09 +01006208 protected:
6209 DEFAULT_COPY_CONSTRUCTOR(LoadClass);
6210
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006211 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006212 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00006213 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006214 // Whether this instruction must generate the initialization check.
6215 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006216 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006217 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
6218 static constexpr size_t kFieldLoadKindSize =
6219 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006220 static constexpr size_t kNumberOfLoadClassPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006221 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006222 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
6223
6224 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006225 return load_kind == LoadKind::kReferrersClass ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006226 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006227 load_kind == LoadKind::kBootImageClassTable ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006228 load_kind == LoadKind::kBssEntry ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006229 load_kind == LoadKind::kRuntimeCall;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006230 }
6231
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006232 void SetLoadKindInternal(LoadKind load_kind);
6233
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006234 // The special input is the HCurrentMethod for kRuntimeCall or kReferrersClass.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006235 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006236 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006237 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006238
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006239 // A type index and dex file where the class can be accessed. The dex file can be:
6240 // - The compiling method's dex file if the class is defined there too.
6241 // - The compiling method's dex file if the class is referenced there.
6242 // - The dex file where the class is defined. When the load kind can only be
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006243 // kBssEntry or kRuntimeCall, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08006244 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006245 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006246
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006247 Handle<mirror::Class> klass_;
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006248
6249 ReferenceTypeInfo loaded_class_rti_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006250};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006251std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
6252
6253// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006254inline void HLoadClass::SetLoadKind(LoadKind load_kind) {
6255 // The load kind should be determined before inserting the instruction to the graph.
6256 DCHECK(GetBlock() == nullptr);
6257 DCHECK(GetEnvironment() == nullptr);
6258 SetPackedField<LoadKindField>(load_kind);
6259 if (load_kind != LoadKind::kRuntimeCall && load_kind != LoadKind::kReferrersClass) {
6260 special_input_ = HUserRecord<HInstruction*>(nullptr);
6261 }
6262 if (!NeedsEnvironment()) {
6263 SetSideEffects(SideEffects::None());
6264 }
6265}
6266
6267// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006268inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006269 // The special input is used for PC-relative loads on some architectures,
6270 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006271 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006272 GetLoadKind() == LoadKind::kBootImageAddress ||
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006273 GetLoadKind() == LoadKind::kBootImageClassTable ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006274 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006275 DCHECK(special_input_.GetInstruction() == nullptr);
6276 special_input_ = HUserRecord<HInstruction*>(special_input);
6277 special_input->AddUseAt(this, 0);
6278}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006279
Vladimir Marko372f10e2016-05-17 16:30:10 +01006280class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006281 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006282 // Determines how to load the String.
6283 enum class LoadKind {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006284 // Use PC-relative boot image String* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006285 // Used for boot image strings referenced by boot image code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006286 kBootImageLinkTimePcRelative,
6287
6288 // Use a known boot image String* address, embedded in the code by the codegen.
6289 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006290 kBootImageAddress,
6291
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006292 // Use a PC-relative load from a boot image InternTable mmapped into the .bss
6293 // of the oat file.
6294 kBootImageInternTable,
6295
Vladimir Markoaad75c62016-10-03 08:46:48 +00006296 // Load from an entry in the .bss section using a PC-relative load.
6297 // Used for strings outside boot image when .bss is accessible with a PC-relative load.
6298 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006299
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006300 // Load from the root table associated with the JIT compiled method.
6301 kJitTableAddress,
6302
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006303 // Load using a simple runtime call. This is the fall-back load kind when
6304 // the codegen is unable to use another appropriate kind.
6305 kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006306
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006307 kLast = kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006308 };
6309
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006310 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006311 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006312 const DexFile& dex_file,
6313 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306314 : HInstruction(kLoadString, SideEffectsForArchRuntimeCalls(), dex_pc),
Vladimir Marko372f10e2016-05-17 16:30:10 +01006315 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006316 string_index_(string_index),
6317 dex_file_(dex_file) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006318 SetPackedField<LoadKindField>(LoadKind::kRuntimeCall);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006319 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006320
Artem Serovcced8ba2017-07-19 18:18:09 +01006321 bool IsClonable() const OVERRIDE { return true; }
6322
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006323 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006324
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006325 LoadKind GetLoadKind() const {
6326 return GetPackedField<LoadKindField>();
6327 }
6328
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006329 const DexFile& GetDexFile() const {
6330 return dex_file_;
6331 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006332
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006333 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006334 return string_index_;
6335 }
6336
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006337 Handle<mirror::String> GetString() const {
6338 return string_;
6339 }
6340
6341 void SetString(Handle<mirror::String> str) {
6342 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006343 }
6344
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006345 bool CanBeMoved() const OVERRIDE { return true; }
6346
Vladimir Marko372f10e2016-05-17 16:30:10 +01006347 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006348
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006349 size_t ComputeHashCode() const OVERRIDE { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006350
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006351 // Will call the runtime if we need to load the string through
6352 // the dex cache and the string is not guaranteed to be there yet.
6353 bool NeedsEnvironment() const OVERRIDE {
6354 LoadKind load_kind = GetLoadKind();
Vladimir Marko764d4542017-05-16 10:31:41 +01006355 if (load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006356 load_kind == LoadKind::kBootImageAddress ||
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006357 load_kind == LoadKind::kBootImageInternTable ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006358 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006359 return false;
6360 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006361 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006362 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006363
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006364 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006365 return GetLoadKind() == LoadKind::kRuntimeCall;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006366 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006367
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07006368 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006369 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006370
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006371 static SideEffects SideEffectsForArchRuntimeCalls() {
6372 return SideEffects::CanTriggerGC();
6373 }
6374
Vladimir Marko372f10e2016-05-17 16:30:10 +01006375 void AddSpecialInput(HInstruction* special_input);
6376
6377 using HInstruction::GetInputRecords; // Keep the const version visible.
6378 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
6379 return ArrayRef<HUserRecord<HInstruction*>>(
6380 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006381 }
6382
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006383 DataType::Type GetType() const OVERRIDE {
6384 return DataType::Type::kReference;
Vladimir Marko372f10e2016-05-17 16:30:10 +01006385 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006386
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006387 DECLARE_INSTRUCTION(LoadString);
6388
Artem Serovcced8ba2017-07-19 18:18:09 +01006389 protected:
6390 DEFAULT_COPY_CONSTRUCTOR(LoadString);
6391
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006392 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006393 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006394 static constexpr size_t kFieldLoadKindSize =
6395 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
6396 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006397 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006398 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006399
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006400 void SetLoadKindInternal(LoadKind load_kind);
6401
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006402 // The special input is the HCurrentMethod for kRuntimeCall.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006403 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00006404 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01006405 HUserRecord<HInstruction*> special_input_;
6406
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006407 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006408 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006409
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006410 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006411};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006412std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
6413
6414// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006415inline void HLoadString::SetLoadKind(LoadKind load_kind) {
6416 // The load kind should be determined before inserting the instruction to the graph.
6417 DCHECK(GetBlock() == nullptr);
6418 DCHECK(GetEnvironment() == nullptr);
6419 DCHECK_EQ(GetLoadKind(), LoadKind::kRuntimeCall);
6420 SetPackedField<LoadKindField>(load_kind);
6421 if (load_kind != LoadKind::kRuntimeCall) {
6422 special_input_ = HUserRecord<HInstruction*>(nullptr);
6423 }
6424 if (!NeedsEnvironment()) {
6425 SetSideEffects(SideEffects::None());
6426 }
6427}
6428
6429// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006430inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006431 // The special input is used for PC-relative loads on some architectures,
6432 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006433 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006434 GetLoadKind() == LoadKind::kBootImageAddress ||
6435 GetLoadKind() == LoadKind::kBootImageInternTable ||
6436 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01006437 // HLoadString::GetInputRecords() returns an empty array at this point,
6438 // so use the GetInputRecords() from the base class to set the input record.
6439 DCHECK(special_input_.GetInstruction() == nullptr);
6440 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006441 special_input->AddUseAt(this, 0);
6442}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006443
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006444/**
6445 * Performs an initialization check on its Class object input.
6446 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01006447class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006448 public:
Roland Levillain3887c462015-08-12 18:15:42 +01006449 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07006450 : HExpression(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306451 kClinitCheck,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006452 DataType::Type::kReference,
Mingyao Yang217eb062017-12-11 15:20:07 -08006453 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006454 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006455 SetRawInputAt(0, constant);
6456 }
6457
Artem Serovcced8ba2017-07-19 18:18:09 +01006458 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006459 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006460 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006461 return true;
6462 }
6463
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006464 bool NeedsEnvironment() const OVERRIDE {
6465 // May call runtime to initialize the class.
6466 return true;
6467 }
6468
Nicolas Geoffray729645a2015-11-19 13:29:02 +00006469 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006470
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006471 HLoadClass* GetLoadClass() const {
6472 DCHECK(InputAt(0)->IsLoadClass());
6473 return InputAt(0)->AsLoadClass();
6474 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006475
6476 DECLARE_INSTRUCTION(ClinitCheck);
6477
Artem Serovcced8ba2017-07-19 18:18:09 +01006478
6479 protected:
6480 DEFAULT_COPY_CONSTRUCTOR(ClinitCheck);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006481};
6482
Vladimir Markofcb503c2016-05-18 12:48:17 +01006483class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006484 public:
6485 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006486 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006487 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006488 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006489 bool is_volatile,
6490 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006491 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006492 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006493 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306494 : HExpression(kStaticFieldGet,
6495 field_type,
6496 SideEffects::FieldReadOfType(field_type, is_volatile),
6497 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006498 field_info_(field,
6499 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006500 field_type,
6501 is_volatile,
6502 field_idx,
6503 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006504 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006505 SetRawInputAt(0, cls);
6506 }
6507
Calin Juravle52c48962014-12-16 17:02:57 +00006508
Artem Serovcced8ba2017-07-19 18:18:09 +01006509 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle10c9cbe2014-12-19 10:50:19 +00006510 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006511
Vladimir Marko372f10e2016-05-17 16:30:10 +01006512 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
6513 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006514 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006515 }
6516
6517 size_t ComputeHashCode() const OVERRIDE {
6518 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6519 }
6520
Calin Juravle52c48962014-12-16 17:02:57 +00006521 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006522 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006523 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006524 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006525
Vladimir Marko61b92282017-10-11 13:23:17 +01006526 void SetType(DataType::Type new_type) {
6527 DCHECK(DataType::IsIntegralType(GetType()));
6528 DCHECK(DataType::IsIntegralType(new_type));
6529 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6530 SetPackedField<TypeField>(new_type);
6531 }
6532
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006533 DECLARE_INSTRUCTION(StaticFieldGet);
6534
Artem Serovcced8ba2017-07-19 18:18:09 +01006535 protected:
6536 DEFAULT_COPY_CONSTRUCTOR(StaticFieldGet);
6537
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006538 private:
6539 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006540};
6541
Vladimir Markofcb503c2016-05-18 12:48:17 +01006542class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006543 public:
6544 HStaticFieldSet(HInstruction* cls,
6545 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006546 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006547 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006548 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006549 bool is_volatile,
6550 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006551 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006552 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006553 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306554 : HTemplateInstruction(kStaticFieldSet,
6555 SideEffects::FieldWriteOfType(field_type, is_volatile),
6556 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006557 field_info_(field,
6558 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006559 field_type,
6560 is_volatile,
6561 field_idx,
6562 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006563 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006564 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006565 SetRawInputAt(0, cls);
6566 SetRawInputAt(1, value);
6567 }
6568
Artem Serovcced8ba2017-07-19 18:18:09 +01006569 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle52c48962014-12-16 17:02:57 +00006570 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006571 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006572 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006573 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006574
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006575 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006576 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6577 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006578
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006579 DECLARE_INSTRUCTION(StaticFieldSet);
6580
Artem Serovcced8ba2017-07-19 18:18:09 +01006581 protected:
6582 DEFAULT_COPY_CONSTRUCTOR(StaticFieldSet);
6583
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006584 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006585 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6586 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6587 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6588 "Too many packed fields.");
6589
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006590 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006591};
6592
Vladimir Markofcb503c2016-05-18 12:48:17 +01006593class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006594 public:
6595 HUnresolvedInstanceFieldGet(HInstruction* obj,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006596 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006597 uint32_t field_index,
6598 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306599 : HExpression(kUnresolvedInstanceFieldGet,
6600 field_type,
6601 SideEffects::AllExceptGCDependency(),
6602 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006603 field_index_(field_index) {
6604 SetRawInputAt(0, obj);
6605 }
6606
Artem Serovcced8ba2017-07-19 18:18:09 +01006607 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006608 bool NeedsEnvironment() const OVERRIDE { return true; }
6609 bool CanThrow() const OVERRIDE { return true; }
6610
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006611 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006612 uint32_t GetFieldIndex() const { return field_index_; }
6613
6614 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6615
Artem Serovcced8ba2017-07-19 18:18:09 +01006616 protected:
6617 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldGet);
6618
Calin Juravlee460d1d2015-09-29 04:52:17 +01006619 private:
6620 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006621};
6622
Vladimir Markofcb503c2016-05-18 12:48:17 +01006623class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006624 public:
6625 HUnresolvedInstanceFieldSet(HInstruction* obj,
6626 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006627 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006628 uint32_t field_index,
6629 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306630 : HTemplateInstruction(kUnresolvedInstanceFieldSet,
6631 SideEffects::AllExceptGCDependency(),
6632 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006633 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006634 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006635 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006636 SetRawInputAt(0, obj);
6637 SetRawInputAt(1, value);
6638 }
6639
Artem Serovcced8ba2017-07-19 18:18:09 +01006640 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006641 bool NeedsEnvironment() const OVERRIDE { return true; }
6642 bool CanThrow() const OVERRIDE { return true; }
6643
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006644 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006645 uint32_t GetFieldIndex() const { return field_index_; }
6646
6647 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6648
Artem Serovcced8ba2017-07-19 18:18:09 +01006649 protected:
6650 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldSet);
6651
Calin Juravlee460d1d2015-09-29 04:52:17 +01006652 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006653 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6654 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006655 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006656 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6657 kFieldFieldType + kFieldFieldTypeSize;
6658 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6659 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006660 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006661
Calin Juravlee460d1d2015-09-29 04:52:17 +01006662 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006663};
6664
Vladimir Markofcb503c2016-05-18 12:48:17 +01006665class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006666 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006667 HUnresolvedStaticFieldGet(DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006668 uint32_t field_index,
6669 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306670 : HExpression(kUnresolvedStaticFieldGet,
6671 field_type,
6672 SideEffects::AllExceptGCDependency(),
6673 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006674 field_index_(field_index) {
6675 }
6676
Artem Serovcced8ba2017-07-19 18:18:09 +01006677 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006678 bool NeedsEnvironment() const OVERRIDE { return true; }
6679 bool CanThrow() const OVERRIDE { return true; }
6680
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006681 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006682 uint32_t GetFieldIndex() const { return field_index_; }
6683
6684 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6685
Artem Serovcced8ba2017-07-19 18:18:09 +01006686 protected:
6687 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldGet);
6688
Calin Juravlee460d1d2015-09-29 04:52:17 +01006689 private:
6690 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006691};
6692
Vladimir Markofcb503c2016-05-18 12:48:17 +01006693class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006694 public:
6695 HUnresolvedStaticFieldSet(HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006696 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006697 uint32_t field_index,
6698 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306699 : HTemplateInstruction(kUnresolvedStaticFieldSet,
6700 SideEffects::AllExceptGCDependency(),
6701 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006702 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006703 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006704 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006705 SetRawInputAt(0, value);
6706 }
6707
Artem Serovcced8ba2017-07-19 18:18:09 +01006708 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006709 bool NeedsEnvironment() const OVERRIDE { return true; }
6710 bool CanThrow() const OVERRIDE { return true; }
6711
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006712 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006713 uint32_t GetFieldIndex() const { return field_index_; }
6714
6715 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6716
Artem Serovcced8ba2017-07-19 18:18:09 +01006717 protected:
6718 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldSet);
6719
Calin Juravlee460d1d2015-09-29 04:52:17 +01006720 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006721 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6722 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006723 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006724 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6725 kFieldFieldType + kFieldFieldTypeSize;
6726 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6727 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006728 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006729
Calin Juravlee460d1d2015-09-29 04:52:17 +01006730 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006731};
6732
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006733// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006734class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006735 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006736 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306737 : HExpression(kLoadException, DataType::Type::kReference, SideEffects::None(), dex_pc) {
6738 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006739
David Brazdilbbd733e2015-08-18 17:48:17 +01006740 bool CanBeNull() const OVERRIDE { return false; }
6741
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006742 DECLARE_INSTRUCTION(LoadException);
6743
Artem Serovcced8ba2017-07-19 18:18:09 +01006744 protected:
6745 DEFAULT_COPY_CONSTRUCTOR(LoadException);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006746};
6747
David Brazdilcb1c0552015-08-04 16:22:25 +01006748// Implicit part of move-exception which clears thread-local exception storage.
6749// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006750class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006751 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006752 explicit HClearException(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306753 : HTemplateInstruction(kClearException, SideEffects::AllWrites(), dex_pc) {
6754 }
David Brazdilcb1c0552015-08-04 16:22:25 +01006755
6756 DECLARE_INSTRUCTION(ClearException);
6757
Artem Serovcced8ba2017-07-19 18:18:09 +01006758 protected:
6759 DEFAULT_COPY_CONSTRUCTOR(ClearException);
David Brazdilcb1c0552015-08-04 16:22:25 +01006760};
6761
Vladimir Markofcb503c2016-05-18 12:48:17 +01006762class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006763 public:
6764 HThrow(HInstruction* exception, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306765 : HTemplateInstruction(kThrow, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006766 SetRawInputAt(0, exception);
6767 }
6768
6769 bool IsControlFlow() const OVERRIDE { return true; }
6770
6771 bool NeedsEnvironment() const OVERRIDE { return true; }
6772
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006773 bool CanThrow() const OVERRIDE { return true; }
6774
Aart Bika8b8e9b2018-01-09 11:01:02 -08006775 bool AlwaysThrows() const OVERRIDE { return true; }
6776
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006777 DECLARE_INSTRUCTION(Throw);
6778
Artem Serovcced8ba2017-07-19 18:18:09 +01006779 protected:
6780 DEFAULT_COPY_CONSTRUCTOR(Throw);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006781};
6782
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006783/**
6784 * Implementation strategies for the code generator of a HInstanceOf
6785 * or `HCheckCast`.
6786 */
6787enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006788 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006789 kExactCheck, // Can do a single class compare.
6790 kClassHierarchyCheck, // Can just walk the super class chain.
6791 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6792 kInterfaceCheck, // No optimization yet when checking against an interface.
6793 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006794 kArrayCheck, // No optimization yet when checking against a generic array.
6795 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006796};
6797
Roland Levillain86503782016-02-11 19:07:30 +00006798std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6799
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006800class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006801 public:
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006802 HInstanceOf(HInstruction* object,
6803 HLoadClass* target_class,
6804 TypeCheckKind check_kind,
6805 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306806 : HExpression(kInstanceOf,
6807 DataType::Type::kBool,
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006808 SideEffectsForArchRuntimeCalls(check_kind),
6809 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006810 SetPackedField<TypeCheckKindField>(check_kind);
6811 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006812 SetRawInputAt(0, object);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006813 SetRawInputAt(1, target_class);
Vladimir Marko87584542017-12-12 17:47:52 +00006814 }
6815
6816 HLoadClass* GetTargetClass() const {
6817 HInstruction* load_class = InputAt(1);
6818 DCHECK(load_class->IsLoadClass());
6819 return load_class->AsLoadClass();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006820 }
6821
Artem Serovcced8ba2017-07-19 18:18:09 +01006822 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006823 bool CanBeMoved() const OVERRIDE { return true; }
6824
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006825 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6826 return true;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006827 }
6828
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00006829 bool NeedsEnvironment() const OVERRIDE {
6830 return CanCallRuntime(GetTypeCheckKind());
6831 }
6832
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006833 // Used only in code generation.
6834 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6835 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6836 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6837 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
6838
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006839 static bool CanCallRuntime(TypeCheckKind check_kind) {
6840 // Mips currently does runtime calls for any other checks.
6841 return check_kind != TypeCheckKind::kExactCheck;
6842 }
6843
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006844 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006845 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006846 }
6847
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006848 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006849
Artem Serovcced8ba2017-07-19 18:18:09 +01006850 protected:
6851 DEFAULT_COPY_CONSTRUCTOR(InstanceOf);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006852
6853 private:
6854 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6855 static constexpr size_t kFieldTypeCheckKindSize =
6856 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6857 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6858 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6859 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6860 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006861};
6862
Vladimir Markofcb503c2016-05-18 12:48:17 +01006863class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006864 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07006865 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306866 : HExpression(kBoundType, DataType::Type::kReference, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006867 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6868 SetPackedFlag<kFlagUpperCanBeNull>(true);
6869 SetPackedFlag<kFlagCanBeNull>(true);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006870 DCHECK_EQ(input->GetType(), DataType::Type::kReference);
Calin Juravleb1498f62015-02-16 13:13:29 +00006871 SetRawInputAt(0, input);
6872 }
6873
Artem Serovcced8ba2017-07-19 18:18:09 +01006874 bool IsClonable() const OVERRIDE { return true; }
6875
David Brazdilf5552582015-12-27 13:36:12 +00006876 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006877 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006878 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006879 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006880
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006881 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006882 DCHECK(GetUpperCanBeNull() || !can_be_null);
6883 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006884 }
6885
Vladimir Markoa1de9182016-02-25 11:37:38 +00006886 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006887
Calin Juravleb1498f62015-02-16 13:13:29 +00006888 DECLARE_INSTRUCTION(BoundType);
6889
Artem Serovcced8ba2017-07-19 18:18:09 +01006890 protected:
6891 DEFAULT_COPY_CONSTRUCTOR(BoundType);
6892
Calin Juravleb1498f62015-02-16 13:13:29 +00006893 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006894 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6895 // is false then CanBeNull() cannot be true).
6896 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6897 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6898 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6899 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6900
Calin Juravleb1498f62015-02-16 13:13:29 +00006901 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006902 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6903 // It is used to bound the type in cases like:
6904 // if (x instanceof ClassX) {
6905 // // uper_bound_ will be ClassX
6906 // }
David Brazdilf5552582015-12-27 13:36:12 +00006907 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006908};
6909
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006910class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006911 public:
6912 HCheckCast(HInstruction* object,
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006913 HLoadClass* target_class,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006914 TypeCheckKind check_kind,
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006915 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306916 : HTemplateInstruction(kCheckCast, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006917 SetPackedField<TypeCheckKindField>(check_kind);
6918 SetPackedFlag<kFlagMustDoNullCheck>(true);
6919 SetRawInputAt(0, object);
6920 SetRawInputAt(1, target_class);
6921 }
6922
6923 HLoadClass* GetTargetClass() const {
6924 HInstruction* load_class = InputAt(1);
6925 DCHECK(load_class->IsLoadClass());
6926 return load_class->AsLoadClass();
6927 }
6928
6929 bool IsClonable() const OVERRIDE { return true; }
6930 bool CanBeMoved() const OVERRIDE { return true; }
6931
6932 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6933 return true;
6934 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006935
6936 bool NeedsEnvironment() const OVERRIDE {
6937 // Instruction may throw a CheckCastError.
6938 return true;
6939 }
6940
6941 bool CanThrow() const OVERRIDE { return true; }
6942
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006943 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6944 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6945 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6946 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
6947
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006948 DECLARE_INSTRUCTION(CheckCast);
6949
Artem Serovcced8ba2017-07-19 18:18:09 +01006950 protected:
6951 DEFAULT_COPY_CONSTRUCTOR(CheckCast);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006952
6953 private:
6954 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6955 static constexpr size_t kFieldTypeCheckKindSize =
6956 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6957 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6958 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6959 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6960 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006961};
6962
Andreas Gampe26de38b2016-07-27 17:53:11 -07006963/**
6964 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
6965 * @details We define the combined barrier types that are actually required
6966 * by the Java Memory Model, rather than using exactly the terminology from
6967 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
6968 * primitives. Note that the JSR-133 cookbook generally does not deal with
6969 * store atomicity issues, and the recipes there are not always entirely sufficient.
6970 * The current recipe is as follows:
6971 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
6972 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
6973 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
6974 * -# Use StoreStore barrier after all stores but before return from any constructor whose
6975 * class has final fields.
6976 * -# Use NTStoreStore to order non-temporal stores with respect to all later
6977 * store-to-memory instructions. Only generated together with non-temporal stores.
6978 */
6979enum MemBarrierKind {
6980 kAnyStore,
6981 kLoadAny,
6982 kStoreStore,
6983 kAnyAny,
6984 kNTStoreStore,
6985 kLastBarrierKind = kNTStoreStore
6986};
6987std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
6988
Vladimir Markofcb503c2016-05-18 12:48:17 +01006989class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006990 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006991 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006992 : HTemplateInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306993 kMemoryBarrier,
6994 SideEffects::AllWritesAndReads(), // Assume write/read on all fields/arrays.
6995 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006996 SetPackedField<BarrierKindField>(barrier_kind);
6997 }
Calin Juravle27df7582015-04-17 19:12:31 +01006998
Artem Serovcced8ba2017-07-19 18:18:09 +01006999 bool IsClonable() const OVERRIDE { return true; }
7000
Vladimir Markoa1de9182016-02-25 11:37:38 +00007001 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01007002
7003 DECLARE_INSTRUCTION(MemoryBarrier);
7004
Artem Serovcced8ba2017-07-19 18:18:09 +01007005 protected:
7006 DEFAULT_COPY_CONSTRUCTOR(MemoryBarrier);
7007
Calin Juravle27df7582015-04-17 19:12:31 +01007008 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007009 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
7010 static constexpr size_t kFieldBarrierKindSize =
7011 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
7012 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
7013 kFieldBarrierKind + kFieldBarrierKindSize;
7014 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
7015 "Too many packed fields.");
7016 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01007017};
7018
Igor Murashkind01745e2017-04-05 16:40:31 -07007019// A constructor fence orders all prior stores to fields that could be accessed via a final field of
7020// the specified object(s), with respect to any subsequent store that might "publish"
7021// (i.e. make visible) the specified object to another thread.
7022//
7023// JLS 17.5.1 "Semantics of final fields" states that a freeze action happens
7024// for all final fields (that were set) at the end of the invoked constructor.
7025//
7026// The constructor fence models the freeze actions for the final fields of an object
7027// being constructed (semantically at the end of the constructor). Constructor fences
7028// have a per-object affinity; two separate objects being constructed get two separate
7029// constructor fences.
7030//
7031// (Note: that if calling a super-constructor or forwarding to another constructor,
7032// the freezes would happen at the end of *that* constructor being invoked).
7033//
7034// The memory model guarantees that when the object being constructed is "published" after
7035// constructor completion (i.e. escapes the current thread via a store), then any final field
7036// writes must be observable on other threads (once they observe that publication).
7037//
7038// Further, anything written before the freeze, and read by dereferencing through the final field,
7039// must also be visible (so final object field could itself have an object with non-final fields;
7040// yet the freeze must also extend to them).
7041//
7042// Constructor example:
7043//
7044// class HasFinal {
7045// final int field; Optimizing IR for <init>()V:
7046// HasFinal() {
7047// field = 123; HInstanceFieldSet(this, HasFinal.field, 123)
7048// // freeze(this.field); HConstructorFence(this)
7049// } HReturn
7050// }
7051//
7052// HConstructorFence can serve double duty as a fence for new-instance/new-array allocations of
7053// already-initialized classes; in that case the allocation must act as a "default-initializer"
7054// of the object which effectively writes the class pointer "final field".
7055//
7056// For example, we can model default-initialiation as roughly the equivalent of the following:
7057//
7058// class Object {
7059// private final Class header;
7060// }
7061//
7062// Java code: Optimizing IR:
7063//
7064// T new_instance<T>() {
7065// Object obj = allocate_memory(T.class.size); obj = HInvoke(art_quick_alloc_object, T)
7066// obj.header = T.class; // header write is done by above call.
7067// // freeze(obj.header) HConstructorFence(obj)
7068// return (T)obj;
7069// }
7070//
7071// See also:
7072// * CompilerDriver::RequiresConstructorBarrier
7073// * QuasiAtomic::ThreadFenceForConstructor
7074//
7075class HConstructorFence FINAL : public HVariableInputSizeInstruction {
7076 // A fence has variable inputs because the inputs can be removed
7077 // after prepare_for_register_allocation phase.
7078 // (TODO: In the future a fence could freeze multiple objects
7079 // after merging two fences together.)
7080 public:
7081 // `fence_object` is the reference that needs to be protected for correct publication.
7082 //
7083 // It makes sense in the following situations:
7084 // * <init> constructors, it's the "this" parameter (i.e. HParameterValue, s.t. IsThis() == true).
7085 // * new-instance-like instructions, it's the return value (i.e. HNewInstance).
7086 //
7087 // After construction the `fence_object` becomes the 0th input.
7088 // This is not an input in a real sense, but just a convenient place to stash the information
7089 // about the associated object.
7090 HConstructorFence(HInstruction* fence_object,
7091 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007092 ArenaAllocator* allocator)
Igor Murashkind01745e2017-04-05 16:40:31 -07007093 // We strongly suspect there is not a more accurate way to describe the fine-grained reordering
7094 // constraints described in the class header. We claim that these SideEffects constraints
7095 // enforce a superset of the real constraints.
7096 //
7097 // The ordering described above is conservatively modeled with SideEffects as follows:
7098 //
7099 // * To prevent reordering of the publication stores:
7100 // ----> "Reads of objects" is the initial SideEffect.
7101 // * For every primitive final field store in the constructor:
7102 // ----> Union that field's type as a read (e.g. "Read of T") into the SideEffect.
7103 // * If there are any stores to reference final fields in the constructor:
7104 // ----> Use a more conservative "AllReads" SideEffect because any stores to any references
7105 // that are reachable from `fence_object` also need to be prevented for reordering
7106 // (and we do not want to do alias analysis to figure out what those stores are).
7107 //
7108 // In the implementation, this initially starts out as an "all reads" side effect; this is an
7109 // even more conservative approach than the one described above, and prevents all of the
7110 // above reordering without analyzing any of the instructions in the constructor.
7111 //
7112 // If in a later phase we discover that there are no writes to reference final fields,
7113 // we can refine the side effect to a smaller set of type reads (see above constraints).
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307114 : HVariableInputSizeInstruction(kConstructorFence,
7115 SideEffects::AllReads(),
Igor Murashkind01745e2017-04-05 16:40:31 -07007116 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007117 allocator,
Igor Murashkind01745e2017-04-05 16:40:31 -07007118 /* number_of_inputs */ 1,
7119 kArenaAllocConstructorFenceInputs) {
7120 DCHECK(fence_object != nullptr);
7121 SetRawInputAt(0, fence_object);
7122 }
7123
7124 // The object associated with this constructor fence.
7125 //
7126 // (Note: This will be null after the prepare_for_register_allocation phase,
7127 // as all constructor fence inputs are removed there).
7128 HInstruction* GetFenceObject() const {
7129 return InputAt(0);
7130 }
7131
7132 // Find all the HConstructorFence uses (`fence_use`) for `this` and:
7133 // - Delete `fence_use` from `this`'s use list.
7134 // - Delete `this` from `fence_use`'s inputs list.
7135 // - If the `fence_use` is dead, remove it from the graph.
7136 //
7137 // A fence is considered dead once it no longer has any uses
7138 // and all of the inputs are dead.
7139 //
7140 // This must *not* be called during/after prepare_for_register_allocation,
7141 // because that removes all the inputs to the fences but the fence is actually
7142 // still considered live.
Igor Murashkin6ef45672017-08-08 13:59:55 -07007143 //
7144 // Returns how many HConstructorFence instructions were removed from graph.
7145 static size_t RemoveConstructorFences(HInstruction* instruction);
Igor Murashkind01745e2017-04-05 16:40:31 -07007146
Igor Murashkindd018df2017-08-09 10:38:31 -07007147 // Combine all inputs of `this` and `other` instruction and remove
7148 // `other` from the graph.
7149 //
7150 // Inputs are unique after the merge.
7151 //
7152 // Requirement: `this` must not be the same as `other.
7153 void Merge(HConstructorFence* other);
7154
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007155 // Check if this constructor fence is protecting
7156 // an HNewInstance or HNewArray that is also the immediate
7157 // predecessor of `this`.
7158 //
Igor Murashkindd018df2017-08-09 10:38:31 -07007159 // If `ignore_inputs` is true, then the immediate predecessor doesn't need
7160 // to be one of the inputs of `this`.
7161 //
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007162 // Returns the associated HNewArray or HNewInstance,
7163 // or null otherwise.
Igor Murashkindd018df2017-08-09 10:38:31 -07007164 HInstruction* GetAssociatedAllocation(bool ignore_inputs = false);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007165
Igor Murashkind01745e2017-04-05 16:40:31 -07007166 DECLARE_INSTRUCTION(ConstructorFence);
7167
Artem Serovcced8ba2017-07-19 18:18:09 +01007168 protected:
7169 DEFAULT_COPY_CONSTRUCTOR(ConstructorFence);
Igor Murashkind01745e2017-04-05 16:40:31 -07007170};
7171
Vladimir Markofcb503c2016-05-18 12:48:17 +01007172class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007173 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007174 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007175 kEnter,
7176 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00007177 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007178 };
7179
7180 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01007181 : HTemplateInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307182 kMonitorOperation,
Vladimir Markoa1de9182016-02-25 11:37:38 +00007183 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
7184 dex_pc) {
7185 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007186 SetRawInputAt(0, object);
7187 }
7188
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007189 // Instruction may go into runtime, so we need an environment.
7190 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00007191
7192 bool CanThrow() const OVERRIDE {
7193 // Verifier guarantees that monitor-exit cannot throw.
7194 // This is important because it allows the HGraphBuilder to remove
7195 // a dead throw-catch loop generated for `synchronized` blocks/methods.
7196 return IsEnter();
7197 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007198
Vladimir Markoa1de9182016-02-25 11:37:38 +00007199 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
7200 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007201
7202 DECLARE_INSTRUCTION(MonitorOperation);
7203
Artem Serovcced8ba2017-07-19 18:18:09 +01007204 protected:
7205 DEFAULT_COPY_CONSTRUCTOR(MonitorOperation);
7206
Calin Juravle52c48962014-12-16 17:02:57 +00007207 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007208 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
7209 static constexpr size_t kFieldOperationKindSize =
7210 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
7211 static constexpr size_t kNumberOfMonitorOperationPackedBits =
7212 kFieldOperationKind + kFieldOperationKindSize;
7213 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7214 "Too many packed fields.");
7215 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007216};
7217
Vladimir Markofcb503c2016-05-18 12:48:17 +01007218class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00007219 public:
7220 HSelect(HInstruction* condition,
7221 HInstruction* true_value,
7222 HInstruction* false_value,
7223 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307224 : HExpression(kSelect, HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
David Brazdil74eb1b22015-12-14 11:44:01 +00007225 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
7226
7227 // First input must be `true_value` or `false_value` to allow codegens to
7228 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
7229 // that architectures which implement HSelect as a conditional move also
7230 // will not need to invert the condition.
7231 SetRawInputAt(0, false_value);
7232 SetRawInputAt(1, true_value);
7233 SetRawInputAt(2, condition);
7234 }
7235
Artem Serovcced8ba2017-07-19 18:18:09 +01007236 bool IsClonable() const OVERRIDE { return true; }
David Brazdil74eb1b22015-12-14 11:44:01 +00007237 HInstruction* GetFalseValue() const { return InputAt(0); }
7238 HInstruction* GetTrueValue() const { return InputAt(1); }
7239 HInstruction* GetCondition() const { return InputAt(2); }
7240
7241 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01007242 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
7243 return true;
7244 }
David Brazdil74eb1b22015-12-14 11:44:01 +00007245
7246 bool CanBeNull() const OVERRIDE {
7247 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
7248 }
7249
7250 DECLARE_INSTRUCTION(Select);
7251
Artem Serovcced8ba2017-07-19 18:18:09 +01007252 protected:
7253 DEFAULT_COPY_CONSTRUCTOR(Select);
David Brazdil74eb1b22015-12-14 11:44:01 +00007254};
7255
Vladimir Markof9f64412015-09-02 14:05:49 +01007256class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007257 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01007258 MoveOperands(Location source,
7259 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007260 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007261 HInstruction* instruction)
7262 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007263
7264 Location GetSource() const { return source_; }
7265 Location GetDestination() const { return destination_; }
7266
7267 void SetSource(Location value) { source_ = value; }
7268 void SetDestination(Location value) { destination_ = value; }
7269
7270 // The parallel move resolver marks moves as "in-progress" by clearing the
7271 // destination (but not the source).
7272 Location MarkPending() {
7273 DCHECK(!IsPending());
7274 Location dest = destination_;
7275 destination_ = Location::NoLocation();
7276 return dest;
7277 }
7278
7279 void ClearPending(Location dest) {
7280 DCHECK(IsPending());
7281 destination_ = dest;
7282 }
7283
7284 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00007285 DCHECK(source_.IsValid() || destination_.IsInvalid());
7286 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007287 }
7288
7289 // True if this blocks a move from the given location.
7290 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08007291 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007292 }
7293
7294 // A move is redundant if it's been eliminated, if its source and
7295 // destination are the same, or if its destination is unneeded.
7296 bool IsRedundant() const {
7297 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
7298 }
7299
7300 // We clear both operands to indicate move that's been eliminated.
7301 void Eliminate() {
7302 source_ = destination_ = Location::NoLocation();
7303 }
7304
7305 bool IsEliminated() const {
7306 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
7307 return source_.IsInvalid();
7308 }
7309
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007310 DataType::Type GetType() const { return type_; }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007311
Nicolas Geoffray90218252015-04-15 11:56:51 +01007312 bool Is64BitMove() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007313 return DataType::Is64BitType(type_);
Nicolas Geoffray90218252015-04-15 11:56:51 +01007314 }
7315
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007316 HInstruction* GetInstruction() const { return instruction_; }
7317
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007318 private:
7319 Location source_;
7320 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01007321 // The type this move is for.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007322 DataType::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007323 // The instruction this move is assocatied with. Null when this move is
7324 // for moving an input in the expected locations of user (including a phi user).
7325 // This is only used in debug mode, to ensure we do not connect interval siblings
7326 // in the same parallel move.
7327 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007328};
7329
Roland Levillainc9285912015-12-18 10:38:42 +00007330std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
7331
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007332static constexpr size_t kDefaultNumberOfMoves = 4;
7333
Vladimir Markofcb503c2016-05-18 12:48:17 +01007334class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007335 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007336 explicit HParallelMove(ArenaAllocator* allocator, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307337 : HTemplateInstruction(kParallelMove, SideEffects::None(), dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007338 moves_(allocator->Adapter(kArenaAllocMoveOperands)) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007339 moves_.reserve(kDefaultNumberOfMoves);
7340 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007341
Nicolas Geoffray90218252015-04-15 11:56:51 +01007342 void AddMove(Location source,
7343 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007344 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007345 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00007346 DCHECK(source.IsValid());
7347 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007348 if (kIsDebugBuild) {
7349 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007350 for (const MoveOperands& move : moves_) {
7351 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007352 // Special case the situation where the move is for the spill slot
7353 // of the instruction.
7354 if ((GetPrevious() == instruction)
7355 || ((GetPrevious() == nullptr)
7356 && instruction->IsPhi()
7357 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007358 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007359 << "Doing parallel moves for the same instruction.";
7360 } else {
7361 DCHECK(false) << "Doing parallel moves for the same instruction.";
7362 }
7363 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00007364 }
7365 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007366 for (const MoveOperands& move : moves_) {
7367 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007368 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01007369 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007370 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007371 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007372 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007373 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007374 }
7375
Vladimir Marko225b6462015-09-28 12:17:40 +01007376 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007377 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007378 }
7379
Vladimir Marko225b6462015-09-28 12:17:40 +01007380 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007381
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01007382 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007383
Artem Serovcced8ba2017-07-19 18:18:09 +01007384 protected:
7385 DEFAULT_COPY_CONSTRUCTOR(ParallelMove);
7386
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007387 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01007388 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007389};
7390
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007391// This instruction computes an intermediate address pointing in the 'middle' of an object. The
7392// result pointer cannot be handled by GC, so extra care is taken to make sure that this value is
7393// never used across anything that can trigger GC.
7394// The result of this instruction is not a pointer in the sense of `DataType::Type::kreference`.
7395// So we represent it by the type `DataType::Type::kInt`.
7396class HIntermediateAddress FINAL : public HExpression<2> {
7397 public:
7398 HIntermediateAddress(HInstruction* base_address, HInstruction* offset, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307399 : HExpression(kIntermediateAddress,
7400 DataType::Type::kInt32,
7401 SideEffects::DependsOnGC(),
7402 dex_pc) {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007403 DCHECK_EQ(DataType::Size(DataType::Type::kInt32),
7404 DataType::Size(DataType::Type::kReference))
7405 << "kPrimInt and kPrimNot have different sizes.";
7406 SetRawInputAt(0, base_address);
7407 SetRawInputAt(1, offset);
7408 }
7409
Artem Serovcced8ba2017-07-19 18:18:09 +01007410 bool IsClonable() const OVERRIDE { return true; }
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007411 bool CanBeMoved() const OVERRIDE { return true; }
7412 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
7413 return true;
7414 }
7415 bool IsActualObject() const OVERRIDE { return false; }
7416
7417 HInstruction* GetBaseAddress() const { return InputAt(0); }
7418 HInstruction* GetOffset() const { return InputAt(1); }
7419
7420 DECLARE_INSTRUCTION(IntermediateAddress);
7421
Artem Serovcced8ba2017-07-19 18:18:09 +01007422 protected:
7423 DEFAULT_COPY_CONSTRUCTOR(IntermediateAddress);
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007424};
7425
7426
Mark Mendell0616ae02015-04-17 12:49:27 -04007427} // namespace art
7428
Aart Bikf8f5a162017-02-06 15:35:29 -08007429#include "nodes_vector.h"
7430
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007431#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
7432#include "nodes_shared.h"
7433#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007434#ifdef ART_ENABLE_CODEGEN_mips
7435#include "nodes_mips.h"
7436#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04007437#ifdef ART_ENABLE_CODEGEN_x86
7438#include "nodes_x86.h"
7439#endif
7440
7441namespace art {
7442
Igor Murashkin6ef45672017-08-08 13:59:55 -07007443class OptimizingCompilerStats;
7444
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007445class HGraphVisitor : public ValueObject {
7446 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007447 explicit HGraphVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7448 : stats_(stats),
7449 graph_(graph) {}
Dave Allison20dfc792014-06-16 20:44:29 -07007450 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007451
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007452 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007453 virtual void VisitBasicBlock(HBasicBlock* block);
7454
Roland Levillain633021e2014-10-01 14:12:25 +01007455 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007456 void VisitInsertionOrder();
7457
Roland Levillain633021e2014-10-01 14:12:25 +01007458 // Visit the graph following dominator tree reverse post-order.
7459 void VisitReversePostOrder();
7460
Nicolas Geoffray787c3072014-03-17 10:20:19 +00007461 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007462
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007463 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007464#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007465 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
7466
7467 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7468
7469#undef DECLARE_VISIT_INSTRUCTION
7470
Igor Murashkin6ef45672017-08-08 13:59:55 -07007471 protected:
7472 OptimizingCompilerStats* stats_;
7473
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007474 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07007475 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007476
7477 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
7478};
7479
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007480class HGraphDelegateVisitor : public HGraphVisitor {
7481 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007482 explicit HGraphDelegateVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7483 : HGraphVisitor(graph, stats) {}
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007484 virtual ~HGraphDelegateVisitor() {}
7485
7486 // Visit functions that delegate to to super class.
7487#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00007488 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007489
7490 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7491
7492#undef DECLARE_VISIT_INSTRUCTION
7493
7494 private:
7495 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
7496};
7497
Artem Serovcced8ba2017-07-19 18:18:09 +01007498// Create a clone of the instruction, insert it into the graph; replace the old one with a new
7499// and remove the old instruction.
7500HInstruction* ReplaceInstrOrPhiByClone(HInstruction* instr);
7501
7502// Create a clone for each clonable instructions/phis and replace the original with the clone.
7503//
7504// Used for testing individual instruction cloner.
7505class CloneAndReplaceInstructionVisitor : public HGraphDelegateVisitor {
7506 public:
7507 explicit CloneAndReplaceInstructionVisitor(HGraph* graph)
Artem Serov7f4aff62017-06-21 17:02:18 +01007508 : HGraphDelegateVisitor(graph), instr_replaced_by_clones_count_(0) {}
Artem Serovcced8ba2017-07-19 18:18:09 +01007509
7510 void VisitInstruction(HInstruction* instruction) OVERRIDE {
7511 if (instruction->IsClonable()) {
7512 ReplaceInstrOrPhiByClone(instruction);
Artem Serov7f4aff62017-06-21 17:02:18 +01007513 instr_replaced_by_clones_count_++;
Artem Serovcced8ba2017-07-19 18:18:09 +01007514 }
7515 }
7516
Artem Serov7f4aff62017-06-21 17:02:18 +01007517 size_t GetInstrReplacedByClonesCount() const { return instr_replaced_by_clones_count_; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007518
7519 private:
Artem Serov7f4aff62017-06-21 17:02:18 +01007520 size_t instr_replaced_by_clones_count_;
Artem Serovcced8ba2017-07-19 18:18:09 +01007521
7522 DISALLOW_COPY_AND_ASSIGN(CloneAndReplaceInstructionVisitor);
7523};
7524
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007525// Iterator over the blocks that art part of the loop. Includes blocks part
7526// of an inner loop. The order in which the blocks are iterated is on their
7527// block id.
7528class HBlocksInLoopIterator : public ValueObject {
7529 public:
7530 explicit HBlocksInLoopIterator(const HLoopInformation& info)
7531 : blocks_in_loop_(info.GetBlocks()),
7532 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
7533 index_(0) {
7534 if (!blocks_in_loop_.IsBitSet(index_)) {
7535 Advance();
7536 }
7537 }
7538
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007539 bool Done() const { return index_ == blocks_.size(); }
7540 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007541 void Advance() {
7542 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007543 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007544 if (blocks_in_loop_.IsBitSet(index_)) {
7545 break;
7546 }
7547 }
7548 }
7549
7550 private:
7551 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007552 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007553 size_t index_;
7554
7555 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
7556};
7557
Mingyao Yang3584bce2015-05-19 16:01:59 -07007558// Iterator over the blocks that art part of the loop. Includes blocks part
7559// of an inner loop. The order in which the blocks are iterated is reverse
7560// post order.
7561class HBlocksInLoopReversePostOrderIterator : public ValueObject {
7562 public:
7563 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
7564 : blocks_in_loop_(info.GetBlocks()),
7565 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
7566 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007567 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007568 Advance();
7569 }
7570 }
7571
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007572 bool Done() const { return index_ == blocks_.size(); }
7573 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07007574 void Advance() {
7575 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007576 for (size_t e = blocks_.size(); index_ < e; ++index_) {
7577 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007578 break;
7579 }
7580 }
7581 }
7582
7583 private:
7584 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007585 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07007586 size_t index_;
7587
7588 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
7589};
7590
Aart Bikf3e61ee2017-04-12 17:09:20 -07007591// Returns int64_t value of a properly typed constant.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007592inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00007593 if (constant->IsIntConstant()) {
7594 return constant->AsIntConstant()->GetValue();
7595 } else if (constant->IsLongConstant()) {
7596 return constant->AsLongConstant()->GetValue();
7597 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00007598 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00007599 return 0;
7600 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007601}
7602
Aart Bikf3e61ee2017-04-12 17:09:20 -07007603// Returns true iff instruction is an integral constant (and sets value on success).
7604inline bool IsInt64AndGet(HInstruction* instruction, /*out*/ int64_t* value) {
7605 if (instruction->IsIntConstant()) {
7606 *value = instruction->AsIntConstant()->GetValue();
7607 return true;
7608 } else if (instruction->IsLongConstant()) {
7609 *value = instruction->AsLongConstant()->GetValue();
7610 return true;
7611 } else if (instruction->IsNullConstant()) {
7612 *value = 0;
7613 return true;
7614 }
7615 return false;
7616}
7617
Aart Bik0148de42017-09-05 09:25:01 -07007618// Returns true iff instruction is the given integral constant.
7619inline bool IsInt64Value(HInstruction* instruction, int64_t value) {
7620 int64_t val = 0;
7621 return IsInt64AndGet(instruction, &val) && val == value;
7622}
7623
7624// Returns true iff instruction is a zero bit pattern.
7625inline bool IsZeroBitPattern(HInstruction* instruction) {
7626 return instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern();
7627}
7628
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007629#define INSTRUCTION_TYPE_CHECK(type, super) \
7630 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
7631 inline const H##type* HInstruction::As##type() const { \
7632 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
7633 } \
7634 inline H##type* HInstruction::As##type() { \
7635 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
7636 }
7637
7638 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
7639#undef INSTRUCTION_TYPE_CHECK
7640
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00007641// Create space in `blocks` for adding `number_of_new_blocks` entries
7642// starting at location `at`. Blocks after `at` are moved accordingly.
7643inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
7644 size_t number_of_new_blocks,
7645 size_t after) {
7646 DCHECK_LT(after, blocks->size());
7647 size_t old_size = blocks->size();
7648 size_t new_size = old_size + number_of_new_blocks;
7649 blocks->resize(new_size);
7650 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
7651}
7652
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00007653/*
7654 * Hunt "under the hood" of array lengths (leading to array references),
7655 * null checks (also leading to array references), and new arrays
7656 * (leading to the actual length). This makes it more likely related
7657 * instructions become actually comparable.
7658 */
7659inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
7660 while (instruction->IsArrayLength() ||
7661 instruction->IsNullCheck() ||
7662 instruction->IsNewArray()) {
7663 instruction = instruction->IsNewArray()
7664 ? instruction->AsNewArray()->GetLength()
7665 : instruction->InputAt(0);
7666 }
7667 return instruction;
7668}
7669
Artem Serov21c7e6f2017-07-27 16:04:42 +01007670void RemoveEnvironmentUses(HInstruction* instruction);
7671bool HasEnvironmentUsedByOthers(HInstruction* instruction);
7672void ResetEnvironmentInputRecords(HInstruction* instruction);
7673
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007674} // namespace art
7675
7676#endif // ART_COMPILER_OPTIMIZING_NODES_H_