blob: 295f570b3485bc791ffcca5886eccefd3770e13d [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
Vladimir Marko60584552015-09-03 13:35:12 +000020#include <algorithm>
Vladimir Markof9f64412015-09-02 14:05:49 +010021#include <array>
Roland Levillain9867bc72015-08-05 10:21:34 +010022#include <type_traits>
23
Mathieu Chartiere5d80f82015-10-15 17:47:48 -070024#include "base/arena_bit_vector.h"
David Brazdil8d5b8b22015-03-24 10:51:52 +000025#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080026#include "base/arena_object.h"
David Brazdild9c90372016-09-14 16:53:55 +010027#include "base/array_ref.h"
Vladimir Marko2c45bc92016-10-25 16:54:12 +010028#include "base/iteration_range.h"
David Sehrc431b9d2018-03-02 12:01:51 -080029#include "base/quasi_atomic.h"
Vladimir Marko60584552015-09-03 13:35:12 +000030#include "base/stl_util.h"
David Brazdild9c90372016-09-14 16:53:55 +010031#include "base/transform_array_ref.h"
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010032#include "data_type.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070033#include "deoptimization_kind.h"
David Sehr9e734c72018-01-04 17:56:19 -080034#include "dex/dex_file.h"
35#include "dex/dex_file_types.h"
David Sehr8c0961f2018-01-23 16:11:38 -080036#include "dex/invoke_type.h"
David Sehr312f3b22018-03-19 08:39:26 -070037#include "dex/method_reference.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000038#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000039#include "handle.h"
40#include "handle_scope.h"
Nicolas Geoffray762869d2016-07-15 15:28:35 +010041#include "intrinsics_enum.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010042#include "locations.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000043#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010044#include "offsets.h"
Vladimir Marko46817b82016-03-29 12:21:58 +010045#include "utils/intrusive_forward_list.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000046
47namespace art {
48
Vladimir Markoca6fff82017-10-03 14:49:14 +010049class ArenaStack;
David Brazdil1abb4192015-02-17 18:33:36 +000050class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000051class HBasicBlock;
Igor Murashkind01745e2017-04-05 16:40:31 -070052class HConstructorFence;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010053class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000054class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010055class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000056class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000057class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000058class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000059class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000060class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000061class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000062class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000063class HNullConstant;
Igor Murashkind01745e2017-04-05 16:40:31 -070064class HParameterValue;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010065class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010066class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010067class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010068class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000069class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010070class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000071class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000072
Mathieu Chartier736b5602015-09-02 14:54:11 -070073namespace mirror {
74class DexCache;
75} // namespace mirror
76
Nicolas Geoffray818f2102014-02-18 16:43:35 +000077static const int kDefaultNumberOfBlocks = 8;
78static const int kDefaultNumberOfSuccessors = 2;
79static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010080static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010081static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000082static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000083
Roland Levillain5b5b9312016-03-22 14:57:31 +000084// The maximum (meaningful) distance (31) that can be used in an integer shift/rotate operation.
85static constexpr int32_t kMaxIntShiftDistance = 0x1f;
86// The maximum (meaningful) distance (63) that can be used in a long shift/rotate operation.
87static constexpr int32_t kMaxLongShiftDistance = 0x3f;
Calin Juravle9aec02f2014-11-18 23:06:35 +000088
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010089static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070090static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010091
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010092static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
93
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060094static constexpr uint32_t kNoDexPc = -1;
95
Vladimir Markodbb7f5b2016-03-30 13:23:58 +010096inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
97 // For the purposes of the compiler, the dex files must actually be the same object
98 // if we want to safely treat them as the same. This is especially important for JIT
99 // as custom class loaders can open the same underlying file (or memory) multiple
100 // times and provide different class resolution but no two class loaders should ever
101 // use the same DexFile object - doing so is an unsupported hack that can lead to
102 // all sorts of weird failures.
103 return &lhs == &rhs;
104}
105
Dave Allison20dfc792014-06-16 20:44:29 -0700106enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -0700107 // All types.
108 kCondEQ, // ==
109 kCondNE, // !=
110 // Signed integers and floating-point numbers.
111 kCondLT, // <
112 kCondLE, // <=
113 kCondGT, // >
114 kCondGE, // >=
115 // Unsigned integers.
116 kCondB, // <
117 kCondBE, // <=
118 kCondA, // >
119 kCondAE, // >=
Scott Wakeling2c76e062016-08-31 09:48:54 +0100120 // First and last aliases.
121 kCondFirst = kCondEQ,
122 kCondLast = kCondAE,
Dave Allison20dfc792014-06-16 20:44:29 -0700123};
124
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000125enum GraphAnalysisResult {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000126 kAnalysisSkipped,
David Brazdilbadd8262016-02-02 16:28:56 +0000127 kAnalysisInvalidBytecode,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000128 kAnalysisFailThrowCatchLoop,
129 kAnalysisFailAmbiguousArrayOp,
130 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000131};
132
Andreas Gampe9186ced2016-12-12 14:28:21 -0800133template <typename T>
134static inline typename std::make_unsigned<T>::type MakeUnsigned(T x) {
135 return static_cast<typename std::make_unsigned<T>::type>(x);
136}
137
Vladimir Markof9f64412015-09-02 14:05:49 +0100138class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100139 public:
140 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
141
142 void AddInstruction(HInstruction* instruction);
143 void RemoveInstruction(HInstruction* instruction);
144
David Brazdilc3d743f2015-04-22 13:40:50 +0100145 // Insert `instruction` before/after an existing instruction `cursor`.
146 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
147 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
148
Roland Levillain6b469232014-09-25 10:10:38 +0100149 // Return true if this list contains `instruction`.
150 bool Contains(HInstruction* instruction) const;
151
Roland Levillainccc07a92014-09-16 14:48:16 +0100152 // Return true if `instruction1` is found before `instruction2` in
153 // this instruction list and false otherwise. Abort if none
154 // of these instructions is found.
155 bool FoundBefore(const HInstruction* instruction1,
156 const HInstruction* instruction2) const;
157
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000158 bool IsEmpty() const { return first_instruction_ == nullptr; }
159 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
160
161 // Update the block of all instructions to be `block`.
162 void SetBlockOfInstructions(HBasicBlock* block) const;
163
164 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000165 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000166 void Add(const HInstructionList& instruction_list);
167
David Brazdil2d7352b2015-04-20 14:52:42 +0100168 // Return the number of instructions in the list. This is an expensive operation.
169 size_t CountSize() const;
170
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100171 private:
172 HInstruction* first_instruction_;
173 HInstruction* last_instruction_;
174
175 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000176 friend class HGraph;
177 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100178 friend class HInstructionIterator;
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +0000179 friend class HInstructionIteratorHandleChanges;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100180 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100181
182 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
183};
184
David Brazdil4833f5a2015-12-16 10:37:39 +0000185class ReferenceTypeInfo : ValueObject {
186 public:
187 typedef Handle<mirror::Class> TypeHandle;
188
Vladimir Markoa1de9182016-02-25 11:37:38 +0000189 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
190
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700191 static ReferenceTypeInfo Create(TypeHandle type_handle) REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil94ab38f2016-06-21 17:48:19 +0100192 return Create(type_handle, type_handle->CannotBeAssignedFromOtherTypes());
193 }
194
Vladimir Markoa1de9182016-02-25 11:37:38 +0000195 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000196 return ReferenceTypeInfo(type_handle, is_exact);
197 }
198
199 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
200
Vladimir Markof39745e2016-01-26 12:16:55 +0000201 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000202 return handle.GetReference() != nullptr;
203 }
204
Vladimir Marko456307a2016-04-19 14:12:13 +0000205 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000206 return IsValidHandle(type_handle_);
207 }
208
209 bool IsExact() const { return is_exact_; }
210
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700211 bool IsObjectClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000212 DCHECK(IsValid());
213 return GetTypeHandle()->IsObjectClass();
214 }
215
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700216 bool IsStringClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000217 DCHECK(IsValid());
218 return GetTypeHandle()->IsStringClass();
219 }
220
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700221 bool IsObjectArray() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000222 DCHECK(IsValid());
223 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
224 }
225
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700226 bool IsInterface() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000227 DCHECK(IsValid());
228 return GetTypeHandle()->IsInterface();
229 }
230
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700231 bool IsArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000232 DCHECK(IsValid());
233 return GetTypeHandle()->IsArrayClass();
234 }
235
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700236 bool IsPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000237 DCHECK(IsValid());
238 return GetTypeHandle()->IsPrimitiveArray();
239 }
240
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700241 bool IsNonPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000242 DCHECK(IsValid());
243 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
244 }
245
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700246 bool CanArrayHold(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000247 DCHECK(IsValid());
248 if (!IsExact()) return false;
249 if (!IsArrayClass()) return false;
250 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
251 }
252
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700253 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000254 DCHECK(IsValid());
255 if (!IsExact()) return false;
256 if (!IsArrayClass()) return false;
257 if (!rti.IsArrayClass()) return false;
258 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
259 rti.GetTypeHandle()->GetComponentType());
260 }
261
262 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
263
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700264 bool IsSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000265 DCHECK(IsValid());
266 DCHECK(rti.IsValid());
267 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
268 }
269
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700270 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000271 DCHECK(IsValid());
272 DCHECK(rti.IsValid());
273 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
274 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
275 }
276
277 // Returns true if the type information provide the same amount of details.
278 // Note that it does not mean that the instructions have the same actual type
279 // (because the type can be the result of a merge).
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700280 bool IsEqual(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000281 if (!IsValid() && !rti.IsValid()) {
282 // Invalid types are equal.
283 return true;
284 }
285 if (!IsValid() || !rti.IsValid()) {
286 // One is valid, the other not.
287 return false;
288 }
289 return IsExact() == rti.IsExact()
290 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
291 }
292
293 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000294 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
295 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
296 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000297
298 // The class of the object.
299 TypeHandle type_handle_;
300 // Whether or not the type is exact or a superclass of the actual type.
301 // Whether or not we have any information about this type.
302 bool is_exact_;
303};
304
305std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
306
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000307// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100308class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000309 public:
Vladimir Markoca6fff82017-10-03 14:49:14 +0100310 HGraph(ArenaAllocator* allocator,
311 ArenaStack* arena_stack,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100312 const DexFile& dex_file,
313 uint32_t method_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700314 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100315 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100316 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000317 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100318 int start_instruction_id = 0)
Vladimir Markoca6fff82017-10-03 14:49:14 +0100319 : allocator_(allocator),
320 arena_stack_(arena_stack),
321 blocks_(allocator->Adapter(kArenaAllocBlockList)),
322 reverse_post_order_(allocator->Adapter(kArenaAllocReversePostOrder)),
323 linear_order_(allocator->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700324 entry_block_(nullptr),
325 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100326 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100327 number_of_vregs_(0),
328 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000329 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400330 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000331 has_try_catch_(false),
Aart Bikb13c65b2017-03-21 20:14:07 -0700332 has_simd_(false),
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800333 has_loops_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000334 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000335 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000336 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100337 dex_file_(dex_file),
338 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100339 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100340 in_ssa_form_(false),
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800341 number_of_cha_guards_(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700342 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000343 cached_null_constant_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100344 cached_int_constants_(std::less<int32_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
345 cached_float_constants_(std::less<int32_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
346 cached_long_constants_(std::less<int64_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
347 cached_double_constants_(std::less<int64_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000348 cached_current_method_(nullptr),
Nicolas Geoffray53fec082017-03-27 12:56:16 +0100349 art_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000350 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
Mingyao Yang063fc772016-08-02 11:02:54 -0700351 osr_(osr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100352 cha_single_implementation_list_(allocator->Adapter(kArenaAllocCHA)) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100353 blocks_.reserve(kDefaultNumberOfBlocks);
354 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000355
David Brazdilbadd8262016-02-02 16:28:56 +0000356 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700357 void InitializeInexactObjectRTI(VariableSizedHandleScope* handles);
David Brazdilbadd8262016-02-02 16:28:56 +0000358
Vladimir Markoca6fff82017-10-03 14:49:14 +0100359 ArenaAllocator* GetAllocator() const { return allocator_; }
360 ArenaStack* GetArenaStack() const { return arena_stack_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100361 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
362
David Brazdil69ba7b72015-06-23 18:27:30 +0100363 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000364 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100365
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000366 HBasicBlock* GetEntryBlock() const { return entry_block_; }
367 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100368 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000369
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000370 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
371 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000372
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000373 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100374
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100375 void ComputeDominanceInformation();
376 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000377 void ClearLoopInformation();
378 void FindBackEdges(ArenaBitVector* visited);
379 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100380 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100381 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000382
David Brazdil4833f5a2015-12-16 10:37:39 +0000383 // Analyze all natural loops in this graph. Returns a code specifying that it
384 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000385 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000386 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100387
David Brazdilffee3d32015-07-06 11:48:53 +0100388 // Iterate over blocks to compute try block membership. Needs reverse post
389 // order and loop information.
390 void ComputeTryBlockInformation();
391
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000392 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000393 // Returns the instruction to replace the invoke expression or null if the
394 // invoke is for a void method. Note that the caller is responsible for replacing
395 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000396 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000397
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000398 // Update the loop and try membership of `block`, which was spawned from `reference`.
399 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
400 // should be the new back edge.
401 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
402 HBasicBlock* reference,
403 bool replace_if_back_edge);
404
Mingyao Yang3584bce2015-05-19 16:01:59 -0700405 // Need to add a couple of blocks to test if the loop body is entered and
406 // put deoptimization instructions, etc.
407 void TransformLoopHeaderForBCE(HBasicBlock* header);
408
Aart Bikf8f5a162017-02-06 15:35:29 -0800409 // Adds a new loop directly after the loop with the given header and exit.
410 // Returns the new preheader.
411 HBasicBlock* TransformLoopForVectorization(HBasicBlock* header,
412 HBasicBlock* body,
413 HBasicBlock* exit);
414
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000415 // Removes `block` from the graph. Assumes `block` has been disconnected from
416 // other blocks and has no instructions or phis.
417 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000418
David Brazdilfc6a86a2015-06-26 10:33:45 +0000419 // Splits the edge between `block` and `successor` while preserving the
420 // indices in the predecessor/successor lists. If there are multiple edges
421 // between the blocks, the lowest indices are used.
422 // Returns the new block which is empty and has the same dex pc as `successor`.
423 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
424
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100425 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
Artem Serovc73ee372017-07-31 15:08:40 +0100426 void OrderLoopHeaderPredecessors(HBasicBlock* header);
Artem Serov09faaea2017-12-07 14:36:01 +0000427
428 // Transform a loop into a format with a single preheader.
429 //
430 // Each phi in the header should be split: original one in the header should only hold
431 // inputs reachable from the back edges and a single input from the preheader. The newly created
432 // phi in the preheader should collate the inputs from the original multiple incoming blocks.
433 //
434 // Loops in the graph typically have a single preheader, so this method is used to "repair" loops
435 // that no longer have this property.
436 void TransformLoopToSinglePreheaderFormat(HBasicBlock* header);
437
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100438 void SimplifyLoop(HBasicBlock* header);
439
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000440 int32_t GetNextInstructionId() {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100441 CHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000442 return current_instruction_id_++;
443 }
444
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000445 int32_t GetCurrentInstructionId() const {
446 return current_instruction_id_;
447 }
448
449 void SetCurrentInstructionId(int32_t id) {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100450 CHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000451 current_instruction_id_ = id;
452 }
453
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100454 uint16_t GetMaximumNumberOfOutVRegs() const {
455 return maximum_number_of_out_vregs_;
456 }
457
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000458 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
459 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100460 }
461
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100462 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
463 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
464 }
465
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000466 void UpdateTemporariesVRegSlots(size_t slots) {
467 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100468 }
469
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000470 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100471 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000472 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100473 }
474
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100475 void SetNumberOfVRegs(uint16_t number_of_vregs) {
476 number_of_vregs_ = number_of_vregs;
477 }
478
479 uint16_t GetNumberOfVRegs() const {
480 return number_of_vregs_;
481 }
482
483 void SetNumberOfInVRegs(uint16_t value) {
484 number_of_in_vregs_ = value;
485 }
486
David Brazdildee58d62016-04-07 09:54:26 +0000487 uint16_t GetNumberOfInVRegs() const {
488 return number_of_in_vregs_;
489 }
490
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100491 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100492 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100493 return number_of_vregs_ - number_of_in_vregs_;
494 }
495
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100496 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100497 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100498 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100499
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100500 ArrayRef<HBasicBlock* const> GetReversePostOrderSkipEntryBlock() {
501 DCHECK(GetReversePostOrder()[0] == entry_block_);
502 return ArrayRef<HBasicBlock* const>(GetReversePostOrder()).SubArray(1);
503 }
504
505 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetPostOrder() const {
506 return ReverseRange(GetReversePostOrder());
507 }
508
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100509 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100510 return linear_order_;
511 }
512
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100513 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetLinearPostOrder() const {
514 return ReverseRange(GetLinearOrder());
515 }
516
Mark Mendell1152c922015-04-24 17:06:35 -0400517 bool HasBoundsChecks() const {
518 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800519 }
520
Mark Mendell1152c922015-04-24 17:06:35 -0400521 void SetHasBoundsChecks(bool value) {
522 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800523 }
524
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000525 bool IsDebuggable() const { return debuggable_; }
526
David Brazdil8d5b8b22015-03-24 10:51:52 +0000527 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000528 // already, it is created and inserted into the graph. This method is only for
529 // integral types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100530 HConstant* GetConstant(DataType::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000531
532 // TODO: This is problematic for the consistency of reference type propagation
533 // because it can be created anytime after the pass and thus it will be left
534 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600535 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000536
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600537 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
538 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000539 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600540 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
541 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000542 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600543 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
544 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000545 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600546 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
547 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000548 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000549
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100550 HCurrentMethod* GetCurrentMethod();
551
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100552 const DexFile& GetDexFile() const {
553 return dex_file_;
554 }
555
556 uint32_t GetMethodIdx() const {
557 return method_idx_;
558 }
559
Igor Murashkind01745e2017-04-05 16:40:31 -0700560 // Get the method name (without the signature), e.g. "<init>"
561 const char* GetMethodName() const;
562
563 // Get the pretty method name (class + name + optionally signature).
564 std::string PrettyMethod(bool with_signature = true) const;
565
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100566 InvokeType GetInvokeType() const {
567 return invoke_type_;
568 }
569
Mark Mendellc4701932015-04-10 13:18:51 -0400570 InstructionSet GetInstructionSet() const {
571 return instruction_set_;
572 }
573
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000574 bool IsCompilingOsr() const { return osr_; }
575
Mingyao Yang063fc772016-08-02 11:02:54 -0700576 ArenaSet<ArtMethod*>& GetCHASingleImplementationList() {
577 return cha_single_implementation_list_;
578 }
579
580 void AddCHASingleImplementationDependency(ArtMethod* method) {
581 cha_single_implementation_list_.insert(method);
582 }
583
584 bool HasShouldDeoptimizeFlag() const {
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800585 return number_of_cha_guards_ != 0;
Mingyao Yang063fc772016-08-02 11:02:54 -0700586 }
587
David Brazdil77a48ae2015-09-15 12:34:04 +0000588 bool HasTryCatch() const { return has_try_catch_; }
589 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100590
Aart Bikb13c65b2017-03-21 20:14:07 -0700591 bool HasSIMD() const { return has_simd_; }
592 void SetHasSIMD(bool value) { has_simd_ = value; }
593
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800594 bool HasLoops() const { return has_loops_; }
595 void SetHasLoops(bool value) { has_loops_ = value; }
596
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000597 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
598 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
599
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100600 ArtMethod* GetArtMethod() const { return art_method_; }
601 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
602
Roland Levillain5e8d5f02016-10-18 18:03:43 +0100603 // Returns an instruction with the opposite Boolean value from 'cond'.
Mark Mendellf6529172015-11-17 11:16:56 -0500604 // The instruction has been inserted into the graph, either as a constant, or
605 // before cursor.
606 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
607
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000608 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
609
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800610 uint32_t GetNumberOfCHAGuards() { return number_of_cha_guards_; }
611 void SetNumberOfCHAGuards(uint32_t num) { number_of_cha_guards_ = num; }
612 void IncrementNumberOfCHAGuards() { number_of_cha_guards_++; }
613
David Brazdil2d7352b2015-04-20 14:52:42 +0100614 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000615 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100616 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000617
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000618 template <class InstructionType, typename ValueType>
619 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600620 ArenaSafeMap<ValueType, InstructionType*>* cache,
621 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000622 // Try to find an existing constant of the given value.
623 InstructionType* constant = nullptr;
624 auto cached_constant = cache->find(value);
625 if (cached_constant != cache->end()) {
626 constant = cached_constant->second;
627 }
628
629 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100630 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000631 if (constant == nullptr || constant->GetBlock() == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100632 constant = new (allocator_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000633 cache->Overwrite(value, constant);
634 InsertConstant(constant);
635 }
636 return constant;
637 }
638
David Brazdil8d5b8b22015-03-24 10:51:52 +0000639 void InsertConstant(HConstant* instruction);
640
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000641 // Cache a float constant into the graph. This method should only be
642 // called by the SsaBuilder when creating "equivalent" instructions.
643 void CacheFloatConstant(HFloatConstant* constant);
644
645 // See CacheFloatConstant comment.
646 void CacheDoubleConstant(HDoubleConstant* constant);
647
Vladimir Markoca6fff82017-10-03 14:49:14 +0100648 ArenaAllocator* const allocator_;
649 ArenaStack* const arena_stack_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000650
651 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100652 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000653
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100654 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100655 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000656
Aart Bik281c6812016-08-26 11:31:48 -0700657 // List of blocks to perform a linear order tree traversal. Unlike the reverse
658 // post order, this order is not incrementally kept up-to-date.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100659 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100660
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000661 HBasicBlock* entry_block_;
662 HBasicBlock* exit_block_;
663
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100664 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100665 uint16_t maximum_number_of_out_vregs_;
666
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100667 // The number of virtual registers in this method. Contains the parameters.
668 uint16_t number_of_vregs_;
669
670 // The number of virtual registers used by parameters of this method.
671 uint16_t number_of_in_vregs_;
672
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000673 // Number of vreg size slots that the temporaries use (used in baseline compiler).
674 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100675
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800676 // Flag whether there are bounds checks in the graph. We can skip
677 // BCE if it's false. It's only best effort to keep it up to date in
678 // the presence of code elimination so there might be false positives.
Mark Mendell1152c922015-04-24 17:06:35 -0400679 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800680
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800681 // Flag whether there are try/catch blocks in the graph. We will skip
682 // try/catch-related passes if it's false. It's only best effort to keep
683 // it up to date in the presence of code elimination so there might be
684 // false positives.
David Brazdil77a48ae2015-09-15 12:34:04 +0000685 bool has_try_catch_;
686
Aart Bikb13c65b2017-03-21 20:14:07 -0700687 // Flag whether SIMD instructions appear in the graph. If true, the
688 // code generators may have to be more careful spilling the wider
689 // contents of SIMD registers.
690 bool has_simd_;
691
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800692 // Flag whether there are any loops in the graph. We can skip loop
693 // optimization if it's false. It's only best effort to keep it up
694 // to date in the presence of code elimination so there might be false
695 // positives.
696 bool has_loops_;
697
698 // Flag whether there are any irreducible loops in the graph. It's only
699 // best effort to keep it up to date in the presence of code elimination
700 // so there might be false positives.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000701 bool has_irreducible_loops_;
702
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000703 // Indicates whether the graph should be compiled in a way that
704 // ensures full debuggability. If false, we can apply more
705 // aggressive optimizations that may limit the level of debugging.
706 const bool debuggable_;
707
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000708 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000709 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000710
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100711 // The dex file from which the method is from.
712 const DexFile& dex_file_;
713
714 // The method index in the dex file.
715 const uint32_t method_idx_;
716
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100717 // If inlined, this encodes how the callee is being invoked.
718 const InvokeType invoke_type_;
719
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100720 // Whether the graph has been transformed to SSA form. Only used
721 // in debug mode to ensure we are not using properties only valid
722 // for non-SSA form (like the number of temporaries).
723 bool in_ssa_form_;
724
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800725 // Number of CHA guards in the graph. Used to short-circuit the
726 // CHA guard optimization pass when there is no CHA guard left.
727 uint32_t number_of_cha_guards_;
728
Mathieu Chartiere401d142015-04-22 13:56:20 -0700729 const InstructionSet instruction_set_;
730
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000731 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000732 HNullConstant* cached_null_constant_;
733 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000734 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000735 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000736 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000737
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100738 HCurrentMethod* cached_current_method_;
739
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100740 // The ArtMethod this graph is for. Note that for AOT, it may be null,
741 // for example for methods whose declaring class could not be resolved
742 // (such as when the superclass could not be found).
743 ArtMethod* art_method_;
744
David Brazdil4833f5a2015-12-16 10:37:39 +0000745 // Keep the RTI of inexact Object to avoid having to pass stack handle
746 // collection pointer to passes which may create NullConstant.
747 ReferenceTypeInfo inexact_object_rti_;
748
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000749 // Whether we are compiling this graph for on stack replacement: this will
750 // make all loops seen as irreducible and emit special stack maps to mark
751 // compiled code entries which the interpreter can directly jump to.
752 const bool osr_;
753
Mingyao Yang063fc772016-08-02 11:02:54 -0700754 // List of methods that are assumed to have single implementation.
755 ArenaSet<ArtMethod*> cha_single_implementation_list_;
756
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000757 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100758 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000759 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000760 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000761 DISALLOW_COPY_AND_ASSIGN(HGraph);
762};
763
Vladimir Markof9f64412015-09-02 14:05:49 +0100764class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000765 public:
766 HLoopInformation(HBasicBlock* header, HGraph* graph)
767 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100768 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000769 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100770 contains_irreducible_loop_(false),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100771 back_edges_(graph->GetAllocator()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100772 // Make bit vector growable, as the number of blocks may change.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100773 blocks_(graph->GetAllocator(),
774 graph->GetBlocks().size(),
775 true,
776 kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100777 back_edges_.reserve(kDefaultNumberOfBackEdges);
778 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100779
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000780 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100781 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000782
783 void Dump(std::ostream& os);
784
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100785 HBasicBlock* GetHeader() const {
786 return header_;
787 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000788
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000789 void SetHeader(HBasicBlock* block) {
790 header_ = block;
791 }
792
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100793 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
794 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
795 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
796
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000797 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100798 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000799 }
800
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100801 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100802 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100803 }
804
David Brazdil46e2a392015-03-16 17:31:52 +0000805 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100806 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100807 }
808
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000809 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100810 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000811 }
812
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100813 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100814
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100815 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100816 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100817 }
818
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100819 // Returns the lifetime position of the back edge that has the
820 // greatest lifetime position.
821 size_t GetLifetimeEnd() const;
822
823 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100824 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100825 }
826
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000827 // Finds blocks that are part of this loop.
828 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100829
Artem Serov7f4aff62017-06-21 17:02:18 +0100830 // Updates blocks population of the loop and all of its outer' ones recursively after the
831 // population of the inner loop is updated.
832 void PopulateInnerLoopUpwards(HLoopInformation* inner_loop);
833
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100834 // Returns whether this loop information contains `block`.
835 // Note that this loop information *must* be populated before entering this function.
836 bool Contains(const HBasicBlock& block) const;
837
838 // Returns whether this loop information is an inner loop of `other`.
839 // Note that `other` *must* be populated before entering this function.
840 bool IsIn(const HLoopInformation& other) const;
841
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800842 // Returns true if instruction is not defined within this loop.
843 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700844
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100845 const ArenaBitVector& GetBlocks() const { return blocks_; }
846
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000847 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000848 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000849
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000850 void ClearAllBlocks() {
851 blocks_.ClearAllBits();
852 }
853
David Brazdil3f4a5222016-05-06 12:46:21 +0100854 bool HasBackEdgeNotDominatedByHeader() const;
855
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100856 bool IsPopulated() const {
857 return blocks_.GetHighestBitSet() != -1;
858 }
859
Anton Shaminf89381f2016-05-16 16:44:13 +0600860 bool DominatesAllBackEdges(HBasicBlock* block);
861
David Sehrc757dec2016-11-04 15:48:34 -0700862 bool HasExitEdge() const;
863
Artem Serov7f4aff62017-06-21 17:02:18 +0100864 // Resets back edge and blocks-in-loop data.
865 void ResetBasicBlockData() {
866 back_edges_.clear();
867 ClearAllBlocks();
868 }
869
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000870 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100871 // Internal recursive implementation of `Populate`.
872 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100873 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100874
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000875 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100876 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000877 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100878 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100879 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100880 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000881
882 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
883};
884
David Brazdilec16f792015-08-19 15:04:01 +0100885// Stores try/catch information for basic blocks.
886// Note that HGraph is constructed so that catch blocks cannot simultaneously
887// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100888class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100889 public:
890 // Try block information constructor.
891 explicit TryCatchInformation(const HTryBoundary& try_entry)
892 : try_entry_(&try_entry),
893 catch_dex_file_(nullptr),
894 catch_type_index_(DexFile::kDexNoIndex16) {
895 DCHECK(try_entry_ != nullptr);
896 }
897
898 // Catch block information constructor.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800899 TryCatchInformation(dex::TypeIndex catch_type_index, const DexFile& dex_file)
David Brazdilec16f792015-08-19 15:04:01 +0100900 : try_entry_(nullptr),
901 catch_dex_file_(&dex_file),
902 catch_type_index_(catch_type_index) {}
903
904 bool IsTryBlock() const { return try_entry_ != nullptr; }
905
906 const HTryBoundary& GetTryEntry() const {
907 DCHECK(IsTryBlock());
908 return *try_entry_;
909 }
910
911 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
912
913 bool IsCatchAllTypeIndex() const {
914 DCHECK(IsCatchBlock());
Andreas Gampea5b09a62016-11-17 15:21:22 -0800915 return !catch_type_index_.IsValid();
David Brazdilec16f792015-08-19 15:04:01 +0100916 }
917
Andreas Gampea5b09a62016-11-17 15:21:22 -0800918 dex::TypeIndex GetCatchTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +0100919 DCHECK(IsCatchBlock());
920 return catch_type_index_;
921 }
922
923 const DexFile& GetCatchDexFile() const {
924 DCHECK(IsCatchBlock());
925 return *catch_dex_file_;
926 }
927
928 private:
929 // One of possibly several TryBoundary instructions entering the block's try.
930 // Only set for try blocks.
931 const HTryBoundary* try_entry_;
932
933 // Exception type information. Only set for catch blocks.
934 const DexFile* catch_dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800935 const dex::TypeIndex catch_type_index_;
David Brazdilec16f792015-08-19 15:04:01 +0100936};
937
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100938static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100939static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100940
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000941// A block in a method. Contains the list of instructions represented
942// as a double linked list. Each block knows its predecessors and
943// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100944
Vladimir Markof9f64412015-09-02 14:05:49 +0100945class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000946 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700947 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000948 : graph_(graph),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100949 predecessors_(graph->GetAllocator()->Adapter(kArenaAllocPredecessors)),
950 successors_(graph->GetAllocator()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000951 loop_information_(nullptr),
952 dominator_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100953 dominated_blocks_(graph->GetAllocator()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100954 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100955 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100956 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000957 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000958 try_catch_information_(nullptr) {
959 predecessors_.reserve(kDefaultNumberOfPredecessors);
960 successors_.reserve(kDefaultNumberOfSuccessors);
961 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
962 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000963
Vladimir Marko60584552015-09-03 13:35:12 +0000964 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100965 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000966 }
967
Vladimir Marko60584552015-09-03 13:35:12 +0000968 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100969 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000970 }
971
David Brazdild26a4112015-11-10 11:07:31 +0000972 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
973 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
974
Vladimir Marko60584552015-09-03 13:35:12 +0000975 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
976 return ContainsElement(successors_, block, start_from);
977 }
978
979 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100980 return dominated_blocks_;
981 }
982
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100983 bool IsEntryBlock() const {
984 return graph_->GetEntryBlock() == this;
985 }
986
987 bool IsExitBlock() const {
988 return graph_->GetExitBlock() == this;
989 }
990
David Brazdil46e2a392015-03-16 17:31:52 +0000991 bool IsSingleGoto() const;
Mads Ager16e52892017-07-14 13:11:37 +0200992 bool IsSingleReturn() const;
Mingyao Yang46721ef2017-10-05 14:45:17 -0700993 bool IsSingleReturnOrReturnVoidAllowingPhis() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000994 bool IsSingleTryBoundary() const;
995
996 // Returns true if this block emits nothing but a jump.
997 bool IsSingleJump() const {
998 HLoopInformation* loop_info = GetLoopInformation();
999 return (IsSingleGoto() || IsSingleTryBoundary())
1000 // Back edges generate a suspend check.
1001 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
1002 }
David Brazdil46e2a392015-03-16 17:31:52 +00001003
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001004 void AddBackEdge(HBasicBlock* back_edge) {
1005 if (loop_information_ == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001006 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001007 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001008 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001009 loop_information_->AddBackEdge(back_edge);
1010 }
1011
Artem Serov7f4aff62017-06-21 17:02:18 +01001012 // Registers a back edge; if the block was not a loop header before the call associates a newly
1013 // created loop info with it.
1014 //
1015 // Used in SuperblockCloner to preserve LoopInformation object instead of reseting loop
1016 // info for all blocks during back edges recalculation.
1017 void AddBackEdgeWhileUpdating(HBasicBlock* back_edge) {
1018 if (loop_information_ == nullptr || loop_information_->GetHeader() != this) {
1019 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
1020 }
1021 loop_information_->AddBackEdge(back_edge);
1022 }
1023
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001024 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001025 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001026
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001027 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001028 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001029 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001030
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001031 HBasicBlock* GetDominator() const { return dominator_; }
1032 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +00001033 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
1034
1035 void RemoveDominatedBlock(HBasicBlock* block) {
1036 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +01001037 }
Vladimir Marko60584552015-09-03 13:35:12 +00001038
1039 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
1040 ReplaceElement(dominated_blocks_, existing, new_block);
1041 }
1042
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001043 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001044
1045 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001046 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001047 }
1048
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001049 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
1050 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +01001051 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001052 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +01001053 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
1054 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001055
Nicolas Geoffray09aa1472016-01-19 10:52:54 +00001056 HInstruction* GetFirstInstructionDisregardMoves() const;
1057
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001058 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001059 successors_.push_back(block);
1060 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001061 }
1062
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001063 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
1064 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001065 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001066 new_block->predecessors_.push_back(this);
1067 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001068 }
1069
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001070 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
1071 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001072 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001073 new_block->successors_.push_back(this);
1074 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001075 }
1076
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001077 // Insert `this` between `predecessor` and `successor. This method
1078 // preserves the indicies, and will update the first edge found between
1079 // `predecessor` and `successor`.
1080 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
1081 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001082 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +00001083 successor->predecessors_[predecessor_index] = this;
1084 predecessor->successors_[successor_index] = this;
1085 successors_.push_back(successor);
1086 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001087 }
1088
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001089 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001090 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001091 }
1092
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001093 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001094 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001095 }
1096
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001097 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001098 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001099 }
1100
1101 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001102 predecessors_.push_back(block);
1103 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001104 }
1105
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001106 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001107 DCHECK_EQ(predecessors_.size(), 2u);
1108 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001109 }
1110
David Brazdil769c9e52015-04-27 13:54:09 +01001111 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001112 DCHECK_EQ(successors_.size(), 2u);
1113 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +01001114 }
1115
David Brazdilfc6a86a2015-06-26 10:33:45 +00001116 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001117 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001118 }
1119
David Brazdilfc6a86a2015-06-26 10:33:45 +00001120 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001121 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001122 }
1123
David Brazdilfc6a86a2015-06-26 10:33:45 +00001124 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001125 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001126 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001127 }
1128
1129 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001130 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001131 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001132 }
1133
1134 // Returns whether the first occurrence of `predecessor` in the list of
1135 // predecessors is at index `idx`.
1136 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001137 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001138 return GetPredecessorIndexOf(predecessor) == idx;
1139 }
1140
David Brazdild7558da2015-09-22 13:04:14 +01001141 // Create a new block between this block and its predecessors. The new block
1142 // is added to the graph, all predecessor edges are relinked to it and an edge
1143 // is created to `this`. Returns the new empty block. Reverse post order or
1144 // loop and try/catch information are not updated.
1145 HBasicBlock* CreateImmediateDominator();
1146
David Brazdilfc6a86a2015-06-26 10:33:45 +00001147 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001148 // created, latter block. Note that this method will add the block to the
1149 // graph, create a Goto at the end of the former block and will create an edge
1150 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001151 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001152 HBasicBlock* SplitBefore(HInstruction* cursor);
1153
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001154 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001155 // created block. Note that this method just updates raw block information,
1156 // like predecessors, successors, dominators, and instruction list. It does not
1157 // update the graph, reverse post order, loop information, nor make sure the
1158 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001159 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1160
1161 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1162 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001163
1164 // Merge `other` at the end of `this`. Successors and dominated blocks of
1165 // `other` are changed to be successors and dominated blocks of `this`. Note
1166 // that this method does not update the graph, reverse post order, loop
1167 // information, nor make sure the blocks are consistent (for example ending
1168 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001169 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001170
1171 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1172 // of `this` are moved to `other`.
1173 // Note that this method does not update the graph, reverse post order, loop
1174 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001175 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001176 void ReplaceWith(HBasicBlock* other);
1177
Aart Bik6b69e0a2017-01-11 10:20:43 -08001178 // Merges the instructions of `other` at the end of `this`.
1179 void MergeInstructionsWith(HBasicBlock* other);
1180
David Brazdil2d7352b2015-04-20 14:52:42 +01001181 // Merge `other` at the end of `this`. This method updates loops, reverse post
1182 // order, links to predecessors, successors, dominators and deletes the block
1183 // from the graph. The two blocks must be successive, i.e. `this` the only
1184 // predecessor of `other` and vice versa.
1185 void MergeWith(HBasicBlock* other);
1186
1187 // Disconnects `this` from all its predecessors, successors and dominator,
1188 // removes it from all loops it is included in and eventually from the graph.
1189 // The block must not dominate any other block. Predecessors and successors
1190 // are safely updated.
1191 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001192
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001193 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001194 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001195 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001196 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Artem Serovcced8ba2017-07-19 18:18:09 +01001197 // Replace phi `initial` with `replacement` within this block.
1198 void ReplaceAndRemovePhiWith(HPhi* initial, HPhi* replacement);
Roland Levillainccc07a92014-09-16 14:48:16 +01001199 // Replace instruction `initial` with `replacement` within this block.
1200 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1201 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001202 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001203 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001204 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1205 // instruction list. With 'ensure_safety' set to true, it verifies that the
1206 // instruction is not in use and removes it from the use lists of its inputs.
1207 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1208 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001209 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001210
1211 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001212 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001213 }
1214
Roland Levillain6b879dd2014-09-22 17:13:44 +01001215 bool IsLoopPreHeaderFirstPredecessor() const {
1216 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001217 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001218 }
1219
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001220 bool IsFirstPredecessorBackEdge() const {
1221 DCHECK(IsLoopHeader());
1222 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1223 }
1224
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001225 HLoopInformation* GetLoopInformation() const {
1226 return loop_information_;
1227 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001228
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001229 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001230 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001231 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001232 void SetInLoop(HLoopInformation* info) {
1233 if (IsLoopHeader()) {
1234 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001235 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001236 loop_information_ = info;
1237 } else if (loop_information_->Contains(*info->GetHeader())) {
1238 // Block is currently part of an outer loop. Make it part of this inner loop.
1239 // Note that a non loop header having a loop information means this loop information
1240 // has already been populated
1241 loop_information_ = info;
1242 } else {
1243 // Block is part of an inner loop. Do not update the loop information.
1244 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1245 // at this point, because this method is being called while populating `info`.
1246 }
1247 }
1248
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001249 // Raw update of the loop information.
1250 void SetLoopInformation(HLoopInformation* info) {
1251 loop_information_ = info;
1252 }
1253
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001254 bool IsInLoop() const { return loop_information_ != nullptr; }
1255
David Brazdilec16f792015-08-19 15:04:01 +01001256 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1257
1258 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1259 try_catch_information_ = try_catch_information;
1260 }
1261
1262 bool IsTryBlock() const {
1263 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1264 }
1265
1266 bool IsCatchBlock() const {
1267 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1268 }
David Brazdilffee3d32015-07-06 11:48:53 +01001269
1270 // Returns the try entry that this block's successors should have. They will
1271 // be in the same try, unless the block ends in a try boundary. In that case,
1272 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001273 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001274
Aart Bik75ff2c92018-04-21 01:28:11 +00001275 bool HasThrowingInstructions() const;
1276
David Brazdila4b8c212015-05-07 09:59:30 +01001277 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001278 bool Dominates(HBasicBlock* block) const;
1279
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001280 size_t GetLifetimeStart() const { return lifetime_start_; }
1281 size_t GetLifetimeEnd() const { return lifetime_end_; }
1282
1283 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1284 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1285
David Brazdil8d5b8b22015-03-24 10:51:52 +00001286 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001287 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001288 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001289 bool HasSinglePhi() const;
1290
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001291 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001292 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001293 ArenaVector<HBasicBlock*> predecessors_;
1294 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001295 HInstructionList instructions_;
1296 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001297 HLoopInformation* loop_information_;
1298 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001299 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001300 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001301 // The dex program counter of the first instruction of this block.
1302 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001303 size_t lifetime_start_;
1304 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001305 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001306
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001307 friend class HGraph;
1308 friend class HInstruction;
1309
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001310 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1311};
1312
David Brazdilb2bd1c52015-03-25 11:17:37 +00001313// Iterates over the LoopInformation of all loops which contain 'block'
1314// from the innermost to the outermost.
1315class HLoopInformationOutwardIterator : public ValueObject {
1316 public:
1317 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1318 : current_(block.GetLoopInformation()) {}
1319
1320 bool Done() const { return current_ == nullptr; }
1321
1322 void Advance() {
1323 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001324 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001325 }
1326
1327 HLoopInformation* Current() const {
1328 DCHECK(!Done());
1329 return current_;
1330 }
1331
1332 private:
1333 HLoopInformation* current_;
1334
1335 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1336};
1337
Alexandre Ramesef20f712015-06-09 10:29:30 +01001338#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001339 M(Above, Condition) \
1340 M(AboveOrEqual, Condition) \
Aart Bik3dad3412018-02-28 12:01:46 -08001341 M(Abs, UnaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001342 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001343 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001344 M(ArrayGet, Instruction) \
1345 M(ArrayLength, Instruction) \
1346 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001347 M(Below, Condition) \
1348 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001349 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001350 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001351 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001352 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001353 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001354 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001355 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001356 M(Compare, BinaryOperation) \
Igor Murashkind01745e2017-04-05 16:40:31 -07001357 M(ConstructorFence, Instruction) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001358 M(CurrentMethod, Instruction) \
Mingyao Yang063fc772016-08-02 11:02:54 -07001359 M(ShouldDeoptimizeFlag, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001360 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001361 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001362 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001363 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001364 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001365 M(Exit, Instruction) \
1366 M(FloatConstant, Constant) \
1367 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001368 M(GreaterThan, Condition) \
1369 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001370 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001371 M(InstanceFieldGet, Instruction) \
1372 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001373 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001374 M(IntConstant, Constant) \
xueliang.zhonge0eb4832017-10-30 13:43:14 +00001375 M(IntermediateAddress, Instruction) \
Calin Juravle175dc732015-08-25 15:42:32 +01001376 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001377 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001378 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001379 M(InvokeVirtual, Invoke) \
Orion Hodsonac141392017-01-13 11:53:47 +00001380 M(InvokePolymorphic, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001381 M(LessThan, Condition) \
1382 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001383 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001384 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001385 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001386 M(LongConstant, Constant) \
Aart Bik1f8d51b2018-02-15 10:42:37 -08001387 M(Max, Instruction) \
Calin Juravle27df7582015-04-17 19:12:31 +01001388 M(MemoryBarrier, Instruction) \
Aart Bik1f8d51b2018-02-15 10:42:37 -08001389 M(Min, BinaryOperation) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001390 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001391 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001392 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001393 M(Neg, UnaryOperation) \
1394 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001395 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001396 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001397 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001398 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001399 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001400 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001401 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001402 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001403 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001404 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001405 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001406 M(Return, Instruction) \
1407 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001408 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001409 M(Shl, BinaryOperation) \
1410 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001411 M(StaticFieldGet, Instruction) \
1412 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001413 M(UnresolvedInstanceFieldGet, Instruction) \
1414 M(UnresolvedInstanceFieldSet, Instruction) \
1415 M(UnresolvedStaticFieldGet, Instruction) \
1416 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001417 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001418 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001419 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001420 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001421 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001422 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001423 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001424 M(Xor, BinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001425 M(VecReplicateScalar, VecUnaryOperation) \
Aart Bik0148de42017-09-05 09:25:01 -07001426 M(VecExtractScalar, VecUnaryOperation) \
1427 M(VecReduce, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001428 M(VecCnv, VecUnaryOperation) \
1429 M(VecNeg, VecUnaryOperation) \
Aart Bik6daebeb2017-04-03 14:35:41 -07001430 M(VecAbs, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001431 M(VecNot, VecUnaryOperation) \
1432 M(VecAdd, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001433 M(VecHalvingAdd, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001434 M(VecSub, VecBinaryOperation) \
1435 M(VecMul, VecBinaryOperation) \
1436 M(VecDiv, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001437 M(VecMin, VecBinaryOperation) \
1438 M(VecMax, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001439 M(VecAnd, VecBinaryOperation) \
1440 M(VecAndNot, VecBinaryOperation) \
1441 M(VecOr, VecBinaryOperation) \
1442 M(VecXor, VecBinaryOperation) \
Aart Bik29aa0822018-03-08 11:28:00 -08001443 M(VecSaturationAdd, VecBinaryOperation) \
1444 M(VecSaturationSub, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001445 M(VecShl, VecBinaryOperation) \
1446 M(VecShr, VecBinaryOperation) \
1447 M(VecUShr, VecBinaryOperation) \
Aart Bik8de59162017-04-21 09:42:01 -07001448 M(VecSetScalars, VecOperation) \
Artem Serovf34dd202017-04-10 17:41:46 +01001449 M(VecMultiplyAccumulate, VecOperation) \
Aart Bikdbbac8f2017-09-01 13:06:08 -07001450 M(VecSADAccumulate, VecOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001451 M(VecLoad, VecMemoryOperation) \
1452 M(VecStore, VecMemoryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001453
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001454/*
1455 * Instructions, shared across several (not all) architectures.
1456 */
1457#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1458#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1459#else
1460#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001461 M(BitwiseNegatedRight, Instruction) \
Anton Kirilov74234da2017-01-13 14:42:47 +00001462 M(DataProcWithShifterOp, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001463 M(MultiplyAccumulate, Instruction) \
Artem Serove1811ed2017-04-27 16:50:47 +01001464 M(IntermediateAddressIndex, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001465#endif
1466
Alexandre Ramesef20f712015-06-09 10:29:30 +01001467#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1468
1469#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1470
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001471#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001472#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001473#else
1474#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1475 M(MipsComputeBaseMethodAddress, Instruction) \
Lena Djokica2901602017-09-21 13:50:52 +02001476 M(MipsPackedSwitch, Instruction) \
1477 M(IntermediateArrayAddressIndex, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001478#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001479
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001480#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1481
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001482#ifndef ART_ENABLE_CODEGEN_x86
1483#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1484#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001485#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1486 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001487 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001488 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001489 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001490#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001491
1492#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1493
1494#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1495 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001496 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001497 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1498 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001499 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001500 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001501 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1502 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1503
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001504#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1505 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001506 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001507 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001508 M(BinaryOperation, Instruction) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001509 M(Invoke, Instruction) \
1510 M(VecOperation, Instruction) \
1511 M(VecUnaryOperation, VecOperation) \
1512 M(VecBinaryOperation, VecOperation) \
1513 M(VecMemoryOperation, VecOperation)
Dave Allison20dfc792014-06-16 20:44:29 -07001514
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001515#define FOR_EACH_INSTRUCTION(M) \
1516 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1517 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1518
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001519#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001520FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1521#undef FORWARD_DECLARATION
1522
Vladimir Marko372f10e2016-05-17 16:30:10 +01001523#define DECLARE_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001524 private: \
1525 H##type& operator=(const H##type&) = delete; \
1526 public: \
Vladimir Marko372f10e2016-05-17 16:30:10 +01001527 const char* DebugName() const OVERRIDE { return #type; } \
1528 bool InstructionTypeEquals(const HInstruction* other) const OVERRIDE { \
1529 return other->Is##type(); \
1530 } \
Artem Serovcced8ba2017-07-19 18:18:09 +01001531 HInstruction* Clone(ArenaAllocator* arena) const OVERRIDE { \
1532 DCHECK(IsClonable()); \
1533 return new (arena) H##type(*this->As##type()); \
1534 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001535 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001536
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001537#define DECLARE_ABSTRACT_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001538 private: \
1539 H##type& operator=(const H##type&) = delete; \
1540 public: \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001541 bool Is##type() const { return As##type() != nullptr; } \
1542 const H##type* As##type() const { return this; } \
1543 H##type* As##type() { return this; }
1544
Artem Serovcced8ba2017-07-19 18:18:09 +01001545#define DEFAULT_COPY_CONSTRUCTOR(type) \
1546 explicit H##type(const H##type& other) = default;
1547
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001548template <typename T>
Vladimir Marko82b07402017-03-01 19:02:04 +00001549class HUseListNode : public ArenaObject<kArenaAllocUseListNode>,
1550 public IntrusiveForwardListNode<HUseListNode<T>> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001551 public:
Igor Murashkind01745e2017-04-05 16:40:31 -07001552 // Get the instruction which has this use as one of the inputs.
David Brazdiled596192015-01-23 10:39:45 +00001553 T GetUser() const { return user_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001554 // Get the position of the input record that this use corresponds to.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001555 size_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001556 // Set the position of the input record that this use corresponds to.
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001557 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001558
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001559 private:
David Brazdiled596192015-01-23 10:39:45 +00001560 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001561 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001562
1563 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001564 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001565
Vladimir Marko46817b82016-03-29 12:21:58 +01001566 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001567
1568 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1569};
1570
David Brazdiled596192015-01-23 10:39:45 +00001571template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001572using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001573
David Brazdil1abb4192015-02-17 18:33:36 +00001574// This class is used by HEnvironment and HInstruction classes to record the
1575// instructions they use and pointers to the corresponding HUseListNodes kept
1576// by the used instructions.
1577template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001578class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001579 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001580 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1581 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001582
Vladimir Marko46817b82016-03-29 12:21:58 +01001583 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1584 : HUserRecord(old_record.instruction_, before_use_node) {}
1585 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1586 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001587 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001588 }
1589
1590 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001591 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1592 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001593
1594 private:
1595 // Instruction used by the user.
1596 HInstruction* instruction_;
1597
Vladimir Marko46817b82016-03-29 12:21:58 +01001598 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1599 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001600};
1601
Vladimir Markoe9004912016-06-16 16:50:52 +01001602// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1603// This is used for HInstruction::GetInputs() to return a container wrapper providing
1604// HInstruction* values even though the underlying container has HUserRecord<>s.
1605struct HInputExtractor {
1606 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1607 return record.GetInstruction();
1608 }
1609 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1610 return record.GetInstruction();
1611 }
1612};
1613
1614using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1615using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1616
Aart Bik854a02b2015-07-14 16:07:00 -07001617/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001618 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001619 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001620 * For write/read dependences on fields/arrays, the dependence analysis uses
1621 * type disambiguation (e.g. a float field write cannot modify the value of an
1622 * integer field read) and the access type (e.g. a reference array write cannot
1623 * modify the value of a reference field read [although it may modify the
1624 * reference fetch prior to reading the field, which is represented by its own
1625 * write/read dependence]). The analysis makes conservative points-to
1626 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1627 * the same, and any reference read depends on any reference read without
1628 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001629 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001630 * The internal representation uses 38-bit and is described in the table below.
1631 * The first line indicates the side effect, and for field/array accesses the
1632 * second line indicates the type of the access (in the order of the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001633 * DataType::Type enum).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001634 * The two numbered lines below indicate the bit position in the bitfield (read
1635 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001636 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001637 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1638 * +-------------+---------+---------+--------------+---------+---------+
1639 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1640 * | 3 |333333322|222222221| 1 |111111110|000000000|
1641 * | 7 |654321098|765432109| 8 |765432109|876543210|
1642 *
1643 * Note that, to ease the implementation, 'changes' bits are least significant
1644 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001645 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001646class SideEffects : public ValueObject {
1647 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001648 SideEffects() : flags_(0) {}
1649
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001650 static SideEffects None() {
1651 return SideEffects(0);
1652 }
1653
1654 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001655 return SideEffects(kAllChangeBits | kAllDependOnBits);
1656 }
1657
1658 static SideEffects AllChanges() {
1659 return SideEffects(kAllChangeBits);
1660 }
1661
1662 static SideEffects AllDependencies() {
1663 return SideEffects(kAllDependOnBits);
1664 }
1665
1666 static SideEffects AllExceptGCDependency() {
1667 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1668 }
1669
1670 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001671 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001672 }
1673
Aart Bik34c3ba92015-07-20 14:08:59 -07001674 static SideEffects AllWrites() {
1675 return SideEffects(kAllWrites);
1676 }
1677
1678 static SideEffects AllReads() {
1679 return SideEffects(kAllReads);
1680 }
1681
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001682 static SideEffects FieldWriteOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001683 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001684 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001685 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001686 }
1687
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001688 static SideEffects ArrayWriteOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001689 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001690 }
1691
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001692 static SideEffects FieldReadOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001693 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001694 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001695 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001696 }
1697
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001698 static SideEffects ArrayReadOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001699 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001700 }
1701
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001702 static SideEffects CanTriggerGC() {
1703 return SideEffects(1ULL << kCanTriggerGCBit);
1704 }
1705
1706 static SideEffects DependsOnGC() {
1707 return SideEffects(1ULL << kDependsOnGCBit);
1708 }
1709
Aart Bik854a02b2015-07-14 16:07:00 -07001710 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001711 SideEffects Union(SideEffects other) const {
1712 return SideEffects(flags_ | other.flags_);
1713 }
1714
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001715 SideEffects Exclusion(SideEffects other) const {
1716 return SideEffects(flags_ & ~other.flags_);
1717 }
1718
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001719 void Add(SideEffects other) {
1720 flags_ |= other.flags_;
1721 }
1722
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001723 bool Includes(SideEffects other) const {
1724 return (other.flags_ & flags_) == other.flags_;
1725 }
1726
1727 bool HasSideEffects() const {
1728 return (flags_ & kAllChangeBits);
1729 }
1730
1731 bool HasDependencies() const {
1732 return (flags_ & kAllDependOnBits);
1733 }
1734
1735 // Returns true if there are no side effects or dependencies.
1736 bool DoesNothing() const {
1737 return flags_ == 0;
1738 }
1739
Aart Bik854a02b2015-07-14 16:07:00 -07001740 // Returns true if something is written.
1741 bool DoesAnyWrite() const {
1742 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001743 }
1744
Aart Bik854a02b2015-07-14 16:07:00 -07001745 // Returns true if something is read.
1746 bool DoesAnyRead() const {
1747 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001748 }
1749
Aart Bik854a02b2015-07-14 16:07:00 -07001750 // Returns true if potentially everything is written and read
1751 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001752 bool DoesAllReadWrite() const {
1753 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1754 }
1755
Aart Bik854a02b2015-07-14 16:07:00 -07001756 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001757 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001758 }
1759
Roland Levillain0d5a2812015-11-13 10:07:31 +00001760 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001761 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001762 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1763 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001764 }
1765
1766 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001767 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001768 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001769 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001770 for (int s = kLastBit; s >= 0; s--) {
1771 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1772 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1773 // This is a bit for the GC side effect.
1774 if (current_bit_is_set) {
1775 flags += "GC";
1776 }
Aart Bik854a02b2015-07-14 16:07:00 -07001777 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001778 } else {
1779 // This is a bit for the array/field analysis.
1780 // The underscore character stands for the 'can trigger GC' bit.
1781 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1782 if (current_bit_is_set) {
1783 flags += kDebug[s];
1784 }
1785 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1786 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1787 flags += "|";
1788 }
1789 }
Aart Bik854a02b2015-07-14 16:07:00 -07001790 }
1791 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001792 }
1793
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001794 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001795
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001796 private:
1797 static constexpr int kFieldArrayAnalysisBits = 9;
1798
1799 static constexpr int kFieldWriteOffset = 0;
1800 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1801 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1802 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1803
1804 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1805
1806 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1807 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1808 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1809 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1810
1811 static constexpr int kLastBit = kDependsOnGCBit;
1812 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1813
1814 // Aliases.
1815
1816 static_assert(kChangeBits == kDependOnBits,
1817 "the 'change' bits should match the 'depend on' bits.");
1818
1819 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1820 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1821 static constexpr uint64_t kAllWrites =
1822 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1823 static constexpr uint64_t kAllReads =
1824 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001825
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001826 // Translates type to bit flag. The type must correspond to a Java type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001827 static uint64_t TypeFlag(DataType::Type type, int offset) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001828 int shift;
1829 switch (type) {
1830 case DataType::Type::kReference: shift = 0; break;
1831 case DataType::Type::kBool: shift = 1; break;
1832 case DataType::Type::kInt8: shift = 2; break;
1833 case DataType::Type::kUint16: shift = 3; break;
1834 case DataType::Type::kInt16: shift = 4; break;
1835 case DataType::Type::kInt32: shift = 5; break;
1836 case DataType::Type::kInt64: shift = 6; break;
1837 case DataType::Type::kFloat32: shift = 7; break;
1838 case DataType::Type::kFloat64: shift = 8; break;
1839 default:
1840 LOG(FATAL) << "Unexpected data type " << type;
1841 UNREACHABLE();
1842 }
Aart Bik854a02b2015-07-14 16:07:00 -07001843 DCHECK_LE(kFieldWriteOffset, shift);
1844 DCHECK_LT(shift, kArrayWriteOffset);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001845 return UINT64_C(1) << (shift + offset);
Aart Bik854a02b2015-07-14 16:07:00 -07001846 }
1847
1848 // Private constructor on direct flags value.
1849 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1850
1851 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001852};
1853
David Brazdiled596192015-01-23 10:39:45 +00001854// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001855class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001856 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001857 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
Mingyao Yang01b47b02017-02-03 12:09:57 -08001858 size_t number_of_vregs,
1859 ArtMethod* method,
1860 uint32_t dex_pc,
1861 HInstruction* holder)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001862 : vregs_(number_of_vregs, allocator->Adapter(kArenaAllocEnvironmentVRegs)),
1863 locations_(allocator->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001864 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001865 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001866 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001867 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001868 }
1869
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001870 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
1871 const HEnvironment& to_copy,
1872 HInstruction* holder)
1873 : HEnvironment(allocator,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001874 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001875 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001876 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001877 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001878
Vladimir Markoec32f642017-06-02 10:51:55 +01001879 void AllocateLocations() {
1880 DCHECK(locations_.empty());
1881 locations_.resize(vregs_.size());
1882 }
1883
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001884 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001885 if (parent_ != nullptr) {
1886 parent_->SetAndCopyParentChain(allocator, parent);
1887 } else {
1888 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1889 parent_->CopyFrom(parent);
1890 if (parent->GetParent() != nullptr) {
1891 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1892 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001893 }
David Brazdiled596192015-01-23 10:39:45 +00001894 }
1895
Vladimir Marko69d310e2017-10-09 14:12:23 +01001896 void CopyFrom(ArrayRef<HInstruction* const> locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001897 void CopyFrom(HEnvironment* environment);
1898
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001899 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1900 // input to the loop phi instead. This is for inserting instructions that
1901 // require an environment (like HDeoptimization) in the loop pre-header.
1902 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001903
1904 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001905 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001906 }
1907
1908 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001909 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001910 }
1911
David Brazdil1abb4192015-02-17 18:33:36 +00001912 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001913
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001914 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001915
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001916 HEnvironment* GetParent() const { return parent_; }
1917
1918 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001919 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001920 }
1921
1922 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001923 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001924 }
1925
1926 uint32_t GetDexPc() const {
1927 return dex_pc_;
1928 }
1929
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001930 ArtMethod* GetMethod() const {
1931 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001932 }
1933
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001934 HInstruction* GetHolder() const {
1935 return holder_;
1936 }
1937
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001938
1939 bool IsFromInlinedInvoke() const {
1940 return GetParent() != nullptr;
1941 }
1942
David Brazdiled596192015-01-23 10:39:45 +00001943 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001944 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1945 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001946 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001947 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001948 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00001949
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001950 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001951 HInstruction* const holder_;
1952
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001953 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001954
1955 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1956};
1957
Vladimir Markof9f64412015-09-02 14:05:49 +01001958class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001959 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05301960#define DECLARE_KIND(type, super) k##type,
1961 enum InstructionKind {
1962 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1963 kLastInstructionKind
1964 };
1965#undef DECLARE_KIND
1966
1967 HInstruction(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01001968 : HInstruction(kind, DataType::Type::kVoid, side_effects, dex_pc) {}
1969
1970 HInstruction(InstructionKind kind, DataType::Type type, SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001971 : previous_(nullptr),
1972 next_(nullptr),
1973 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001974 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001975 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001976 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001977 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001978 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001979 locations_(nullptr),
1980 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001981 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001982 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001983 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05301984 SetPackedField<InstructionKindField>(kind);
Vladimir Markobd785672018-05-03 17:09:09 +01001985 SetPackedField<TypeField>(type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001986 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1987 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001988
Dave Allison20dfc792014-06-16 20:44:29 -07001989 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001990
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001991
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001992 HInstruction* GetNext() const { return next_; }
1993 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001994
Calin Juravle77520bc2015-01-12 18:45:46 +00001995 HInstruction* GetNextDisregardingMoves() const;
1996 HInstruction* GetPreviousDisregardingMoves() const;
1997
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001998 HBasicBlock* GetBlock() const { return block_; }
Vladimir Markoca6fff82017-10-03 14:49:14 +01001999 ArenaAllocator* GetAllocator() const { return block_->GetGraph()->GetAllocator(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002000 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002001 bool IsInBlock() const { return block_ != nullptr; }
2002 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00002003 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
2004 bool IsIrreducibleLoopHeaderPhi() const {
2005 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
2006 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002007
Vladimir Marko372f10e2016-05-17 16:30:10 +01002008 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
2009
2010 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
2011 // One virtual method is enough, just const_cast<> and then re-add the const.
2012 return ArrayRef<const HUserRecord<HInstruction*>>(
2013 const_cast<HInstruction*>(this)->GetInputRecords());
2014 }
2015
Vladimir Markoe9004912016-06-16 16:50:52 +01002016 HInputsRef GetInputs() {
2017 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002018 }
2019
Vladimir Markoe9004912016-06-16 16:50:52 +01002020 HConstInputsRef GetInputs() const {
2021 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002022 }
2023
2024 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00002025 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002026
Aart Bik2767f4b2016-10-28 15:03:53 -07002027 bool HasInput(HInstruction* input) const {
2028 for (const HInstruction* i : GetInputs()) {
2029 if (i == input) {
2030 return true;
2031 }
2032 }
2033 return false;
2034 }
2035
Vladimir Marko372f10e2016-05-17 16:30:10 +01002036 void SetRawInputAt(size_t index, HInstruction* input) {
2037 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
2038 }
2039
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002040 virtual void Accept(HGraphVisitor* visitor) = 0;
2041 virtual const char* DebugName() const = 0;
2042
Vladimir Markobd785672018-05-03 17:09:09 +01002043 DataType::Type GetType() const {
2044 return TypeField::Decode(GetPackedFields());
2045 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002046
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002047 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002048
2049 uint32_t GetDexPc() const { return dex_pc_; }
2050
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002051 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01002052
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002053 // Can the instruction throw?
2054 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
2055 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01002056 virtual bool CanThrow() const { return false; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08002057
2058 // Does the instruction always throw an exception unconditionally?
2059 virtual bool AlwaysThrows() const { return false; }
2060
David Brazdilec16f792015-08-19 15:04:01 +01002061 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002062
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002063 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002064 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002065
Calin Juravle10e244f2015-01-26 18:54:32 +00002066 // Does not apply for all instructions, but having this at top level greatly
2067 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00002068 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002069 virtual bool CanBeNull() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002070 DCHECK_EQ(GetType(), DataType::Type::kReference) << "CanBeNull only applies to reference types";
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002071 return true;
2072 }
Calin Juravle10e244f2015-01-26 18:54:32 +00002073
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002074 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01002075 return false;
2076 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002077
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002078 virtual bool IsActualObject() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002079 return GetType() == DataType::Type::kReference;
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002080 }
2081
Calin Juravle2e768302015-07-28 14:41:11 +00002082 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00002083
Calin Juravle61d544b2015-02-23 16:46:57 +00002084 ReferenceTypeInfo GetReferenceTypeInfo() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002085 DCHECK_EQ(GetType(), DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002086 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00002087 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00002088 }
Calin Juravleacf735c2015-02-12 15:25:22 +00002089
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002090 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00002091 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002092 // Note: fixup_end remains valid across push_front().
2093 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
2094 HUseListNode<HInstruction*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002095 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HInstruction*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002096 uses_.push_front(*new_node);
2097 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002098 }
2099
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002100 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01002101 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002102 // Note: env_fixup_end remains valid across push_front().
2103 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
2104 HUseListNode<HEnvironment*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002105 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HEnvironment*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002106 env_uses_.push_front(*new_node);
2107 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002108 }
2109
David Brazdil1abb4192015-02-17 18:33:36 +00002110 void RemoveAsUserOfInput(size_t input) {
2111 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002112 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2113 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2114 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00002115 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002116
Vladimir Marko372f10e2016-05-17 16:30:10 +01002117 void RemoveAsUserOfAllInputs() {
2118 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2119 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2120 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2121 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
2122 }
2123 }
2124
David Brazdil1abb4192015-02-17 18:33:36 +00002125 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2126 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002127
Vladimir Marko46817b82016-03-29 12:21:58 +01002128 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2129 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2130 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002131 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002132 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002133 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002134
Aart Bikcc42be02016-10-20 16:14:16 -07002135 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002136 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002137 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002138 !CanThrow() &&
2139 !IsSuspendCheck() &&
2140 !IsControlFlow() &&
2141 !IsNativeDebugInfo() &&
2142 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002143 // If we added an explicit barrier then we should keep it.
Igor Murashkind01745e2017-04-05 16:40:31 -07002144 !IsMemoryBarrier() &&
2145 !IsConstructorFence();
Aart Bik482095d2016-10-10 15:39:10 -07002146 }
2147
Aart Bikcc42be02016-10-20 16:14:16 -07002148 bool IsDeadAndRemovable() const {
2149 return IsRemovable() && !HasUses();
2150 }
2151
Roland Levillain6c82d402014-10-13 16:10:27 +01002152 // Does this instruction strictly dominate `other_instruction`?
2153 // Returns false if this instruction and `other_instruction` are the same.
2154 // Aborts if this instruction and `other_instruction` are both phis.
2155 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002156
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002157 int GetId() const { return id_; }
2158 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002159
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002160 int GetSsaIndex() const { return ssa_index_; }
2161 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2162 bool HasSsaIndex() const { return ssa_index_ != -1; }
2163
2164 bool HasEnvironment() const { return environment_ != nullptr; }
2165 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002166 // Set the `environment_` field. Raw because this method does not
2167 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002168 void SetRawEnvironment(HEnvironment* environment) {
2169 DCHECK(environment_ == nullptr);
2170 DCHECK_EQ(environment->GetHolder(), this);
2171 environment_ = environment;
2172 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002173
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002174 void InsertRawEnvironment(HEnvironment* environment) {
2175 DCHECK(environment_ != nullptr);
2176 DCHECK_EQ(environment->GetHolder(), this);
2177 DCHECK(environment->GetParent() == nullptr);
2178 environment->parent_ = environment_;
2179 environment_ = environment;
2180 }
2181
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002182 void RemoveEnvironment();
2183
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002184 // Set the environment of this instruction, copying it from `environment`. While
2185 // copying, the uses lists are being updated.
2186 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002187 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002188 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002189 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002190 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002191 if (environment->GetParent() != nullptr) {
2192 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2193 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002194 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002195
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002196 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2197 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002198 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002199 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002200 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002201 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002202 if (environment->GetParent() != nullptr) {
2203 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2204 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002205 }
2206
Nicolas Geoffray39468442014-09-02 15:17:15 +01002207 // Returns the number of entries in the environment. Typically, that is the
2208 // number of dex registers in a method. It could be more in case of inlining.
2209 size_t EnvironmentSize() const;
2210
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002211 LocationSummary* GetLocations() const { return locations_; }
2212 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002213
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002214 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002215 void ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002216 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002217
Alexandre Rames188d4312015-04-09 18:30:21 +01002218 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2219 // uses of this instruction by `other` are *not* updated.
2220 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2221 ReplaceWith(other);
2222 other->ReplaceInput(this, use_index);
2223 }
2224
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002225 // Move `this` instruction before `cursor`
2226 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002227
Vladimir Markofb337ea2015-11-25 15:25:10 +00002228 // Move `this` before its first user and out of any loops. If there is no
2229 // out-of-loop user that dominates all other users, move the instruction
2230 // to the end of the out-of-loop common dominator of the user's blocks.
2231 //
2232 // This can be used only on non-throwing instructions with no side effects that
2233 // have at least one use but no environment uses.
2234 void MoveBeforeFirstUserAndOutOfLoops();
2235
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002236#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002237 bool Is##type() const; \
2238 const H##type* As##type() const; \
2239 H##type* As##type();
2240
2241 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2242#undef INSTRUCTION_TYPE_CHECK
2243
2244#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002245 bool Is##type() const { return (As##type() != nullptr); } \
2246 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002247 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002248 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002249#undef INSTRUCTION_TYPE_CHECK
2250
Artem Serovcced8ba2017-07-19 18:18:09 +01002251 // Return a clone of the instruction if it is clonable (shallow copy by default, custom copy
2252 // if a custom copy-constructor is provided for a particular type). If IsClonable() is false for
2253 // the instruction then the behaviour of this function is undefined.
2254 //
2255 // Note: It is semantically valid to create a clone of the instruction only until
2256 // prepare_for_register_allocator phase as lifetime, intervals and codegen info are not
2257 // copied.
2258 //
2259 // Note: HEnvironment and some other fields are not copied and are set to default values, see
2260 // 'explicit HInstruction(const HInstruction& other)' for details.
2261 virtual HInstruction* Clone(ArenaAllocator* arena ATTRIBUTE_UNUSED) const {
2262 LOG(FATAL) << "Cloning is not implemented for the instruction " <<
2263 DebugName() << " " << GetId();
2264 UNREACHABLE();
2265 }
2266
2267 // Return whether instruction can be cloned (copied).
2268 virtual bool IsClonable() const { return false; }
2269
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002270 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002271 // TODO: this method is used by LICM and GVN with possibly different
2272 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002273 virtual bool CanBeMoved() const { return false; }
2274
2275 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002276 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002277 return false;
2278 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002279
2280 // Returns whether any data encoded in the two instructions is equal.
2281 // This method does not look at the inputs. Both instructions must be
2282 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002283 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002284 return false;
2285 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002286
2287 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002288 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002289 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002290 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002291
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002292 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2293 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2294 // the virtual function because the __attribute__((__pure__)) doesn't really
2295 // apply the strong requirement for virtual functions, preventing optimizations.
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302296 InstructionKind GetKind() const { return GetPackedField<InstructionKindField>(); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002297
2298 virtual size_t ComputeHashCode() const {
2299 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002300 for (const HInstruction* input : GetInputs()) {
2301 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002302 }
2303 return result;
2304 }
2305
2306 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002307 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002308 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002309
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002310 size_t GetLifetimePosition() const { return lifetime_position_; }
2311 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2312 LiveInterval* GetLiveInterval() const { return live_interval_; }
2313 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2314 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2315
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002316 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2317
2318 // Returns whether the code generation of the instruction will require to have access
2319 // to the current method. Such instructions are:
2320 // (1): Instructions that require an environment, as calling the runtime requires
2321 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002322 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2323 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002324 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002325 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002326 }
2327
Vladimir Markodc151b22015-10-15 18:02:30 +01002328 // Returns whether the code generation of the instruction will require to have access
2329 // to the dex cache of the current method's declaring class via the current method.
2330 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002331
Mark Mendellc4701932015-04-10 13:18:51 -04002332 // Does this instruction have any use in an environment before
2333 // control flow hits 'other'?
2334 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2335
2336 // Remove all references to environment uses of this instruction.
2337 // The caller must ensure that this is safe to do.
2338 void RemoveEnvironmentUsers();
2339
Vladimir Markoa1de9182016-02-25 11:37:38 +00002340 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2341 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002342
David Brazdil1abb4192015-02-17 18:33:36 +00002343 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002344 // If set, the machine code for this instruction is assumed to be generated by
2345 // its users. Used by liveness analysis to compute use positions accordingly.
2346 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2347 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302348 static constexpr size_t kFieldInstructionKind = kFlagReferenceTypeIsExact + 1;
2349 static constexpr size_t kFieldInstructionKindSize =
2350 MinimumBitsToStore(static_cast<size_t>(InstructionKind::kLastInstructionKind - 1));
Vladimir Markobd785672018-05-03 17:09:09 +01002351 static constexpr size_t kFieldType =
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302352 kFieldInstructionKind + kFieldInstructionKindSize;
Vladimir Markobd785672018-05-03 17:09:09 +01002353 static constexpr size_t kFieldTypeSize =
2354 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
2355 static constexpr size_t kNumberOfGenericPackedBits = kFieldType + kFieldTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00002356 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2357
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302358 static_assert(kNumberOfGenericPackedBits <= kMaxNumberOfPackedBits,
2359 "Too many generic packed fields");
2360
Vladimir Markobd785672018-05-03 17:09:09 +01002361 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
2362
Vladimir Marko372f10e2016-05-17 16:30:10 +01002363 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2364 return GetInputRecords()[i];
2365 }
2366
2367 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2368 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2369 input_records[index] = input;
2370 }
David Brazdil1abb4192015-02-17 18:33:36 +00002371
Vladimir Markoa1de9182016-02-25 11:37:38 +00002372 uint32_t GetPackedFields() const {
2373 return packed_fields_;
2374 }
2375
2376 template <size_t flag>
2377 bool GetPackedFlag() const {
2378 return (packed_fields_ & (1u << flag)) != 0u;
2379 }
2380
2381 template <size_t flag>
2382 void SetPackedFlag(bool value = true) {
2383 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2384 }
2385
2386 template <typename BitFieldType>
2387 typename BitFieldType::value_type GetPackedField() const {
2388 return BitFieldType::Decode(packed_fields_);
2389 }
2390
2391 template <typename BitFieldType>
2392 void SetPackedField(typename BitFieldType::value_type value) {
2393 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2394 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2395 }
2396
Artem Serovcced8ba2017-07-19 18:18:09 +01002397 // Copy construction for the instruction (used for Clone function).
2398 //
2399 // Fields (e.g. lifetime, intervals and codegen info) associated with phases starting from
2400 // prepare_for_register_allocator are not copied (set to default values).
2401 //
2402 // Copy constructors must be provided for every HInstruction type; default copy constructor is
2403 // fine for most of them. However for some of the instructions a custom copy constructor must be
2404 // specified (when instruction has non-trivially copyable fields and must have a special behaviour
2405 // for copying them).
2406 explicit HInstruction(const HInstruction& other)
2407 : previous_(nullptr),
2408 next_(nullptr),
2409 block_(nullptr),
2410 dex_pc_(other.dex_pc_),
2411 id_(-1),
2412 ssa_index_(-1),
2413 packed_fields_(other.packed_fields_),
2414 environment_(nullptr),
2415 locations_(nullptr),
2416 live_interval_(nullptr),
2417 lifetime_position_(kNoLifetime),
2418 side_effects_(other.side_effects_),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302419 reference_type_handle_(other.reference_type_handle_) {
2420 }
Artem Serovcced8ba2017-07-19 18:18:09 +01002421
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002422 private:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302423 using InstructionKindField =
2424 BitField<InstructionKind, kFieldInstructionKind, kFieldInstructionKindSize>;
2425
Vladimir Marko46817b82016-03-29 12:21:58 +01002426 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2427 auto before_use_node = uses_.before_begin();
2428 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2429 HInstruction* user = use_node->GetUser();
2430 size_t input_index = use_node->GetIndex();
2431 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2432 before_use_node = use_node;
2433 }
2434 }
2435
2436 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2437 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2438 if (next != uses_.end()) {
2439 HInstruction* next_user = next->GetUser();
2440 size_t next_index = next->GetIndex();
2441 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2442 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2443 }
2444 }
2445
2446 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2447 auto before_env_use_node = env_uses_.before_begin();
2448 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2449 HEnvironment* user = env_use_node->GetUser();
2450 size_t input_index = env_use_node->GetIndex();
2451 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2452 before_env_use_node = env_use_node;
2453 }
2454 }
2455
2456 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2457 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2458 if (next != env_uses_.end()) {
2459 HEnvironment* next_user = next->GetUser();
2460 size_t next_index = next->GetIndex();
2461 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2462 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2463 }
2464 }
David Brazdil1abb4192015-02-17 18:33:36 +00002465
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002466 HInstruction* previous_;
2467 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002468 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002469 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002470
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002471 // An instruction gets an id when it is added to the graph.
2472 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002473 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002474 int id_;
2475
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002476 // When doing liveness analysis, instructions that have uses get an SSA index.
2477 int ssa_index_;
2478
Vladimir Markoa1de9182016-02-25 11:37:38 +00002479 // Packed fields.
2480 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002481
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002482 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002483 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002484
2485 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002486 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002487
Nicolas Geoffray39468442014-09-02 15:17:15 +01002488 // The environment associated with this instruction. Not null if the instruction
2489 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002490 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002491
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002492 // Set by the code generator.
2493 LocationSummary* locations_;
2494
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002495 // Set by the liveness analysis.
2496 LiveInterval* live_interval_;
2497
2498 // Set by the liveness analysis, this is the position in a linear
2499 // order of blocks where this instruction's live interval start.
2500 size_t lifetime_position_;
2501
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002502 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002503
Vladimir Markoa1de9182016-02-25 11:37:38 +00002504 // The reference handle part of the reference type info.
2505 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002506 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002507 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002508
David Brazdil1abb4192015-02-17 18:33:36 +00002509 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002510 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002511 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002512 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002513 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002514};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002515std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002516
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002517// Iterates over the instructions, while preserving the next instruction
2518// in case the current instruction gets removed from the list by the user
2519// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002520class HInstructionIterator : public ValueObject {
2521 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002522 explicit HInstructionIterator(const HInstructionList& instructions)
2523 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002524 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002525 }
2526
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002527 bool Done() const { return instruction_ == nullptr; }
2528 HInstruction* Current() const { return instruction_; }
2529 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002530 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002531 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002532 }
2533
2534 private:
2535 HInstruction* instruction_;
2536 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002537
2538 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002539};
2540
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002541// Iterates over the instructions without saving the next instruction,
2542// therefore handling changes in the graph potentially made by the user
2543// of this iterator.
2544class HInstructionIteratorHandleChanges : public ValueObject {
2545 public:
2546 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2547 : instruction_(instructions.first_instruction_) {
2548 }
2549
2550 bool Done() const { return instruction_ == nullptr; }
2551 HInstruction* Current() const { return instruction_; }
2552 void Advance() {
2553 instruction_ = instruction_->GetNext();
2554 }
2555
2556 private:
2557 HInstruction* instruction_;
2558
2559 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2560};
2561
2562
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002563class HBackwardInstructionIterator : public ValueObject {
2564 public:
2565 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2566 : instruction_(instructions.last_instruction_) {
2567 next_ = Done() ? nullptr : instruction_->GetPrevious();
2568 }
2569
2570 bool Done() const { return instruction_ == nullptr; }
2571 HInstruction* Current() const { return instruction_; }
2572 void Advance() {
2573 instruction_ = next_;
2574 next_ = Done() ? nullptr : instruction_->GetPrevious();
2575 }
2576
2577 private:
2578 HInstruction* instruction_;
2579 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002580
2581 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002582};
2583
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002584class HVariableInputSizeInstruction : public HInstruction {
2585 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002586 using HInstruction::GetInputRecords; // Keep the const version visible.
2587 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
2588 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2589 }
2590
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002591 void AddInput(HInstruction* input);
2592 void InsertInputAt(size_t index, HInstruction* input);
2593 void RemoveInputAt(size_t index);
2594
Igor Murashkind01745e2017-04-05 16:40:31 -07002595 // Removes all the inputs.
2596 // Also removes this instructions from each input's use list
2597 // (for non-environment uses only).
2598 void RemoveAllInputs();
2599
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002600 protected:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302601 HVariableInputSizeInstruction(InstructionKind inst_kind,
2602 SideEffects side_effects,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002603 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002604 ArenaAllocator* allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002605 size_t number_of_inputs,
2606 ArenaAllocKind kind)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302607 : HInstruction(inst_kind, side_effects, dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002608 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Vladimir Markobd785672018-05-03 17:09:09 +01002609 HVariableInputSizeInstruction(InstructionKind inst_kind,
2610 DataType::Type type,
2611 SideEffects side_effects,
2612 uint32_t dex_pc,
2613 ArenaAllocator* allocator,
2614 size_t number_of_inputs,
2615 ArenaAllocKind kind)
2616 : HInstruction(inst_kind, type, side_effects, dex_pc),
2617 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002618
Artem Serovcced8ba2017-07-19 18:18:09 +01002619 DEFAULT_COPY_CONSTRUCTOR(VariableInputSizeInstruction);
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002620
Artem Serovcced8ba2017-07-19 18:18:09 +01002621 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002622};
2623
Vladimir Markof9f64412015-09-02 14:05:49 +01002624template<size_t N>
Vladimir Markobd785672018-05-03 17:09:09 +01002625class HExpression : public HInstruction {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002626 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002627 HExpression<N>(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302628 : HInstruction(kind, side_effects, dex_pc), inputs_() {}
Vladimir Markobd785672018-05-03 17:09:09 +01002629 HExpression<N>(InstructionKind kind,
2630 DataType::Type type,
2631 SideEffects side_effects,
2632 uint32_t dex_pc)
2633 : HInstruction(kind, type, side_effects, dex_pc), inputs_() {}
2634 virtual ~HExpression() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002635
Vladimir Marko372f10e2016-05-17 16:30:10 +01002636 using HInstruction::GetInputRecords; // Keep the const version visible.
2637 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2638 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002639 }
2640
Artem Serovcced8ba2017-07-19 18:18:09 +01002641 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002642 DEFAULT_COPY_CONSTRUCTOR(Expression<N>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002643
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002644 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002645 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002646
2647 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002648};
2649
Vladimir Markobd785672018-05-03 17:09:09 +01002650// HExpression specialization for N=0.
Vladimir Markof9f64412015-09-02 14:05:49 +01002651template<>
Vladimir Markobd785672018-05-03 17:09:09 +01002652class HExpression<0> : public HInstruction {
Vladimir Markof9f64412015-09-02 14:05:49 +01002653 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002654 using HInstruction::HInstruction;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002655
Vladimir Markobd785672018-05-03 17:09:09 +01002656 virtual ~HExpression() {}
Vladimir Markof9f64412015-09-02 14:05:49 +01002657
Vladimir Marko372f10e2016-05-17 16:30:10 +01002658 using HInstruction::GetInputRecords; // Keep the const version visible.
2659 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2660 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002661 }
2662
Artem Serovcced8ba2017-07-19 18:18:09 +01002663 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002664 DEFAULT_COPY_CONSTRUCTOR(Expression<0>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002665
Vladimir Markof9f64412015-09-02 14:05:49 +01002666 private:
2667 friend class SsaBuilder;
2668};
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 Markobd785672018-05-03 17:09:09 +01002672class HReturnVoid FINAL : public HExpression<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)
Vladimir Markobd785672018-05-03 17:09:09 +01002675 : HExpression(kReturnVoid, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302676 }
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 Markobd785672018-05-03 17:09:09 +01002688class HReturn FINAL : public HExpression<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)
Vladimir Markobd785672018-05-03 17:09:09 +01002691 : HExpression(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,
Vladimir Markobd785672018-05-03 17:09:09 +01002712 ToPhiType(type),
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002713 SideEffects::None(),
2714 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002715 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002716 number_of_inputs,
2717 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002718 reg_number_(reg_number) {
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 void SetType(DataType::Type new_type) {
David Brazdildee58d62016-04-07 09:54:26 +00002737 // Make sure that only valid type changes occur. The following are allowed:
2738 // (1) int -> float/ref (primitive type propagation),
2739 // (2) long -> double (primitive type propagation).
2740 DCHECK(GetType() == new_type ||
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002741 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kFloat32) ||
2742 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kReference) ||
2743 (GetType() == DataType::Type::kInt64 && new_type == DataType::Type::kFloat64));
David Brazdildee58d62016-04-07 09:54:26 +00002744 SetPackedField<TypeField>(new_type);
2745 }
2746
2747 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2748 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2749
2750 uint32_t GetRegNumber() const { return reg_number_; }
2751
2752 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2753 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2754 bool IsDead() const { return !IsLive(); }
2755 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2756
Vladimir Markoe9004912016-06-16 16:50:52 +01002757 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002758 return other != nullptr
2759 && other->IsPhi()
2760 && other->AsPhi()->GetBlock() == GetBlock()
2761 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2762 }
2763
Nicolas Geoffrayf57c1ae2017-06-28 17:40:18 +01002764 bool HasEquivalentPhi() const {
2765 if (GetPrevious() != nullptr && GetPrevious()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2766 return true;
2767 }
2768 if (GetNext() != nullptr && GetNext()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2769 return true;
2770 }
2771 return false;
2772 }
2773
David Brazdildee58d62016-04-07 09:54:26 +00002774 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2775 // An equivalent phi is a phi having the same dex register and type.
2776 // It assumes that phis with the same dex register are adjacent.
2777 HPhi* GetNextEquivalentPhiWithSameType() {
2778 HInstruction* next = GetNext();
2779 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2780 if (next->GetType() == GetType()) {
2781 return next->AsPhi();
2782 }
2783 next = next->GetNext();
2784 }
2785 return nullptr;
2786 }
2787
2788 DECLARE_INSTRUCTION(Phi);
2789
Artem Serovcced8ba2017-07-19 18:18:09 +01002790 protected:
2791 DEFAULT_COPY_CONSTRUCTOR(Phi);
2792
David Brazdildee58d62016-04-07 09:54:26 +00002793 private:
Vladimir Markobd785672018-05-03 17:09:09 +01002794 static constexpr size_t kFlagIsLive = HInstruction::kNumberOfGenericPackedBits;
David Brazdildee58d62016-04-07 09:54:26 +00002795 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2796 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2797 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
David Brazdildee58d62016-04-07 09:54:26 +00002798
David Brazdildee58d62016-04-07 09:54:26 +00002799 const uint32_t reg_number_;
David Brazdildee58d62016-04-07 09:54:26 +00002800};
2801
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002802// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002803// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002804// exit block.
Vladimir Markobd785672018-05-03 17:09:09 +01002805class HExit FINAL : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002806 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302807 explicit HExit(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002808 : HExpression(kExit, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302809 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002810
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002811 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002812
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002813 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002814
Artem Serovcced8ba2017-07-19 18:18:09 +01002815 protected:
2816 DEFAULT_COPY_CONSTRUCTOR(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002817};
2818
2819// Jumps from one block to another.
Vladimir Markobd785672018-05-03 17:09:09 +01002820class HGoto FINAL : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002821 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302822 explicit HGoto(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002823 : HExpression(kGoto, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302824 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002825
Artem Serovcced8ba2017-07-19 18:18:09 +01002826 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002827 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002828
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002829 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002830 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002831 }
2832
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002833 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002834
Artem Serovcced8ba2017-07-19 18:18:09 +01002835 protected:
2836 DEFAULT_COPY_CONSTRUCTOR(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002837};
2838
Roland Levillain9867bc72015-08-05 10:21:34 +01002839class HConstant : public HExpression<0> {
2840 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302841 explicit HConstant(InstructionKind kind, DataType::Type type, uint32_t dex_pc = kNoDexPc)
2842 : HExpression(kind, type, SideEffects::None(), dex_pc) {
2843 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002844
2845 bool CanBeMoved() const OVERRIDE { return true; }
2846
Roland Levillain1a653882016-03-18 18:05:57 +00002847 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002848 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002849 // Is this constant 0 in the arithmetic sense?
2850 virtual bool IsArithmeticZero() const { return false; }
2851 // Is this constant a 0-bit pattern?
2852 virtual bool IsZeroBitPattern() const { return false; }
2853 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002854 virtual bool IsOne() const { return false; }
2855
David Brazdil77a48ae2015-09-15 12:34:04 +00002856 virtual uint64_t GetValueAsUint64() const = 0;
2857
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002858 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002859
Artem Serovcced8ba2017-07-19 18:18:09 +01002860 protected:
2861 DEFAULT_COPY_CONSTRUCTOR(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002862};
2863
Vladimir Markofcb503c2016-05-18 12:48:17 +01002864class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002865 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002866 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002867 return true;
2868 }
2869
David Brazdil77a48ae2015-09-15 12:34:04 +00002870 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2871
Roland Levillain9867bc72015-08-05 10:21:34 +01002872 size_t ComputeHashCode() const OVERRIDE { return 0; }
2873
Roland Levillain1a653882016-03-18 18:05:57 +00002874 // The null constant representation is a 0-bit pattern.
2875 virtual bool IsZeroBitPattern() const { return true; }
2876
Roland Levillain9867bc72015-08-05 10:21:34 +01002877 DECLARE_INSTRUCTION(NullConstant);
2878
Artem Serovcced8ba2017-07-19 18:18:09 +01002879 protected:
2880 DEFAULT_COPY_CONSTRUCTOR(NullConstant);
2881
Roland Levillain9867bc72015-08-05 10:21:34 +01002882 private:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002883 explicit HNullConstant(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302884 : HConstant(kNullConstant, DataType::Type::kReference, dex_pc) {
2885 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002886
2887 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01002888};
2889
2890// Constants of the type int. Those can be from Dex instructions, or
2891// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002892class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002893 public:
2894 int32_t GetValue() const { return value_; }
2895
David Brazdil9f389d42015-10-01 14:32:56 +01002896 uint64_t GetValueAsUint64() const OVERRIDE {
2897 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2898 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002899
Vladimir Marko372f10e2016-05-17 16:30:10 +01002900 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002901 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002902 return other->AsIntConstant()->value_ == value_;
2903 }
2904
2905 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2906
2907 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002908 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2909 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002910 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2911
Roland Levillain1a653882016-03-18 18:05:57 +00002912 // Integer constants are used to encode Boolean values as well,
2913 // where 1 means true and 0 means false.
2914 bool IsTrue() const { return GetValue() == 1; }
2915 bool IsFalse() const { return GetValue() == 0; }
2916
Roland Levillain9867bc72015-08-05 10:21:34 +01002917 DECLARE_INSTRUCTION(IntConstant);
2918
Artem Serovcced8ba2017-07-19 18:18:09 +01002919 protected:
2920 DEFAULT_COPY_CONSTRUCTOR(IntConstant);
2921
Roland Levillain9867bc72015-08-05 10:21:34 +01002922 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002923 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302924 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc), value_(value) {
2925 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002926 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302927 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc),
2928 value_(value ? 1 : 0) {
2929 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002930
2931 const int32_t value_;
2932
2933 friend class HGraph;
2934 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2935 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Roland Levillain9867bc72015-08-05 10:21:34 +01002936};
2937
Vladimir Markofcb503c2016-05-18 12:48:17 +01002938class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002939 public:
2940 int64_t GetValue() const { return value_; }
2941
David Brazdil77a48ae2015-09-15 12:34:04 +00002942 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2943
Vladimir Marko372f10e2016-05-17 16:30:10 +01002944 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002945 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002946 return other->AsLongConstant()->value_ == value_;
2947 }
2948
2949 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2950
2951 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002952 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2953 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002954 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2955
2956 DECLARE_INSTRUCTION(LongConstant);
2957
Artem Serovcced8ba2017-07-19 18:18:09 +01002958 protected:
2959 DEFAULT_COPY_CONSTRUCTOR(LongConstant);
2960
Roland Levillain9867bc72015-08-05 10:21:34 +01002961 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002962 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302963 : HConstant(kLongConstant, DataType::Type::kInt64, dex_pc),
2964 value_(value) {
2965 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002966
2967 const int64_t value_;
2968
2969 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01002970};
Dave Allison20dfc792014-06-16 20:44:29 -07002971
Vladimir Markofcb503c2016-05-18 12:48:17 +01002972class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002973 public:
2974 float GetValue() const { return value_; }
2975
2976 uint64_t GetValueAsUint64() const OVERRIDE {
2977 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2978 }
2979
Vladimir Marko372f10e2016-05-17 16:30:10 +01002980 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002981 DCHECK(other->IsFloatConstant()) << other->DebugName();
2982 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2983 }
2984
2985 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2986
2987 bool IsMinusOne() const OVERRIDE {
2988 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2989 }
Roland Levillain1a653882016-03-18 18:05:57 +00002990 bool IsArithmeticZero() const OVERRIDE {
2991 return std::fpclassify(value_) == FP_ZERO;
2992 }
2993 bool IsArithmeticPositiveZero() const {
2994 return IsArithmeticZero() && !std::signbit(value_);
2995 }
2996 bool IsArithmeticNegativeZero() const {
2997 return IsArithmeticZero() && std::signbit(value_);
2998 }
2999 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003000 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00003001 }
3002 bool IsOne() const OVERRIDE {
3003 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3004 }
3005 bool IsNaN() const {
3006 return std::isnan(value_);
3007 }
3008
3009 DECLARE_INSTRUCTION(FloatConstant);
3010
Artem Serovcced8ba2017-07-19 18:18:09 +01003011 protected:
3012 DEFAULT_COPY_CONSTRUCTOR(FloatConstant);
3013
Roland Levillain31dd3d62016-02-16 12:21:02 +00003014 private:
3015 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303016 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3017 value_(value) {
3018 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003019 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303020 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3021 value_(bit_cast<float, int32_t>(value)) {
3022 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003023
3024 const float value_;
3025
3026 // Only the SsaBuilder and HGraph can create floating-point constants.
3027 friend class SsaBuilder;
3028 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003029};
3030
Vladimir Markofcb503c2016-05-18 12:48:17 +01003031class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003032 public:
3033 double GetValue() const { return value_; }
3034
3035 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
3036
Vladimir Marko372f10e2016-05-17 16:30:10 +01003037 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003038 DCHECK(other->IsDoubleConstant()) << other->DebugName();
3039 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
3040 }
3041
3042 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
3043
3044 bool IsMinusOne() const OVERRIDE {
3045 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
3046 }
Roland Levillain1a653882016-03-18 18:05:57 +00003047 bool IsArithmeticZero() const OVERRIDE {
3048 return std::fpclassify(value_) == FP_ZERO;
3049 }
3050 bool IsArithmeticPositiveZero() const {
3051 return IsArithmeticZero() && !std::signbit(value_);
3052 }
3053 bool IsArithmeticNegativeZero() const {
3054 return IsArithmeticZero() && std::signbit(value_);
3055 }
3056 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003057 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00003058 }
3059 bool IsOne() const OVERRIDE {
3060 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3061 }
3062 bool IsNaN() const {
3063 return std::isnan(value_);
3064 }
3065
3066 DECLARE_INSTRUCTION(DoubleConstant);
3067
Artem Serovcced8ba2017-07-19 18:18:09 +01003068 protected:
3069 DEFAULT_COPY_CONSTRUCTOR(DoubleConstant);
3070
Roland Levillain31dd3d62016-02-16 12:21:02 +00003071 private:
3072 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303073 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3074 value_(value) {
3075 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003076 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303077 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3078 value_(bit_cast<double, int64_t>(value)) {
3079 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003080
3081 const double value_;
3082
3083 // Only the SsaBuilder and HGraph can create floating-point constants.
3084 friend class SsaBuilder;
3085 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003086};
3087
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003088// Conditional branch. A block ending with an HIf instruction must have
3089// two successors.
Vladimir Markobd785672018-05-03 17:09:09 +01003090class HIf FINAL : public HExpression<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003091 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003092 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003093 : HExpression(kIf, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003094 SetRawInputAt(0, input);
3095 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003096
Artem Serovcced8ba2017-07-19 18:18:09 +01003097 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003098 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003099
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003100 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003101 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003102 }
3103
3104 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003105 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003106 }
3107
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003108 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003109
Artem Serovcced8ba2017-07-19 18:18:09 +01003110 protected:
3111 DEFAULT_COPY_CONSTRUCTOR(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003112};
3113
David Brazdilfc6a86a2015-06-26 10:33:45 +00003114
3115// Abstract instruction which marks the beginning and/or end of a try block and
3116// links it to the respective exception handlers. Behaves the same as a Goto in
3117// non-exceptional control flow.
3118// Normal-flow successor is stored at index zero, exception handlers under
3119// higher indices in no particular order.
Vladimir Markobd785672018-05-03 17:09:09 +01003120class HTryBoundary FINAL : public HExpression<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003121 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003122 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01003123 kEntry,
3124 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003125 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01003126 };
3127
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003128 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003129 : HExpression(kTryBoundary, SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003130 SetPackedField<BoundaryKindField>(kind);
3131 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003132
3133 bool IsControlFlow() const OVERRIDE { return true; }
3134
3135 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01003136 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003137
David Brazdild26a4112015-11-10 11:07:31 +00003138 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
3139 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
3140 }
3141
David Brazdilfc6a86a2015-06-26 10:33:45 +00003142 // Returns whether `handler` is among its exception handlers (non-zero index
3143 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01003144 bool HasExceptionHandler(const HBasicBlock& handler) const {
3145 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00003146 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00003147 }
3148
3149 // If not present already, adds `handler` to its block's list of exception
3150 // handlers.
3151 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01003152 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003153 GetBlock()->AddSuccessor(handler);
3154 }
3155 }
3156
Vladimir Markoa1de9182016-02-25 11:37:38 +00003157 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
3158 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003159
David Brazdilffee3d32015-07-06 11:48:53 +01003160 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
3161
David Brazdilfc6a86a2015-06-26 10:33:45 +00003162 DECLARE_INSTRUCTION(TryBoundary);
3163
Artem Serovcced8ba2017-07-19 18:18:09 +01003164 protected:
3165 DEFAULT_COPY_CONSTRUCTOR(TryBoundary);
3166
David Brazdilfc6a86a2015-06-26 10:33:45 +00003167 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003168 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
3169 static constexpr size_t kFieldBoundaryKindSize =
3170 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
3171 static constexpr size_t kNumberOfTryBoundaryPackedBits =
3172 kFieldBoundaryKind + kFieldBoundaryKindSize;
3173 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
3174 "Too many packed fields.");
3175 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00003176};
3177
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003178// Deoptimize to interpreter, upon checking a condition.
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003179class HDeoptimize FINAL : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003180 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003181 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
3182 // across.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003183 HDeoptimize(ArenaAllocator* allocator,
3184 HInstruction* cond,
3185 DeoptimizationKind kind,
3186 uint32_t dex_pc)
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003187 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303188 kDeoptimize,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003189 SideEffects::All(),
3190 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003191 allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003192 /* number_of_inputs */ 1,
3193 kArenaAllocMisc) {
3194 SetPackedFlag<kFieldCanBeMoved>(false);
3195 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003196 SetRawInputAt(0, cond);
3197 }
3198
Artem Serovcced8ba2017-07-19 18:18:09 +01003199 bool IsClonable() const OVERRIDE { return true; }
3200
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003201 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
3202 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
3203 // instead of `guard`.
3204 // We set CanTriggerGC to prevent any intermediate address to be live
3205 // at the point of the `HDeoptimize`.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003206 HDeoptimize(ArenaAllocator* allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003207 HInstruction* cond,
3208 HInstruction* guard,
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003209 DeoptimizationKind kind,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003210 uint32_t dex_pc)
3211 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303212 kDeoptimize,
Vladimir Markobd785672018-05-03 17:09:09 +01003213 guard->GetType(),
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003214 SideEffects::CanTriggerGC(),
3215 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003216 allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003217 /* number_of_inputs */ 2,
3218 kArenaAllocMisc) {
3219 SetPackedFlag<kFieldCanBeMoved>(true);
3220 SetPackedField<DeoptimizeKindField>(kind);
3221 SetRawInputAt(0, cond);
3222 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003223 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003224
3225 bool CanBeMoved() const OVERRIDE { return GetPackedFlag<kFieldCanBeMoved>(); }
3226
3227 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
3228 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
3229 }
3230
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003231 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003232
Aart Bik75ff2c92018-04-21 01:28:11 +00003233 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003234
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003235 DeoptimizationKind GetDeoptimizationKind() const { return GetPackedField<DeoptimizeKindField>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003236
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003237 bool GuardsAnInput() const {
3238 return InputCount() == 2;
3239 }
3240
3241 HInstruction* GuardedInput() const {
3242 DCHECK(GuardsAnInput());
3243 return InputAt(1);
3244 }
3245
3246 void RemoveGuard() {
3247 RemoveInputAt(1);
3248 }
3249
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003250 DECLARE_INSTRUCTION(Deoptimize);
3251
Artem Serovcced8ba2017-07-19 18:18:09 +01003252 protected:
3253 DEFAULT_COPY_CONSTRUCTOR(Deoptimize);
3254
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003255 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003256 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3257 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3258 static constexpr size_t kFieldDeoptimizeKindSize =
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003259 MinimumBitsToStore(static_cast<size_t>(DeoptimizationKind::kLast));
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003260 static constexpr size_t kNumberOfDeoptimizePackedBits =
3261 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3262 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3263 "Too many packed fields.");
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003264 using DeoptimizeKindField =
3265 BitField<DeoptimizationKind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003266};
3267
Mingyao Yang063fc772016-08-02 11:02:54 -07003268// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3269// The compiled code checks this flag value in a guard before devirtualized call and
3270// if it's true, starts to do deoptimization.
3271// It has a 4-byte slot on stack.
3272// TODO: allocate a register for this flag.
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003273class HShouldDeoptimizeFlag FINAL : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003274 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003275 // CHA guards are only optimized in a separate pass and it has no side effects
3276 // with regard to other passes.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003277 HShouldDeoptimizeFlag(ArenaAllocator* allocator, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303278 : HVariableInputSizeInstruction(kShouldDeoptimizeFlag,
Vladimir Markobd785672018-05-03 17:09:09 +01003279 DataType::Type::kInt32,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303280 SideEffects::None(),
3281 dex_pc,
3282 allocator,
3283 0,
3284 kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003285 }
3286
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003287 // We do all CHA guard elimination/motion in a single pass, after which there is no
3288 // further guard elimination/motion since a guard might have been used for justification
3289 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3290 // to avoid other optimizations trying to move it.
Mingyao Yang063fc772016-08-02 11:02:54 -07003291 bool CanBeMoved() const OVERRIDE { return false; }
3292
3293 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3294
Artem Serovcced8ba2017-07-19 18:18:09 +01003295 protected:
3296 DEFAULT_COPY_CONSTRUCTOR(ShouldDeoptimizeFlag);
Mingyao Yang063fc772016-08-02 11:02:54 -07003297};
3298
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003299// Represents the ArtMethod that was passed as a first argument to
3300// the method. It is used by instructions that depend on it, like
3301// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003302class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003303 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003304 explicit HCurrentMethod(DataType::Type type, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303305 : HExpression(kCurrentMethod, type, SideEffects::None(), dex_pc) {
3306 }
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003307
3308 DECLARE_INSTRUCTION(CurrentMethod);
3309
Artem Serovcced8ba2017-07-19 18:18:09 +01003310 protected:
3311 DEFAULT_COPY_CONSTRUCTOR(CurrentMethod);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003312};
3313
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003314// Fetches an ArtMethod from the virtual table or the interface method table
3315// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003316class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003317 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003318 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003319 kVTable,
3320 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003321 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003322 };
3323 HClassTableGet(HInstruction* cls,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003324 DataType::Type type,
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003325 TableKind kind,
3326 size_t index,
3327 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303328 : HExpression(kClassTableGet, type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003329 index_(index) {
3330 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003331 SetRawInputAt(0, cls);
3332 }
3333
Artem Serovcced8ba2017-07-19 18:18:09 +01003334 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003335 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003336 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003337 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003338 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003339 }
3340
Vladimir Markoa1de9182016-02-25 11:37:38 +00003341 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003342 size_t GetIndex() const { return index_; }
3343
3344 DECLARE_INSTRUCTION(ClassTableGet);
3345
Artem Serovcced8ba2017-07-19 18:18:09 +01003346 protected:
3347 DEFAULT_COPY_CONSTRUCTOR(ClassTableGet);
3348
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003349 private:
Vladimir Markobd785672018-05-03 17:09:09 +01003350 static constexpr size_t kFieldTableKind = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003351 static constexpr size_t kFieldTableKindSize =
3352 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3353 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3354 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3355 "Too many packed fields.");
3356 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3357
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003358 // The index of the ArtMethod in the table.
3359 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003360};
3361
Mark Mendellfe57faa2015-09-18 09:26:15 -04003362// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3363// have one successor for each entry in the switch table, and the final successor
3364// will be the block containing the next Dex opcode.
Vladimir Markobd785672018-05-03 17:09:09 +01003365class HPackedSwitch FINAL : public HExpression<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003366 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003367 HPackedSwitch(int32_t start_value,
3368 uint32_t num_entries,
3369 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003370 uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003371 : HExpression(kPackedSwitch, SideEffects::None(), dex_pc),
Mark Mendellfe57faa2015-09-18 09:26:15 -04003372 start_value_(start_value),
3373 num_entries_(num_entries) {
3374 SetRawInputAt(0, input);
3375 }
3376
Artem Serovcced8ba2017-07-19 18:18:09 +01003377 bool IsClonable() const OVERRIDE { return true; }
3378
Mark Mendellfe57faa2015-09-18 09:26:15 -04003379 bool IsControlFlow() const OVERRIDE { return true; }
3380
3381 int32_t GetStartValue() const { return start_value_; }
3382
Vladimir Marko211c2112015-09-24 16:52:33 +01003383 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003384
3385 HBasicBlock* GetDefaultBlock() const {
3386 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003387 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003388 }
3389 DECLARE_INSTRUCTION(PackedSwitch);
3390
Artem Serovcced8ba2017-07-19 18:18:09 +01003391 protected:
3392 DEFAULT_COPY_CONSTRUCTOR(PackedSwitch);
3393
Mark Mendellfe57faa2015-09-18 09:26:15 -04003394 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003395 const int32_t start_value_;
3396 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003397};
3398
Roland Levillain88cb1752014-10-20 16:36:47 +01003399class HUnaryOperation : public HExpression<1> {
3400 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303401 HUnaryOperation(InstructionKind kind,
3402 DataType::Type result_type,
3403 HInstruction* input,
3404 uint32_t dex_pc = kNoDexPc)
3405 : HExpression(kind, result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003406 SetRawInputAt(0, input);
3407 }
3408
Artem Serovcced8ba2017-07-19 18:18:09 +01003409 // All of the UnaryOperation instructions are clonable.
3410 bool IsClonable() const OVERRIDE { return true; }
3411
Roland Levillain88cb1752014-10-20 16:36:47 +01003412 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003413 DataType::Type GetResultType() const { return GetType(); }
Roland Levillain88cb1752014-10-20 16:36:47 +01003414
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003415 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003416 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003417 return true;
3418 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003419
Roland Levillain31dd3d62016-02-16 12:21:02 +00003420 // Try to statically evaluate `this` and return a HConstant
3421 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003422 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003423 HConstant* TryStaticEvaluation() const;
3424
3425 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003426 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3427 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003428 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3429 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003430
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003431 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003432
Artem Serovcced8ba2017-07-19 18:18:09 +01003433 protected:
3434 DEFAULT_COPY_CONSTRUCTOR(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003435};
3436
Dave Allison20dfc792014-06-16 20:44:29 -07003437class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003438 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303439 HBinaryOperation(InstructionKind kind,
3440 DataType::Type result_type,
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003441 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003442 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003443 SideEffects side_effects = SideEffects::None(),
3444 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303445 : HExpression(kind, result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003446 SetRawInputAt(0, left);
3447 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003448 }
3449
Artem Serovcced8ba2017-07-19 18:18:09 +01003450 // All of the BinaryOperation instructions are clonable.
3451 bool IsClonable() const OVERRIDE { return true; }
3452
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003453 HInstruction* GetLeft() const { return InputAt(0); }
3454 HInstruction* GetRight() const { return InputAt(1); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003455 DataType::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003456
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003457 virtual bool IsCommutative() const { return false; }
3458
3459 // Put constant on the right.
3460 // Returns whether order is changed.
3461 bool OrderInputsWithConstantOnTheRight() {
3462 HInstruction* left = InputAt(0);
3463 HInstruction* right = InputAt(1);
3464 if (left->IsConstant() && !right->IsConstant()) {
3465 ReplaceInput(right, 0);
3466 ReplaceInput(left, 1);
3467 return true;
3468 }
3469 return false;
3470 }
3471
3472 // Order inputs by instruction id, but favor constant on the right side.
3473 // This helps GVN for commutative ops.
3474 void OrderInputs() {
3475 DCHECK(IsCommutative());
3476 HInstruction* left = InputAt(0);
3477 HInstruction* right = InputAt(1);
3478 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3479 return;
3480 }
3481 if (OrderInputsWithConstantOnTheRight()) {
3482 return;
3483 }
3484 // Order according to instruction id.
3485 if (left->GetId() > right->GetId()) {
3486 ReplaceInput(right, 0);
3487 ReplaceInput(left, 1);
3488 }
3489 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003490
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003491 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003492 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003493 return true;
3494 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003495
Roland Levillain31dd3d62016-02-16 12:21:02 +00003496 // Try to statically evaluate `this` and return a HConstant
3497 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003498 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003499 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003500
3501 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003502 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3503 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003504 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3505 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003506 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003507 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3508 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003509 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3510 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003511 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3512 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003513 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003514 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3515 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003516
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003517 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003518 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003519 HConstant* GetConstantRight() const;
3520
3521 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003522 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003523 HInstruction* GetLeastConstantLeft() const;
3524
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003525 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003526
Artem Serovcced8ba2017-07-19 18:18:09 +01003527 protected:
3528 DEFAULT_COPY_CONSTRUCTOR(BinaryOperation);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003529};
3530
Mark Mendellc4701932015-04-10 13:18:51 -04003531// The comparison bias applies for floating point operations and indicates how NaN
3532// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003533enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003534 kNoBias, // bias is not applicable (i.e. for long operation)
3535 kGtBias, // return 1 for NaN comparisons
3536 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003537 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003538};
3539
Roland Levillain31dd3d62016-02-16 12:21:02 +00003540std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3541
Dave Allison20dfc792014-06-16 20:44:29 -07003542class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003543 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303544 HCondition(InstructionKind kind,
3545 HInstruction* first,
3546 HInstruction* second,
3547 uint32_t dex_pc = kNoDexPc)
3548 : HBinaryOperation(kind,
3549 DataType::Type::kBool,
3550 first,
3551 second,
3552 SideEffects::None(),
3553 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003554 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3555 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003556
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003557 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003558 // `instruction`, and disregard moves in between.
3559 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003560
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003561 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003562
3563 virtual IfCondition GetCondition() const = 0;
3564
Mark Mendellc4701932015-04-10 13:18:51 -04003565 virtual IfCondition GetOppositeCondition() const = 0;
3566
Vladimir Markoa1de9182016-02-25 11:37:38 +00003567 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003568 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3569
Vladimir Markoa1de9182016-02-25 11:37:38 +00003570 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3571 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003572
Vladimir Marko372f10e2016-05-17 16:30:10 +01003573 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003574 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003575 }
3576
Roland Levillain4fa13f62015-07-06 18:11:54 +01003577 bool IsFPConditionTrueIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003578 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003579 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003580 if (if_cond == kCondNE) {
3581 return true;
3582 } else if (if_cond == kCondEQ) {
3583 return false;
3584 }
3585 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003586 }
3587
3588 bool IsFPConditionFalseIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003589 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003590 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003591 if (if_cond == kCondEQ) {
3592 return true;
3593 } else if (if_cond == kCondNE) {
3594 return false;
3595 }
3596 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003597 }
3598
Roland Levillain31dd3d62016-02-16 12:21:02 +00003599 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003600 // Needed if we merge a HCompare into a HCondition.
Vladimir Markobd785672018-05-03 17:09:09 +01003601 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003602 static constexpr size_t kFieldComparisonBiasSize =
3603 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3604 static constexpr size_t kNumberOfConditionPackedBits =
3605 kFieldComparisonBias + kFieldComparisonBiasSize;
3606 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3607 using ComparisonBiasField =
3608 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3609
Roland Levillain31dd3d62016-02-16 12:21:02 +00003610 template <typename T>
3611 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3612
3613 template <typename T>
3614 int32_t CompareFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003615 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003616 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3617 // Handle the bias.
3618 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3619 }
3620
3621 // Return an integer constant containing the result of a condition evaluated at compile time.
3622 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3623 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3624 }
3625
Artem Serovcced8ba2017-07-19 18:18:09 +01003626 DEFAULT_COPY_CONSTRUCTOR(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003627};
3628
3629// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003630class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003631 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003632 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303633 : HCondition(kEqual, first, second, dex_pc) {
3634 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003635
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003636 bool IsCommutative() const OVERRIDE { return true; }
3637
Vladimir Marko9e23df52015-11-10 17:14:35 +00003638 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3639 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003640 return MakeConstantCondition(true, GetDexPc());
3641 }
3642 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3643 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3644 }
3645 // In the following Evaluate methods, a HCompare instruction has
3646 // been merged into this HEqual instruction; evaluate it as
3647 // `Compare(x, y) == 0`.
3648 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3649 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3650 GetDexPc());
3651 }
3652 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3653 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3654 }
3655 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3656 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003657 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003658
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003659 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003660
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003661 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003662 return kCondEQ;
3663 }
3664
Mark Mendellc4701932015-04-10 13:18:51 -04003665 IfCondition GetOppositeCondition() const OVERRIDE {
3666 return kCondNE;
3667 }
3668
Artem Serovcced8ba2017-07-19 18:18:09 +01003669 protected:
3670 DEFAULT_COPY_CONSTRUCTOR(Equal);
3671
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003672 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003673 template <typename T> static bool Compute(T x, T y) { return x == y; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003674};
3675
Vladimir Markofcb503c2016-05-18 12:48:17 +01003676class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003677 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303678 HNotEqual(HInstruction* first, HInstruction* second,
3679 uint32_t dex_pc = kNoDexPc)
3680 : HCondition(kNotEqual, first, second, dex_pc) {
3681 }
Dave Allison20dfc792014-06-16 20:44:29 -07003682
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003683 bool IsCommutative() const OVERRIDE { return true; }
3684
Vladimir Marko9e23df52015-11-10 17:14:35 +00003685 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3686 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003687 return MakeConstantCondition(false, GetDexPc());
3688 }
3689 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3690 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3691 }
3692 // In the following Evaluate methods, a HCompare instruction has
3693 // been merged into this HNotEqual instruction; evaluate it as
3694 // `Compare(x, y) != 0`.
3695 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3696 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3697 }
3698 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3699 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3700 }
3701 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3702 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003703 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003704
Dave Allison20dfc792014-06-16 20:44:29 -07003705 DECLARE_INSTRUCTION(NotEqual);
3706
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003707 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003708 return kCondNE;
3709 }
3710
Mark Mendellc4701932015-04-10 13:18:51 -04003711 IfCondition GetOppositeCondition() const OVERRIDE {
3712 return kCondEQ;
3713 }
3714
Artem Serovcced8ba2017-07-19 18:18:09 +01003715 protected:
3716 DEFAULT_COPY_CONSTRUCTOR(NotEqual);
3717
Dave Allison20dfc792014-06-16 20:44:29 -07003718 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003719 template <typename T> static bool Compute(T x, T y) { return x != y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003720};
3721
Vladimir Markofcb503c2016-05-18 12:48:17 +01003722class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003723 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303724 HLessThan(HInstruction* first, HInstruction* second,
3725 uint32_t dex_pc = kNoDexPc)
3726 : HCondition(kLessThan, first, second, dex_pc) {
3727 }
Dave Allison20dfc792014-06-16 20:44:29 -07003728
Roland Levillain9867bc72015-08-05 10:21:34 +01003729 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003730 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003731 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003732 // In the following Evaluate methods, a HCompare instruction has
3733 // been merged into this HLessThan instruction; evaluate it as
3734 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003735 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003736 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3737 }
3738 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3739 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3740 }
3741 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3742 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003743 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003744
Dave Allison20dfc792014-06-16 20:44:29 -07003745 DECLARE_INSTRUCTION(LessThan);
3746
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003747 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003748 return kCondLT;
3749 }
3750
Mark Mendellc4701932015-04-10 13:18:51 -04003751 IfCondition GetOppositeCondition() const OVERRIDE {
3752 return kCondGE;
3753 }
3754
Artem Serovcced8ba2017-07-19 18:18:09 +01003755 protected:
3756 DEFAULT_COPY_CONSTRUCTOR(LessThan);
3757
Dave Allison20dfc792014-06-16 20:44:29 -07003758 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003759 template <typename T> static bool Compute(T x, T y) { return x < y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003760};
3761
Vladimir Markofcb503c2016-05-18 12:48:17 +01003762class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003763 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303764 HLessThanOrEqual(HInstruction* first, HInstruction* second,
3765 uint32_t dex_pc = kNoDexPc)
3766 : HCondition(kLessThanOrEqual, first, second, dex_pc) {
3767 }
Dave Allison20dfc792014-06-16 20:44:29 -07003768
Roland Levillain9867bc72015-08-05 10:21:34 +01003769 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003770 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003771 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003772 // In the following Evaluate methods, a HCompare instruction has
3773 // been merged into this HLessThanOrEqual instruction; evaluate it as
3774 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003775 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003776 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3777 }
3778 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3779 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3780 }
3781 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3782 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003783 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003784
Dave Allison20dfc792014-06-16 20:44:29 -07003785 DECLARE_INSTRUCTION(LessThanOrEqual);
3786
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003787 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003788 return kCondLE;
3789 }
3790
Mark Mendellc4701932015-04-10 13:18:51 -04003791 IfCondition GetOppositeCondition() const OVERRIDE {
3792 return kCondGT;
3793 }
3794
Artem Serovcced8ba2017-07-19 18:18:09 +01003795 protected:
3796 DEFAULT_COPY_CONSTRUCTOR(LessThanOrEqual);
3797
Dave Allison20dfc792014-06-16 20:44:29 -07003798 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003799 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003800};
3801
Vladimir Markofcb503c2016-05-18 12:48:17 +01003802class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003803 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003804 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303805 : HCondition(kGreaterThan, first, second, dex_pc) {
3806 }
Dave Allison20dfc792014-06-16 20:44:29 -07003807
Roland Levillain9867bc72015-08-05 10:21:34 +01003808 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003809 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003810 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003811 // In the following Evaluate methods, a HCompare instruction has
3812 // been merged into this HGreaterThan instruction; evaluate it as
3813 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003814 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003815 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3816 }
3817 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3818 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3819 }
3820 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3821 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003822 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003823
Dave Allison20dfc792014-06-16 20:44:29 -07003824 DECLARE_INSTRUCTION(GreaterThan);
3825
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003826 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003827 return kCondGT;
3828 }
3829
Mark Mendellc4701932015-04-10 13:18:51 -04003830 IfCondition GetOppositeCondition() const OVERRIDE {
3831 return kCondLE;
3832 }
3833
Artem Serovcced8ba2017-07-19 18:18:09 +01003834 protected:
3835 DEFAULT_COPY_CONSTRUCTOR(GreaterThan);
3836
Dave Allison20dfc792014-06-16 20:44:29 -07003837 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003838 template <typename T> static bool Compute(T x, T y) { return x > y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003839};
3840
Vladimir Markofcb503c2016-05-18 12:48:17 +01003841class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003842 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003843 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303844 : HCondition(kGreaterThanOrEqual, first, second, dex_pc) {
3845 }
Dave Allison20dfc792014-06-16 20:44:29 -07003846
Roland Levillain9867bc72015-08-05 10:21:34 +01003847 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003848 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003849 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003850 // In the following Evaluate methods, a HCompare instruction has
3851 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3852 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003853 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003854 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3855 }
3856 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3857 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3858 }
3859 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3860 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003861 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003862
Dave Allison20dfc792014-06-16 20:44:29 -07003863 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3864
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003865 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003866 return kCondGE;
3867 }
3868
Mark Mendellc4701932015-04-10 13:18:51 -04003869 IfCondition GetOppositeCondition() const OVERRIDE {
3870 return kCondLT;
3871 }
3872
Artem Serovcced8ba2017-07-19 18:18:09 +01003873 protected:
3874 DEFAULT_COPY_CONSTRUCTOR(GreaterThanOrEqual);
3875
Dave Allison20dfc792014-06-16 20:44:29 -07003876 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003877 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003878};
3879
Vladimir Markofcb503c2016-05-18 12:48:17 +01003880class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003881 public:
3882 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303883 : HCondition(kBelow, first, second, dex_pc) {
3884 }
Aart Bike9f37602015-10-09 11:15:55 -07003885
3886 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003887 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003888 }
3889 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003890 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3891 }
3892 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3893 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3894 LOG(FATAL) << DebugName() << " is not defined for float values";
3895 UNREACHABLE();
3896 }
3897 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3898 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3899 LOG(FATAL) << DebugName() << " is not defined for double values";
3900 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003901 }
3902
3903 DECLARE_INSTRUCTION(Below);
3904
3905 IfCondition GetCondition() const OVERRIDE {
3906 return kCondB;
3907 }
3908
3909 IfCondition GetOppositeCondition() const OVERRIDE {
3910 return kCondAE;
3911 }
3912
Artem Serovcced8ba2017-07-19 18:18:09 +01003913 protected:
3914 DEFAULT_COPY_CONSTRUCTOR(Below);
3915
Aart Bike9f37602015-10-09 11:15:55 -07003916 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003917 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003918 return MakeUnsigned(x) < MakeUnsigned(y);
3919 }
Aart Bike9f37602015-10-09 11:15:55 -07003920};
3921
Vladimir Markofcb503c2016-05-18 12:48:17 +01003922class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003923 public:
3924 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303925 : HCondition(kBelowOrEqual, first, second, dex_pc) {
3926 }
Aart Bike9f37602015-10-09 11:15:55 -07003927
3928 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003929 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003930 }
3931 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003932 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3933 }
3934 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3935 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3936 LOG(FATAL) << DebugName() << " is not defined for float values";
3937 UNREACHABLE();
3938 }
3939 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3940 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3941 LOG(FATAL) << DebugName() << " is not defined for double values";
3942 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003943 }
3944
3945 DECLARE_INSTRUCTION(BelowOrEqual);
3946
3947 IfCondition GetCondition() const OVERRIDE {
3948 return kCondBE;
3949 }
3950
3951 IfCondition GetOppositeCondition() const OVERRIDE {
3952 return kCondA;
3953 }
3954
Artem Serovcced8ba2017-07-19 18:18:09 +01003955 protected:
3956 DEFAULT_COPY_CONSTRUCTOR(BelowOrEqual);
3957
Aart Bike9f37602015-10-09 11:15:55 -07003958 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003959 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003960 return MakeUnsigned(x) <= MakeUnsigned(y);
3961 }
Aart Bike9f37602015-10-09 11:15:55 -07003962};
3963
Vladimir Markofcb503c2016-05-18 12:48:17 +01003964class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003965 public:
3966 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303967 : HCondition(kAbove, first, second, dex_pc) {
3968 }
Aart Bike9f37602015-10-09 11:15:55 -07003969
3970 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003971 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003972 }
3973 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003974 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3975 }
3976 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3977 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3978 LOG(FATAL) << DebugName() << " is not defined for float values";
3979 UNREACHABLE();
3980 }
3981 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3982 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3983 LOG(FATAL) << DebugName() << " is not defined for double values";
3984 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003985 }
3986
3987 DECLARE_INSTRUCTION(Above);
3988
3989 IfCondition GetCondition() const OVERRIDE {
3990 return kCondA;
3991 }
3992
3993 IfCondition GetOppositeCondition() const OVERRIDE {
3994 return kCondBE;
3995 }
3996
Artem Serovcced8ba2017-07-19 18:18:09 +01003997 protected:
3998 DEFAULT_COPY_CONSTRUCTOR(Above);
3999
Aart Bike9f37602015-10-09 11:15:55 -07004000 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004001 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004002 return MakeUnsigned(x) > MakeUnsigned(y);
4003 }
Aart Bike9f37602015-10-09 11:15:55 -07004004};
4005
Vladimir Markofcb503c2016-05-18 12:48:17 +01004006class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004007 public:
4008 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304009 : HCondition(kAboveOrEqual, first, second, dex_pc) {
4010 }
Aart Bike9f37602015-10-09 11:15:55 -07004011
4012 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004013 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004014 }
4015 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004016 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4017 }
4018 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4019 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4020 LOG(FATAL) << DebugName() << " is not defined for float values";
4021 UNREACHABLE();
4022 }
4023 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4024 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4025 LOG(FATAL) << DebugName() << " is not defined for double values";
4026 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004027 }
4028
4029 DECLARE_INSTRUCTION(AboveOrEqual);
4030
4031 IfCondition GetCondition() const OVERRIDE {
4032 return kCondAE;
4033 }
4034
4035 IfCondition GetOppositeCondition() const OVERRIDE {
4036 return kCondB;
4037 }
4038
Artem Serovcced8ba2017-07-19 18:18:09 +01004039 protected:
4040 DEFAULT_COPY_CONSTRUCTOR(AboveOrEqual);
4041
Aart Bike9f37602015-10-09 11:15:55 -07004042 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004043 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004044 return MakeUnsigned(x) >= MakeUnsigned(y);
4045 }
Aart Bike9f37602015-10-09 11:15:55 -07004046};
Dave Allison20dfc792014-06-16 20:44:29 -07004047
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004048// Instruction to check how two inputs compare to each other.
4049// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01004050class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004051 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00004052 // Note that `comparison_type` is the type of comparison performed
4053 // between the comparison's inputs, not the type of the instantiated
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004054 // HCompare instruction (which is always DataType::Type::kInt).
4055 HCompare(DataType::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004056 HInstruction* first,
4057 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04004058 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004059 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304060 : HBinaryOperation(kCompare,
4061 DataType::Type::kInt32,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004062 first,
4063 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00004064 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004065 dex_pc) {
4066 SetPackedField<ComparisonBiasField>(bias);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004067 DCHECK_EQ(comparison_type, DataType::Kind(first->GetType()));
4068 DCHECK_EQ(comparison_type, DataType::Kind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004069 }
4070
Roland Levillain9867bc72015-08-05 10:21:34 +01004071 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004072 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
4073
4074 template <typename T>
4075 int32_t ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004076 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004077 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
4078 // Handle the bias.
4079 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
4080 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004081
Roland Levillain9867bc72015-08-05 10:21:34 +01004082 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004083 // Note that there is no "cmp-int" Dex instruction so we shouldn't
4084 // reach this code path when processing a freshly built HIR
4085 // graph. However HCompare integer instructions can be synthesized
4086 // by the instruction simplifier to implement IntegerCompare and
4087 // IntegerSignum intrinsics, so we have to handle this case.
4088 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004089 }
4090 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004091 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4092 }
4093 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4094 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4095 }
4096 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4097 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01004098 }
4099
Vladimir Marko372f10e2016-05-17 16:30:10 +01004100 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004101 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00004102 }
4103
Vladimir Markoa1de9182016-02-25 11:37:38 +00004104 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04004105
Roland Levillain31dd3d62016-02-16 12:21:02 +00004106 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00004107 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00004108 bool IsGtBias() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004109 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004110 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00004111 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004112
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004113 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type type ATTRIBUTE_UNUSED) {
Roland Levillain1693a1f2016-03-15 14:57:31 +00004114 // Comparisons do not require a runtime call in any back end.
4115 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004116 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004117
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004118 DECLARE_INSTRUCTION(Compare);
4119
Roland Levillain31dd3d62016-02-16 12:21:02 +00004120 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01004121 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004122 static constexpr size_t kFieldComparisonBiasSize =
4123 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
4124 static constexpr size_t kNumberOfComparePackedBits =
4125 kFieldComparisonBias + kFieldComparisonBiasSize;
4126 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4127 using ComparisonBiasField =
4128 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
4129
Roland Levillain31dd3d62016-02-16 12:21:02 +00004130 // Return an integer constant containing the result of a comparison evaluated at compile time.
4131 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
4132 DCHECK(value == -1 || value == 0 || value == 1) << value;
4133 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
4134 }
4135
Artem Serovcced8ba2017-07-19 18:18:09 +01004136 DEFAULT_COPY_CONSTRUCTOR(Compare);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004137};
4138
Nicolas Geoffray2b615ba2017-01-06 14:40:07 +00004139class HNewInstance FINAL : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00004140 public:
4141 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00004142 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004143 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00004144 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00004145 bool finalizable,
4146 QuickEntrypointEnum entrypoint)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304147 : HExpression(kNewInstance,
4148 DataType::Type::kReference,
4149 SideEffects::CanTriggerGC(),
4150 dex_pc),
David Brazdil6de19382016-01-08 17:37:10 +00004151 type_index_(type_index),
4152 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00004153 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004154 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00004155 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00004156 }
4157
Artem Serovcced8ba2017-07-19 18:18:09 +01004158 bool IsClonable() const OVERRIDE { return true; }
4159
Andreas Gampea5b09a62016-11-17 15:21:22 -08004160 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00004161 const DexFile& GetDexFile() const { return dex_file_; }
4162
4163 // Calls runtime so needs an environment.
4164 bool NeedsEnvironment() const OVERRIDE { return true; }
4165
Mingyao Yang062157f2016-03-02 10:15:36 -08004166 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
4167 bool CanThrow() const OVERRIDE { return true; }
4168
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004169 bool NeedsChecks() const {
4170 return entrypoint_ == kQuickAllocObjectWithChecks;
4171 }
David Brazdil6de19382016-01-08 17:37:10 +00004172
Vladimir Markoa1de9182016-02-25 11:37:38 +00004173 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00004174
4175 bool CanBeNull() const OVERRIDE { return false; }
4176
4177 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4178
4179 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
4180 entrypoint_ = entrypoint;
4181 }
4182
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004183 HLoadClass* GetLoadClass() const {
4184 HInstruction* input = InputAt(0);
4185 if (input->IsClinitCheck()) {
4186 input = input->InputAt(0);
4187 }
4188 DCHECK(input->IsLoadClass());
4189 return input->AsLoadClass();
4190 }
4191
David Brazdil6de19382016-01-08 17:37:10 +00004192 bool IsStringAlloc() const;
4193
4194 DECLARE_INSTRUCTION(NewInstance);
4195
Artem Serovcced8ba2017-07-19 18:18:09 +01004196 protected:
4197 DEFAULT_COPY_CONSTRUCTOR(NewInstance);
4198
David Brazdil6de19382016-01-08 17:37:10 +00004199 private:
Vladimir Markobd785672018-05-03 17:09:09 +01004200 static constexpr size_t kFlagFinalizable = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004201 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
4202 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
4203 "Too many packed fields.");
4204
Andreas Gampea5b09a62016-11-17 15:21:22 -08004205 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00004206 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00004207 QuickEntrypointEnum entrypoint_;
David Brazdil6de19382016-01-08 17:37:10 +00004208};
4209
Agi Csaki05f20562015-08-19 14:58:14 -07004210enum IntrinsicNeedsEnvironmentOrCache {
4211 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
4212 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07004213};
4214
Aart Bik5d75afe2015-12-14 11:57:01 -08004215enum IntrinsicSideEffects {
4216 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
4217 kReadSideEffects, // Intrinsic may read heap memory.
4218 kWriteSideEffects, // Intrinsic may write heap memory.
4219 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
4220};
4221
4222enum IntrinsicExceptions {
4223 kNoThrow, // Intrinsic does not throw any exceptions.
4224 kCanThrow // Intrinsic may throw exceptions.
4225};
4226
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004227class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004228 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004229 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004230
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01004231 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004232 SetRawInputAt(index, argument);
4233 }
4234
Roland Levillain3e3d7332015-04-28 11:00:54 +01004235 // Return the number of arguments. This number can be lower than
4236 // the number of inputs returned by InputCount(), as some invoke
4237 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
4238 // inputs at the end of their list of inputs.
4239 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
4240
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004241 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4242
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004243 InvokeType GetInvokeType() const {
4244 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004245 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004246
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01004247 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004248 return intrinsic_;
4249 }
4250
Aart Bik5d75afe2015-12-14 11:57:01 -08004251 void SetIntrinsic(Intrinsics intrinsic,
4252 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
4253 IntrinsicSideEffects side_effects,
4254 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004255
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004256 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00004257 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004258 }
4259
Aart Bikff7d89c2016-11-07 08:49:28 -08004260 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4261
Vladimir Markoa1de9182016-02-25 11:37:38 +00004262 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004263
Aart Bika8b8e9b2018-01-09 11:01:02 -08004264 void SetAlwaysThrows(bool always_throws) { SetPackedFlag<kFlagAlwaysThrows>(always_throws); }
4265
4266 bool AlwaysThrows() const OVERRIDE { return GetPackedFlag<kFlagAlwaysThrows>(); }
4267
Aart Bik71bf7b42016-11-16 10:17:46 -08004268 bool CanBeMoved() const OVERRIDE { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004269
Vladimir Marko372f10e2016-05-17 16:30:10 +01004270 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08004271 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4272 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004273
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004274 uint32_t* GetIntrinsicOptimizations() {
4275 return &intrinsic_optimizations_;
4276 }
4277
4278 const uint32_t* GetIntrinsicOptimizations() const {
4279 return &intrinsic_optimizations_;
4280 }
4281
4282 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4283
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004284 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004285 void SetResolvedMethod(ArtMethod* method) { resolved_method_ = method; }
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004286
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004287 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004288
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004289 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004290 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4291 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004292 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
Vladimir Markobd785672018-05-03 17:09:09 +01004293 static constexpr size_t kFlagCanThrow = kFieldInvokeType + kFieldInvokeTypeSize;
Aart Bika8b8e9b2018-01-09 11:01:02 -08004294 static constexpr size_t kFlagAlwaysThrows = kFlagCanThrow + 1;
4295 static constexpr size_t kNumberOfInvokePackedBits = kFlagAlwaysThrows + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004296 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004297 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004298
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304299 HInvoke(InstructionKind kind,
4300 ArenaAllocator* allocator,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004301 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004302 uint32_t number_of_other_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004303 DataType::Type return_type,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004304 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004305 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004306 ArtMethod* resolved_method,
4307 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004308 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304309 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01004310 return_type,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004311 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4312 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004313 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004314 number_of_arguments + number_of_other_inputs,
4315 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004316 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004317 resolved_method_(resolved_method),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004318 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07004319 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004320 intrinsic_optimizations_(0) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004321 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004322 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004323 }
4324
Artem Serovcced8ba2017-07-19 18:18:09 +01004325 DEFAULT_COPY_CONSTRUCTOR(Invoke);
4326
Roland Levillain3e3d7332015-04-28 11:00:54 +01004327 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004328 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004329 const uint32_t dex_method_index_;
4330 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004331
4332 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4333 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004334};
4335
Vladimir Markofcb503c2016-05-18 12:48:17 +01004336class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004337 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004338 HInvokeUnresolved(ArenaAllocator* allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004339 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004340 DataType::Type return_type,
Calin Juravle175dc732015-08-25 15:42:32 +01004341 uint32_t dex_pc,
4342 uint32_t dex_method_index,
4343 InvokeType invoke_type)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304344 : HInvoke(kInvokeUnresolved,
4345 allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004346 number_of_arguments,
4347 0u /* number_of_other_inputs */,
4348 return_type,
4349 dex_pc,
4350 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004351 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01004352 invoke_type) {
4353 }
4354
Artem Serovcced8ba2017-07-19 18:18:09 +01004355 bool IsClonable() const OVERRIDE { return true; }
4356
Calin Juravle175dc732015-08-25 15:42:32 +01004357 DECLARE_INSTRUCTION(InvokeUnresolved);
4358
Artem Serovcced8ba2017-07-19 18:18:09 +01004359 protected:
4360 DEFAULT_COPY_CONSTRUCTOR(InvokeUnresolved);
Calin Juravle175dc732015-08-25 15:42:32 +01004361};
4362
Orion Hodsonac141392017-01-13 11:53:47 +00004363class HInvokePolymorphic FINAL : public HInvoke {
4364 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004365 HInvokePolymorphic(ArenaAllocator* allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004366 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004367 DataType::Type return_type,
Orion Hodsonac141392017-01-13 11:53:47 +00004368 uint32_t dex_pc,
4369 uint32_t dex_method_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304370 : HInvoke(kInvokePolymorphic,
4371 allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004372 number_of_arguments,
4373 0u /* number_of_other_inputs */,
4374 return_type,
4375 dex_pc,
4376 dex_method_index,
4377 nullptr,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304378 kVirtual) {
4379 }
Orion Hodsonac141392017-01-13 11:53:47 +00004380
Artem Serovcced8ba2017-07-19 18:18:09 +01004381 bool IsClonable() const OVERRIDE { return true; }
4382
Orion Hodsonac141392017-01-13 11:53:47 +00004383 DECLARE_INSTRUCTION(InvokePolymorphic);
4384
Artem Serovcced8ba2017-07-19 18:18:09 +01004385 protected:
4386 DEFAULT_COPY_CONSTRUCTOR(InvokePolymorphic);
Orion Hodsonac141392017-01-13 11:53:47 +00004387};
4388
Vladimir Markofcb503c2016-05-18 12:48:17 +01004389class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004390 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004391 // Requirements of this method call regarding the class
4392 // initialization (clinit) check of its declaring class.
4393 enum class ClinitCheckRequirement {
4394 kNone, // Class already initialized.
4395 kExplicit, // Static call having explicit clinit check as last input.
4396 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004397 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004398 };
4399
Vladimir Marko58155012015-08-19 12:49:41 +00004400 // Determines how to load the target ArtMethod*.
4401 enum class MethodLoadKind {
4402 // Use a String init ArtMethod* loaded from Thread entrypoints.
4403 kStringInit,
4404
4405 // Use the method's own ArtMethod* loaded by the register allocator.
4406 kRecursive,
4407
Vladimir Marko65979462017-05-19 17:25:12 +01004408 // Use PC-relative boot image ArtMethod* address that will be known at link time.
4409 // Used for boot image methods referenced by boot image code.
4410 kBootImageLinkTimePcRelative,
4411
Vladimir Marko58155012015-08-19 12:49:41 +00004412 // Use ArtMethod* at a known address, embed the direct address in the code.
4413 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
4414 kDirectAddress,
4415
Vladimir Markob066d432018-01-03 13:14:37 +00004416 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
4417 // Used for app->boot calls with relocatable image.
4418 kBootImageRelRo,
4419
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004420 // Load from an entry in the .bss section using a PC-relative load.
4421 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
4422 kBssEntry,
Vladimir Marko58155012015-08-19 12:49:41 +00004423
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004424 // Make a runtime call to resolve and call the method. This is the last-resort-kind
4425 // used when other kinds are unimplemented on a particular architecture.
4426 kRuntimeCall,
Vladimir Marko58155012015-08-19 12:49:41 +00004427 };
4428
4429 // Determines the location of the code pointer.
4430 enum class CodePtrLocation {
4431 // Recursive call, use local PC-relative call instruction.
4432 kCallSelf,
4433
Vladimir Marko58155012015-08-19 12:49:41 +00004434 // Use code pointer from the ArtMethod*.
4435 // Used when we don't know the target code. This is also the last-resort-kind used when
4436 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4437 kCallArtMethod,
4438 };
4439
4440 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004441 MethodLoadKind method_load_kind;
4442 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004443 // The method load data holds
4444 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4445 // Note that there are multiple string init methods, each having its own offset.
4446 // - the method address for kDirectAddress
Vladimir Markodc151b22015-10-15 18:02:30 +01004447 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004448 };
4449
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004450 HInvokeStaticOrDirect(ArenaAllocator* allocator,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004451 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004452 DataType::Type return_type,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004453 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004454 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004455 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004456 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004457 InvokeType invoke_type,
4458 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004459 ClinitCheckRequirement clinit_check_requirement)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304460 : HInvoke(kInvokeStaticOrDirect,
4461 allocator,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004462 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004463 // There is potentially one extra argument for the HCurrentMethod node, and
4464 // potentially one other if the clinit check is explicit, and potentially
4465 // one other if the method is a string factory.
4466 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004467 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004468 return_type,
4469 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004470 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004471 resolved_method,
4472 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004473 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004474 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004475 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4476 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004477
Artem Serovcced8ba2017-07-19 18:18:09 +01004478 bool IsClonable() const OVERRIDE { return true; }
4479
Vladimir Markodc151b22015-10-15 18:02:30 +01004480 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004481 bool had_current_method_input = HasCurrentMethodInput();
4482 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4483
4484 // Using the current method is the default and once we find a better
4485 // method load kind, we should not go back to using the current method.
4486 DCHECK(had_current_method_input || !needs_current_method_input);
4487
4488 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004489 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4490 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004491 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004492 dispatch_info_ = dispatch_info;
4493 }
4494
Aart Bik6daebeb2017-04-03 14:35:41 -07004495 DispatchInfo GetDispatchInfo() const {
4496 return dispatch_info_;
4497 }
4498
Vladimir Markoc53c0792015-11-19 15:48:33 +00004499 void AddSpecialInput(HInstruction* input) {
4500 // We allow only one special input.
4501 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4502 DCHECK(InputCount() == GetSpecialInputIndex() ||
4503 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4504 InsertInputAt(GetSpecialInputIndex(), input);
4505 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004506
Vladimir Marko372f10e2016-05-17 16:30:10 +01004507 using HInstruction::GetInputRecords; // Keep the const version visible.
4508 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
4509 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4510 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4511 DCHECK(!input_records.empty());
4512 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4513 HInstruction* last_input = input_records.back().GetInstruction();
4514 // Note: `last_input` may be null during arguments setup.
4515 if (last_input != nullptr) {
4516 // `last_input` is the last input of a static invoke marked as having
4517 // an explicit clinit check. It must either be:
4518 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4519 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4520 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4521 }
4522 }
4523 return input_records;
4524 }
4525
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004526 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004527 // We access the method via the dex cache so we can't do an implicit null check.
4528 // TODO: for intrinsics we can generate implicit null checks.
4529 return false;
4530 }
4531
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004532 bool CanBeNull() const OVERRIDE {
Vladimir Markobd785672018-05-03 17:09:09 +01004533 return GetType() == DataType::Type::kReference && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004534 }
4535
Vladimir Markoc53c0792015-11-19 15:48:33 +00004536 // Get the index of the special input, if any.
4537 //
David Brazdil6de19382016-01-08 17:37:10 +00004538 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4539 // method pointer; otherwise there may be one platform-specific special input,
4540 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004541 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004542 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004543
Vladimir Marko58155012015-08-19 12:49:41 +00004544 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4545 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4546 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004547 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004548 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004549 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko65979462017-05-19 17:25:12 +01004550 bool HasPcRelativeMethodLoadKind() const {
4551 return GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markob066d432018-01-03 13:14:37 +00004552 GetMethodLoadKind() == MethodLoadKind::kBootImageRelRo ||
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004553 GetMethodLoadKind() == MethodLoadKind::kBssEntry;
Vladimir Marko65979462017-05-19 17:25:12 +01004554 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004555 bool HasCurrentMethodInput() const {
4556 // This function can be called only after the invoke has been fully initialized by the builder.
4557 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004558 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004559 return true;
4560 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004561 DCHECK(InputCount() == GetSpecialInputIndex() ||
4562 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004563 return false;
4564 }
4565 }
Vladimir Marko58155012015-08-19 12:49:41 +00004566
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004567 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004568 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004569 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004570 }
4571
4572 uint64_t GetMethodAddress() const {
4573 DCHECK(HasMethodAddress());
4574 return dispatch_info_.method_load_data;
4575 }
4576
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004577 const DexFile& GetDexFileForPcRelativeDexCache() const;
4578
Vladimir Markoa1de9182016-02-25 11:37:38 +00004579 ClinitCheckRequirement GetClinitCheckRequirement() const {
4580 return GetPackedField<ClinitCheckRequirementField>();
4581 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004582
Roland Levillain4c0eb422015-04-24 16:43:49 +01004583 // Is this instruction a call to a static method?
4584 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004585 return GetInvokeType() == kStatic;
4586 }
4587
4588 MethodReference GetTargetMethod() const {
4589 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004590 }
4591
Vladimir Markofbb184a2015-11-13 14:47:00 +00004592 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4593 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4594 // instruction; only relevant for static calls with explicit clinit check.
4595 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004596 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004597 size_t last_input_index = inputs_.size() - 1u;
4598 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004599 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004600 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004601 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004602 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004603 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004604 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004605 }
4606
4607 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004608 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004609 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004610 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004611 }
4612
4613 // Is this a call to a static method whose declaring class has an
4614 // implicit intialization check requirement?
4615 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004616 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004617 }
4618
Vladimir Markob554b5a2015-11-06 12:57:55 +00004619 // Does this method load kind need the current method as an input?
4620 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004621 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kRuntimeCall;
Vladimir Markob554b5a2015-11-06 12:57:55 +00004622 }
4623
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004624 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004625
Artem Serovcced8ba2017-07-19 18:18:09 +01004626 protected:
4627 DEFAULT_COPY_CONSTRUCTOR(InvokeStaticOrDirect);
4628
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004629 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004630 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004631 static constexpr size_t kFieldClinitCheckRequirementSize =
4632 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4633 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4634 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4635 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4636 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004637 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4638 kFieldClinitCheckRequirement,
4639 kFieldClinitCheckRequirementSize>;
4640
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004641 // Cached values of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffrayea179f42018-02-08 22:30:18 +00004642 const MethodReference target_method_;
Vladimir Marko58155012015-08-19 12:49:41 +00004643 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004644};
Vladimir Markof64242a2015-12-01 14:58:23 +00004645std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004646std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004647
Vladimir Markofcb503c2016-05-18 12:48:17 +01004648class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004649 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004650 HInvokeVirtual(ArenaAllocator* allocator,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004651 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004652 DataType::Type return_type,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004653 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004654 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004655 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004656 uint32_t vtable_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304657 : HInvoke(kInvokeVirtual,
4658 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004659 number_of_arguments,
4660 0u,
4661 return_type,
4662 dex_pc,
4663 dex_method_index,
4664 resolved_method,
4665 kVirtual),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304666 vtable_index_(vtable_index) {
4667 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004668
Artem Serovcced8ba2017-07-19 18:18:09 +01004669 bool IsClonable() const OVERRIDE { return true; }
4670
Aart Bik71bf7b42016-11-16 10:17:46 -08004671 bool CanBeNull() const OVERRIDE {
4672 switch (GetIntrinsic()) {
4673 case Intrinsics::kThreadCurrentThread:
4674 case Intrinsics::kStringBufferAppend:
4675 case Intrinsics::kStringBufferToString:
4676 case Intrinsics::kStringBuilderAppend:
4677 case Intrinsics::kStringBuilderToString:
4678 return false;
4679 default:
4680 return HInvoke::CanBeNull();
4681 }
4682 }
4683
Calin Juravle641547a2015-04-21 22:08:51 +01004684 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004685 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004686 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004687 }
4688
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004689 uint32_t GetVTableIndex() const { return vtable_index_; }
4690
4691 DECLARE_INSTRUCTION(InvokeVirtual);
4692
Artem Serovcced8ba2017-07-19 18:18:09 +01004693 protected:
4694 DEFAULT_COPY_CONSTRUCTOR(InvokeVirtual);
4695
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004696 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004697 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004698 const uint32_t vtable_index_;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004699};
4700
Vladimir Markofcb503c2016-05-18 12:48:17 +01004701class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004702 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004703 HInvokeInterface(ArenaAllocator* allocator,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004704 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004705 DataType::Type return_type,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004706 uint32_t dex_pc,
4707 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004708 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004709 uint32_t imt_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304710 : HInvoke(kInvokeInterface,
4711 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004712 number_of_arguments,
4713 0u,
4714 return_type,
4715 dex_pc,
4716 dex_method_index,
4717 resolved_method,
4718 kInterface),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304719 imt_index_(imt_index) {
4720 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004721
Artem Serovcced8ba2017-07-19 18:18:09 +01004722 bool IsClonable() const OVERRIDE { return true; }
4723
Calin Juravle641547a2015-04-21 22:08:51 +01004724 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004725 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004726 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004727 }
4728
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004729 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
4730 // The assembly stub currently needs it.
4731 return true;
4732 }
4733
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004734 uint32_t GetImtIndex() const { return imt_index_; }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004735
4736 DECLARE_INSTRUCTION(InvokeInterface);
4737
Artem Serovcced8ba2017-07-19 18:18:09 +01004738 protected:
4739 DEFAULT_COPY_CONSTRUCTOR(InvokeInterface);
4740
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004741 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004742 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004743 const uint32_t imt_index_;
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004744};
4745
Vladimir Markofcb503c2016-05-18 12:48:17 +01004746class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004747 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004748 HNeg(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304749 : HUnaryOperation(kNeg, result_type, input, dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004750 DCHECK_EQ(result_type, DataType::Kind(input->GetType()));
Roland Levillain937e6cd2016-03-22 11:54:37 +00004751 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004752
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004753 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004754
4755 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004756 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004757 }
4758 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004759 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004760 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004761 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4762 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4763 }
4764 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4765 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4766 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004767
Roland Levillain88cb1752014-10-20 16:36:47 +01004768 DECLARE_INSTRUCTION(Neg);
4769
Artem Serovcced8ba2017-07-19 18:18:09 +01004770 protected:
4771 DEFAULT_COPY_CONSTRUCTOR(Neg);
Roland Levillain88cb1752014-10-20 16:36:47 +01004772};
4773
Vladimir Markofcb503c2016-05-18 12:48:17 +01004774class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004775 public:
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004776 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304777 : HExpression(kNewArray, DataType::Type::kReference, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004778 SetRawInputAt(0, cls);
4779 SetRawInputAt(1, length);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004780 }
4781
Artem Serovcced8ba2017-07-19 18:18:09 +01004782 bool IsClonable() const OVERRIDE { return true; }
4783
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004784 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004785 bool NeedsEnvironment() const OVERRIDE { return true; }
4786
Mingyao Yang0c365e62015-03-31 15:09:29 -07004787 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4788 bool CanThrow() const OVERRIDE { return true; }
4789
Calin Juravle10e244f2015-01-26 18:54:32 +00004790 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004791
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004792 HLoadClass* GetLoadClass() const {
4793 DCHECK(InputAt(0)->IsLoadClass());
4794 return InputAt(0)->AsLoadClass();
4795 }
4796
4797 HInstruction* GetLength() const {
4798 return InputAt(1);
4799 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004800
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004801 DECLARE_INSTRUCTION(NewArray);
4802
Artem Serovcced8ba2017-07-19 18:18:09 +01004803 protected:
4804 DEFAULT_COPY_CONSTRUCTOR(NewArray);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004805};
4806
Vladimir Markofcb503c2016-05-18 12:48:17 +01004807class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004808 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004809 HAdd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004810 HInstruction* left,
4811 HInstruction* right,
4812 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304813 : HBinaryOperation(kAdd, result_type, left, right, SideEffects::None(), dex_pc) {
4814 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004815
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004816 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004817
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004818 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004819
4820 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004821 return GetBlock()->GetGraph()->GetIntConstant(
4822 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004823 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004824 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004825 return GetBlock()->GetGraph()->GetLongConstant(
4826 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004827 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004828 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4829 return GetBlock()->GetGraph()->GetFloatConstant(
4830 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4831 }
4832 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4833 return GetBlock()->GetGraph()->GetDoubleConstant(
4834 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4835 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004836
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004837 DECLARE_INSTRUCTION(Add);
4838
Artem Serovcced8ba2017-07-19 18:18:09 +01004839 protected:
4840 DEFAULT_COPY_CONSTRUCTOR(Add);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004841};
4842
Vladimir Markofcb503c2016-05-18 12:48:17 +01004843class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004844 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004845 HSub(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004846 HInstruction* left,
4847 HInstruction* right,
4848 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304849 : HBinaryOperation(kSub, result_type, left, right, SideEffects::None(), dex_pc) {
4850 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004851
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004852 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004853
4854 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004855 return GetBlock()->GetGraph()->GetIntConstant(
4856 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004857 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004858 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004859 return GetBlock()->GetGraph()->GetLongConstant(
4860 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004861 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004862 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4863 return GetBlock()->GetGraph()->GetFloatConstant(
4864 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4865 }
4866 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4867 return GetBlock()->GetGraph()->GetDoubleConstant(
4868 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4869 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004870
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004871 DECLARE_INSTRUCTION(Sub);
4872
Artem Serovcced8ba2017-07-19 18:18:09 +01004873 protected:
4874 DEFAULT_COPY_CONSTRUCTOR(Sub);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004875};
4876
Vladimir Markofcb503c2016-05-18 12:48:17 +01004877class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004878 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004879 HMul(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004880 HInstruction* left,
4881 HInstruction* right,
4882 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304883 : HBinaryOperation(kMul, result_type, left, right, SideEffects::None(), dex_pc) {
4884 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004885
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004886 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004887
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004888 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004889
4890 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004891 return GetBlock()->GetGraph()->GetIntConstant(
4892 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004893 }
4894 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004895 return GetBlock()->GetGraph()->GetLongConstant(
4896 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004897 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004898 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4899 return GetBlock()->GetGraph()->GetFloatConstant(
4900 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4901 }
4902 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4903 return GetBlock()->GetGraph()->GetDoubleConstant(
4904 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4905 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004906
4907 DECLARE_INSTRUCTION(Mul);
4908
Artem Serovcced8ba2017-07-19 18:18:09 +01004909 protected:
4910 DEFAULT_COPY_CONSTRUCTOR(Mul);
Calin Juravle34bacdf2014-10-07 20:23:36 +01004911};
4912
Vladimir Markofcb503c2016-05-18 12:48:17 +01004913class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004914 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004915 HDiv(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004916 HInstruction* left,
4917 HInstruction* right,
4918 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304919 : HBinaryOperation(kDiv, result_type, left, right, SideEffects::None(), dex_pc) {
4920 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004921
Roland Levillain9867bc72015-08-05 10:21:34 +01004922 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004923 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004924 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004925 // Our graph structure ensures we never have 0 for `y` during
4926 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004927 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004928 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004929 return (y == -1) ? -x : x / y;
4930 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004931
Roland Levillain31dd3d62016-02-16 12:21:02 +00004932 template <typename T>
4933 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004934 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004935 return x / y;
4936 }
4937
Roland Levillain9867bc72015-08-05 10:21:34 +01004938 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004939 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004940 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004941 }
4942 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004943 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004944 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4945 }
4946 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4947 return GetBlock()->GetGraph()->GetFloatConstant(
4948 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4949 }
4950 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4951 return GetBlock()->GetGraph()->GetDoubleConstant(
4952 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004953 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004954
4955 DECLARE_INSTRUCTION(Div);
4956
Artem Serovcced8ba2017-07-19 18:18:09 +01004957 protected:
4958 DEFAULT_COPY_CONSTRUCTOR(Div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00004959};
4960
Vladimir Markofcb503c2016-05-18 12:48:17 +01004961class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004962 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004963 HRem(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004964 HInstruction* left,
4965 HInstruction* right,
4966 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304967 : HBinaryOperation(kRem, result_type, left, right, SideEffects::None(), dex_pc) {
4968 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004969
Roland Levillain9867bc72015-08-05 10:21:34 +01004970 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004971 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004972 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004973 // Our graph structure ensures we never have 0 for `y` during
4974 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004975 DCHECK_NE(y, 0);
4976 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4977 return (y == -1) ? 0 : x % y;
4978 }
4979
Roland Levillain31dd3d62016-02-16 12:21:02 +00004980 template <typename T>
4981 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004982 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004983 return std::fmod(x, y);
4984 }
4985
Roland Levillain9867bc72015-08-05 10:21:34 +01004986 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004987 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004988 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004989 }
4990 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004991 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004992 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004993 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004994 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4995 return GetBlock()->GetGraph()->GetFloatConstant(
4996 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4997 }
4998 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4999 return GetBlock()->GetGraph()->GetDoubleConstant(
5000 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5001 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005002
5003 DECLARE_INSTRUCTION(Rem);
5004
Artem Serovcced8ba2017-07-19 18:18:09 +01005005 protected:
5006 DEFAULT_COPY_CONSTRUCTOR(Rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00005007};
5008
Aart Bik1f8d51b2018-02-15 10:42:37 -08005009class HMin FINAL : public HBinaryOperation {
5010 public:
5011 HMin(DataType::Type result_type,
5012 HInstruction* left,
5013 HInstruction* right,
5014 uint32_t dex_pc)
5015 : HBinaryOperation(kMin, result_type, left, right, SideEffects::None(), dex_pc) {}
5016
5017 bool IsCommutative() const OVERRIDE { return true; }
5018
5019 // Evaluation for integral values.
5020 template <typename T> static T ComputeIntegral(T x, T y) {
5021 return (x <= y) ? x : y;
5022 }
5023
5024 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
5025 return GetBlock()->GetGraph()->GetIntConstant(
5026 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5027 }
5028 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
5029 return GetBlock()->GetGraph()->GetLongConstant(
5030 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5031 }
5032 // TODO: Evaluation for floating-point values.
5033 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5034 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { return nullptr; }
5035 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5036 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { return nullptr; }
5037
5038 DECLARE_INSTRUCTION(Min);
5039
5040 protected:
5041 DEFAULT_COPY_CONSTRUCTOR(Min);
5042};
5043
5044class HMax FINAL : public HBinaryOperation {
5045 public:
5046 HMax(DataType::Type result_type,
5047 HInstruction* left,
5048 HInstruction* right,
5049 uint32_t dex_pc)
5050 : HBinaryOperation(kMax, result_type, left, right, SideEffects::None(), dex_pc) {}
5051
5052 bool IsCommutative() const OVERRIDE { return true; }
5053
5054 // Evaluation for integral values.
5055 template <typename T> static T ComputeIntegral(T x, T y) {
5056 return (x >= y) ? x : y;
5057 }
5058
5059 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
5060 return GetBlock()->GetGraph()->GetIntConstant(
5061 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5062 }
5063 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
5064 return GetBlock()->GetGraph()->GetLongConstant(
5065 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5066 }
5067 // TODO: Evaluation for floating-point values.
5068 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5069 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { return nullptr; }
5070 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5071 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { return nullptr; }
5072
5073 DECLARE_INSTRUCTION(Max);
5074
5075 protected:
5076 DEFAULT_COPY_CONSTRUCTOR(Max);
5077};
5078
Aart Bik3dad3412018-02-28 12:01:46 -08005079class HAbs FINAL : public HUnaryOperation {
5080 public:
5081 HAbs(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
5082 : HUnaryOperation(kAbs, result_type, input, dex_pc) {}
5083
5084 // Evaluation for integral values.
5085 template <typename T> static T ComputeIntegral(T x) {
5086 return x < 0 ? -x : x;
5087 }
5088
5089 // Evaluation for floating-point values.
5090 // Note, as a "quality of implementation", rather than pure "spec compliance",
5091 // we require that Math.abs() clears the sign bit (but changes nothing else)
5092 // for all floating-point numbers, including NaN (signaling NaN may become quiet though).
5093 // http://b/30758343
5094 template <typename T, typename S> static T ComputeFP(T x) {
5095 S bits = bit_cast<S, T>(x);
5096 return bit_cast<T, S>(bits & std::numeric_limits<S>::max());
5097 }
5098
5099 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
5100 return GetBlock()->GetGraph()->GetIntConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5101 }
5102 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
5103 return GetBlock()->GetGraph()->GetLongConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5104 }
5105 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
5106 return GetBlock()->GetGraph()->GetFloatConstant(
5107 ComputeFP<float, int32_t>(x->GetValue()), GetDexPc());
5108 }
5109 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
5110 return GetBlock()->GetGraph()->GetDoubleConstant(
5111 ComputeFP<double, int64_t>(x->GetValue()), GetDexPc());
5112 }
5113
5114 DECLARE_INSTRUCTION(Abs);
5115
5116 protected:
5117 DEFAULT_COPY_CONSTRUCTOR(Abs);
5118};
5119
Vladimir Markofcb503c2016-05-18 12:48:17 +01005120class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00005121 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00005122 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
5123 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00005124 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305125 : HExpression(kDivZeroCheck, value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00005126 SetRawInputAt(0, value);
5127 }
5128
5129 bool CanBeMoved() const OVERRIDE { return true; }
5130
Vladimir Marko372f10e2016-05-17 16:30:10 +01005131 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00005132 return true;
5133 }
5134
5135 bool NeedsEnvironment() const OVERRIDE { return true; }
5136 bool CanThrow() const OVERRIDE { return true; }
5137
Calin Juravled0d48522014-11-04 16:40:20 +00005138 DECLARE_INSTRUCTION(DivZeroCheck);
5139
Artem Serovcced8ba2017-07-19 18:18:09 +01005140 protected:
5141 DEFAULT_COPY_CONSTRUCTOR(DivZeroCheck);
Calin Juravled0d48522014-11-04 16:40:20 +00005142};
5143
Vladimir Markofcb503c2016-05-18 12:48:17 +01005144class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005145 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005146 HShl(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005147 HInstruction* value,
5148 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005149 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305150 : HBinaryOperation(kShl, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005151 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5152 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005153 }
5154
Roland Levillain5b5b9312016-03-22 14:57:31 +00005155 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005156 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005157 return value << (distance & max_shift_distance);
5158 }
5159
5160 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005161 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005162 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005163 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005164 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005165 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005166 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005167 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005168 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5169 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5170 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5171 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005172 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005173 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5174 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005175 LOG(FATAL) << DebugName() << " is not defined for float values";
5176 UNREACHABLE();
5177 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005178 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5179 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005180 LOG(FATAL) << DebugName() << " is not defined for double values";
5181 UNREACHABLE();
5182 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005183
5184 DECLARE_INSTRUCTION(Shl);
5185
Artem Serovcced8ba2017-07-19 18:18:09 +01005186 protected:
5187 DEFAULT_COPY_CONSTRUCTOR(Shl);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005188};
5189
Vladimir Markofcb503c2016-05-18 12:48:17 +01005190class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005191 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005192 HShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005193 HInstruction* value,
5194 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005195 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305196 : HBinaryOperation(kShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005197 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5198 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005199 }
5200
Roland Levillain5b5b9312016-03-22 14:57:31 +00005201 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005202 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005203 return value >> (distance & max_shift_distance);
5204 }
5205
5206 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005207 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005208 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005209 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005210 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005211 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005212 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005213 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005214 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5215 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5216 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5217 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005218 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005219 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5220 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005221 LOG(FATAL) << DebugName() << " is not defined for float values";
5222 UNREACHABLE();
5223 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005224 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5225 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005226 LOG(FATAL) << DebugName() << " is not defined for double values";
5227 UNREACHABLE();
5228 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005229
5230 DECLARE_INSTRUCTION(Shr);
5231
Artem Serovcced8ba2017-07-19 18:18:09 +01005232 protected:
5233 DEFAULT_COPY_CONSTRUCTOR(Shr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005234};
5235
Vladimir Markofcb503c2016-05-18 12:48:17 +01005236class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005237 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005238 HUShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005239 HInstruction* value,
5240 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005241 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305242 : HBinaryOperation(kUShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005243 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5244 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00005245 }
5246
Roland Levillain5b5b9312016-03-22 14:57:31 +00005247 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005248 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005249 typedef typename std::make_unsigned<T>::type V;
5250 V ux = static_cast<V>(value);
5251 return static_cast<T>(ux >> (distance & max_shift_distance));
5252 }
5253
5254 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005255 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005256 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005257 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005258 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005259 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005260 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005261 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005262 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5263 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5264 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5265 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00005266 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005267 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5268 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005269 LOG(FATAL) << DebugName() << " is not defined for float values";
5270 UNREACHABLE();
5271 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005272 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5273 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005274 LOG(FATAL) << DebugName() << " is not defined for double values";
5275 UNREACHABLE();
5276 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005277
5278 DECLARE_INSTRUCTION(UShr);
5279
Artem Serovcced8ba2017-07-19 18:18:09 +01005280 protected:
5281 DEFAULT_COPY_CONSTRUCTOR(UShr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005282};
5283
Vladimir Markofcb503c2016-05-18 12:48:17 +01005284class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005285 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005286 HAnd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005287 HInstruction* left,
5288 HInstruction* right,
5289 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305290 : HBinaryOperation(kAnd, result_type, left, right, SideEffects::None(), dex_pc) {
5291 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005292
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005293 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005294
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005295 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005296
5297 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005298 return GetBlock()->GetGraph()->GetIntConstant(
5299 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005300 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005301 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005302 return GetBlock()->GetGraph()->GetLongConstant(
5303 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005304 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005305 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5306 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5307 LOG(FATAL) << DebugName() << " is not defined for float values";
5308 UNREACHABLE();
5309 }
5310 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5311 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5312 LOG(FATAL) << DebugName() << " is not defined for double values";
5313 UNREACHABLE();
5314 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005315
5316 DECLARE_INSTRUCTION(And);
5317
Artem Serovcced8ba2017-07-19 18:18:09 +01005318 protected:
5319 DEFAULT_COPY_CONSTRUCTOR(And);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005320};
5321
Vladimir Markofcb503c2016-05-18 12:48:17 +01005322class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005323 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005324 HOr(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005325 HInstruction* left,
5326 HInstruction* right,
5327 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305328 : HBinaryOperation(kOr, result_type, left, right, SideEffects::None(), dex_pc) {
5329 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005330
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005331 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005332
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005333 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005334
5335 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005336 return GetBlock()->GetGraph()->GetIntConstant(
5337 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005338 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005339 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005340 return GetBlock()->GetGraph()->GetLongConstant(
5341 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005342 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005343 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5344 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5345 LOG(FATAL) << DebugName() << " is not defined for float values";
5346 UNREACHABLE();
5347 }
5348 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5349 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5350 LOG(FATAL) << DebugName() << " is not defined for double values";
5351 UNREACHABLE();
5352 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005353
5354 DECLARE_INSTRUCTION(Or);
5355
Artem Serovcced8ba2017-07-19 18:18:09 +01005356 protected:
5357 DEFAULT_COPY_CONSTRUCTOR(Or);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005358};
5359
Vladimir Markofcb503c2016-05-18 12:48:17 +01005360class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005361 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005362 HXor(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005363 HInstruction* left,
5364 HInstruction* right,
5365 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305366 : HBinaryOperation(kXor, result_type, left, right, SideEffects::None(), dex_pc) {
5367 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005368
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005369 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005370
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005371 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005372
5373 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005374 return GetBlock()->GetGraph()->GetIntConstant(
5375 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005376 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005377 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005378 return GetBlock()->GetGraph()->GetLongConstant(
5379 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005380 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005381 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5382 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5383 LOG(FATAL) << DebugName() << " is not defined for float values";
5384 UNREACHABLE();
5385 }
5386 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5387 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5388 LOG(FATAL) << DebugName() << " is not defined for double values";
5389 UNREACHABLE();
5390 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005391
5392 DECLARE_INSTRUCTION(Xor);
5393
Artem Serovcced8ba2017-07-19 18:18:09 +01005394 protected:
5395 DEFAULT_COPY_CONSTRUCTOR(Xor);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005396};
5397
Vladimir Markofcb503c2016-05-18 12:48:17 +01005398class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005399 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005400 HRor(DataType::Type result_type, HInstruction* value, HInstruction* distance)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305401 : HBinaryOperation(kRor, result_type, value, distance) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005402 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5403 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00005404 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005405
Roland Levillain5b5b9312016-03-22 14:57:31 +00005406 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005407 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005408 typedef typename std::make_unsigned<T>::type V;
5409 V ux = static_cast<V>(value);
5410 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005411 return static_cast<T>(ux);
5412 } else {
5413 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005414 return static_cast<T>(ux >> (distance & max_shift_value)) |
5415 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005416 }
5417 }
5418
Roland Levillain5b5b9312016-03-22 14:57:31 +00005419 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005420 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005421 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005422 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005423 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005424 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005425 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005426 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005427 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5428 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5429 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5430 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005431 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005432 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5433 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005434 LOG(FATAL) << DebugName() << " is not defined for float values";
5435 UNREACHABLE();
5436 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005437 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5438 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005439 LOG(FATAL) << DebugName() << " is not defined for double values";
5440 UNREACHABLE();
5441 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005442
5443 DECLARE_INSTRUCTION(Ror);
5444
Artem Serovcced8ba2017-07-19 18:18:09 +01005445 protected:
5446 DEFAULT_COPY_CONSTRUCTOR(Ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005447};
5448
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005449// The value of a parameter in this method. Its location depends on
5450// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01005451class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005452 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005453 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005454 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005455 uint8_t index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005456 DataType::Type parameter_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005457 bool is_this = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305458 : HExpression(kParameterValue, parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005459 dex_file_(dex_file),
5460 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005461 index_(index) {
5462 SetPackedFlag<kFlagIsThis>(is_this);
5463 SetPackedFlag<kFlagCanBeNull>(!is_this);
5464 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005465
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005466 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005467 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005468 uint8_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07005469 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005470
Vladimir Markoa1de9182016-02-25 11:37:38 +00005471 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
5472 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005473
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005474 DECLARE_INSTRUCTION(ParameterValue);
5475
Artem Serovcced8ba2017-07-19 18:18:09 +01005476 protected:
5477 DEFAULT_COPY_CONSTRUCTOR(ParameterValue);
5478
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005479 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005480 // Whether or not the parameter value corresponds to 'this' argument.
Vladimir Markobd785672018-05-03 17:09:09 +01005481 static constexpr size_t kFlagIsThis = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005482 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5483 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5484 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5485 "Too many packed fields.");
5486
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005487 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005488 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005489 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005490 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005491 const uint8_t index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005492};
5493
Vladimir Markofcb503c2016-05-18 12:48:17 +01005494class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005495 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005496 HNot(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305497 : HUnaryOperation(kNot, result_type, input, dex_pc) {
5498 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005499
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005500 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005501 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005502 return true;
5503 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005504
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005505 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005506
5507 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005508 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005509 }
5510 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005511 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005512 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005513 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5514 LOG(FATAL) << DebugName() << " is not defined for float values";
5515 UNREACHABLE();
5516 }
5517 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5518 LOG(FATAL) << DebugName() << " is not defined for double values";
5519 UNREACHABLE();
5520 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005521
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005522 DECLARE_INSTRUCTION(Not);
5523
Artem Serovcced8ba2017-07-19 18:18:09 +01005524 protected:
5525 DEFAULT_COPY_CONSTRUCTOR(Not);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005526};
5527
Vladimir Markofcb503c2016-05-18 12:48:17 +01005528class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005529 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005530 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305531 : HUnaryOperation(kBooleanNot, DataType::Type::kBool, input, dex_pc) {
5532 }
David Brazdil66d126e2015-04-03 16:02:44 +01005533
5534 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005535 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01005536 return true;
5537 }
5538
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005539 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005540 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005541 return !x;
5542 }
5543
Roland Levillain9867bc72015-08-05 10:21:34 +01005544 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005545 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005546 }
5547 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5548 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005549 UNREACHABLE();
5550 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005551 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5552 LOG(FATAL) << DebugName() << " is not defined for float values";
5553 UNREACHABLE();
5554 }
5555 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5556 LOG(FATAL) << DebugName() << " is not defined for double values";
5557 UNREACHABLE();
5558 }
David Brazdil66d126e2015-04-03 16:02:44 +01005559
5560 DECLARE_INSTRUCTION(BooleanNot);
5561
Artem Serovcced8ba2017-07-19 18:18:09 +01005562 protected:
5563 DEFAULT_COPY_CONSTRUCTOR(BooleanNot);
David Brazdil66d126e2015-04-03 16:02:44 +01005564};
5565
Vladimir Markofcb503c2016-05-18 12:48:17 +01005566class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005567 public:
5568 // Instantiate a type conversion of `input` to `result_type`.
Vladimir Markoc8fb2112017-10-03 11:37:52 +01005569 HTypeConversion(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305570 : HExpression(kTypeConversion, result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005571 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005572 // Invariant: We should never generate a conversion to a Boolean value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005573 DCHECK_NE(DataType::Type::kBool, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005574 }
5575
5576 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005577 DataType::Type GetInputType() const { return GetInput()->GetType(); }
5578 DataType::Type GetResultType() const { return GetType(); }
Roland Levillaindff1f282014-11-05 14:15:05 +00005579
5580 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005581 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5582 return true;
5583 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005584
Mark Mendelle82549b2015-05-06 10:55:34 -04005585 // Try to statically evaluate the conversion and return a HConstant
5586 // containing the result. If the input cannot be converted, return nullptr.
5587 HConstant* TryStaticEvaluation() const;
5588
Roland Levillaindff1f282014-11-05 14:15:05 +00005589 DECLARE_INSTRUCTION(TypeConversion);
5590
Artem Serovcced8ba2017-07-19 18:18:09 +01005591 protected:
5592 DEFAULT_COPY_CONSTRUCTOR(TypeConversion);
Roland Levillaindff1f282014-11-05 14:15:05 +00005593};
5594
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005595static constexpr uint32_t kNoRegNumber = -1;
5596
Vladimir Markofcb503c2016-05-18 12:48:17 +01005597class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005598 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005599 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
5600 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005601 HNullCheck(HInstruction* value, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305602 : HExpression(kNullCheck, value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005603 SetRawInputAt(0, value);
5604 }
5605
Artem Serovcced8ba2017-07-19 18:18:09 +01005606 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00005607 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005608 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005609 return true;
5610 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005611
Calin Juravle10e244f2015-01-26 18:54:32 +00005612 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005613
Calin Juravle10e244f2015-01-26 18:54:32 +00005614 bool CanThrow() const OVERRIDE { return true; }
5615
5616 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005617
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005618 DECLARE_INSTRUCTION(NullCheck);
5619
Artem Serovcced8ba2017-07-19 18:18:09 +01005620 protected:
5621 DEFAULT_COPY_CONSTRUCTOR(NullCheck);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005622};
5623
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005624// Embeds an ArtField and all the information required by the compiler. We cache
5625// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005626class FieldInfo : public ValueObject {
5627 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005628 FieldInfo(ArtField* field,
5629 MemberOffset field_offset,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005630 DataType::Type field_type,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005631 bool is_volatile,
5632 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005633 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005634 const DexFile& dex_file)
5635 : field_(field),
5636 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005637 field_type_(field_type),
5638 is_volatile_(is_volatile),
5639 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005640 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005641 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005642
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005643 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005644 MemberOffset GetFieldOffset() const { return field_offset_; }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005645 DataType::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005646 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005647 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005648 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005649 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005650
5651 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005652 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005653 const MemberOffset field_offset_;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005654 const DataType::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005655 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005656 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005657 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005658 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005659};
5660
Vladimir Markofcb503c2016-05-18 12:48:17 +01005661class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005662 public:
5663 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005664 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005665 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005666 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005667 bool is_volatile,
5668 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005669 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005670 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005671 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305672 : HExpression(kInstanceFieldGet,
5673 field_type,
5674 SideEffects::FieldReadOfType(field_type, is_volatile),
5675 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005676 field_info_(field,
5677 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005678 field_type,
5679 is_volatile,
5680 field_idx,
5681 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005682 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005683 SetRawInputAt(0, value);
5684 }
5685
Artem Serovcced8ba2017-07-19 18:18:09 +01005686 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005687 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005688
Vladimir Marko372f10e2016-05-17 16:30:10 +01005689 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5690 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005691 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005692 }
5693
Calin Juravle641547a2015-04-21 22:08:51 +01005694 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005695 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005696 }
5697
5698 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005699 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5700 }
5701
Calin Juravle52c48962014-12-16 17:02:57 +00005702 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005703 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005704 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005705 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005706
Vladimir Marko61b92282017-10-11 13:23:17 +01005707 void SetType(DataType::Type new_type) {
5708 DCHECK(DataType::IsIntegralType(GetType()));
5709 DCHECK(DataType::IsIntegralType(new_type));
5710 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5711 SetPackedField<TypeField>(new_type);
5712 }
5713
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005714 DECLARE_INSTRUCTION(InstanceFieldGet);
5715
Artem Serovcced8ba2017-07-19 18:18:09 +01005716 protected:
5717 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldGet);
5718
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005719 private:
5720 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005721};
5722
Vladimir Markobd785672018-05-03 17:09:09 +01005723class HInstanceFieldSet FINAL : public HExpression<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005724 public:
5725 HInstanceFieldSet(HInstruction* object,
5726 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005727 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005728 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005729 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005730 bool is_volatile,
5731 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005732 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005733 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005734 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01005735 : HExpression(kInstanceFieldSet,
5736 SideEffects::FieldWriteOfType(field_type, is_volatile),
5737 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005738 field_info_(field,
5739 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005740 field_type,
5741 is_volatile,
5742 field_idx,
5743 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005744 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005745 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005746 SetRawInputAt(0, object);
5747 SetRawInputAt(1, value);
5748 }
5749
Artem Serovcced8ba2017-07-19 18:18:09 +01005750 bool IsClonable() const OVERRIDE { return true; }
5751
Calin Juravle641547a2015-04-21 22:08:51 +01005752 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005753 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005754 }
5755
Calin Juravle52c48962014-12-16 17:02:57 +00005756 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005757 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005758 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005759 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005760 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005761 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5762 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005763
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005764 DECLARE_INSTRUCTION(InstanceFieldSet);
5765
Artem Serovcced8ba2017-07-19 18:18:09 +01005766 protected:
5767 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldSet);
5768
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005769 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005770 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5771 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5772 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5773 "Too many packed fields.");
5774
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005775 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005776};
5777
Vladimir Markofcb503c2016-05-18 12:48:17 +01005778class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005779 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005780 HArrayGet(HInstruction* array,
5781 HInstruction* index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005782 DataType::Type type,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005783 uint32_t dex_pc)
5784 : HArrayGet(array,
5785 index,
5786 type,
5787 SideEffects::ArrayReadOfType(type),
5788 dex_pc,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305789 /* is_string_char_at */ false) {
5790 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005791
5792 HArrayGet(HInstruction* array,
5793 HInstruction* index,
5794 DataType::Type type,
5795 SideEffects side_effects,
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005796 uint32_t dex_pc,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005797 bool is_string_char_at)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305798 : HExpression(kArrayGet, type, side_effects, dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005799 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005800 SetRawInputAt(0, array);
5801 SetRawInputAt(1, index);
5802 }
5803
Artem Serovcced8ba2017-07-19 18:18:09 +01005804 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005805 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005806 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005807 return true;
5808 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005809 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005810 // TODO: We can be smarter here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01005811 // Currently, unless the array is the result of NewArray, the array access is always
5812 // preceded by some form of null NullCheck necessary for the bounds check, usually
5813 // implicit null check on the ArrayLength input to BoundsCheck or Deoptimize for
5814 // dynamic BCE. There are cases when these could be removed to produce better code.
5815 // If we ever add optimizations to do so we should allow an implicit check here
5816 // (as long as the address falls in the first page).
5817 //
5818 // As an example of such fancy optimization, we could eliminate BoundsCheck for
5819 // a = cond ? new int[1] : null;
5820 // a[0]; // The Phi does not need bounds check for either input.
Calin Juravle77520bc2015-01-12 18:45:46 +00005821 return false;
5822 }
5823
David Brazdil4833f5a2015-12-16 10:37:39 +00005824 bool IsEquivalentOf(HArrayGet* other) const {
5825 bool result = (GetDexPc() == other->GetDexPc());
5826 if (kIsDebugBuild && result) {
5827 DCHECK_EQ(GetBlock(), other->GetBlock());
5828 DCHECK_EQ(GetArray(), other->GetArray());
5829 DCHECK_EQ(GetIndex(), other->GetIndex());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005830 if (DataType::IsIntOrLongType(GetType())) {
5831 DCHECK(DataType::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005832 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005833 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
5834 DCHECK(DataType::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005835 }
5836 }
5837 return result;
5838 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005839
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005840 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5841
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005842 HInstruction* GetArray() const { return InputAt(0); }
5843 HInstruction* GetIndex() const { return InputAt(1); }
5844
Vladimir Marko61b92282017-10-11 13:23:17 +01005845 void SetType(DataType::Type new_type) {
5846 DCHECK(DataType::IsIntegralType(GetType()));
5847 DCHECK(DataType::IsIntegralType(new_type));
5848 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5849 SetPackedField<TypeField>(new_type);
5850 }
5851
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005852 DECLARE_INSTRUCTION(ArrayGet);
5853
Artem Serovcced8ba2017-07-19 18:18:09 +01005854 protected:
5855 DEFAULT_COPY_CONSTRUCTOR(ArrayGet);
5856
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005857 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005858 // We treat a String as an array, creating the HArrayGet from String.charAt()
5859 // intrinsic in the instruction simplifier. We can always determine whether
5860 // a particular HArrayGet is actually a String.charAt() by looking at the type
5861 // of the input but that requires holding the mutator lock, so we prefer to use
5862 // a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01005863 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005864 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5865 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5866 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005867};
5868
Vladimir Markobd785672018-05-03 17:09:09 +01005869class HArraySet FINAL : public HExpression<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005870 public:
5871 HArraySet(HInstruction* array,
5872 HInstruction* index,
5873 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005874 DataType::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005875 uint32_t dex_pc)
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005876 : HArraySet(array,
5877 index,
5878 value,
5879 expected_component_type,
5880 // Make a best guess for side effects now, may be refined during SSA building.
5881 ComputeSideEffects(GetComponentType(value->GetType(), expected_component_type)),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305882 dex_pc) {
5883 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005884
5885 HArraySet(HInstruction* array,
5886 HInstruction* index,
5887 HInstruction* value,
5888 DataType::Type expected_component_type,
5889 SideEffects side_effects,
5890 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01005891 : HExpression(kArraySet, side_effects, dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005892 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005893 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005894 SetPackedFlag<kFlagValueCanBeNull>(true);
5895 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005896 SetRawInputAt(0, array);
5897 SetRawInputAt(1, index);
5898 SetRawInputAt(2, value);
5899 }
5900
Artem Serovcced8ba2017-07-19 18:18:09 +01005901 bool IsClonable() const OVERRIDE { return true; }
5902
Calin Juravle77520bc2015-01-12 18:45:46 +00005903 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005904 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005905 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005906 }
5907
Mingyao Yang81014cb2015-06-02 03:16:27 -07005908 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005909 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005910
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005911 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005912 // TODO: Same as for ArrayGet.
5913 return false;
5914 }
5915
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005916 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005917 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005918 }
5919
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005920 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005921 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005922 }
5923
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005924 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005925 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005926 }
5927
Vladimir Markoa1de9182016-02-25 11:37:38 +00005928 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5929 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5930 bool StaticTypeOfArrayIsObjectArray() const {
5931 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5932 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005933
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005934 HInstruction* GetArray() const { return InputAt(0); }
5935 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005936 HInstruction* GetValue() const { return InputAt(2); }
5937
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005938 DataType::Type GetComponentType() const {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005939 return GetComponentType(GetValue()->GetType(), GetRawExpectedComponentType());
5940 }
5941
5942 static DataType::Type GetComponentType(DataType::Type value_type,
5943 DataType::Type expected_component_type) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005944 // The Dex format does not type floating point index operations. Since the
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005945 // `expected_component_type` comes from SSA building and can therefore not
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005946 // be correct, we also check what is the value type. If it is a floating
5947 // point type, we must use that type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005948 return ((value_type == DataType::Type::kFloat32) || (value_type == DataType::Type::kFloat64))
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005949 ? value_type
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005950 : expected_component_type;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005951 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005952
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005953 DataType::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005954 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005955 }
5956
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005957 static SideEffects ComputeSideEffects(DataType::Type type) {
5958 return SideEffects::ArrayWriteOfType(type).Union(SideEffectsForArchRuntimeCalls(type));
Aart Bik18b36ab2016-04-13 16:41:35 -07005959 }
5960
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005961 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type value_type) {
5962 return (value_type == DataType::Type::kReference) ? SideEffects::CanTriggerGC()
5963 : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005964 }
5965
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005966 DECLARE_INSTRUCTION(ArraySet);
5967
Artem Serovcced8ba2017-07-19 18:18:09 +01005968 protected:
5969 DEFAULT_COPY_CONSTRUCTOR(ArraySet);
5970
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005971 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005972 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5973 static constexpr size_t kFieldExpectedComponentTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005974 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00005975 static constexpr size_t kFlagNeedsTypeCheck =
5976 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5977 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005978 // Cached information for the reference_type_info_ so that codegen
5979 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005980 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5981 static constexpr size_t kNumberOfArraySetPackedBits =
5982 kFlagStaticTypeOfArrayIsObjectArray + 1;
5983 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5984 using ExpectedComponentTypeField =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005985 BitField<DataType::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005986};
5987
Vladimir Markofcb503c2016-05-18 12:48:17 +01005988class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005989 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005990 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305991 : HExpression(kArrayLength, DataType::Type::kInt32, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005992 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005993 // Note that arrays do not change length, so the instruction does not
5994 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005995 SetRawInputAt(0, array);
5996 }
5997
Artem Serovcced8ba2017-07-19 18:18:09 +01005998 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle77520bc2015-01-12 18:45:46 +00005999 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006000 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006001 return true;
6002 }
Calin Juravle641547a2015-04-21 22:08:51 +01006003 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
6004 return obj == InputAt(0);
6005 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006006
Vladimir Markodce016e2016-04-28 13:10:02 +01006007 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
6008
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006009 DECLARE_INSTRUCTION(ArrayLength);
6010
Artem Serovcced8ba2017-07-19 18:18:09 +01006011 protected:
6012 DEFAULT_COPY_CONSTRUCTOR(ArrayLength);
6013
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006014 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01006015 // We treat a String as an array, creating the HArrayLength from String.length()
6016 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
6017 // determine whether a particular HArrayLength is actually a String.length() by
6018 // looking at the type of the input but that requires holding the mutator lock, so
6019 // we prefer to use a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01006020 static constexpr size_t kFlagIsStringLength = kNumberOfGenericPackedBits;
Vladimir Markodce016e2016-04-28 13:10:02 +01006021 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
6022 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6023 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006024};
6025
Vladimir Markofcb503c2016-05-18 12:48:17 +01006026class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006027 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00006028 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
6029 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006030 HBoundsCheck(HInstruction* index,
6031 HInstruction* length,
6032 uint32_t dex_pc,
Vladimir Marko0259c242017-12-04 11:27:47 +00006033 bool is_string_char_at = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306034 : HExpression(kBoundsCheck, index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006035 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(index->GetType()));
Vladimir Marko0259c242017-12-04 11:27:47 +00006036 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006037 SetRawInputAt(0, index);
6038 SetRawInputAt(1, length);
6039 }
6040
Artem Serovcced8ba2017-07-19 18:18:09 +01006041 bool IsClonable() const OVERRIDE { return true; }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006042 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006043 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006044 return true;
6045 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006046
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006047 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006048
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006049 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01006050
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00006051 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006052
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006053 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006054
6055 DECLARE_INSTRUCTION(BoundsCheck);
6056
Artem Serovcced8ba2017-07-19 18:18:09 +01006057 protected:
6058 DEFAULT_COPY_CONSTRUCTOR(BoundsCheck);
6059
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006060 private:
Vladimir Markobd785672018-05-03 17:09:09 +01006061 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006062};
6063
Vladimir Markobd785672018-05-03 17:09:09 +01006064class HSuspendCheck FINAL : public HExpression<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006065 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00006066 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01006067 : HExpression(kSuspendCheck, SideEffects::CanTriggerGC(), dex_pc),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306068 slow_path_(nullptr) {
6069 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006070
Artem Serovcced8ba2017-07-19 18:18:09 +01006071 bool IsClonable() const OVERRIDE { return true; }
6072
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006073 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006074 return true;
6075 }
6076
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006077 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
6078 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006079
6080 DECLARE_INSTRUCTION(SuspendCheck);
6081
Artem Serovcced8ba2017-07-19 18:18:09 +01006082 protected:
6083 DEFAULT_COPY_CONSTRUCTOR(SuspendCheck);
6084
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006085 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006086 // Only used for code generation, in order to share the same slow path between back edges
6087 // of a same loop.
6088 SlowPathCode* slow_path_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006089};
6090
David Srbecky0cf44932015-12-09 14:09:59 +00006091// Pseudo-instruction which provides the native debugger with mapping information.
6092// It ensures that we can generate line number and local variables at this point.
Vladimir Markobd785672018-05-03 17:09:09 +01006093class HNativeDebugInfo : public HExpression<0> {
David Srbecky0cf44932015-12-09 14:09:59 +00006094 public:
6095 explicit HNativeDebugInfo(uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006096 : HExpression<0>(kNativeDebugInfo, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306097 }
David Srbecky0cf44932015-12-09 14:09:59 +00006098
6099 bool NeedsEnvironment() const OVERRIDE {
6100 return true;
6101 }
6102
6103 DECLARE_INSTRUCTION(NativeDebugInfo);
6104
Artem Serovcced8ba2017-07-19 18:18:09 +01006105 protected:
6106 DEFAULT_COPY_CONSTRUCTOR(NativeDebugInfo);
David Srbecky0cf44932015-12-09 14:09:59 +00006107};
6108
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006109/**
6110 * Instruction to load a Class object.
6111 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006112class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006113 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006114 // Determines how to load the Class.
6115 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006116 // We cannot load this class. See HSharpening::SharpenLoadClass.
6117 kInvalid = -1,
6118
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006119 // Use the Class* from the method's own ArtMethod*.
6120 kReferrersClass,
6121
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006122 // Use PC-relative boot image Class* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006123 // Used for boot image classes referenced by boot image code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006124 kBootImageLinkTimePcRelative,
6125
6126 // Use a known boot image Class* address, embedded in the code by the codegen.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006127 // Used for boot image classes referenced by apps in JIT- and AOT-compiled code (non-PIC).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006128 kBootImageAddress,
6129
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006130 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
6131 // Used for boot image classes referenced by apps in AOT-compiled code (PIC).
6132 kBootImageRelRo,
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006133
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006134 // Load from an entry in the .bss section using a PC-relative load.
6135 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
6136 kBssEntry,
6137
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006138 // Load from the root table associated with the JIT compiled method.
6139 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006140
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006141 // Load using a simple runtime call. This is the fall-back load kind when
6142 // the codegen is unable to use another appropriate kind.
6143 kRuntimeCall,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006144
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006145 kLast = kRuntimeCall
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006146 };
6147
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006148 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08006149 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006150 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006151 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006152 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01006153 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00006154 bool needs_access_check)
Vladimir Markobd785672018-05-03 17:09:09 +01006155 : HInstruction(kLoadClass,
6156 DataType::Type::kReference,
6157 SideEffectsForArchRuntimeCalls(),
6158 dex_pc),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006159 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006160 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006161 dex_file_(dex_file),
Vladimir Marko175e7862018-03-27 09:03:13 +00006162 klass_(klass) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01006163 // Referrers class should not need access check. We never inline unverified
6164 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006165 DCHECK(!is_referrers_class || !needs_access_check);
6166
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006167 SetPackedField<LoadKindField>(
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006168 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kRuntimeCall);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006169 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00006170 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006171 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Marko175e7862018-03-27 09:03:13 +00006172 SetPackedFlag<kFlagValidLoadedClassRTI>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006173 }
6174
Artem Serovcced8ba2017-07-19 18:18:09 +01006175 bool IsClonable() const OVERRIDE { return true; }
6176
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006177 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006178
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006179 LoadKind GetLoadKind() const {
6180 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006181 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006182
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006183 bool HasPcRelativeLoadKind() const {
6184 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6185 GetLoadKind() == LoadKind::kBootImageRelRo ||
6186 GetLoadKind() == LoadKind::kBssEntry;
6187 }
6188
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006189 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006190
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006191 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006192
Andreas Gampea5b09a62016-11-17 15:21:22 -08006193 size_t ComputeHashCode() const OVERRIDE { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006194
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01006195 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006196
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006197 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006198 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006199 }
6200
Calin Juravle0ba218d2015-05-19 18:46:01 +01006201 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00006202 // The entrypoint the code generator is going to call does not do
6203 // clinit of the class.
6204 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00006205 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006206 }
6207
6208 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00006209 return NeedsAccessCheck() ||
6210 MustGenerateClinitCheck() ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006211 GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006212 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01006213 }
6214
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006215 bool CanThrow() const OVERRIDE {
Vladimir Marko41559982017-01-06 14:04:23 +00006216 return NeedsAccessCheck() ||
6217 MustGenerateClinitCheck() ||
6218 // If the class is in the boot image, the lookup in the runtime call cannot throw.
6219 // This keeps CanThrow() consistent between non-PIC (using kBootImageAddress) and
6220 // PIC and subsequently avoids a DCE behavior dependency on the PIC option.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006221 ((GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006222 GetLoadKind() == LoadKind::kBssEntry) &&
6223 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006224 }
6225
Calin Juravleacf735c2015-02-12 15:25:22 +00006226 ReferenceTypeInfo GetLoadedClassRTI() {
Vladimir Marko175e7862018-03-27 09:03:13 +00006227 if (GetPackedFlag<kFlagValidLoadedClassRTI>()) {
6228 // Note: The is_exact flag from the return value should not be used.
6229 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact */ true);
6230 } else {
6231 return ReferenceTypeInfo::CreateInvalid();
6232 }
Calin Juravleacf735c2015-02-12 15:25:22 +00006233 }
6234
Vladimir Marko175e7862018-03-27 09:03:13 +00006235 // Loaded class RTI is marked as valid by RTP if the klass_ is admissible.
6236 void SetValidLoadedClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
6237 DCHECK(klass_ != nullptr);
6238 SetPackedFlag<kFlagValidLoadedClassRTI>(true);
Calin Juravleacf735c2015-02-12 15:25:22 +00006239 }
6240
Andreas Gampea5b09a62016-11-17 15:21:22 -08006241 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006242 const DexFile& GetDexFile() const { return dex_file_; }
6243
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006244 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006245 return GetLoadKind() == LoadKind::kRuntimeCall;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006246 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00006247
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006248 static SideEffects SideEffectsForArchRuntimeCalls() {
6249 return SideEffects::CanTriggerGC();
6250 }
6251
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006252 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006253 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006254 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006255 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006256
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006257 void MarkInBootImage() {
6258 SetPackedFlag<kFlagIsInBootImage>(true);
6259 }
6260
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006261 void AddSpecialInput(HInstruction* special_input);
6262
6263 using HInstruction::GetInputRecords; // Keep the const version visible.
6264 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
6265 return ArrayRef<HUserRecord<HInstruction*>>(
6266 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6267 }
6268
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006269 Handle<mirror::Class> GetClass() const {
6270 return klass_;
6271 }
6272
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006273 DECLARE_INSTRUCTION(LoadClass);
6274
Artem Serovcced8ba2017-07-19 18:18:09 +01006275 protected:
6276 DEFAULT_COPY_CONSTRUCTOR(LoadClass);
6277
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006278 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006279 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00006280 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006281 // Whether this instruction must generate the initialization check.
6282 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006283 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006284 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
6285 static constexpr size_t kFieldLoadKindSize =
6286 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
Vladimir Marko175e7862018-03-27 09:03:13 +00006287 static constexpr size_t kFlagValidLoadedClassRTI = kFieldLoadKind + kFieldLoadKindSize;
6288 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagValidLoadedClassRTI + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006289 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006290 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
6291
6292 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006293 return load_kind == LoadKind::kReferrersClass ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006294 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006295 load_kind == LoadKind::kBssEntry ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006296 load_kind == LoadKind::kRuntimeCall;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006297 }
6298
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006299 void SetLoadKindInternal(LoadKind load_kind);
6300
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006301 // The special input is the HCurrentMethod for kRuntimeCall or kReferrersClass.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006302 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006303 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006304 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006305
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006306 // A type index and dex file where the class can be accessed. The dex file can be:
6307 // - The compiling method's dex file if the class is defined there too.
6308 // - The compiling method's dex file if the class is referenced there.
6309 // - The dex file where the class is defined. When the load kind can only be
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006310 // kBssEntry or kRuntimeCall, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08006311 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006312 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006313
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006314 Handle<mirror::Class> klass_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006315};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006316std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
6317
6318// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006319inline void HLoadClass::SetLoadKind(LoadKind load_kind) {
6320 // The load kind should be determined before inserting the instruction to the graph.
6321 DCHECK(GetBlock() == nullptr);
6322 DCHECK(GetEnvironment() == nullptr);
6323 SetPackedField<LoadKindField>(load_kind);
6324 if (load_kind != LoadKind::kRuntimeCall && load_kind != LoadKind::kReferrersClass) {
6325 special_input_ = HUserRecord<HInstruction*>(nullptr);
6326 }
6327 if (!NeedsEnvironment()) {
6328 SetSideEffects(SideEffects::None());
6329 }
6330}
6331
6332// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006333inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006334 // The special input is used for PC-relative loads on some architectures,
6335 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006336 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006337 GetLoadKind() == LoadKind::kBootImageAddress ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006338 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006339 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006340 DCHECK(special_input_.GetInstruction() == nullptr);
6341 special_input_ = HUserRecord<HInstruction*>(special_input);
6342 special_input->AddUseAt(this, 0);
6343}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006344
Vladimir Marko372f10e2016-05-17 16:30:10 +01006345class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006346 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006347 // Determines how to load the String.
6348 enum class LoadKind {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006349 // Use PC-relative boot image String* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006350 // Used for boot image strings referenced by boot image code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006351 kBootImageLinkTimePcRelative,
6352
6353 // Use a known boot image String* address, embedded in the code by the codegen.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006354 // Used for boot image strings referenced by apps in JIT- and AOT-compiled code (non-PIC).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006355 kBootImageAddress,
6356
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006357 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
6358 // Used for boot image strings referenced by apps in AOT-compiled code (PIC).
6359 kBootImageRelRo,
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006360
Vladimir Markoaad75c62016-10-03 08:46:48 +00006361 // Load from an entry in the .bss section using a PC-relative load.
6362 // Used for strings outside boot image when .bss is accessible with a PC-relative load.
6363 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006364
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006365 // Load from the root table associated with the JIT compiled method.
6366 kJitTableAddress,
6367
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006368 // Load using a simple runtime call. This is the fall-back load kind when
6369 // the codegen is unable to use another appropriate kind.
6370 kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006371
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006372 kLast = kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006373 };
6374
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006375 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006376 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006377 const DexFile& dex_file,
6378 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006379 : HInstruction(kLoadString,
6380 DataType::Type::kReference,
6381 SideEffectsForArchRuntimeCalls(),
6382 dex_pc),
Vladimir Marko372f10e2016-05-17 16:30:10 +01006383 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006384 string_index_(string_index),
6385 dex_file_(dex_file) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006386 SetPackedField<LoadKindField>(LoadKind::kRuntimeCall);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006387 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006388
Artem Serovcced8ba2017-07-19 18:18:09 +01006389 bool IsClonable() const OVERRIDE { return true; }
6390
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006391 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006392
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006393 LoadKind GetLoadKind() const {
6394 return GetPackedField<LoadKindField>();
6395 }
6396
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006397 bool HasPcRelativeLoadKind() const {
6398 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6399 GetLoadKind() == LoadKind::kBootImageRelRo ||
6400 GetLoadKind() == LoadKind::kBssEntry;
6401 }
6402
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006403 const DexFile& GetDexFile() const {
6404 return dex_file_;
6405 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006406
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006407 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006408 return string_index_;
6409 }
6410
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006411 Handle<mirror::String> GetString() const {
6412 return string_;
6413 }
6414
6415 void SetString(Handle<mirror::String> str) {
6416 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006417 }
6418
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006419 bool CanBeMoved() const OVERRIDE { return true; }
6420
Vladimir Marko372f10e2016-05-17 16:30:10 +01006421 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006422
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006423 size_t ComputeHashCode() const OVERRIDE { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006424
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006425 // Will call the runtime if we need to load the string through
6426 // the dex cache and the string is not guaranteed to be there yet.
6427 bool NeedsEnvironment() const OVERRIDE {
6428 LoadKind load_kind = GetLoadKind();
Vladimir Marko764d4542017-05-16 10:31:41 +01006429 if (load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006430 load_kind == LoadKind::kBootImageAddress ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006431 load_kind == LoadKind::kBootImageRelRo ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006432 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006433 return false;
6434 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006435 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006436 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006437
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006438 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006439 return GetLoadKind() == LoadKind::kRuntimeCall;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006440 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006441
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07006442 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006443 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006444
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006445 static SideEffects SideEffectsForArchRuntimeCalls() {
6446 return SideEffects::CanTriggerGC();
6447 }
6448
Vladimir Marko372f10e2016-05-17 16:30:10 +01006449 void AddSpecialInput(HInstruction* special_input);
6450
6451 using HInstruction::GetInputRecords; // Keep the const version visible.
6452 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
6453 return ArrayRef<HUserRecord<HInstruction*>>(
6454 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006455 }
6456
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006457 DECLARE_INSTRUCTION(LoadString);
6458
Artem Serovcced8ba2017-07-19 18:18:09 +01006459 protected:
6460 DEFAULT_COPY_CONSTRUCTOR(LoadString);
6461
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006462 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006463 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006464 static constexpr size_t kFieldLoadKindSize =
6465 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
6466 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006467 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006468 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006469
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006470 void SetLoadKindInternal(LoadKind load_kind);
6471
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006472 // The special input is the HCurrentMethod for kRuntimeCall.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006473 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00006474 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01006475 HUserRecord<HInstruction*> special_input_;
6476
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006477 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006478 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006479
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006480 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006481};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006482std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
6483
6484// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006485inline void HLoadString::SetLoadKind(LoadKind load_kind) {
6486 // The load kind should be determined before inserting the instruction to the graph.
6487 DCHECK(GetBlock() == nullptr);
6488 DCHECK(GetEnvironment() == nullptr);
6489 DCHECK_EQ(GetLoadKind(), LoadKind::kRuntimeCall);
6490 SetPackedField<LoadKindField>(load_kind);
6491 if (load_kind != LoadKind::kRuntimeCall) {
6492 special_input_ = HUserRecord<HInstruction*>(nullptr);
6493 }
6494 if (!NeedsEnvironment()) {
6495 SetSideEffects(SideEffects::None());
6496 }
6497}
6498
6499// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006500inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006501 // The special input is used for PC-relative loads on some architectures,
6502 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006503 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006504 GetLoadKind() == LoadKind::kBootImageAddress ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006505 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006506 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01006507 // HLoadString::GetInputRecords() returns an empty array at this point,
6508 // so use the GetInputRecords() from the base class to set the input record.
6509 DCHECK(special_input_.GetInstruction() == nullptr);
6510 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006511 special_input->AddUseAt(this, 0);
6512}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006513
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006514/**
6515 * Performs an initialization check on its Class object input.
6516 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01006517class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006518 public:
Roland Levillain3887c462015-08-12 18:15:42 +01006519 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07006520 : HExpression(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306521 kClinitCheck,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006522 DataType::Type::kReference,
Mingyao Yang217eb062017-12-11 15:20:07 -08006523 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006524 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006525 SetRawInputAt(0, constant);
6526 }
6527
Artem Serovcced8ba2017-07-19 18:18:09 +01006528 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006529 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006530 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006531 return true;
6532 }
6533
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006534 bool NeedsEnvironment() const OVERRIDE {
6535 // May call runtime to initialize the class.
6536 return true;
6537 }
6538
Nicolas Geoffray729645a2015-11-19 13:29:02 +00006539 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006540
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006541 HLoadClass* GetLoadClass() const {
6542 DCHECK(InputAt(0)->IsLoadClass());
6543 return InputAt(0)->AsLoadClass();
6544 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006545
6546 DECLARE_INSTRUCTION(ClinitCheck);
6547
Artem Serovcced8ba2017-07-19 18:18:09 +01006548
6549 protected:
6550 DEFAULT_COPY_CONSTRUCTOR(ClinitCheck);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006551};
6552
Vladimir Markofcb503c2016-05-18 12:48:17 +01006553class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006554 public:
6555 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006556 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006557 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006558 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006559 bool is_volatile,
6560 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006561 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006562 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006563 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306564 : HExpression(kStaticFieldGet,
6565 field_type,
6566 SideEffects::FieldReadOfType(field_type, is_volatile),
6567 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006568 field_info_(field,
6569 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006570 field_type,
6571 is_volatile,
6572 field_idx,
6573 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006574 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006575 SetRawInputAt(0, cls);
6576 }
6577
Calin Juravle52c48962014-12-16 17:02:57 +00006578
Artem Serovcced8ba2017-07-19 18:18:09 +01006579 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle10c9cbe2014-12-19 10:50:19 +00006580 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006581
Vladimir Marko372f10e2016-05-17 16:30:10 +01006582 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
6583 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006584 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006585 }
6586
6587 size_t ComputeHashCode() const OVERRIDE {
6588 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6589 }
6590
Calin Juravle52c48962014-12-16 17:02:57 +00006591 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006592 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006593 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006594 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006595
Vladimir Marko61b92282017-10-11 13:23:17 +01006596 void SetType(DataType::Type new_type) {
6597 DCHECK(DataType::IsIntegralType(GetType()));
6598 DCHECK(DataType::IsIntegralType(new_type));
6599 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6600 SetPackedField<TypeField>(new_type);
6601 }
6602
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006603 DECLARE_INSTRUCTION(StaticFieldGet);
6604
Artem Serovcced8ba2017-07-19 18:18:09 +01006605 protected:
6606 DEFAULT_COPY_CONSTRUCTOR(StaticFieldGet);
6607
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006608 private:
6609 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006610};
6611
Vladimir Markobd785672018-05-03 17:09:09 +01006612class HStaticFieldSet FINAL : public HExpression<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006613 public:
6614 HStaticFieldSet(HInstruction* cls,
6615 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006616 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006617 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006618 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006619 bool is_volatile,
6620 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006621 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006622 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006623 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006624 : HExpression(kStaticFieldSet,
6625 SideEffects::FieldWriteOfType(field_type, is_volatile),
6626 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006627 field_info_(field,
6628 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006629 field_type,
6630 is_volatile,
6631 field_idx,
6632 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006633 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006634 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006635 SetRawInputAt(0, cls);
6636 SetRawInputAt(1, value);
6637 }
6638
Artem Serovcced8ba2017-07-19 18:18:09 +01006639 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle52c48962014-12-16 17:02:57 +00006640 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006641 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006642 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006643 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006644
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006645 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006646 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6647 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006648
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006649 DECLARE_INSTRUCTION(StaticFieldSet);
6650
Artem Serovcced8ba2017-07-19 18:18:09 +01006651 protected:
6652 DEFAULT_COPY_CONSTRUCTOR(StaticFieldSet);
6653
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006654 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006655 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6656 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6657 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6658 "Too many packed fields.");
6659
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006660 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006661};
6662
Vladimir Markofcb503c2016-05-18 12:48:17 +01006663class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006664 public:
6665 HUnresolvedInstanceFieldGet(HInstruction* obj,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006666 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006667 uint32_t field_index,
6668 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306669 : HExpression(kUnresolvedInstanceFieldGet,
6670 field_type,
6671 SideEffects::AllExceptGCDependency(),
6672 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006673 field_index_(field_index) {
6674 SetRawInputAt(0, obj);
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(UnresolvedInstanceFieldGet);
6685
Artem Serovcced8ba2017-07-19 18:18:09 +01006686 protected:
6687 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldGet);
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 Markobd785672018-05-03 17:09:09 +01006693class HUnresolvedInstanceFieldSet FINAL : public HExpression<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006694 public:
6695 HUnresolvedInstanceFieldSet(HInstruction* obj,
6696 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006697 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006698 uint32_t field_index,
6699 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006700 : HExpression(kUnresolvedInstanceFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006701 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006702 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006703 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006704 SetRawInputAt(0, obj);
6705 SetRawInputAt(1, 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(UnresolvedInstanceFieldSet);
6716
Artem Serovcced8ba2017-07-19 18:18:09 +01006717 protected:
6718 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldSet);
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
Vladimir Markofcb503c2016-05-18 12:48:17 +01006733class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006734 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006735 HUnresolvedStaticFieldGet(DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006736 uint32_t field_index,
6737 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306738 : HExpression(kUnresolvedStaticFieldGet,
6739 field_type,
6740 SideEffects::AllExceptGCDependency(),
6741 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006742 field_index_(field_index) {
6743 }
6744
Artem Serovcced8ba2017-07-19 18:18:09 +01006745 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006746 bool NeedsEnvironment() const OVERRIDE { return true; }
6747 bool CanThrow() const OVERRIDE { return true; }
6748
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006749 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006750 uint32_t GetFieldIndex() const { return field_index_; }
6751
6752 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6753
Artem Serovcced8ba2017-07-19 18:18:09 +01006754 protected:
6755 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldGet);
6756
Calin Juravlee460d1d2015-09-29 04:52:17 +01006757 private:
6758 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006759};
6760
Vladimir Markobd785672018-05-03 17:09:09 +01006761class HUnresolvedStaticFieldSet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006762 public:
6763 HUnresolvedStaticFieldSet(HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006764 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006765 uint32_t field_index,
6766 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006767 : HExpression(kUnresolvedStaticFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006768 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006769 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006770 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006771 SetRawInputAt(0, value);
6772 }
6773
Artem Serovcced8ba2017-07-19 18:18:09 +01006774 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006775 bool NeedsEnvironment() const OVERRIDE { return true; }
6776 bool CanThrow() const OVERRIDE { return true; }
6777
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006778 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006779 uint32_t GetFieldIndex() const { return field_index_; }
6780
6781 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6782
Artem Serovcced8ba2017-07-19 18:18:09 +01006783 protected:
6784 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldSet);
6785
Calin Juravlee460d1d2015-09-29 04:52:17 +01006786 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006787 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6788 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006789 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006790 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6791 kFieldFieldType + kFieldFieldTypeSize;
6792 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6793 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006794 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006795
Calin Juravlee460d1d2015-09-29 04:52:17 +01006796 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006797};
6798
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006799// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006800class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006801 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006802 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306803 : HExpression(kLoadException, DataType::Type::kReference, SideEffects::None(), dex_pc) {
6804 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006805
David Brazdilbbd733e2015-08-18 17:48:17 +01006806 bool CanBeNull() const OVERRIDE { return false; }
6807
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006808 DECLARE_INSTRUCTION(LoadException);
6809
Artem Serovcced8ba2017-07-19 18:18:09 +01006810 protected:
6811 DEFAULT_COPY_CONSTRUCTOR(LoadException);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006812};
6813
David Brazdilcb1c0552015-08-04 16:22:25 +01006814// Implicit part of move-exception which clears thread-local exception storage.
6815// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markobd785672018-05-03 17:09:09 +01006816class HClearException FINAL : public HExpression<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006817 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006818 explicit HClearException(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01006819 : HExpression(kClearException, SideEffects::AllWrites(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306820 }
David Brazdilcb1c0552015-08-04 16:22:25 +01006821
6822 DECLARE_INSTRUCTION(ClearException);
6823
Artem Serovcced8ba2017-07-19 18:18:09 +01006824 protected:
6825 DEFAULT_COPY_CONSTRUCTOR(ClearException);
David Brazdilcb1c0552015-08-04 16:22:25 +01006826};
6827
Vladimir Markobd785672018-05-03 17:09:09 +01006828class HThrow FINAL : public HExpression<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006829 public:
6830 HThrow(HInstruction* exception, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006831 : HExpression(kThrow, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006832 SetRawInputAt(0, exception);
6833 }
6834
6835 bool IsControlFlow() const OVERRIDE { return true; }
6836
6837 bool NeedsEnvironment() const OVERRIDE { return true; }
6838
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006839 bool CanThrow() const OVERRIDE { return true; }
6840
Aart Bika8b8e9b2018-01-09 11:01:02 -08006841 bool AlwaysThrows() const OVERRIDE { return true; }
6842
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006843 DECLARE_INSTRUCTION(Throw);
6844
Artem Serovcced8ba2017-07-19 18:18:09 +01006845 protected:
6846 DEFAULT_COPY_CONSTRUCTOR(Throw);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006847};
6848
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006849/**
6850 * Implementation strategies for the code generator of a HInstanceOf
6851 * or `HCheckCast`.
6852 */
6853enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006854 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006855 kExactCheck, // Can do a single class compare.
6856 kClassHierarchyCheck, // Can just walk the super class chain.
6857 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6858 kInterfaceCheck, // No optimization yet when checking against an interface.
6859 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006860 kArrayCheck, // No optimization yet when checking against a generic array.
Vladimir Marko175e7862018-03-27 09:03:13 +00006861 kBitstringCheck, // Compare the type check bitstring.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006862 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006863};
6864
Roland Levillain86503782016-02-11 19:07:30 +00006865std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6866
Vladimir Marko175e7862018-03-27 09:03:13 +00006867// Note: HTypeCheckInstruction is just a helper class, not an abstract instruction with an
6868// `IsTypeCheckInstruction()`. (New virtual methods in the HInstruction class have a high cost.)
6869class HTypeCheckInstruction : public HVariableInputSizeInstruction {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006870 public:
Vladimir Marko175e7862018-03-27 09:03:13 +00006871 HTypeCheckInstruction(InstructionKind kind,
Vladimir Markobd785672018-05-03 17:09:09 +01006872 DataType::Type type,
Vladimir Marko175e7862018-03-27 09:03:13 +00006873 HInstruction* object,
6874 HInstruction* target_class_or_null,
6875 TypeCheckKind check_kind,
6876 Handle<mirror::Class> klass,
6877 uint32_t dex_pc,
6878 ArenaAllocator* allocator,
6879 HIntConstant* bitstring_path_to_root,
6880 HIntConstant* bitstring_mask,
6881 SideEffects side_effects)
6882 : HVariableInputSizeInstruction(
6883 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01006884 type,
Vladimir Marko175e7862018-03-27 09:03:13 +00006885 side_effects,
6886 dex_pc,
6887 allocator,
6888 /* number_of_inputs */ check_kind == TypeCheckKind::kBitstringCheck ? 4u : 2u,
6889 kArenaAllocTypeCheckInputs),
6890 klass_(klass) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006891 SetPackedField<TypeCheckKindField>(check_kind);
6892 SetPackedFlag<kFlagMustDoNullCheck>(true);
Vladimir Marko175e7862018-03-27 09:03:13 +00006893 SetPackedFlag<kFlagValidTargetClassRTI>(false);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006894 SetRawInputAt(0, object);
Vladimir Marko175e7862018-03-27 09:03:13 +00006895 SetRawInputAt(1, target_class_or_null);
6896 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_path_to_root != nullptr);
6897 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_mask != nullptr);
6898 if (check_kind == TypeCheckKind::kBitstringCheck) {
6899 DCHECK(target_class_or_null->IsNullConstant());
6900 SetRawInputAt(2, bitstring_path_to_root);
6901 SetRawInputAt(3, bitstring_mask);
6902 } else {
6903 DCHECK(target_class_or_null->IsLoadClass());
6904 }
Vladimir Marko87584542017-12-12 17:47:52 +00006905 }
6906
6907 HLoadClass* GetTargetClass() const {
Vladimir Marko175e7862018-03-27 09:03:13 +00006908 DCHECK_NE(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
Vladimir Marko87584542017-12-12 17:47:52 +00006909 HInstruction* load_class = InputAt(1);
6910 DCHECK(load_class->IsLoadClass());
6911 return load_class->AsLoadClass();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006912 }
6913
Vladimir Marko175e7862018-03-27 09:03:13 +00006914 uint32_t GetBitstringPathToRoot() const {
6915 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
6916 HInstruction* path_to_root = InputAt(2);
6917 DCHECK(path_to_root->IsIntConstant());
6918 return static_cast<uint32_t>(path_to_root->AsIntConstant()->GetValue());
6919 }
6920
6921 uint32_t GetBitstringMask() const {
6922 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
6923 HInstruction* mask = InputAt(3);
6924 DCHECK(mask->IsIntConstant());
6925 return static_cast<uint32_t>(mask->AsIntConstant()->GetValue());
6926 }
6927
Artem Serovcced8ba2017-07-19 18:18:09 +01006928 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006929 bool CanBeMoved() const OVERRIDE { return true; }
6930
Vladimir Marko175e7862018-03-27 09:03:13 +00006931 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
6932 DCHECK(other->IsInstanceOf() || other->IsCheckCast()) << other->DebugName();
6933 return GetPackedFields() == down_cast<const HTypeCheckInstruction*>(other)->GetPackedFields();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006934 }
6935
Andreas Gampe3fbd3ad2018-03-26 21:14:46 +00006936 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6937 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6938 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6939 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
6940
Vladimir Marko175e7862018-03-27 09:03:13 +00006941 ReferenceTypeInfo GetTargetClassRTI() {
6942 if (GetPackedFlag<kFlagValidTargetClassRTI>()) {
6943 // Note: The is_exact flag from the return value should not be used.
6944 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact */ true);
6945 } else {
6946 return ReferenceTypeInfo::CreateInvalid();
6947 }
6948 }
6949
6950 // Target class RTI is marked as valid by RTP if the klass_ is admissible.
6951 void SetValidTargetClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
6952 DCHECK(klass_ != nullptr);
6953 SetPackedFlag<kFlagValidTargetClassRTI>(true);
6954 }
6955
6956 Handle<mirror::Class> GetClass() const {
6957 return klass_;
6958 }
6959
6960 protected:
6961 DEFAULT_COPY_CONSTRUCTOR(TypeCheckInstruction);
6962
6963 private:
6964 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6965 static constexpr size_t kFieldTypeCheckKindSize =
6966 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6967 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6968 static constexpr size_t kFlagValidTargetClassRTI = kFlagMustDoNullCheck + 1;
6969 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagValidTargetClassRTI + 1;
6970 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6971 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
6972
6973 Handle<mirror::Class> klass_;
6974};
6975
6976class HInstanceOf FINAL : public HTypeCheckInstruction {
6977 public:
6978 HInstanceOf(HInstruction* object,
6979 HInstruction* target_class_or_null,
6980 TypeCheckKind check_kind,
6981 Handle<mirror::Class> klass,
6982 uint32_t dex_pc,
6983 ArenaAllocator* allocator,
6984 HIntConstant* bitstring_path_to_root,
6985 HIntConstant* bitstring_mask)
6986 : HTypeCheckInstruction(kInstanceOf,
Vladimir Markobd785672018-05-03 17:09:09 +01006987 DataType::Type::kBool,
Vladimir Marko175e7862018-03-27 09:03:13 +00006988 object,
6989 target_class_or_null,
6990 check_kind,
6991 klass,
6992 dex_pc,
6993 allocator,
6994 bitstring_path_to_root,
6995 bitstring_mask,
6996 SideEffectsForArchRuntimeCalls(check_kind)) {}
6997
Vladimir Marko175e7862018-03-27 09:03:13 +00006998 bool NeedsEnvironment() const OVERRIDE {
6999 return CanCallRuntime(GetTypeCheckKind());
7000 }
7001
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007002 static bool CanCallRuntime(TypeCheckKind check_kind) {
7003 // Mips currently does runtime calls for any other checks.
7004 return check_kind != TypeCheckKind::kExactCheck;
7005 }
7006
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007007 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007008 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01007009 }
7010
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007011 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007012
Artem Serovcced8ba2017-07-19 18:18:09 +01007013 protected:
7014 DEFAULT_COPY_CONSTRUCTOR(InstanceOf);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007015};
7016
Vladimir Markofcb503c2016-05-18 12:48:17 +01007017class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00007018 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07007019 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307020 : HExpression(kBoundType, DataType::Type::kReference, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00007021 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
7022 SetPackedFlag<kFlagUpperCanBeNull>(true);
7023 SetPackedFlag<kFlagCanBeNull>(true);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007024 DCHECK_EQ(input->GetType(), DataType::Type::kReference);
Calin Juravleb1498f62015-02-16 13:13:29 +00007025 SetRawInputAt(0, input);
7026 }
7027
Artem Serovcced8ba2017-07-19 18:18:09 +01007028 bool IsClonable() const OVERRIDE { return true; }
7029
David Brazdilf5552582015-12-27 13:36:12 +00007030 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007031 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00007032 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00007033 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007034
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007035 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007036 DCHECK(GetUpperCanBeNull() || !can_be_null);
7037 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007038 }
7039
Vladimir Markoa1de9182016-02-25 11:37:38 +00007040 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007041
Calin Juravleb1498f62015-02-16 13:13:29 +00007042 DECLARE_INSTRUCTION(BoundType);
7043
Artem Serovcced8ba2017-07-19 18:18:09 +01007044 protected:
7045 DEFAULT_COPY_CONSTRUCTOR(BoundType);
7046
Calin Juravleb1498f62015-02-16 13:13:29 +00007047 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007048 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
7049 // is false then CanBeNull() cannot be true).
Vladimir Markobd785672018-05-03 17:09:09 +01007050 static constexpr size_t kFlagUpperCanBeNull = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007051 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
7052 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
7053 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7054
Calin Juravleb1498f62015-02-16 13:13:29 +00007055 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007056 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
7057 // It is used to bound the type in cases like:
7058 // if (x instanceof ClassX) {
7059 // // uper_bound_ will be ClassX
7060 // }
David Brazdilf5552582015-12-27 13:36:12 +00007061 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00007062};
7063
Vladimir Marko175e7862018-03-27 09:03:13 +00007064class HCheckCast FINAL : public HTypeCheckInstruction {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007065 public:
7066 HCheckCast(HInstruction* object,
Vladimir Marko175e7862018-03-27 09:03:13 +00007067 HInstruction* target_class_or_null,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007068 TypeCheckKind check_kind,
Vladimir Marko175e7862018-03-27 09:03:13 +00007069 Handle<mirror::Class> klass,
7070 uint32_t dex_pc,
7071 ArenaAllocator* allocator,
7072 HIntConstant* bitstring_path_to_root,
7073 HIntConstant* bitstring_mask)
7074 : HTypeCheckInstruction(kCheckCast,
Vladimir Markobd785672018-05-03 17:09:09 +01007075 DataType::Type::kVoid,
Vladimir Marko175e7862018-03-27 09:03:13 +00007076 object,
7077 target_class_or_null,
7078 check_kind,
7079 klass,
7080 dex_pc,
7081 allocator,
7082 bitstring_path_to_root,
7083 bitstring_mask,
7084 SideEffects::CanTriggerGC()) {}
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007085
7086 bool NeedsEnvironment() const OVERRIDE {
7087 // Instruction may throw a CheckCastError.
7088 return true;
7089 }
7090
7091 bool CanThrow() const OVERRIDE { return true; }
7092
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007093 DECLARE_INSTRUCTION(CheckCast);
7094
Artem Serovcced8ba2017-07-19 18:18:09 +01007095 protected:
7096 DEFAULT_COPY_CONSTRUCTOR(CheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007097};
7098
Andreas Gampe26de38b2016-07-27 17:53:11 -07007099/**
7100 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
7101 * @details We define the combined barrier types that are actually required
7102 * by the Java Memory Model, rather than using exactly the terminology from
7103 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
7104 * primitives. Note that the JSR-133 cookbook generally does not deal with
7105 * store atomicity issues, and the recipes there are not always entirely sufficient.
7106 * The current recipe is as follows:
7107 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
7108 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
7109 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
7110 * -# Use StoreStore barrier after all stores but before return from any constructor whose
7111 * class has final fields.
7112 * -# Use NTStoreStore to order non-temporal stores with respect to all later
7113 * store-to-memory instructions. Only generated together with non-temporal stores.
7114 */
7115enum MemBarrierKind {
7116 kAnyStore,
7117 kLoadAny,
7118 kStoreStore,
7119 kAnyAny,
7120 kNTStoreStore,
7121 kLastBarrierKind = kNTStoreStore
7122};
7123std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
7124
Vladimir Markobd785672018-05-03 17:09:09 +01007125class HMemoryBarrier FINAL : public HExpression<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01007126 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007127 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007128 : HExpression(kMemoryBarrier,
7129 SideEffects::AllWritesAndReads(), // Assume write/read on all fields/arrays.
7130 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007131 SetPackedField<BarrierKindField>(barrier_kind);
7132 }
Calin Juravle27df7582015-04-17 19:12:31 +01007133
Artem Serovcced8ba2017-07-19 18:18:09 +01007134 bool IsClonable() const OVERRIDE { return true; }
7135
Vladimir Markoa1de9182016-02-25 11:37:38 +00007136 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01007137
7138 DECLARE_INSTRUCTION(MemoryBarrier);
7139
Artem Serovcced8ba2017-07-19 18:18:09 +01007140 protected:
7141 DEFAULT_COPY_CONSTRUCTOR(MemoryBarrier);
7142
Calin Juravle27df7582015-04-17 19:12:31 +01007143 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007144 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
7145 static constexpr size_t kFieldBarrierKindSize =
7146 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
7147 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
7148 kFieldBarrierKind + kFieldBarrierKindSize;
7149 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
7150 "Too many packed fields.");
7151 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01007152};
7153
Igor Murashkind01745e2017-04-05 16:40:31 -07007154// A constructor fence orders all prior stores to fields that could be accessed via a final field of
7155// the specified object(s), with respect to any subsequent store that might "publish"
7156// (i.e. make visible) the specified object to another thread.
7157//
7158// JLS 17.5.1 "Semantics of final fields" states that a freeze action happens
7159// for all final fields (that were set) at the end of the invoked constructor.
7160//
7161// The constructor fence models the freeze actions for the final fields of an object
7162// being constructed (semantically at the end of the constructor). Constructor fences
7163// have a per-object affinity; two separate objects being constructed get two separate
7164// constructor fences.
7165//
7166// (Note: that if calling a super-constructor or forwarding to another constructor,
7167// the freezes would happen at the end of *that* constructor being invoked).
7168//
7169// The memory model guarantees that when the object being constructed is "published" after
7170// constructor completion (i.e. escapes the current thread via a store), then any final field
7171// writes must be observable on other threads (once they observe that publication).
7172//
7173// Further, anything written before the freeze, and read by dereferencing through the final field,
7174// must also be visible (so final object field could itself have an object with non-final fields;
7175// yet the freeze must also extend to them).
7176//
7177// Constructor example:
7178//
7179// class HasFinal {
7180// final int field; Optimizing IR for <init>()V:
7181// HasFinal() {
7182// field = 123; HInstanceFieldSet(this, HasFinal.field, 123)
7183// // freeze(this.field); HConstructorFence(this)
7184// } HReturn
7185// }
7186//
7187// HConstructorFence can serve double duty as a fence for new-instance/new-array allocations of
7188// already-initialized classes; in that case the allocation must act as a "default-initializer"
7189// of the object which effectively writes the class pointer "final field".
7190//
7191// For example, we can model default-initialiation as roughly the equivalent of the following:
7192//
7193// class Object {
7194// private final Class header;
7195// }
7196//
7197// Java code: Optimizing IR:
7198//
7199// T new_instance<T>() {
7200// Object obj = allocate_memory(T.class.size); obj = HInvoke(art_quick_alloc_object, T)
7201// obj.header = T.class; // header write is done by above call.
7202// // freeze(obj.header) HConstructorFence(obj)
7203// return (T)obj;
7204// }
7205//
7206// See also:
7207// * CompilerDriver::RequiresConstructorBarrier
7208// * QuasiAtomic::ThreadFenceForConstructor
7209//
7210class HConstructorFence FINAL : public HVariableInputSizeInstruction {
7211 // A fence has variable inputs because the inputs can be removed
7212 // after prepare_for_register_allocation phase.
7213 // (TODO: In the future a fence could freeze multiple objects
7214 // after merging two fences together.)
7215 public:
7216 // `fence_object` is the reference that needs to be protected for correct publication.
7217 //
7218 // It makes sense in the following situations:
7219 // * <init> constructors, it's the "this" parameter (i.e. HParameterValue, s.t. IsThis() == true).
7220 // * new-instance-like instructions, it's the return value (i.e. HNewInstance).
7221 //
7222 // After construction the `fence_object` becomes the 0th input.
7223 // This is not an input in a real sense, but just a convenient place to stash the information
7224 // about the associated object.
7225 HConstructorFence(HInstruction* fence_object,
7226 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007227 ArenaAllocator* allocator)
Igor Murashkind01745e2017-04-05 16:40:31 -07007228 // We strongly suspect there is not a more accurate way to describe the fine-grained reordering
7229 // constraints described in the class header. We claim that these SideEffects constraints
7230 // enforce a superset of the real constraints.
7231 //
7232 // The ordering described above is conservatively modeled with SideEffects as follows:
7233 //
7234 // * To prevent reordering of the publication stores:
7235 // ----> "Reads of objects" is the initial SideEffect.
7236 // * For every primitive final field store in the constructor:
7237 // ----> Union that field's type as a read (e.g. "Read of T") into the SideEffect.
7238 // * If there are any stores to reference final fields in the constructor:
7239 // ----> Use a more conservative "AllReads" SideEffect because any stores to any references
7240 // that are reachable from `fence_object` also need to be prevented for reordering
7241 // (and we do not want to do alias analysis to figure out what those stores are).
7242 //
7243 // In the implementation, this initially starts out as an "all reads" side effect; this is an
7244 // even more conservative approach than the one described above, and prevents all of the
7245 // above reordering without analyzing any of the instructions in the constructor.
7246 //
7247 // If in a later phase we discover that there are no writes to reference final fields,
7248 // we can refine the side effect to a smaller set of type reads (see above constraints).
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307249 : HVariableInputSizeInstruction(kConstructorFence,
7250 SideEffects::AllReads(),
Igor Murashkind01745e2017-04-05 16:40:31 -07007251 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007252 allocator,
Igor Murashkind01745e2017-04-05 16:40:31 -07007253 /* number_of_inputs */ 1,
7254 kArenaAllocConstructorFenceInputs) {
7255 DCHECK(fence_object != nullptr);
7256 SetRawInputAt(0, fence_object);
7257 }
7258
7259 // The object associated with this constructor fence.
7260 //
7261 // (Note: This will be null after the prepare_for_register_allocation phase,
7262 // as all constructor fence inputs are removed there).
7263 HInstruction* GetFenceObject() const {
7264 return InputAt(0);
7265 }
7266
7267 // Find all the HConstructorFence uses (`fence_use`) for `this` and:
7268 // - Delete `fence_use` from `this`'s use list.
7269 // - Delete `this` from `fence_use`'s inputs list.
7270 // - If the `fence_use` is dead, remove it from the graph.
7271 //
7272 // A fence is considered dead once it no longer has any uses
7273 // and all of the inputs are dead.
7274 //
7275 // This must *not* be called during/after prepare_for_register_allocation,
7276 // because that removes all the inputs to the fences but the fence is actually
7277 // still considered live.
Igor Murashkin6ef45672017-08-08 13:59:55 -07007278 //
7279 // Returns how many HConstructorFence instructions were removed from graph.
7280 static size_t RemoveConstructorFences(HInstruction* instruction);
Igor Murashkind01745e2017-04-05 16:40:31 -07007281
Igor Murashkindd018df2017-08-09 10:38:31 -07007282 // Combine all inputs of `this` and `other` instruction and remove
7283 // `other` from the graph.
7284 //
7285 // Inputs are unique after the merge.
7286 //
7287 // Requirement: `this` must not be the same as `other.
7288 void Merge(HConstructorFence* other);
7289
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007290 // Check if this constructor fence is protecting
7291 // an HNewInstance or HNewArray that is also the immediate
7292 // predecessor of `this`.
7293 //
Igor Murashkindd018df2017-08-09 10:38:31 -07007294 // If `ignore_inputs` is true, then the immediate predecessor doesn't need
7295 // to be one of the inputs of `this`.
7296 //
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007297 // Returns the associated HNewArray or HNewInstance,
7298 // or null otherwise.
Igor Murashkindd018df2017-08-09 10:38:31 -07007299 HInstruction* GetAssociatedAllocation(bool ignore_inputs = false);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007300
Igor Murashkind01745e2017-04-05 16:40:31 -07007301 DECLARE_INSTRUCTION(ConstructorFence);
7302
Artem Serovcced8ba2017-07-19 18:18:09 +01007303 protected:
7304 DEFAULT_COPY_CONSTRUCTOR(ConstructorFence);
Igor Murashkind01745e2017-04-05 16:40:31 -07007305};
7306
Vladimir Markobd785672018-05-03 17:09:09 +01007307class HMonitorOperation FINAL : public HExpression<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007308 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007309 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007310 kEnter,
7311 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00007312 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007313 };
7314
7315 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007316 : HExpression(kMonitorOperation,
7317 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
7318 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007319 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007320 SetRawInputAt(0, object);
7321 }
7322
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007323 // Instruction may go into runtime, so we need an environment.
7324 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00007325
7326 bool CanThrow() const OVERRIDE {
7327 // Verifier guarantees that monitor-exit cannot throw.
7328 // This is important because it allows the HGraphBuilder to remove
7329 // a dead throw-catch loop generated for `synchronized` blocks/methods.
7330 return IsEnter();
7331 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007332
Vladimir Markoa1de9182016-02-25 11:37:38 +00007333 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
7334 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007335
7336 DECLARE_INSTRUCTION(MonitorOperation);
7337
Artem Serovcced8ba2017-07-19 18:18:09 +01007338 protected:
7339 DEFAULT_COPY_CONSTRUCTOR(MonitorOperation);
7340
Calin Juravle52c48962014-12-16 17:02:57 +00007341 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007342 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
7343 static constexpr size_t kFieldOperationKindSize =
7344 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
7345 static constexpr size_t kNumberOfMonitorOperationPackedBits =
7346 kFieldOperationKind + kFieldOperationKindSize;
7347 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7348 "Too many packed fields.");
7349 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007350};
7351
Vladimir Markofcb503c2016-05-18 12:48:17 +01007352class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00007353 public:
7354 HSelect(HInstruction* condition,
7355 HInstruction* true_value,
7356 HInstruction* false_value,
7357 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307358 : HExpression(kSelect, HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
David Brazdil74eb1b22015-12-14 11:44:01 +00007359 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
7360
7361 // First input must be `true_value` or `false_value` to allow codegens to
7362 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
7363 // that architectures which implement HSelect as a conditional move also
7364 // will not need to invert the condition.
7365 SetRawInputAt(0, false_value);
7366 SetRawInputAt(1, true_value);
7367 SetRawInputAt(2, condition);
7368 }
7369
Artem Serovcced8ba2017-07-19 18:18:09 +01007370 bool IsClonable() const OVERRIDE { return true; }
David Brazdil74eb1b22015-12-14 11:44:01 +00007371 HInstruction* GetFalseValue() const { return InputAt(0); }
7372 HInstruction* GetTrueValue() const { return InputAt(1); }
7373 HInstruction* GetCondition() const { return InputAt(2); }
7374
7375 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01007376 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
7377 return true;
7378 }
David Brazdil74eb1b22015-12-14 11:44:01 +00007379
7380 bool CanBeNull() const OVERRIDE {
7381 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
7382 }
7383
7384 DECLARE_INSTRUCTION(Select);
7385
Artem Serovcced8ba2017-07-19 18:18:09 +01007386 protected:
7387 DEFAULT_COPY_CONSTRUCTOR(Select);
David Brazdil74eb1b22015-12-14 11:44:01 +00007388};
7389
Vladimir Markof9f64412015-09-02 14:05:49 +01007390class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007391 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01007392 MoveOperands(Location source,
7393 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007394 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007395 HInstruction* instruction)
7396 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007397
7398 Location GetSource() const { return source_; }
7399 Location GetDestination() const { return destination_; }
7400
7401 void SetSource(Location value) { source_ = value; }
7402 void SetDestination(Location value) { destination_ = value; }
7403
7404 // The parallel move resolver marks moves as "in-progress" by clearing the
7405 // destination (but not the source).
7406 Location MarkPending() {
7407 DCHECK(!IsPending());
7408 Location dest = destination_;
7409 destination_ = Location::NoLocation();
7410 return dest;
7411 }
7412
7413 void ClearPending(Location dest) {
7414 DCHECK(IsPending());
7415 destination_ = dest;
7416 }
7417
7418 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00007419 DCHECK(source_.IsValid() || destination_.IsInvalid());
7420 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007421 }
7422
7423 // True if this blocks a move from the given location.
7424 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08007425 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007426 }
7427
7428 // A move is redundant if it's been eliminated, if its source and
7429 // destination are the same, or if its destination is unneeded.
7430 bool IsRedundant() const {
7431 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
7432 }
7433
7434 // We clear both operands to indicate move that's been eliminated.
7435 void Eliminate() {
7436 source_ = destination_ = Location::NoLocation();
7437 }
7438
7439 bool IsEliminated() const {
7440 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
7441 return source_.IsInvalid();
7442 }
7443
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007444 DataType::Type GetType() const { return type_; }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007445
Nicolas Geoffray90218252015-04-15 11:56:51 +01007446 bool Is64BitMove() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007447 return DataType::Is64BitType(type_);
Nicolas Geoffray90218252015-04-15 11:56:51 +01007448 }
7449
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007450 HInstruction* GetInstruction() const { return instruction_; }
7451
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007452 private:
7453 Location source_;
7454 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01007455 // The type this move is for.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007456 DataType::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007457 // The instruction this move is assocatied with. Null when this move is
7458 // for moving an input in the expected locations of user (including a phi user).
7459 // This is only used in debug mode, to ensure we do not connect interval siblings
7460 // in the same parallel move.
7461 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007462};
7463
Roland Levillainc9285912015-12-18 10:38:42 +00007464std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
7465
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007466static constexpr size_t kDefaultNumberOfMoves = 4;
7467
Vladimir Markobd785672018-05-03 17:09:09 +01007468class HParallelMove FINAL : public HExpression<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007469 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007470 explicit HParallelMove(ArenaAllocator* allocator, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007471 : HExpression(kParallelMove, SideEffects::None(), dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007472 moves_(allocator->Adapter(kArenaAllocMoveOperands)) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007473 moves_.reserve(kDefaultNumberOfMoves);
7474 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007475
Nicolas Geoffray90218252015-04-15 11:56:51 +01007476 void AddMove(Location source,
7477 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007478 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007479 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00007480 DCHECK(source.IsValid());
7481 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007482 if (kIsDebugBuild) {
7483 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007484 for (const MoveOperands& move : moves_) {
7485 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007486 // Special case the situation where the move is for the spill slot
7487 // of the instruction.
7488 if ((GetPrevious() == instruction)
7489 || ((GetPrevious() == nullptr)
7490 && instruction->IsPhi()
7491 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007492 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007493 << "Doing parallel moves for the same instruction.";
7494 } else {
7495 DCHECK(false) << "Doing parallel moves for the same instruction.";
7496 }
7497 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00007498 }
7499 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007500 for (const MoveOperands& move : moves_) {
7501 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007502 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01007503 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007504 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007505 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007506 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007507 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007508 }
7509
Vladimir Marko225b6462015-09-28 12:17:40 +01007510 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007511 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007512 }
7513
Vladimir Marko225b6462015-09-28 12:17:40 +01007514 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007515
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01007516 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007517
Artem Serovcced8ba2017-07-19 18:18:09 +01007518 protected:
7519 DEFAULT_COPY_CONSTRUCTOR(ParallelMove);
7520
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007521 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01007522 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007523};
7524
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007525// This instruction computes an intermediate address pointing in the 'middle' of an object. The
7526// result pointer cannot be handled by GC, so extra care is taken to make sure that this value is
7527// never used across anything that can trigger GC.
7528// The result of this instruction is not a pointer in the sense of `DataType::Type::kreference`.
7529// So we represent it by the type `DataType::Type::kInt`.
7530class HIntermediateAddress FINAL : public HExpression<2> {
7531 public:
7532 HIntermediateAddress(HInstruction* base_address, HInstruction* offset, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307533 : HExpression(kIntermediateAddress,
7534 DataType::Type::kInt32,
7535 SideEffects::DependsOnGC(),
7536 dex_pc) {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007537 DCHECK_EQ(DataType::Size(DataType::Type::kInt32),
7538 DataType::Size(DataType::Type::kReference))
7539 << "kPrimInt and kPrimNot have different sizes.";
7540 SetRawInputAt(0, base_address);
7541 SetRawInputAt(1, offset);
7542 }
7543
Artem Serovcced8ba2017-07-19 18:18:09 +01007544 bool IsClonable() const OVERRIDE { return true; }
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007545 bool CanBeMoved() const OVERRIDE { return true; }
7546 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
7547 return true;
7548 }
7549 bool IsActualObject() const OVERRIDE { return false; }
7550
7551 HInstruction* GetBaseAddress() const { return InputAt(0); }
7552 HInstruction* GetOffset() const { return InputAt(1); }
7553
7554 DECLARE_INSTRUCTION(IntermediateAddress);
7555
Artem Serovcced8ba2017-07-19 18:18:09 +01007556 protected:
7557 DEFAULT_COPY_CONSTRUCTOR(IntermediateAddress);
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007558};
7559
7560
Mark Mendell0616ae02015-04-17 12:49:27 -04007561} // namespace art
7562
Aart Bikf8f5a162017-02-06 15:35:29 -08007563#include "nodes_vector.h"
7564
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007565#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
7566#include "nodes_shared.h"
7567#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007568#ifdef ART_ENABLE_CODEGEN_mips
7569#include "nodes_mips.h"
7570#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04007571#ifdef ART_ENABLE_CODEGEN_x86
7572#include "nodes_x86.h"
7573#endif
7574
7575namespace art {
7576
Igor Murashkin6ef45672017-08-08 13:59:55 -07007577class OptimizingCompilerStats;
7578
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007579class HGraphVisitor : public ValueObject {
7580 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007581 explicit HGraphVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7582 : stats_(stats),
7583 graph_(graph) {}
Dave Allison20dfc792014-06-16 20:44:29 -07007584 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007585
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007586 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007587 virtual void VisitBasicBlock(HBasicBlock* block);
7588
Roland Levillain633021e2014-10-01 14:12:25 +01007589 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007590 void VisitInsertionOrder();
7591
Roland Levillain633021e2014-10-01 14:12:25 +01007592 // Visit the graph following dominator tree reverse post-order.
7593 void VisitReversePostOrder();
7594
Nicolas Geoffray787c3072014-03-17 10:20:19 +00007595 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007596
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007597 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007598#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007599 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
7600
7601 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7602
7603#undef DECLARE_VISIT_INSTRUCTION
7604
Igor Murashkin6ef45672017-08-08 13:59:55 -07007605 protected:
7606 OptimizingCompilerStats* stats_;
7607
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007608 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07007609 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007610
7611 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
7612};
7613
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007614class HGraphDelegateVisitor : public HGraphVisitor {
7615 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007616 explicit HGraphDelegateVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7617 : HGraphVisitor(graph, stats) {}
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007618 virtual ~HGraphDelegateVisitor() {}
7619
7620 // Visit functions that delegate to to super class.
7621#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00007622 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007623
7624 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7625
7626#undef DECLARE_VISIT_INSTRUCTION
7627
7628 private:
7629 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
7630};
7631
Artem Serovcced8ba2017-07-19 18:18:09 +01007632// Create a clone of the instruction, insert it into the graph; replace the old one with a new
7633// and remove the old instruction.
7634HInstruction* ReplaceInstrOrPhiByClone(HInstruction* instr);
7635
7636// Create a clone for each clonable instructions/phis and replace the original with the clone.
7637//
7638// Used for testing individual instruction cloner.
7639class CloneAndReplaceInstructionVisitor : public HGraphDelegateVisitor {
7640 public:
7641 explicit CloneAndReplaceInstructionVisitor(HGraph* graph)
Artem Serov7f4aff62017-06-21 17:02:18 +01007642 : HGraphDelegateVisitor(graph), instr_replaced_by_clones_count_(0) {}
Artem Serovcced8ba2017-07-19 18:18:09 +01007643
7644 void VisitInstruction(HInstruction* instruction) OVERRIDE {
7645 if (instruction->IsClonable()) {
7646 ReplaceInstrOrPhiByClone(instruction);
Artem Serov7f4aff62017-06-21 17:02:18 +01007647 instr_replaced_by_clones_count_++;
Artem Serovcced8ba2017-07-19 18:18:09 +01007648 }
7649 }
7650
Artem Serov7f4aff62017-06-21 17:02:18 +01007651 size_t GetInstrReplacedByClonesCount() const { return instr_replaced_by_clones_count_; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007652
7653 private:
Artem Serov7f4aff62017-06-21 17:02:18 +01007654 size_t instr_replaced_by_clones_count_;
Artem Serovcced8ba2017-07-19 18:18:09 +01007655
7656 DISALLOW_COPY_AND_ASSIGN(CloneAndReplaceInstructionVisitor);
7657};
7658
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007659// Iterator over the blocks that art part of the loop. Includes blocks part
7660// of an inner loop. The order in which the blocks are iterated is on their
7661// block id.
7662class HBlocksInLoopIterator : public ValueObject {
7663 public:
7664 explicit HBlocksInLoopIterator(const HLoopInformation& info)
7665 : blocks_in_loop_(info.GetBlocks()),
7666 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
7667 index_(0) {
7668 if (!blocks_in_loop_.IsBitSet(index_)) {
7669 Advance();
7670 }
7671 }
7672
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007673 bool Done() const { return index_ == blocks_.size(); }
7674 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007675 void Advance() {
7676 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007677 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007678 if (blocks_in_loop_.IsBitSet(index_)) {
7679 break;
7680 }
7681 }
7682 }
7683
7684 private:
7685 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007686 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007687 size_t index_;
7688
7689 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
7690};
7691
Mingyao Yang3584bce2015-05-19 16:01:59 -07007692// Iterator over the blocks that art part of the loop. Includes blocks part
7693// of an inner loop. The order in which the blocks are iterated is reverse
7694// post order.
7695class HBlocksInLoopReversePostOrderIterator : public ValueObject {
7696 public:
7697 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
7698 : blocks_in_loop_(info.GetBlocks()),
7699 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
7700 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007701 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007702 Advance();
7703 }
7704 }
7705
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007706 bool Done() const { return index_ == blocks_.size(); }
7707 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07007708 void Advance() {
7709 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007710 for (size_t e = blocks_.size(); index_ < e; ++index_) {
7711 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007712 break;
7713 }
7714 }
7715 }
7716
7717 private:
7718 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007719 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07007720 size_t index_;
7721
7722 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
7723};
7724
Aart Bikf3e61ee2017-04-12 17:09:20 -07007725// Returns int64_t value of a properly typed constant.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007726inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00007727 if (constant->IsIntConstant()) {
7728 return constant->AsIntConstant()->GetValue();
7729 } else if (constant->IsLongConstant()) {
7730 return constant->AsLongConstant()->GetValue();
7731 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00007732 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00007733 return 0;
7734 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007735}
7736
Aart Bikf3e61ee2017-04-12 17:09:20 -07007737// Returns true iff instruction is an integral constant (and sets value on success).
7738inline bool IsInt64AndGet(HInstruction* instruction, /*out*/ int64_t* value) {
7739 if (instruction->IsIntConstant()) {
7740 *value = instruction->AsIntConstant()->GetValue();
7741 return true;
7742 } else if (instruction->IsLongConstant()) {
7743 *value = instruction->AsLongConstant()->GetValue();
7744 return true;
7745 } else if (instruction->IsNullConstant()) {
7746 *value = 0;
7747 return true;
7748 }
7749 return false;
7750}
7751
Aart Bik0148de42017-09-05 09:25:01 -07007752// Returns true iff instruction is the given integral constant.
7753inline bool IsInt64Value(HInstruction* instruction, int64_t value) {
7754 int64_t val = 0;
7755 return IsInt64AndGet(instruction, &val) && val == value;
7756}
7757
7758// Returns true iff instruction is a zero bit pattern.
7759inline bool IsZeroBitPattern(HInstruction* instruction) {
7760 return instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern();
7761}
7762
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007763#define INSTRUCTION_TYPE_CHECK(type, super) \
7764 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
7765 inline const H##type* HInstruction::As##type() const { \
7766 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
7767 } \
7768 inline H##type* HInstruction::As##type() { \
7769 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
7770 }
7771
7772 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
7773#undef INSTRUCTION_TYPE_CHECK
7774
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00007775// Create space in `blocks` for adding `number_of_new_blocks` entries
7776// starting at location `at`. Blocks after `at` are moved accordingly.
7777inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
7778 size_t number_of_new_blocks,
7779 size_t after) {
7780 DCHECK_LT(after, blocks->size());
7781 size_t old_size = blocks->size();
7782 size_t new_size = old_size + number_of_new_blocks;
7783 blocks->resize(new_size);
7784 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
7785}
7786
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00007787/*
7788 * Hunt "under the hood" of array lengths (leading to array references),
7789 * null checks (also leading to array references), and new arrays
7790 * (leading to the actual length). This makes it more likely related
7791 * instructions become actually comparable.
7792 */
7793inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
7794 while (instruction->IsArrayLength() ||
7795 instruction->IsNullCheck() ||
7796 instruction->IsNewArray()) {
7797 instruction = instruction->IsNewArray()
7798 ? instruction->AsNewArray()->GetLength()
7799 : instruction->InputAt(0);
7800 }
7801 return instruction;
7802}
7803
Artem Serov21c7e6f2017-07-27 16:04:42 +01007804void RemoveEnvironmentUses(HInstruction* instruction);
7805bool HasEnvironmentUsedByOthers(HInstruction* instruction);
7806void ResetEnvironmentInputRecords(HInstruction* instruction);
7807
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007808} // namespace art
7809
7810#endif // ART_COMPILER_OPTIMIZING_NODES_H_