blob: b315c81693acfd70bb4364d65f034c0bb7cae055 [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
David Brazdila4b8c212015-05-07 09:59:30 +01001275 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001276 bool Dominates(HBasicBlock* block) const;
1277
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001278 size_t GetLifetimeStart() const { return lifetime_start_; }
1279 size_t GetLifetimeEnd() const { return lifetime_end_; }
1280
1281 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1282 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1283
David Brazdil8d5b8b22015-03-24 10:51:52 +00001284 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001285 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001286 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001287 bool HasSinglePhi() const;
1288
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001289 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001290 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001291 ArenaVector<HBasicBlock*> predecessors_;
1292 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001293 HInstructionList instructions_;
1294 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001295 HLoopInformation* loop_information_;
1296 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001297 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001298 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001299 // The dex program counter of the first instruction of this block.
1300 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001301 size_t lifetime_start_;
1302 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001303 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001304
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001305 friend class HGraph;
1306 friend class HInstruction;
1307
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001308 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1309};
1310
David Brazdilb2bd1c52015-03-25 11:17:37 +00001311// Iterates over the LoopInformation of all loops which contain 'block'
1312// from the innermost to the outermost.
1313class HLoopInformationOutwardIterator : public ValueObject {
1314 public:
1315 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1316 : current_(block.GetLoopInformation()) {}
1317
1318 bool Done() const { return current_ == nullptr; }
1319
1320 void Advance() {
1321 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001322 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001323 }
1324
1325 HLoopInformation* Current() const {
1326 DCHECK(!Done());
1327 return current_;
1328 }
1329
1330 private:
1331 HLoopInformation* current_;
1332
1333 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1334};
1335
Alexandre Ramesef20f712015-06-09 10:29:30 +01001336#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001337 M(Above, Condition) \
1338 M(AboveOrEqual, Condition) \
Aart Bik3dad3412018-02-28 12:01:46 -08001339 M(Abs, UnaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001340 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001341 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001342 M(ArrayGet, Instruction) \
1343 M(ArrayLength, Instruction) \
1344 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001345 M(Below, Condition) \
1346 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001347 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001348 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001349 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001350 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001351 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001352 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001353 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001354 M(Compare, BinaryOperation) \
Igor Murashkind01745e2017-04-05 16:40:31 -07001355 M(ConstructorFence, Instruction) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001356 M(CurrentMethod, Instruction) \
Mingyao Yang063fc772016-08-02 11:02:54 -07001357 M(ShouldDeoptimizeFlag, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001358 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001359 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001360 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001361 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001362 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001363 M(Exit, Instruction) \
1364 M(FloatConstant, Constant) \
1365 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001366 M(GreaterThan, Condition) \
1367 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001368 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001369 M(InstanceFieldGet, Instruction) \
1370 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001371 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001372 M(IntConstant, Constant) \
xueliang.zhonge0eb4832017-10-30 13:43:14 +00001373 M(IntermediateAddress, Instruction) \
Calin Juravle175dc732015-08-25 15:42:32 +01001374 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001375 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001376 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001377 M(InvokeVirtual, Invoke) \
Orion Hodsonac141392017-01-13 11:53:47 +00001378 M(InvokePolymorphic, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001379 M(LessThan, Condition) \
1380 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001381 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001382 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001383 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001384 M(LongConstant, Constant) \
Aart Bik1f8d51b2018-02-15 10:42:37 -08001385 M(Max, Instruction) \
Calin Juravle27df7582015-04-17 19:12:31 +01001386 M(MemoryBarrier, Instruction) \
Aart Bik1f8d51b2018-02-15 10:42:37 -08001387 M(Min, BinaryOperation) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001388 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001389 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001390 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001391 M(Neg, UnaryOperation) \
1392 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001393 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001394 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001395 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001396 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001397 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001398 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001399 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001400 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001401 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001402 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001403 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001404 M(Return, Instruction) \
1405 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001406 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001407 M(Shl, BinaryOperation) \
1408 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001409 M(StaticFieldGet, Instruction) \
1410 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001411 M(UnresolvedInstanceFieldGet, Instruction) \
1412 M(UnresolvedInstanceFieldSet, Instruction) \
1413 M(UnresolvedStaticFieldGet, Instruction) \
1414 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001415 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001416 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001417 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001418 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001419 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001420 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001421 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001422 M(Xor, BinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001423 M(VecReplicateScalar, VecUnaryOperation) \
Aart Bik0148de42017-09-05 09:25:01 -07001424 M(VecExtractScalar, VecUnaryOperation) \
1425 M(VecReduce, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001426 M(VecCnv, VecUnaryOperation) \
1427 M(VecNeg, VecUnaryOperation) \
Aart Bik6daebeb2017-04-03 14:35:41 -07001428 M(VecAbs, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001429 M(VecNot, VecUnaryOperation) \
1430 M(VecAdd, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001431 M(VecHalvingAdd, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001432 M(VecSub, VecBinaryOperation) \
1433 M(VecMul, VecBinaryOperation) \
1434 M(VecDiv, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001435 M(VecMin, VecBinaryOperation) \
1436 M(VecMax, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001437 M(VecAnd, VecBinaryOperation) \
1438 M(VecAndNot, VecBinaryOperation) \
1439 M(VecOr, VecBinaryOperation) \
1440 M(VecXor, VecBinaryOperation) \
Aart Bik29aa0822018-03-08 11:28:00 -08001441 M(VecSaturationAdd, VecBinaryOperation) \
1442 M(VecSaturationSub, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001443 M(VecShl, VecBinaryOperation) \
1444 M(VecShr, VecBinaryOperation) \
1445 M(VecUShr, VecBinaryOperation) \
Aart Bik8de59162017-04-21 09:42:01 -07001446 M(VecSetScalars, VecOperation) \
Artem Serovf34dd202017-04-10 17:41:46 +01001447 M(VecMultiplyAccumulate, VecOperation) \
Aart Bikdbbac8f2017-09-01 13:06:08 -07001448 M(VecSADAccumulate, VecOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001449 M(VecLoad, VecMemoryOperation) \
1450 M(VecStore, VecMemoryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001451
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001452/*
1453 * Instructions, shared across several (not all) architectures.
1454 */
1455#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1456#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1457#else
1458#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001459 M(BitwiseNegatedRight, Instruction) \
Anton Kirilov74234da2017-01-13 14:42:47 +00001460 M(DataProcWithShifterOp, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001461 M(MultiplyAccumulate, Instruction) \
Artem Serove1811ed2017-04-27 16:50:47 +01001462 M(IntermediateAddressIndex, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001463#endif
1464
Alexandre Ramesef20f712015-06-09 10:29:30 +01001465#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1466
1467#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1468
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001469#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001470#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001471#else
1472#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1473 M(MipsComputeBaseMethodAddress, Instruction) \
Lena Djokica2901602017-09-21 13:50:52 +02001474 M(MipsPackedSwitch, Instruction) \
1475 M(IntermediateArrayAddressIndex, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001476#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001477
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001478#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1479
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001480#ifndef ART_ENABLE_CODEGEN_x86
1481#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1482#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001483#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1484 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001485 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001486 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001487 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001488#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001489
1490#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1491
1492#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1493 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001494 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001495 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1496 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001497 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001498 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001499 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1500 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1501
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001502#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1503 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001504 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001505 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001506 M(BinaryOperation, Instruction) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001507 M(Invoke, Instruction) \
1508 M(VecOperation, Instruction) \
1509 M(VecUnaryOperation, VecOperation) \
1510 M(VecBinaryOperation, VecOperation) \
1511 M(VecMemoryOperation, VecOperation)
Dave Allison20dfc792014-06-16 20:44:29 -07001512
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001513#define FOR_EACH_INSTRUCTION(M) \
1514 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1515 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1516
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001517#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001518FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1519#undef FORWARD_DECLARATION
1520
Vladimir Marko372f10e2016-05-17 16:30:10 +01001521#define DECLARE_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001522 private: \
1523 H##type& operator=(const H##type&) = delete; \
1524 public: \
Vladimir Marko372f10e2016-05-17 16:30:10 +01001525 const char* DebugName() const OVERRIDE { return #type; } \
1526 bool InstructionTypeEquals(const HInstruction* other) const OVERRIDE { \
1527 return other->Is##type(); \
1528 } \
Artem Serovcced8ba2017-07-19 18:18:09 +01001529 HInstruction* Clone(ArenaAllocator* arena) const OVERRIDE { \
1530 DCHECK(IsClonable()); \
1531 return new (arena) H##type(*this->As##type()); \
1532 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001533 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001534
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001535#define DECLARE_ABSTRACT_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001536 private: \
1537 H##type& operator=(const H##type&) = delete; \
1538 public: \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001539 bool Is##type() const { return As##type() != nullptr; } \
1540 const H##type* As##type() const { return this; } \
1541 H##type* As##type() { return this; }
1542
Artem Serovcced8ba2017-07-19 18:18:09 +01001543#define DEFAULT_COPY_CONSTRUCTOR(type) \
1544 explicit H##type(const H##type& other) = default;
1545
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001546template <typename T>
Vladimir Marko82b07402017-03-01 19:02:04 +00001547class HUseListNode : public ArenaObject<kArenaAllocUseListNode>,
1548 public IntrusiveForwardListNode<HUseListNode<T>> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001549 public:
Igor Murashkind01745e2017-04-05 16:40:31 -07001550 // Get the instruction which has this use as one of the inputs.
David Brazdiled596192015-01-23 10:39:45 +00001551 T GetUser() const { return user_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001552 // Get the position of the input record that this use corresponds to.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001553 size_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001554 // Set the position of the input record that this use corresponds to.
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001555 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001556
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001557 private:
David Brazdiled596192015-01-23 10:39:45 +00001558 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001559 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001560
1561 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001562 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001563
Vladimir Marko46817b82016-03-29 12:21:58 +01001564 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001565
1566 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1567};
1568
David Brazdiled596192015-01-23 10:39:45 +00001569template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001570using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001571
David Brazdil1abb4192015-02-17 18:33:36 +00001572// This class is used by HEnvironment and HInstruction classes to record the
1573// instructions they use and pointers to the corresponding HUseListNodes kept
1574// by the used instructions.
1575template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001576class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001577 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001578 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1579 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001580
Vladimir Marko46817b82016-03-29 12:21:58 +01001581 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1582 : HUserRecord(old_record.instruction_, before_use_node) {}
1583 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1584 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001585 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001586 }
1587
1588 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001589 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1590 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001591
1592 private:
1593 // Instruction used by the user.
1594 HInstruction* instruction_;
1595
Vladimir Marko46817b82016-03-29 12:21:58 +01001596 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1597 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001598};
1599
Vladimir Markoe9004912016-06-16 16:50:52 +01001600// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1601// This is used for HInstruction::GetInputs() to return a container wrapper providing
1602// HInstruction* values even though the underlying container has HUserRecord<>s.
1603struct HInputExtractor {
1604 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1605 return record.GetInstruction();
1606 }
1607 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1608 return record.GetInstruction();
1609 }
1610};
1611
1612using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1613using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1614
Aart Bik854a02b2015-07-14 16:07:00 -07001615/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001616 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001617 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001618 * For write/read dependences on fields/arrays, the dependence analysis uses
1619 * type disambiguation (e.g. a float field write cannot modify the value of an
1620 * integer field read) and the access type (e.g. a reference array write cannot
1621 * modify the value of a reference field read [although it may modify the
1622 * reference fetch prior to reading the field, which is represented by its own
1623 * write/read dependence]). The analysis makes conservative points-to
1624 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1625 * the same, and any reference read depends on any reference read without
1626 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001627 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001628 * The internal representation uses 38-bit and is described in the table below.
1629 * The first line indicates the side effect, and for field/array accesses the
1630 * second line indicates the type of the access (in the order of the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001631 * DataType::Type enum).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001632 * The two numbered lines below indicate the bit position in the bitfield (read
1633 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001634 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001635 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1636 * +-------------+---------+---------+--------------+---------+---------+
1637 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1638 * | 3 |333333322|222222221| 1 |111111110|000000000|
1639 * | 7 |654321098|765432109| 8 |765432109|876543210|
1640 *
1641 * Note that, to ease the implementation, 'changes' bits are least significant
1642 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001643 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001644class SideEffects : public ValueObject {
1645 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001646 SideEffects() : flags_(0) {}
1647
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001648 static SideEffects None() {
1649 return SideEffects(0);
1650 }
1651
1652 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001653 return SideEffects(kAllChangeBits | kAllDependOnBits);
1654 }
1655
1656 static SideEffects AllChanges() {
1657 return SideEffects(kAllChangeBits);
1658 }
1659
1660 static SideEffects AllDependencies() {
1661 return SideEffects(kAllDependOnBits);
1662 }
1663
1664 static SideEffects AllExceptGCDependency() {
1665 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1666 }
1667
1668 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001669 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001670 }
1671
Aart Bik34c3ba92015-07-20 14:08:59 -07001672 static SideEffects AllWrites() {
1673 return SideEffects(kAllWrites);
1674 }
1675
1676 static SideEffects AllReads() {
1677 return SideEffects(kAllReads);
1678 }
1679
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001680 static SideEffects FieldWriteOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001681 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001682 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001683 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001684 }
1685
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001686 static SideEffects ArrayWriteOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001687 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001688 }
1689
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001690 static SideEffects FieldReadOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001691 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001692 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001693 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001694 }
1695
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001696 static SideEffects ArrayReadOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001697 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001698 }
1699
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001700 static SideEffects CanTriggerGC() {
1701 return SideEffects(1ULL << kCanTriggerGCBit);
1702 }
1703
1704 static SideEffects DependsOnGC() {
1705 return SideEffects(1ULL << kDependsOnGCBit);
1706 }
1707
Aart Bik854a02b2015-07-14 16:07:00 -07001708 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001709 SideEffects Union(SideEffects other) const {
1710 return SideEffects(flags_ | other.flags_);
1711 }
1712
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001713 SideEffects Exclusion(SideEffects other) const {
1714 return SideEffects(flags_ & ~other.flags_);
1715 }
1716
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001717 void Add(SideEffects other) {
1718 flags_ |= other.flags_;
1719 }
1720
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001721 bool Includes(SideEffects other) const {
1722 return (other.flags_ & flags_) == other.flags_;
1723 }
1724
1725 bool HasSideEffects() const {
1726 return (flags_ & kAllChangeBits);
1727 }
1728
1729 bool HasDependencies() const {
1730 return (flags_ & kAllDependOnBits);
1731 }
1732
1733 // Returns true if there are no side effects or dependencies.
1734 bool DoesNothing() const {
1735 return flags_ == 0;
1736 }
1737
Aart Bik854a02b2015-07-14 16:07:00 -07001738 // Returns true if something is written.
1739 bool DoesAnyWrite() const {
1740 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001741 }
1742
Aart Bik854a02b2015-07-14 16:07:00 -07001743 // Returns true if something is read.
1744 bool DoesAnyRead() const {
1745 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001746 }
1747
Aart Bik854a02b2015-07-14 16:07:00 -07001748 // Returns true if potentially everything is written and read
1749 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001750 bool DoesAllReadWrite() const {
1751 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1752 }
1753
Aart Bik854a02b2015-07-14 16:07:00 -07001754 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001755 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001756 }
1757
Roland Levillain0d5a2812015-11-13 10:07:31 +00001758 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001759 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001760 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1761 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001762 }
1763
1764 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001765 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001766 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001767 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001768 for (int s = kLastBit; s >= 0; s--) {
1769 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1770 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1771 // This is a bit for the GC side effect.
1772 if (current_bit_is_set) {
1773 flags += "GC";
1774 }
Aart Bik854a02b2015-07-14 16:07:00 -07001775 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001776 } else {
1777 // This is a bit for the array/field analysis.
1778 // The underscore character stands for the 'can trigger GC' bit.
1779 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1780 if (current_bit_is_set) {
1781 flags += kDebug[s];
1782 }
1783 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1784 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1785 flags += "|";
1786 }
1787 }
Aart Bik854a02b2015-07-14 16:07:00 -07001788 }
1789 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001790 }
1791
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001792 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001793
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001794 private:
1795 static constexpr int kFieldArrayAnalysisBits = 9;
1796
1797 static constexpr int kFieldWriteOffset = 0;
1798 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1799 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1800 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1801
1802 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1803
1804 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1805 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1806 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1807 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1808
1809 static constexpr int kLastBit = kDependsOnGCBit;
1810 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1811
1812 // Aliases.
1813
1814 static_assert(kChangeBits == kDependOnBits,
1815 "the 'change' bits should match the 'depend on' bits.");
1816
1817 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1818 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1819 static constexpr uint64_t kAllWrites =
1820 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1821 static constexpr uint64_t kAllReads =
1822 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001823
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001824 // Translates type to bit flag. The type must correspond to a Java type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001825 static uint64_t TypeFlag(DataType::Type type, int offset) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001826 int shift;
1827 switch (type) {
1828 case DataType::Type::kReference: shift = 0; break;
1829 case DataType::Type::kBool: shift = 1; break;
1830 case DataType::Type::kInt8: shift = 2; break;
1831 case DataType::Type::kUint16: shift = 3; break;
1832 case DataType::Type::kInt16: shift = 4; break;
1833 case DataType::Type::kInt32: shift = 5; break;
1834 case DataType::Type::kInt64: shift = 6; break;
1835 case DataType::Type::kFloat32: shift = 7; break;
1836 case DataType::Type::kFloat64: shift = 8; break;
1837 default:
1838 LOG(FATAL) << "Unexpected data type " << type;
1839 UNREACHABLE();
1840 }
Aart Bik854a02b2015-07-14 16:07:00 -07001841 DCHECK_LE(kFieldWriteOffset, shift);
1842 DCHECK_LT(shift, kArrayWriteOffset);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001843 return UINT64_C(1) << (shift + offset);
Aart Bik854a02b2015-07-14 16:07:00 -07001844 }
1845
1846 // Private constructor on direct flags value.
1847 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1848
1849 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001850};
1851
David Brazdiled596192015-01-23 10:39:45 +00001852// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001853class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001854 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001855 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
Mingyao Yang01b47b02017-02-03 12:09:57 -08001856 size_t number_of_vregs,
1857 ArtMethod* method,
1858 uint32_t dex_pc,
1859 HInstruction* holder)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001860 : vregs_(number_of_vregs, allocator->Adapter(kArenaAllocEnvironmentVRegs)),
1861 locations_(allocator->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001862 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001863 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001864 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001865 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001866 }
1867
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001868 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
1869 const HEnvironment& to_copy,
1870 HInstruction* holder)
1871 : HEnvironment(allocator,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001872 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001873 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001874 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001875 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001876
Vladimir Markoec32f642017-06-02 10:51:55 +01001877 void AllocateLocations() {
1878 DCHECK(locations_.empty());
1879 locations_.resize(vregs_.size());
1880 }
1881
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001882 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001883 if (parent_ != nullptr) {
1884 parent_->SetAndCopyParentChain(allocator, parent);
1885 } else {
1886 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1887 parent_->CopyFrom(parent);
1888 if (parent->GetParent() != nullptr) {
1889 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1890 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001891 }
David Brazdiled596192015-01-23 10:39:45 +00001892 }
1893
Vladimir Marko69d310e2017-10-09 14:12:23 +01001894 void CopyFrom(ArrayRef<HInstruction* const> locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001895 void CopyFrom(HEnvironment* environment);
1896
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001897 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1898 // input to the loop phi instead. This is for inserting instructions that
1899 // require an environment (like HDeoptimization) in the loop pre-header.
1900 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001901
1902 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001903 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001904 }
1905
1906 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001907 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001908 }
1909
David Brazdil1abb4192015-02-17 18:33:36 +00001910 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001911
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001912 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001913
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001914 HEnvironment* GetParent() const { return parent_; }
1915
1916 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001917 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001918 }
1919
1920 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001921 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001922 }
1923
1924 uint32_t GetDexPc() const {
1925 return dex_pc_;
1926 }
1927
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001928 ArtMethod* GetMethod() const {
1929 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001930 }
1931
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001932 HInstruction* GetHolder() const {
1933 return holder_;
1934 }
1935
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001936
1937 bool IsFromInlinedInvoke() const {
1938 return GetParent() != nullptr;
1939 }
1940
David Brazdiled596192015-01-23 10:39:45 +00001941 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001942 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1943 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001944 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001945 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001946 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00001947
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001948 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001949 HInstruction* const holder_;
1950
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001951 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001952
1953 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1954};
1955
Vladimir Markof9f64412015-09-02 14:05:49 +01001956class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001957 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05301958#define DECLARE_KIND(type, super) k##type,
1959 enum InstructionKind {
1960 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1961 kLastInstructionKind
1962 };
1963#undef DECLARE_KIND
1964
1965 HInstruction(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001966 : previous_(nullptr),
1967 next_(nullptr),
1968 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001969 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001970 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001971 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001972 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001973 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001974 locations_(nullptr),
1975 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001976 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001977 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001978 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05301979 SetPackedField<InstructionKindField>(kind);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001980 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1981 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001982
Dave Allison20dfc792014-06-16 20:44:29 -07001983 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001984
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001985
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001986 HInstruction* GetNext() const { return next_; }
1987 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001988
Calin Juravle77520bc2015-01-12 18:45:46 +00001989 HInstruction* GetNextDisregardingMoves() const;
1990 HInstruction* GetPreviousDisregardingMoves() const;
1991
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001992 HBasicBlock* GetBlock() const { return block_; }
Vladimir Markoca6fff82017-10-03 14:49:14 +01001993 ArenaAllocator* GetAllocator() const { return block_->GetGraph()->GetAllocator(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001994 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001995 bool IsInBlock() const { return block_ != nullptr; }
1996 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001997 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1998 bool IsIrreducibleLoopHeaderPhi() const {
1999 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
2000 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002001
Vladimir Marko372f10e2016-05-17 16:30:10 +01002002 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
2003
2004 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
2005 // One virtual method is enough, just const_cast<> and then re-add the const.
2006 return ArrayRef<const HUserRecord<HInstruction*>>(
2007 const_cast<HInstruction*>(this)->GetInputRecords());
2008 }
2009
Vladimir Markoe9004912016-06-16 16:50:52 +01002010 HInputsRef GetInputs() {
2011 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002012 }
2013
Vladimir Markoe9004912016-06-16 16:50:52 +01002014 HConstInputsRef GetInputs() const {
2015 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002016 }
2017
2018 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00002019 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002020
Aart Bik2767f4b2016-10-28 15:03:53 -07002021 bool HasInput(HInstruction* input) const {
2022 for (const HInstruction* i : GetInputs()) {
2023 if (i == input) {
2024 return true;
2025 }
2026 }
2027 return false;
2028 }
2029
Vladimir Marko372f10e2016-05-17 16:30:10 +01002030 void SetRawInputAt(size_t index, HInstruction* input) {
2031 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
2032 }
2033
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002034 virtual void Accept(HGraphVisitor* visitor) = 0;
2035 virtual const char* DebugName() const = 0;
2036
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002037 virtual DataType::Type GetType() const { return DataType::Type::kVoid; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002038
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002039 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002040
2041 uint32_t GetDexPc() const { return dex_pc_; }
2042
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002043 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01002044
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002045 // Can the instruction throw?
2046 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
2047 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01002048 virtual bool CanThrow() const { return false; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08002049
2050 // Does the instruction always throw an exception unconditionally?
2051 virtual bool AlwaysThrows() const { return false; }
2052
David Brazdilec16f792015-08-19 15:04:01 +01002053 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002054
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002055 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002056 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002057
Calin Juravle10e244f2015-01-26 18:54:32 +00002058 // Does not apply for all instructions, but having this at top level greatly
2059 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00002060 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002061 virtual bool CanBeNull() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002062 DCHECK_EQ(GetType(), DataType::Type::kReference) << "CanBeNull only applies to reference types";
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002063 return true;
2064 }
Calin Juravle10e244f2015-01-26 18:54:32 +00002065
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002066 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01002067 return false;
2068 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002069
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002070 virtual bool IsActualObject() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002071 return GetType() == DataType::Type::kReference;
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002072 }
2073
Calin Juravle2e768302015-07-28 14:41:11 +00002074 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00002075
Calin Juravle61d544b2015-02-23 16:46:57 +00002076 ReferenceTypeInfo GetReferenceTypeInfo() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002077 DCHECK_EQ(GetType(), DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002078 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00002079 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00002080 }
Calin Juravleacf735c2015-02-12 15:25:22 +00002081
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002082 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00002083 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002084 // Note: fixup_end remains valid across push_front().
2085 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
2086 HUseListNode<HInstruction*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002087 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HInstruction*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002088 uses_.push_front(*new_node);
2089 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002090 }
2091
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002092 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01002093 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002094 // Note: env_fixup_end remains valid across push_front().
2095 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
2096 HUseListNode<HEnvironment*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002097 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HEnvironment*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002098 env_uses_.push_front(*new_node);
2099 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002100 }
2101
David Brazdil1abb4192015-02-17 18:33:36 +00002102 void RemoveAsUserOfInput(size_t input) {
2103 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002104 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2105 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2106 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00002107 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002108
Vladimir Marko372f10e2016-05-17 16:30:10 +01002109 void RemoveAsUserOfAllInputs() {
2110 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2111 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2112 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2113 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
2114 }
2115 }
2116
David Brazdil1abb4192015-02-17 18:33:36 +00002117 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2118 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002119
Vladimir Marko46817b82016-03-29 12:21:58 +01002120 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2121 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2122 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002123 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002124 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002125 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002126
Aart Bikcc42be02016-10-20 16:14:16 -07002127 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002128 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002129 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002130 !CanThrow() &&
2131 !IsSuspendCheck() &&
2132 !IsControlFlow() &&
Aart Bik240384b2018-04-17 14:56:29 -07002133 !IsDeoptimize() &&
Aart Bik482095d2016-10-10 15:39:10 -07002134 !IsNativeDebugInfo() &&
2135 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002136 // If we added an explicit barrier then we should keep it.
Igor Murashkind01745e2017-04-05 16:40:31 -07002137 !IsMemoryBarrier() &&
2138 !IsConstructorFence();
Aart Bik482095d2016-10-10 15:39:10 -07002139 }
2140
Aart Bikcc42be02016-10-20 16:14:16 -07002141 bool IsDeadAndRemovable() const {
2142 return IsRemovable() && !HasUses();
2143 }
2144
Roland Levillain6c82d402014-10-13 16:10:27 +01002145 // Does this instruction strictly dominate `other_instruction`?
2146 // Returns false if this instruction and `other_instruction` are the same.
2147 // Aborts if this instruction and `other_instruction` are both phis.
2148 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002149
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002150 int GetId() const { return id_; }
2151 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002152
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002153 int GetSsaIndex() const { return ssa_index_; }
2154 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2155 bool HasSsaIndex() const { return ssa_index_ != -1; }
2156
2157 bool HasEnvironment() const { return environment_ != nullptr; }
2158 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002159 // Set the `environment_` field. Raw because this method does not
2160 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002161 void SetRawEnvironment(HEnvironment* environment) {
2162 DCHECK(environment_ == nullptr);
2163 DCHECK_EQ(environment->GetHolder(), this);
2164 environment_ = environment;
2165 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002166
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002167 void InsertRawEnvironment(HEnvironment* environment) {
2168 DCHECK(environment_ != nullptr);
2169 DCHECK_EQ(environment->GetHolder(), this);
2170 DCHECK(environment->GetParent() == nullptr);
2171 environment->parent_ = environment_;
2172 environment_ = environment;
2173 }
2174
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002175 void RemoveEnvironment();
2176
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002177 // Set the environment of this instruction, copying it from `environment`. While
2178 // copying, the uses lists are being updated.
2179 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002180 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002181 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002182 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002183 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002184 if (environment->GetParent() != nullptr) {
2185 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2186 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002187 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002188
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002189 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2190 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002191 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002192 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002193 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002194 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002195 if (environment->GetParent() != nullptr) {
2196 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2197 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002198 }
2199
Nicolas Geoffray39468442014-09-02 15:17:15 +01002200 // Returns the number of entries in the environment. Typically, that is the
2201 // number of dex registers in a method. It could be more in case of inlining.
2202 size_t EnvironmentSize() const;
2203
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002204 LocationSummary* GetLocations() const { return locations_; }
2205 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002206
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002207 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002208 void ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002209 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002210
Alexandre Rames188d4312015-04-09 18:30:21 +01002211 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2212 // uses of this instruction by `other` are *not* updated.
2213 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2214 ReplaceWith(other);
2215 other->ReplaceInput(this, use_index);
2216 }
2217
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002218 // Move `this` instruction before `cursor`
2219 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002220
Vladimir Markofb337ea2015-11-25 15:25:10 +00002221 // Move `this` before its first user and out of any loops. If there is no
2222 // out-of-loop user that dominates all other users, move the instruction
2223 // to the end of the out-of-loop common dominator of the user's blocks.
2224 //
2225 // This can be used only on non-throwing instructions with no side effects that
2226 // have at least one use but no environment uses.
2227 void MoveBeforeFirstUserAndOutOfLoops();
2228
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002229#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002230 bool Is##type() const; \
2231 const H##type* As##type() const; \
2232 H##type* As##type();
2233
2234 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2235#undef INSTRUCTION_TYPE_CHECK
2236
2237#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002238 bool Is##type() const { return (As##type() != nullptr); } \
2239 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002240 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002241 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002242#undef INSTRUCTION_TYPE_CHECK
2243
Artem Serovcced8ba2017-07-19 18:18:09 +01002244 // Return a clone of the instruction if it is clonable (shallow copy by default, custom copy
2245 // if a custom copy-constructor is provided for a particular type). If IsClonable() is false for
2246 // the instruction then the behaviour of this function is undefined.
2247 //
2248 // Note: It is semantically valid to create a clone of the instruction only until
2249 // prepare_for_register_allocator phase as lifetime, intervals and codegen info are not
2250 // copied.
2251 //
2252 // Note: HEnvironment and some other fields are not copied and are set to default values, see
2253 // 'explicit HInstruction(const HInstruction& other)' for details.
2254 virtual HInstruction* Clone(ArenaAllocator* arena ATTRIBUTE_UNUSED) const {
2255 LOG(FATAL) << "Cloning is not implemented for the instruction " <<
2256 DebugName() << " " << GetId();
2257 UNREACHABLE();
2258 }
2259
2260 // Return whether instruction can be cloned (copied).
2261 virtual bool IsClonable() const { return false; }
2262
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002263 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002264 // TODO: this method is used by LICM and GVN with possibly different
2265 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002266 virtual bool CanBeMoved() const { return false; }
2267
2268 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002269 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002270 return false;
2271 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002272
2273 // Returns whether any data encoded in the two instructions is equal.
2274 // This method does not look at the inputs. Both instructions must be
2275 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002276 virtual bool InstructionDataEquals(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 two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002281 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002282 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002283 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002284
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002285 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2286 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2287 // the virtual function because the __attribute__((__pure__)) doesn't really
2288 // apply the strong requirement for virtual functions, preventing optimizations.
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302289 InstructionKind GetKind() const { return GetPackedField<InstructionKindField>(); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002290
2291 virtual size_t ComputeHashCode() const {
2292 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002293 for (const HInstruction* input : GetInputs()) {
2294 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002295 }
2296 return result;
2297 }
2298
2299 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002300 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002301 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002302
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002303 size_t GetLifetimePosition() const { return lifetime_position_; }
2304 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2305 LiveInterval* GetLiveInterval() const { return live_interval_; }
2306 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2307 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2308
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002309 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2310
2311 // Returns whether the code generation of the instruction will require to have access
2312 // to the current method. Such instructions are:
2313 // (1): Instructions that require an environment, as calling the runtime requires
2314 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002315 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2316 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002317 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002318 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002319 }
2320
Vladimir Markodc151b22015-10-15 18:02:30 +01002321 // Returns whether the code generation of the instruction will require to have access
2322 // to the dex cache of the current method's declaring class via the current method.
2323 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002324
Mark Mendellc4701932015-04-10 13:18:51 -04002325 // Does this instruction have any use in an environment before
2326 // control flow hits 'other'?
2327 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2328
2329 // Remove all references to environment uses of this instruction.
2330 // The caller must ensure that this is safe to do.
2331 void RemoveEnvironmentUsers();
2332
Vladimir Markoa1de9182016-02-25 11:37:38 +00002333 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2334 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002335
David Brazdil1abb4192015-02-17 18:33:36 +00002336 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002337 // If set, the machine code for this instruction is assumed to be generated by
2338 // its users. Used by liveness analysis to compute use positions accordingly.
2339 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2340 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302341 static constexpr size_t kFieldInstructionKind = kFlagReferenceTypeIsExact + 1;
2342 static constexpr size_t kFieldInstructionKindSize =
2343 MinimumBitsToStore(static_cast<size_t>(InstructionKind::kLastInstructionKind - 1));
2344 static constexpr size_t kNumberOfGenericPackedBits =
2345 kFieldInstructionKind + kFieldInstructionKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00002346 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2347
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302348 static_assert(kNumberOfGenericPackedBits <= kMaxNumberOfPackedBits,
2349 "Too many generic packed fields");
2350
Vladimir Marko372f10e2016-05-17 16:30:10 +01002351 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2352 return GetInputRecords()[i];
2353 }
2354
2355 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2356 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2357 input_records[index] = input;
2358 }
David Brazdil1abb4192015-02-17 18:33:36 +00002359
Vladimir Markoa1de9182016-02-25 11:37:38 +00002360 uint32_t GetPackedFields() const {
2361 return packed_fields_;
2362 }
2363
2364 template <size_t flag>
2365 bool GetPackedFlag() const {
2366 return (packed_fields_ & (1u << flag)) != 0u;
2367 }
2368
2369 template <size_t flag>
2370 void SetPackedFlag(bool value = true) {
2371 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2372 }
2373
2374 template <typename BitFieldType>
2375 typename BitFieldType::value_type GetPackedField() const {
2376 return BitFieldType::Decode(packed_fields_);
2377 }
2378
2379 template <typename BitFieldType>
2380 void SetPackedField(typename BitFieldType::value_type value) {
2381 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2382 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2383 }
2384
Artem Serovcced8ba2017-07-19 18:18:09 +01002385 // Copy construction for the instruction (used for Clone function).
2386 //
2387 // Fields (e.g. lifetime, intervals and codegen info) associated with phases starting from
2388 // prepare_for_register_allocator are not copied (set to default values).
2389 //
2390 // Copy constructors must be provided for every HInstruction type; default copy constructor is
2391 // fine for most of them. However for some of the instructions a custom copy constructor must be
2392 // specified (when instruction has non-trivially copyable fields and must have a special behaviour
2393 // for copying them).
2394 explicit HInstruction(const HInstruction& other)
2395 : previous_(nullptr),
2396 next_(nullptr),
2397 block_(nullptr),
2398 dex_pc_(other.dex_pc_),
2399 id_(-1),
2400 ssa_index_(-1),
2401 packed_fields_(other.packed_fields_),
2402 environment_(nullptr),
2403 locations_(nullptr),
2404 live_interval_(nullptr),
2405 lifetime_position_(kNoLifetime),
2406 side_effects_(other.side_effects_),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302407 reference_type_handle_(other.reference_type_handle_) {
2408 }
Artem Serovcced8ba2017-07-19 18:18:09 +01002409
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002410 private:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302411 using InstructionKindField =
2412 BitField<InstructionKind, kFieldInstructionKind, kFieldInstructionKindSize>;
2413
Vladimir Marko46817b82016-03-29 12:21:58 +01002414 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2415 auto before_use_node = uses_.before_begin();
2416 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2417 HInstruction* user = use_node->GetUser();
2418 size_t input_index = use_node->GetIndex();
2419 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2420 before_use_node = use_node;
2421 }
2422 }
2423
2424 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2425 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2426 if (next != uses_.end()) {
2427 HInstruction* next_user = next->GetUser();
2428 size_t next_index = next->GetIndex();
2429 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2430 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2431 }
2432 }
2433
2434 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2435 auto before_env_use_node = env_uses_.before_begin();
2436 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2437 HEnvironment* user = env_use_node->GetUser();
2438 size_t input_index = env_use_node->GetIndex();
2439 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2440 before_env_use_node = env_use_node;
2441 }
2442 }
2443
2444 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2445 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2446 if (next != env_uses_.end()) {
2447 HEnvironment* next_user = next->GetUser();
2448 size_t next_index = next->GetIndex();
2449 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2450 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2451 }
2452 }
David Brazdil1abb4192015-02-17 18:33:36 +00002453
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002454 HInstruction* previous_;
2455 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002456 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002457 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002458
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002459 // An instruction gets an id when it is added to the graph.
2460 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002461 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002462 int id_;
2463
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002464 // When doing liveness analysis, instructions that have uses get an SSA index.
2465 int ssa_index_;
2466
Vladimir Markoa1de9182016-02-25 11:37:38 +00002467 // Packed fields.
2468 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002469
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002470 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002471 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002472
2473 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002474 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002475
Nicolas Geoffray39468442014-09-02 15:17:15 +01002476 // The environment associated with this instruction. Not null if the instruction
2477 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002478 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002479
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002480 // Set by the code generator.
2481 LocationSummary* locations_;
2482
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002483 // Set by the liveness analysis.
2484 LiveInterval* live_interval_;
2485
2486 // Set by the liveness analysis, this is the position in a linear
2487 // order of blocks where this instruction's live interval start.
2488 size_t lifetime_position_;
2489
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002490 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002491
Vladimir Markoa1de9182016-02-25 11:37:38 +00002492 // The reference handle part of the reference type info.
2493 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002494 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002495 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002496
David Brazdil1abb4192015-02-17 18:33:36 +00002497 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002498 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002499 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002500 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002501 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002502};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002503std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002504
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002505// Iterates over the instructions, while preserving the next instruction
2506// in case the current instruction gets removed from the list by the user
2507// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002508class HInstructionIterator : public ValueObject {
2509 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002510 explicit HInstructionIterator(const HInstructionList& instructions)
2511 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002512 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002513 }
2514
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002515 bool Done() const { return instruction_ == nullptr; }
2516 HInstruction* Current() const { return instruction_; }
2517 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002518 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002519 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002520 }
2521
2522 private:
2523 HInstruction* instruction_;
2524 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002525
2526 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002527};
2528
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002529// Iterates over the instructions without saving the next instruction,
2530// therefore handling changes in the graph potentially made by the user
2531// of this iterator.
2532class HInstructionIteratorHandleChanges : public ValueObject {
2533 public:
2534 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2535 : instruction_(instructions.first_instruction_) {
2536 }
2537
2538 bool Done() const { return instruction_ == nullptr; }
2539 HInstruction* Current() const { return instruction_; }
2540 void Advance() {
2541 instruction_ = instruction_->GetNext();
2542 }
2543
2544 private:
2545 HInstruction* instruction_;
2546
2547 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2548};
2549
2550
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002551class HBackwardInstructionIterator : public ValueObject {
2552 public:
2553 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2554 : instruction_(instructions.last_instruction_) {
2555 next_ = Done() ? nullptr : instruction_->GetPrevious();
2556 }
2557
2558 bool Done() const { return instruction_ == nullptr; }
2559 HInstruction* Current() const { return instruction_; }
2560 void Advance() {
2561 instruction_ = next_;
2562 next_ = Done() ? nullptr : instruction_->GetPrevious();
2563 }
2564
2565 private:
2566 HInstruction* instruction_;
2567 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002568
2569 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002570};
2571
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002572class HVariableInputSizeInstruction : public HInstruction {
2573 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002574 using HInstruction::GetInputRecords; // Keep the const version visible.
2575 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
2576 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2577 }
2578
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002579 void AddInput(HInstruction* input);
2580 void InsertInputAt(size_t index, HInstruction* input);
2581 void RemoveInputAt(size_t index);
2582
Igor Murashkind01745e2017-04-05 16:40:31 -07002583 // Removes all the inputs.
2584 // Also removes this instructions from each input's use list
2585 // (for non-environment uses only).
2586 void RemoveAllInputs();
2587
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002588 protected:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302589 HVariableInputSizeInstruction(InstructionKind inst_kind,
2590 SideEffects side_effects,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002591 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002592 ArenaAllocator* allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002593 size_t number_of_inputs,
2594 ArenaAllocKind kind)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302595 : HInstruction(inst_kind, side_effects, dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002596 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002597
Artem Serovcced8ba2017-07-19 18:18:09 +01002598 DEFAULT_COPY_CONSTRUCTOR(VariableInputSizeInstruction);
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002599
Artem Serovcced8ba2017-07-19 18:18:09 +01002600 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002601};
2602
Vladimir Markof9f64412015-09-02 14:05:49 +01002603template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002604class HTemplateInstruction: public HInstruction {
2605 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302606 HTemplateInstruction<N>(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
2607 : HInstruction(kind, side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002608 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002609
Vladimir Marko372f10e2016-05-17 16:30:10 +01002610 using HInstruction::GetInputRecords; // Keep the const version visible.
2611 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2612 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002613 }
2614
Artem Serovcced8ba2017-07-19 18:18:09 +01002615 protected:
2616 DEFAULT_COPY_CONSTRUCTOR(TemplateInstruction<N>);
2617
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002618 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002619 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002620
2621 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002622};
2623
Vladimir Markof9f64412015-09-02 14:05:49 +01002624// HTemplateInstruction specialization for N=0.
2625template<>
2626class HTemplateInstruction<0>: public HInstruction {
2627 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302628 explicit HTemplateInstruction<0>(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
2629 : HInstruction(kind, side_effects, dex_pc) {}
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002630
Vladimir Markof9f64412015-09-02 14:05:49 +01002631 virtual ~HTemplateInstruction() {}
2632
Vladimir Marko372f10e2016-05-17 16:30:10 +01002633 using HInstruction::GetInputRecords; // Keep the const version visible.
2634 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2635 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002636 }
2637
Artem Serovcced8ba2017-07-19 18:18:09 +01002638 protected:
2639 DEFAULT_COPY_CONSTRUCTOR(TemplateInstruction<0>);
2640
Vladimir Markof9f64412015-09-02 14:05:49 +01002641 private:
2642 friend class SsaBuilder;
2643};
2644
Dave Allison20dfc792014-06-16 20:44:29 -07002645template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002646class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002647 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302648 using HInstruction::InstructionKind;
2649 HExpression<N>(InstructionKind kind,
2650 DataType::Type type,
2651 SideEffects side_effects,
2652 uint32_t dex_pc)
2653 : HTemplateInstruction<N>(kind, side_effects, dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00002654 this->template SetPackedField<TypeField>(type);
2655 }
Dave Allison20dfc792014-06-16 20:44:29 -07002656 virtual ~HExpression() {}
2657
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002658 DataType::Type GetType() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00002659 return TypeField::Decode(this->GetPackedFields());
2660 }
Dave Allison20dfc792014-06-16 20:44:29 -07002661
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002662 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002663 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2664 static constexpr size_t kFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002665 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00002666 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2667 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2668 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002669 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
Artem Serovcced8ba2017-07-19 18:18:09 +01002670 DEFAULT_COPY_CONSTRUCTOR(Expression<N>);
Dave Allison20dfc792014-06-16 20:44:29 -07002671};
2672
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002673// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2674// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002675class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002676 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002677 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302678 : HTemplateInstruction(kReturnVoid, SideEffects::None(), dex_pc) {
2679 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002680
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002681 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002682
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002683 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002684
Artem Serovcced8ba2017-07-19 18:18:09 +01002685 protected:
2686 DEFAULT_COPY_CONSTRUCTOR(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002687};
2688
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002689// Represents dex's RETURN opcodes. A HReturn is a control flow
2690// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002691class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002692 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002693 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302694 : HTemplateInstruction(kReturn, SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002695 SetRawInputAt(0, value);
2696 }
2697
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002698 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002699
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002700 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002701
Artem Serovcced8ba2017-07-19 18:18:09 +01002702 protected:
2703 DEFAULT_COPY_CONSTRUCTOR(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002704};
2705
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002706class HPhi FINAL : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002707 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002708 HPhi(ArenaAllocator* allocator,
David Brazdildee58d62016-04-07 09:54:26 +00002709 uint32_t reg_number,
2710 size_t number_of_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002711 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +00002712 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002713 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302714 kPhi,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002715 SideEffects::None(),
2716 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002717 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002718 number_of_inputs,
2719 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002720 reg_number_(reg_number) {
2721 SetPackedField<TypeField>(ToPhiType(type));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002722 DCHECK_NE(GetType(), DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +00002723 // Phis are constructed live and marked dead if conflicting or unused.
2724 // Individual steps of SsaBuilder should assume that if a phi has been
2725 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2726 SetPackedFlag<kFlagIsLive>(true);
2727 SetPackedFlag<kFlagCanBeNull>(true);
2728 }
2729
Artem Serovcced8ba2017-07-19 18:18:09 +01002730 bool IsClonable() const OVERRIDE { return true; }
2731
David Brazdildee58d62016-04-07 09:54:26 +00002732 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002733 static DataType::Type ToPhiType(DataType::Type type) {
2734 return DataType::Kind(type);
David Brazdildee58d62016-04-07 09:54:26 +00002735 }
2736
2737 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2738
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002739 DataType::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2740 void SetType(DataType::Type new_type) {
David Brazdildee58d62016-04-07 09:54:26 +00002741 // Make sure that only valid type changes occur. The following are allowed:
2742 // (1) int -> float/ref (primitive type propagation),
2743 // (2) long -> double (primitive type propagation).
2744 DCHECK(GetType() == new_type ||
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002745 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kFloat32) ||
2746 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kReference) ||
2747 (GetType() == DataType::Type::kInt64 && new_type == DataType::Type::kFloat64));
David Brazdildee58d62016-04-07 09:54:26 +00002748 SetPackedField<TypeField>(new_type);
2749 }
2750
2751 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2752 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2753
2754 uint32_t GetRegNumber() const { return reg_number_; }
2755
2756 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2757 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2758 bool IsDead() const { return !IsLive(); }
2759 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2760
Vladimir Markoe9004912016-06-16 16:50:52 +01002761 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002762 return other != nullptr
2763 && other->IsPhi()
2764 && other->AsPhi()->GetBlock() == GetBlock()
2765 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2766 }
2767
Nicolas Geoffrayf57c1ae2017-06-28 17:40:18 +01002768 bool HasEquivalentPhi() const {
2769 if (GetPrevious() != nullptr && GetPrevious()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2770 return true;
2771 }
2772 if (GetNext() != nullptr && GetNext()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2773 return true;
2774 }
2775 return false;
2776 }
2777
David Brazdildee58d62016-04-07 09:54:26 +00002778 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2779 // An equivalent phi is a phi having the same dex register and type.
2780 // It assumes that phis with the same dex register are adjacent.
2781 HPhi* GetNextEquivalentPhiWithSameType() {
2782 HInstruction* next = GetNext();
2783 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2784 if (next->GetType() == GetType()) {
2785 return next->AsPhi();
2786 }
2787 next = next->GetNext();
2788 }
2789 return nullptr;
2790 }
2791
2792 DECLARE_INSTRUCTION(Phi);
2793
Artem Serovcced8ba2017-07-19 18:18:09 +01002794 protected:
2795 DEFAULT_COPY_CONSTRUCTOR(Phi);
2796
David Brazdildee58d62016-04-07 09:54:26 +00002797 private:
2798 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2799 static constexpr size_t kFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002800 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
David Brazdildee58d62016-04-07 09:54:26 +00002801 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2802 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2803 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2804 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002805 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
David Brazdildee58d62016-04-07 09:54:26 +00002806
David Brazdildee58d62016-04-07 09:54:26 +00002807 const uint32_t reg_number_;
David Brazdildee58d62016-04-07 09:54:26 +00002808};
2809
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002810// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002811// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002812// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002813class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002814 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302815 explicit HExit(uint32_t dex_pc = kNoDexPc)
2816 : HTemplateInstruction(kExit, SideEffects::None(), dex_pc) {
2817 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002818
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002819 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002820
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002821 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002822
Artem Serovcced8ba2017-07-19 18:18:09 +01002823 protected:
2824 DEFAULT_COPY_CONSTRUCTOR(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002825};
2826
2827// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002828class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002829 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302830 explicit HGoto(uint32_t dex_pc = kNoDexPc)
2831 : HTemplateInstruction(kGoto, SideEffects::None(), dex_pc) {
2832 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002833
Artem Serovcced8ba2017-07-19 18:18:09 +01002834 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002835 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002836
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002837 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002838 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002839 }
2840
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002841 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002842
Artem Serovcced8ba2017-07-19 18:18:09 +01002843 protected:
2844 DEFAULT_COPY_CONSTRUCTOR(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002845};
2846
Roland Levillain9867bc72015-08-05 10:21:34 +01002847class HConstant : public HExpression<0> {
2848 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302849 explicit HConstant(InstructionKind kind, DataType::Type type, uint32_t dex_pc = kNoDexPc)
2850 : HExpression(kind, type, SideEffects::None(), dex_pc) {
2851 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002852
2853 bool CanBeMoved() const OVERRIDE { return true; }
2854
Roland Levillain1a653882016-03-18 18:05:57 +00002855 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002856 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002857 // Is this constant 0 in the arithmetic sense?
2858 virtual bool IsArithmeticZero() const { return false; }
2859 // Is this constant a 0-bit pattern?
2860 virtual bool IsZeroBitPattern() const { return false; }
2861 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002862 virtual bool IsOne() const { return false; }
2863
David Brazdil77a48ae2015-09-15 12:34:04 +00002864 virtual uint64_t GetValueAsUint64() const = 0;
2865
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002866 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002867
Artem Serovcced8ba2017-07-19 18:18:09 +01002868 protected:
2869 DEFAULT_COPY_CONSTRUCTOR(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002870};
2871
Vladimir Markofcb503c2016-05-18 12:48:17 +01002872class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002873 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002874 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002875 return true;
2876 }
2877
David Brazdil77a48ae2015-09-15 12:34:04 +00002878 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2879
Roland Levillain9867bc72015-08-05 10:21:34 +01002880 size_t ComputeHashCode() const OVERRIDE { return 0; }
2881
Roland Levillain1a653882016-03-18 18:05:57 +00002882 // The null constant representation is a 0-bit pattern.
2883 virtual bool IsZeroBitPattern() const { return true; }
2884
Roland Levillain9867bc72015-08-05 10:21:34 +01002885 DECLARE_INSTRUCTION(NullConstant);
2886
Artem Serovcced8ba2017-07-19 18:18:09 +01002887 protected:
2888 DEFAULT_COPY_CONSTRUCTOR(NullConstant);
2889
Roland Levillain9867bc72015-08-05 10:21:34 +01002890 private:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002891 explicit HNullConstant(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302892 : HConstant(kNullConstant, DataType::Type::kReference, dex_pc) {
2893 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002894
2895 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01002896};
2897
2898// Constants of the type int. Those can be from Dex instructions, or
2899// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002900class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002901 public:
2902 int32_t GetValue() const { return value_; }
2903
David Brazdil9f389d42015-10-01 14:32:56 +01002904 uint64_t GetValueAsUint64() const OVERRIDE {
2905 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2906 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002907
Vladimir Marko372f10e2016-05-17 16:30:10 +01002908 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002909 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002910 return other->AsIntConstant()->value_ == value_;
2911 }
2912
2913 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2914
2915 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002916 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2917 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002918 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2919
Roland Levillain1a653882016-03-18 18:05:57 +00002920 // Integer constants are used to encode Boolean values as well,
2921 // where 1 means true and 0 means false.
2922 bool IsTrue() const { return GetValue() == 1; }
2923 bool IsFalse() const { return GetValue() == 0; }
2924
Roland Levillain9867bc72015-08-05 10:21:34 +01002925 DECLARE_INSTRUCTION(IntConstant);
2926
Artem Serovcced8ba2017-07-19 18:18:09 +01002927 protected:
2928 DEFAULT_COPY_CONSTRUCTOR(IntConstant);
2929
Roland Levillain9867bc72015-08-05 10:21:34 +01002930 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002931 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302932 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc), value_(value) {
2933 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002934 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302935 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc),
2936 value_(value ? 1 : 0) {
2937 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002938
2939 const int32_t value_;
2940
2941 friend class HGraph;
2942 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2943 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Roland Levillain9867bc72015-08-05 10:21:34 +01002944};
2945
Vladimir Markofcb503c2016-05-18 12:48:17 +01002946class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002947 public:
2948 int64_t GetValue() const { return value_; }
2949
David Brazdil77a48ae2015-09-15 12:34:04 +00002950 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2951
Vladimir Marko372f10e2016-05-17 16:30:10 +01002952 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002953 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002954 return other->AsLongConstant()->value_ == value_;
2955 }
2956
2957 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2958
2959 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002960 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2961 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002962 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2963
2964 DECLARE_INSTRUCTION(LongConstant);
2965
Artem Serovcced8ba2017-07-19 18:18:09 +01002966 protected:
2967 DEFAULT_COPY_CONSTRUCTOR(LongConstant);
2968
Roland Levillain9867bc72015-08-05 10:21:34 +01002969 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002970 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302971 : HConstant(kLongConstant, DataType::Type::kInt64, dex_pc),
2972 value_(value) {
2973 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002974
2975 const int64_t value_;
2976
2977 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01002978};
Dave Allison20dfc792014-06-16 20:44:29 -07002979
Vladimir Markofcb503c2016-05-18 12:48:17 +01002980class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002981 public:
2982 float GetValue() const { return value_; }
2983
2984 uint64_t GetValueAsUint64() const OVERRIDE {
2985 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2986 }
2987
Vladimir Marko372f10e2016-05-17 16:30:10 +01002988 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002989 DCHECK(other->IsFloatConstant()) << other->DebugName();
2990 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2991 }
2992
2993 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2994
2995 bool IsMinusOne() const OVERRIDE {
2996 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2997 }
Roland Levillain1a653882016-03-18 18:05:57 +00002998 bool IsArithmeticZero() const OVERRIDE {
2999 return std::fpclassify(value_) == FP_ZERO;
3000 }
3001 bool IsArithmeticPositiveZero() const {
3002 return IsArithmeticZero() && !std::signbit(value_);
3003 }
3004 bool IsArithmeticNegativeZero() const {
3005 return IsArithmeticZero() && std::signbit(value_);
3006 }
3007 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003008 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00003009 }
3010 bool IsOne() const OVERRIDE {
3011 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3012 }
3013 bool IsNaN() const {
3014 return std::isnan(value_);
3015 }
3016
3017 DECLARE_INSTRUCTION(FloatConstant);
3018
Artem Serovcced8ba2017-07-19 18:18:09 +01003019 protected:
3020 DEFAULT_COPY_CONSTRUCTOR(FloatConstant);
3021
Roland Levillain31dd3d62016-02-16 12:21:02 +00003022 private:
3023 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303024 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3025 value_(value) {
3026 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003027 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303028 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3029 value_(bit_cast<float, int32_t>(value)) {
3030 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003031
3032 const float value_;
3033
3034 // Only the SsaBuilder and HGraph can create floating-point constants.
3035 friend class SsaBuilder;
3036 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003037};
3038
Vladimir Markofcb503c2016-05-18 12:48:17 +01003039class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003040 public:
3041 double GetValue() const { return value_; }
3042
3043 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
3044
Vladimir Marko372f10e2016-05-17 16:30:10 +01003045 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003046 DCHECK(other->IsDoubleConstant()) << other->DebugName();
3047 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
3048 }
3049
3050 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
3051
3052 bool IsMinusOne() const OVERRIDE {
3053 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
3054 }
Roland Levillain1a653882016-03-18 18:05:57 +00003055 bool IsArithmeticZero() const OVERRIDE {
3056 return std::fpclassify(value_) == FP_ZERO;
3057 }
3058 bool IsArithmeticPositiveZero() const {
3059 return IsArithmeticZero() && !std::signbit(value_);
3060 }
3061 bool IsArithmeticNegativeZero() const {
3062 return IsArithmeticZero() && std::signbit(value_);
3063 }
3064 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003065 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00003066 }
3067 bool IsOne() const OVERRIDE {
3068 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3069 }
3070 bool IsNaN() const {
3071 return std::isnan(value_);
3072 }
3073
3074 DECLARE_INSTRUCTION(DoubleConstant);
3075
Artem Serovcced8ba2017-07-19 18:18:09 +01003076 protected:
3077 DEFAULT_COPY_CONSTRUCTOR(DoubleConstant);
3078
Roland Levillain31dd3d62016-02-16 12:21:02 +00003079 private:
3080 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303081 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3082 value_(value) {
3083 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003084 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303085 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3086 value_(bit_cast<double, int64_t>(value)) {
3087 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003088
3089 const double value_;
3090
3091 // Only the SsaBuilder and HGraph can create floating-point constants.
3092 friend class SsaBuilder;
3093 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003094};
3095
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003096// Conditional branch. A block ending with an HIf instruction must have
3097// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003098class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003099 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003100 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303101 : HTemplateInstruction(kIf, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003102 SetRawInputAt(0, input);
3103 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003104
Artem Serovcced8ba2017-07-19 18:18:09 +01003105 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003106 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003107
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003108 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003109 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003110 }
3111
3112 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003113 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003114 }
3115
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003116 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003117
Artem Serovcced8ba2017-07-19 18:18:09 +01003118 protected:
3119 DEFAULT_COPY_CONSTRUCTOR(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003120};
3121
David Brazdilfc6a86a2015-06-26 10:33:45 +00003122
3123// Abstract instruction which marks the beginning and/or end of a try block and
3124// links it to the respective exception handlers. Behaves the same as a Goto in
3125// non-exceptional control flow.
3126// Normal-flow successor is stored at index zero, exception handlers under
3127// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003128class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003129 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003130 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01003131 kEntry,
3132 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003133 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01003134 };
3135
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003136 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303137 : HTemplateInstruction(kTryBoundary, SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003138 SetPackedField<BoundaryKindField>(kind);
3139 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003140
3141 bool IsControlFlow() const OVERRIDE { return true; }
3142
3143 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01003144 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003145
David Brazdild26a4112015-11-10 11:07:31 +00003146 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
3147 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
3148 }
3149
David Brazdilfc6a86a2015-06-26 10:33:45 +00003150 // Returns whether `handler` is among its exception handlers (non-zero index
3151 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01003152 bool HasExceptionHandler(const HBasicBlock& handler) const {
3153 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00003154 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00003155 }
3156
3157 // If not present already, adds `handler` to its block's list of exception
3158 // handlers.
3159 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01003160 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003161 GetBlock()->AddSuccessor(handler);
3162 }
3163 }
3164
Vladimir Markoa1de9182016-02-25 11:37:38 +00003165 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
3166 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003167
David Brazdilffee3d32015-07-06 11:48:53 +01003168 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
3169
David Brazdilfc6a86a2015-06-26 10:33:45 +00003170 DECLARE_INSTRUCTION(TryBoundary);
3171
Artem Serovcced8ba2017-07-19 18:18:09 +01003172 protected:
3173 DEFAULT_COPY_CONSTRUCTOR(TryBoundary);
3174
David Brazdilfc6a86a2015-06-26 10:33:45 +00003175 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003176 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
3177 static constexpr size_t kFieldBoundaryKindSize =
3178 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
3179 static constexpr size_t kNumberOfTryBoundaryPackedBits =
3180 kFieldBoundaryKind + kFieldBoundaryKindSize;
3181 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
3182 "Too many packed fields.");
3183 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00003184};
3185
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003186// Deoptimize to interpreter, upon checking a condition.
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003187class HDeoptimize FINAL : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003188 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003189 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
3190 // across.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003191 HDeoptimize(ArenaAllocator* allocator,
3192 HInstruction* cond,
3193 DeoptimizationKind kind,
3194 uint32_t dex_pc)
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003195 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303196 kDeoptimize,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003197 SideEffects::All(),
3198 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003199 allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003200 /* number_of_inputs */ 1,
3201 kArenaAllocMisc) {
3202 SetPackedFlag<kFieldCanBeMoved>(false);
3203 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003204 SetRawInputAt(0, cond);
3205 }
3206
Artem Serovcced8ba2017-07-19 18:18:09 +01003207 bool IsClonable() const OVERRIDE { return true; }
3208
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003209 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
3210 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
3211 // instead of `guard`.
3212 // We set CanTriggerGC to prevent any intermediate address to be live
3213 // at the point of the `HDeoptimize`.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003214 HDeoptimize(ArenaAllocator* allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003215 HInstruction* cond,
3216 HInstruction* guard,
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003217 DeoptimizationKind kind,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003218 uint32_t dex_pc)
3219 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303220 kDeoptimize,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003221 SideEffects::CanTriggerGC(),
3222 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003223 allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003224 /* number_of_inputs */ 2,
3225 kArenaAllocMisc) {
3226 SetPackedFlag<kFieldCanBeMoved>(true);
3227 SetPackedField<DeoptimizeKindField>(kind);
3228 SetRawInputAt(0, cond);
3229 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003230 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003231
3232 bool CanBeMoved() const OVERRIDE { return GetPackedFlag<kFieldCanBeMoved>(); }
3233
3234 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
3235 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
3236 }
3237
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003238 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003239
Aart Bik240384b2018-04-17 14:56:29 -07003240 // Even though deoptimize is often used for "exceptional cases" to go back to
3241 // the interpreter, it never throws an exception.
3242 bool CanThrow() const OVERRIDE { return false; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003243
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003244 DeoptimizationKind GetDeoptimizationKind() const { return GetPackedField<DeoptimizeKindField>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003245
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003246 DataType::Type GetType() const OVERRIDE {
3247 return GuardsAnInput() ? GuardedInput()->GetType() : DataType::Type::kVoid;
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003248 }
3249
3250 bool GuardsAnInput() const {
3251 return InputCount() == 2;
3252 }
3253
3254 HInstruction* GuardedInput() const {
3255 DCHECK(GuardsAnInput());
3256 return InputAt(1);
3257 }
3258
3259 void RemoveGuard() {
3260 RemoveInputAt(1);
3261 }
3262
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003263 DECLARE_INSTRUCTION(Deoptimize);
3264
Artem Serovcced8ba2017-07-19 18:18:09 +01003265 protected:
3266 DEFAULT_COPY_CONSTRUCTOR(Deoptimize);
3267
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003268 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003269 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3270 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3271 static constexpr size_t kFieldDeoptimizeKindSize =
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003272 MinimumBitsToStore(static_cast<size_t>(DeoptimizationKind::kLast));
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003273 static constexpr size_t kNumberOfDeoptimizePackedBits =
3274 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3275 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3276 "Too many packed fields.");
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003277 using DeoptimizeKindField =
3278 BitField<DeoptimizationKind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003279};
3280
Mingyao Yang063fc772016-08-02 11:02:54 -07003281// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3282// The compiled code checks this flag value in a guard before devirtualized call and
3283// if it's true, starts to do deoptimization.
3284// It has a 4-byte slot on stack.
3285// TODO: allocate a register for this flag.
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003286class HShouldDeoptimizeFlag FINAL : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003287 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003288 // CHA guards are only optimized in a separate pass and it has no side effects
3289 // with regard to other passes.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003290 HShouldDeoptimizeFlag(ArenaAllocator* allocator, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303291 : HVariableInputSizeInstruction(kShouldDeoptimizeFlag,
3292 SideEffects::None(),
3293 dex_pc,
3294 allocator,
3295 0,
3296 kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003297 }
3298
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003299 DataType::Type GetType() const OVERRIDE { return DataType::Type::kInt32; }
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003300
3301 // We do all CHA guard elimination/motion in a single pass, after which there is no
3302 // further guard elimination/motion since a guard might have been used for justification
3303 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3304 // to avoid other optimizations trying to move it.
Mingyao Yang063fc772016-08-02 11:02:54 -07003305 bool CanBeMoved() const OVERRIDE { return false; }
3306
3307 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3308
Artem Serovcced8ba2017-07-19 18:18:09 +01003309 protected:
3310 DEFAULT_COPY_CONSTRUCTOR(ShouldDeoptimizeFlag);
Mingyao Yang063fc772016-08-02 11:02:54 -07003311};
3312
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003313// Represents the ArtMethod that was passed as a first argument to
3314// the method. It is used by instructions that depend on it, like
3315// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003316class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003317 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003318 explicit HCurrentMethod(DataType::Type type, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303319 : HExpression(kCurrentMethod, type, SideEffects::None(), dex_pc) {
3320 }
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003321
3322 DECLARE_INSTRUCTION(CurrentMethod);
3323
Artem Serovcced8ba2017-07-19 18:18:09 +01003324 protected:
3325 DEFAULT_COPY_CONSTRUCTOR(CurrentMethod);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003326};
3327
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003328// Fetches an ArtMethod from the virtual table or the interface method table
3329// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003330class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003331 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003332 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003333 kVTable,
3334 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003335 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003336 };
3337 HClassTableGet(HInstruction* cls,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003338 DataType::Type type,
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003339 TableKind kind,
3340 size_t index,
3341 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303342 : HExpression(kClassTableGet, type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003343 index_(index) {
3344 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003345 SetRawInputAt(0, cls);
3346 }
3347
Artem Serovcced8ba2017-07-19 18:18:09 +01003348 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003349 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003350 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003351 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003352 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003353 }
3354
Vladimir Markoa1de9182016-02-25 11:37:38 +00003355 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003356 size_t GetIndex() const { return index_; }
3357
3358 DECLARE_INSTRUCTION(ClassTableGet);
3359
Artem Serovcced8ba2017-07-19 18:18:09 +01003360 protected:
3361 DEFAULT_COPY_CONSTRUCTOR(ClassTableGet);
3362
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003363 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003364 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
3365 static constexpr size_t kFieldTableKindSize =
3366 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3367 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3368 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3369 "Too many packed fields.");
3370 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3371
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003372 // The index of the ArtMethod in the table.
3373 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003374};
3375
Mark Mendellfe57faa2015-09-18 09:26:15 -04003376// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3377// have one successor for each entry in the switch table, and the final successor
3378// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003379class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003380 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003381 HPackedSwitch(int32_t start_value,
3382 uint32_t num_entries,
3383 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003384 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303385 : HTemplateInstruction(kPackedSwitch, SideEffects::None(), dex_pc),
Mark Mendellfe57faa2015-09-18 09:26:15 -04003386 start_value_(start_value),
3387 num_entries_(num_entries) {
3388 SetRawInputAt(0, input);
3389 }
3390
Artem Serovcced8ba2017-07-19 18:18:09 +01003391 bool IsClonable() const OVERRIDE { return true; }
3392
Mark Mendellfe57faa2015-09-18 09:26:15 -04003393 bool IsControlFlow() const OVERRIDE { return true; }
3394
3395 int32_t GetStartValue() const { return start_value_; }
3396
Vladimir Marko211c2112015-09-24 16:52:33 +01003397 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003398
3399 HBasicBlock* GetDefaultBlock() const {
3400 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003401 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003402 }
3403 DECLARE_INSTRUCTION(PackedSwitch);
3404
Artem Serovcced8ba2017-07-19 18:18:09 +01003405 protected:
3406 DEFAULT_COPY_CONSTRUCTOR(PackedSwitch);
3407
Mark Mendellfe57faa2015-09-18 09:26:15 -04003408 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003409 const int32_t start_value_;
3410 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003411};
3412
Roland Levillain88cb1752014-10-20 16:36:47 +01003413class HUnaryOperation : public HExpression<1> {
3414 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303415 HUnaryOperation(InstructionKind kind,
3416 DataType::Type result_type,
3417 HInstruction* input,
3418 uint32_t dex_pc = kNoDexPc)
3419 : HExpression(kind, result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003420 SetRawInputAt(0, input);
3421 }
3422
Artem Serovcced8ba2017-07-19 18:18:09 +01003423 // All of the UnaryOperation instructions are clonable.
3424 bool IsClonable() const OVERRIDE { return true; }
3425
Roland Levillain88cb1752014-10-20 16:36:47 +01003426 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003427 DataType::Type GetResultType() const { return GetType(); }
Roland Levillain88cb1752014-10-20 16:36:47 +01003428
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003429 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003430 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003431 return true;
3432 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003433
Roland Levillain31dd3d62016-02-16 12:21:02 +00003434 // Try to statically evaluate `this` and return a HConstant
3435 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003436 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003437 HConstant* TryStaticEvaluation() const;
3438
3439 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003440 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3441 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003442 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3443 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003444
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003445 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003446
Artem Serovcced8ba2017-07-19 18:18:09 +01003447 protected:
3448 DEFAULT_COPY_CONSTRUCTOR(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003449};
3450
Dave Allison20dfc792014-06-16 20:44:29 -07003451class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003452 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303453 HBinaryOperation(InstructionKind kind,
3454 DataType::Type result_type,
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003455 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003456 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003457 SideEffects side_effects = SideEffects::None(),
3458 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303459 : HExpression(kind, result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003460 SetRawInputAt(0, left);
3461 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003462 }
3463
Artem Serovcced8ba2017-07-19 18:18:09 +01003464 // All of the BinaryOperation instructions are clonable.
3465 bool IsClonable() const OVERRIDE { return true; }
3466
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003467 HInstruction* GetLeft() const { return InputAt(0); }
3468 HInstruction* GetRight() const { return InputAt(1); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003469 DataType::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003470
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003471 virtual bool IsCommutative() const { return false; }
3472
3473 // Put constant on the right.
3474 // Returns whether order is changed.
3475 bool OrderInputsWithConstantOnTheRight() {
3476 HInstruction* left = InputAt(0);
3477 HInstruction* right = InputAt(1);
3478 if (left->IsConstant() && !right->IsConstant()) {
3479 ReplaceInput(right, 0);
3480 ReplaceInput(left, 1);
3481 return true;
3482 }
3483 return false;
3484 }
3485
3486 // Order inputs by instruction id, but favor constant on the right side.
3487 // This helps GVN for commutative ops.
3488 void OrderInputs() {
3489 DCHECK(IsCommutative());
3490 HInstruction* left = InputAt(0);
3491 HInstruction* right = InputAt(1);
3492 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3493 return;
3494 }
3495 if (OrderInputsWithConstantOnTheRight()) {
3496 return;
3497 }
3498 // Order according to instruction id.
3499 if (left->GetId() > right->GetId()) {
3500 ReplaceInput(right, 0);
3501 ReplaceInput(left, 1);
3502 }
3503 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003504
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003505 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003506 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003507 return true;
3508 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003509
Roland Levillain31dd3d62016-02-16 12:21:02 +00003510 // Try to statically evaluate `this` and return a HConstant
3511 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003512 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003513 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003514
3515 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003516 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3517 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003518 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3519 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003520 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003521 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3522 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003523 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3524 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003525 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3526 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003527 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003528 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3529 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003530
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003531 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003532 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003533 HConstant* GetConstantRight() const;
3534
3535 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003536 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003537 HInstruction* GetLeastConstantLeft() const;
3538
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003539 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003540
Artem Serovcced8ba2017-07-19 18:18:09 +01003541 protected:
3542 DEFAULT_COPY_CONSTRUCTOR(BinaryOperation);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003543};
3544
Mark Mendellc4701932015-04-10 13:18:51 -04003545// The comparison bias applies for floating point operations and indicates how NaN
3546// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003547enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003548 kNoBias, // bias is not applicable (i.e. for long operation)
3549 kGtBias, // return 1 for NaN comparisons
3550 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003551 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003552};
3553
Roland Levillain31dd3d62016-02-16 12:21:02 +00003554std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3555
Dave Allison20dfc792014-06-16 20:44:29 -07003556class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003557 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303558 HCondition(InstructionKind kind,
3559 HInstruction* first,
3560 HInstruction* second,
3561 uint32_t dex_pc = kNoDexPc)
3562 : HBinaryOperation(kind,
3563 DataType::Type::kBool,
3564 first,
3565 second,
3566 SideEffects::None(),
3567 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003568 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3569 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003570
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003571 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003572 // `instruction`, and disregard moves in between.
3573 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003574
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003575 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003576
3577 virtual IfCondition GetCondition() const = 0;
3578
Mark Mendellc4701932015-04-10 13:18:51 -04003579 virtual IfCondition GetOppositeCondition() const = 0;
3580
Vladimir Markoa1de9182016-02-25 11:37:38 +00003581 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003582 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3583
Vladimir Markoa1de9182016-02-25 11:37:38 +00003584 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3585 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003586
Vladimir Marko372f10e2016-05-17 16:30:10 +01003587 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003588 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003589 }
3590
Roland Levillain4fa13f62015-07-06 18:11:54 +01003591 bool IsFPConditionTrueIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003592 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003593 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003594 if (if_cond == kCondNE) {
3595 return true;
3596 } else if (if_cond == kCondEQ) {
3597 return false;
3598 }
3599 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003600 }
3601
3602 bool IsFPConditionFalseIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003603 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003604 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003605 if (if_cond == kCondEQ) {
3606 return true;
3607 } else if (if_cond == kCondNE) {
3608 return false;
3609 }
3610 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003611 }
3612
Roland Levillain31dd3d62016-02-16 12:21:02 +00003613 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003614 // Needed if we merge a HCompare into a HCondition.
3615 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3616 static constexpr size_t kFieldComparisonBiasSize =
3617 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3618 static constexpr size_t kNumberOfConditionPackedBits =
3619 kFieldComparisonBias + kFieldComparisonBiasSize;
3620 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3621 using ComparisonBiasField =
3622 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3623
Roland Levillain31dd3d62016-02-16 12:21:02 +00003624 template <typename T>
3625 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3626
3627 template <typename T>
3628 int32_t CompareFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003629 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003630 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3631 // Handle the bias.
3632 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3633 }
3634
3635 // Return an integer constant containing the result of a condition evaluated at compile time.
3636 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3637 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3638 }
3639
Artem Serovcced8ba2017-07-19 18:18:09 +01003640 DEFAULT_COPY_CONSTRUCTOR(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003641};
3642
3643// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003644class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003645 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003646 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303647 : HCondition(kEqual, first, second, dex_pc) {
3648 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003649
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003650 bool IsCommutative() const OVERRIDE { return true; }
3651
Vladimir Marko9e23df52015-11-10 17:14:35 +00003652 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3653 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003654 return MakeConstantCondition(true, GetDexPc());
3655 }
3656 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3657 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3658 }
3659 // In the following Evaluate methods, a HCompare instruction has
3660 // been merged into this HEqual instruction; evaluate it as
3661 // `Compare(x, y) == 0`.
3662 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3663 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3664 GetDexPc());
3665 }
3666 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3667 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3668 }
3669 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3670 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003671 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003672
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003673 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003674
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003675 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003676 return kCondEQ;
3677 }
3678
Mark Mendellc4701932015-04-10 13:18:51 -04003679 IfCondition GetOppositeCondition() const OVERRIDE {
3680 return kCondNE;
3681 }
3682
Artem Serovcced8ba2017-07-19 18:18:09 +01003683 protected:
3684 DEFAULT_COPY_CONSTRUCTOR(Equal);
3685
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003686 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003687 template <typename T> static bool Compute(T x, T y) { return x == y; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003688};
3689
Vladimir Markofcb503c2016-05-18 12:48:17 +01003690class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003691 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303692 HNotEqual(HInstruction* first, HInstruction* second,
3693 uint32_t dex_pc = kNoDexPc)
3694 : HCondition(kNotEqual, first, second, dex_pc) {
3695 }
Dave Allison20dfc792014-06-16 20:44:29 -07003696
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003697 bool IsCommutative() const OVERRIDE { return true; }
3698
Vladimir Marko9e23df52015-11-10 17:14:35 +00003699 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3700 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003701 return MakeConstantCondition(false, GetDexPc());
3702 }
3703 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3704 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3705 }
3706 // In the following Evaluate methods, a HCompare instruction has
3707 // been merged into this HNotEqual instruction; evaluate it as
3708 // `Compare(x, y) != 0`.
3709 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3710 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3711 }
3712 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3713 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3714 }
3715 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3716 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003717 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003718
Dave Allison20dfc792014-06-16 20:44:29 -07003719 DECLARE_INSTRUCTION(NotEqual);
3720
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003721 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003722 return kCondNE;
3723 }
3724
Mark Mendellc4701932015-04-10 13:18:51 -04003725 IfCondition GetOppositeCondition() const OVERRIDE {
3726 return kCondEQ;
3727 }
3728
Artem Serovcced8ba2017-07-19 18:18:09 +01003729 protected:
3730 DEFAULT_COPY_CONSTRUCTOR(NotEqual);
3731
Dave Allison20dfc792014-06-16 20:44:29 -07003732 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003733 template <typename T> static bool Compute(T x, T y) { return x != y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003734};
3735
Vladimir Markofcb503c2016-05-18 12:48:17 +01003736class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003737 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303738 HLessThan(HInstruction* first, HInstruction* second,
3739 uint32_t dex_pc = kNoDexPc)
3740 : HCondition(kLessThan, first, second, dex_pc) {
3741 }
Dave Allison20dfc792014-06-16 20:44:29 -07003742
Roland Levillain9867bc72015-08-05 10:21:34 +01003743 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003744 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003745 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003746 // In the following Evaluate methods, a HCompare instruction has
3747 // been merged into this HLessThan instruction; evaluate it as
3748 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003749 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003750 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3751 }
3752 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3753 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3754 }
3755 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3756 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003757 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003758
Dave Allison20dfc792014-06-16 20:44:29 -07003759 DECLARE_INSTRUCTION(LessThan);
3760
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003761 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003762 return kCondLT;
3763 }
3764
Mark Mendellc4701932015-04-10 13:18:51 -04003765 IfCondition GetOppositeCondition() const OVERRIDE {
3766 return kCondGE;
3767 }
3768
Artem Serovcced8ba2017-07-19 18:18:09 +01003769 protected:
3770 DEFAULT_COPY_CONSTRUCTOR(LessThan);
3771
Dave Allison20dfc792014-06-16 20:44:29 -07003772 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003773 template <typename T> static bool Compute(T x, T y) { return x < y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003774};
3775
Vladimir Markofcb503c2016-05-18 12:48:17 +01003776class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003777 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303778 HLessThanOrEqual(HInstruction* first, HInstruction* second,
3779 uint32_t dex_pc = kNoDexPc)
3780 : HCondition(kLessThanOrEqual, first, second, dex_pc) {
3781 }
Dave Allison20dfc792014-06-16 20:44:29 -07003782
Roland Levillain9867bc72015-08-05 10:21:34 +01003783 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003784 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003785 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003786 // In the following Evaluate methods, a HCompare instruction has
3787 // been merged into this HLessThanOrEqual instruction; evaluate it as
3788 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003789 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003790 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3791 }
3792 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3793 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3794 }
3795 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3796 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003797 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003798
Dave Allison20dfc792014-06-16 20:44:29 -07003799 DECLARE_INSTRUCTION(LessThanOrEqual);
3800
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003801 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003802 return kCondLE;
3803 }
3804
Mark Mendellc4701932015-04-10 13:18:51 -04003805 IfCondition GetOppositeCondition() const OVERRIDE {
3806 return kCondGT;
3807 }
3808
Artem Serovcced8ba2017-07-19 18:18:09 +01003809 protected:
3810 DEFAULT_COPY_CONSTRUCTOR(LessThanOrEqual);
3811
Dave Allison20dfc792014-06-16 20:44:29 -07003812 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003813 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003814};
3815
Vladimir Markofcb503c2016-05-18 12:48:17 +01003816class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003817 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003818 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303819 : HCondition(kGreaterThan, first, second, dex_pc) {
3820 }
Dave Allison20dfc792014-06-16 20:44:29 -07003821
Roland Levillain9867bc72015-08-05 10:21:34 +01003822 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003823 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003824 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003825 // In the following Evaluate methods, a HCompare instruction has
3826 // been merged into this HGreaterThan instruction; evaluate it as
3827 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003828 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003829 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3830 }
3831 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3832 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3833 }
3834 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3835 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003836 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003837
Dave Allison20dfc792014-06-16 20:44:29 -07003838 DECLARE_INSTRUCTION(GreaterThan);
3839
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003840 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003841 return kCondGT;
3842 }
3843
Mark Mendellc4701932015-04-10 13:18:51 -04003844 IfCondition GetOppositeCondition() const OVERRIDE {
3845 return kCondLE;
3846 }
3847
Artem Serovcced8ba2017-07-19 18:18:09 +01003848 protected:
3849 DEFAULT_COPY_CONSTRUCTOR(GreaterThan);
3850
Dave Allison20dfc792014-06-16 20:44:29 -07003851 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003852 template <typename T> static bool Compute(T x, T y) { return x > y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003853};
3854
Vladimir Markofcb503c2016-05-18 12:48:17 +01003855class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003856 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003857 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303858 : HCondition(kGreaterThanOrEqual, first, second, dex_pc) {
3859 }
Dave Allison20dfc792014-06-16 20:44:29 -07003860
Roland Levillain9867bc72015-08-05 10:21:34 +01003861 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003862 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003863 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003864 // In the following Evaluate methods, a HCompare instruction has
3865 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3866 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003867 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003868 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3869 }
3870 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3871 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3872 }
3873 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3874 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003875 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003876
Dave Allison20dfc792014-06-16 20:44:29 -07003877 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3878
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003879 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003880 return kCondGE;
3881 }
3882
Mark Mendellc4701932015-04-10 13:18:51 -04003883 IfCondition GetOppositeCondition() const OVERRIDE {
3884 return kCondLT;
3885 }
3886
Artem Serovcced8ba2017-07-19 18:18:09 +01003887 protected:
3888 DEFAULT_COPY_CONSTRUCTOR(GreaterThanOrEqual);
3889
Dave Allison20dfc792014-06-16 20:44:29 -07003890 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003891 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003892};
3893
Vladimir Markofcb503c2016-05-18 12:48:17 +01003894class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003895 public:
3896 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303897 : HCondition(kBelow, first, second, dex_pc) {
3898 }
Aart Bike9f37602015-10-09 11:15:55 -07003899
3900 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003901 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003902 }
3903 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003904 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3905 }
3906 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3907 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3908 LOG(FATAL) << DebugName() << " is not defined for float values";
3909 UNREACHABLE();
3910 }
3911 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3912 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3913 LOG(FATAL) << DebugName() << " is not defined for double values";
3914 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003915 }
3916
3917 DECLARE_INSTRUCTION(Below);
3918
3919 IfCondition GetCondition() const OVERRIDE {
3920 return kCondB;
3921 }
3922
3923 IfCondition GetOppositeCondition() const OVERRIDE {
3924 return kCondAE;
3925 }
3926
Artem Serovcced8ba2017-07-19 18:18:09 +01003927 protected:
3928 DEFAULT_COPY_CONSTRUCTOR(Below);
3929
Aart Bike9f37602015-10-09 11:15:55 -07003930 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003931 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003932 return MakeUnsigned(x) < MakeUnsigned(y);
3933 }
Aart Bike9f37602015-10-09 11:15:55 -07003934};
3935
Vladimir Markofcb503c2016-05-18 12:48:17 +01003936class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003937 public:
3938 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303939 : HCondition(kBelowOrEqual, first, second, dex_pc) {
3940 }
Aart Bike9f37602015-10-09 11:15:55 -07003941
3942 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003943 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003944 }
3945 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003946 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3947 }
3948 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3949 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3950 LOG(FATAL) << DebugName() << " is not defined for float values";
3951 UNREACHABLE();
3952 }
3953 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3954 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3955 LOG(FATAL) << DebugName() << " is not defined for double values";
3956 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003957 }
3958
3959 DECLARE_INSTRUCTION(BelowOrEqual);
3960
3961 IfCondition GetCondition() const OVERRIDE {
3962 return kCondBE;
3963 }
3964
3965 IfCondition GetOppositeCondition() const OVERRIDE {
3966 return kCondA;
3967 }
3968
Artem Serovcced8ba2017-07-19 18:18:09 +01003969 protected:
3970 DEFAULT_COPY_CONSTRUCTOR(BelowOrEqual);
3971
Aart Bike9f37602015-10-09 11:15:55 -07003972 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003973 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003974 return MakeUnsigned(x) <= MakeUnsigned(y);
3975 }
Aart Bike9f37602015-10-09 11:15:55 -07003976};
3977
Vladimir Markofcb503c2016-05-18 12:48:17 +01003978class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003979 public:
3980 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303981 : HCondition(kAbove, first, second, dex_pc) {
3982 }
Aart Bike9f37602015-10-09 11:15:55 -07003983
3984 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003985 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003986 }
3987 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003988 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3989 }
3990 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3991 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3992 LOG(FATAL) << DebugName() << " is not defined for float values";
3993 UNREACHABLE();
3994 }
3995 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3996 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3997 LOG(FATAL) << DebugName() << " is not defined for double values";
3998 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003999 }
4000
4001 DECLARE_INSTRUCTION(Above);
4002
4003 IfCondition GetCondition() const OVERRIDE {
4004 return kCondA;
4005 }
4006
4007 IfCondition GetOppositeCondition() const OVERRIDE {
4008 return kCondBE;
4009 }
4010
Artem Serovcced8ba2017-07-19 18:18:09 +01004011 protected:
4012 DEFAULT_COPY_CONSTRUCTOR(Above);
4013
Aart Bike9f37602015-10-09 11:15:55 -07004014 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004015 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004016 return MakeUnsigned(x) > MakeUnsigned(y);
4017 }
Aart Bike9f37602015-10-09 11:15:55 -07004018};
4019
Vladimir Markofcb503c2016-05-18 12:48:17 +01004020class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004021 public:
4022 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304023 : HCondition(kAboveOrEqual, first, second, dex_pc) {
4024 }
Aart Bike9f37602015-10-09 11:15:55 -07004025
4026 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004027 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004028 }
4029 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004030 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4031 }
4032 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4033 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4034 LOG(FATAL) << DebugName() << " is not defined for float values";
4035 UNREACHABLE();
4036 }
4037 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4038 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4039 LOG(FATAL) << DebugName() << " is not defined for double values";
4040 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004041 }
4042
4043 DECLARE_INSTRUCTION(AboveOrEqual);
4044
4045 IfCondition GetCondition() const OVERRIDE {
4046 return kCondAE;
4047 }
4048
4049 IfCondition GetOppositeCondition() const OVERRIDE {
4050 return kCondB;
4051 }
4052
Artem Serovcced8ba2017-07-19 18:18:09 +01004053 protected:
4054 DEFAULT_COPY_CONSTRUCTOR(AboveOrEqual);
4055
Aart Bike9f37602015-10-09 11:15:55 -07004056 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004057 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004058 return MakeUnsigned(x) >= MakeUnsigned(y);
4059 }
Aart Bike9f37602015-10-09 11:15:55 -07004060};
Dave Allison20dfc792014-06-16 20:44:29 -07004061
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004062// Instruction to check how two inputs compare to each other.
4063// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01004064class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004065 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00004066 // Note that `comparison_type` is the type of comparison performed
4067 // between the comparison's inputs, not the type of the instantiated
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004068 // HCompare instruction (which is always DataType::Type::kInt).
4069 HCompare(DataType::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004070 HInstruction* first,
4071 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04004072 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004073 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304074 : HBinaryOperation(kCompare,
4075 DataType::Type::kInt32,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004076 first,
4077 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00004078 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004079 dex_pc) {
4080 SetPackedField<ComparisonBiasField>(bias);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004081 DCHECK_EQ(comparison_type, DataType::Kind(first->GetType()));
4082 DCHECK_EQ(comparison_type, DataType::Kind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004083 }
4084
Roland Levillain9867bc72015-08-05 10:21:34 +01004085 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004086 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
4087
4088 template <typename T>
4089 int32_t ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004090 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004091 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
4092 // Handle the bias.
4093 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
4094 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004095
Roland Levillain9867bc72015-08-05 10:21:34 +01004096 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004097 // Note that there is no "cmp-int" Dex instruction so we shouldn't
4098 // reach this code path when processing a freshly built HIR
4099 // graph. However HCompare integer instructions can be synthesized
4100 // by the instruction simplifier to implement IntegerCompare and
4101 // IntegerSignum intrinsics, so we have to handle this case.
4102 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004103 }
4104 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004105 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4106 }
4107 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4108 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4109 }
4110 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4111 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01004112 }
4113
Vladimir Marko372f10e2016-05-17 16:30:10 +01004114 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004115 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00004116 }
4117
Vladimir Markoa1de9182016-02-25 11:37:38 +00004118 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04004119
Roland Levillain31dd3d62016-02-16 12:21:02 +00004120 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00004121 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00004122 bool IsGtBias() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004123 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004124 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00004125 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004126
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004127 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type type ATTRIBUTE_UNUSED) {
Roland Levillain1693a1f2016-03-15 14:57:31 +00004128 // Comparisons do not require a runtime call in any back end.
4129 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004130 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004131
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004132 DECLARE_INSTRUCTION(Compare);
4133
Roland Levillain31dd3d62016-02-16 12:21:02 +00004134 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004135 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
4136 static constexpr size_t kFieldComparisonBiasSize =
4137 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
4138 static constexpr size_t kNumberOfComparePackedBits =
4139 kFieldComparisonBias + kFieldComparisonBiasSize;
4140 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4141 using ComparisonBiasField =
4142 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
4143
Roland Levillain31dd3d62016-02-16 12:21:02 +00004144 // Return an integer constant containing the result of a comparison evaluated at compile time.
4145 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
4146 DCHECK(value == -1 || value == 0 || value == 1) << value;
4147 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
4148 }
4149
Artem Serovcced8ba2017-07-19 18:18:09 +01004150 DEFAULT_COPY_CONSTRUCTOR(Compare);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004151};
4152
Nicolas Geoffray2b615ba2017-01-06 14:40:07 +00004153class HNewInstance FINAL : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00004154 public:
4155 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00004156 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004157 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00004158 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00004159 bool finalizable,
4160 QuickEntrypointEnum entrypoint)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304161 : HExpression(kNewInstance,
4162 DataType::Type::kReference,
4163 SideEffects::CanTriggerGC(),
4164 dex_pc),
David Brazdil6de19382016-01-08 17:37:10 +00004165 type_index_(type_index),
4166 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00004167 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004168 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00004169 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00004170 }
4171
Artem Serovcced8ba2017-07-19 18:18:09 +01004172 bool IsClonable() const OVERRIDE { return true; }
4173
Andreas Gampea5b09a62016-11-17 15:21:22 -08004174 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00004175 const DexFile& GetDexFile() const { return dex_file_; }
4176
4177 // Calls runtime so needs an environment.
4178 bool NeedsEnvironment() const OVERRIDE { return true; }
4179
Mingyao Yang062157f2016-03-02 10:15:36 -08004180 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
4181 bool CanThrow() const OVERRIDE { return true; }
4182
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004183 bool NeedsChecks() const {
4184 return entrypoint_ == kQuickAllocObjectWithChecks;
4185 }
David Brazdil6de19382016-01-08 17:37:10 +00004186
Vladimir Markoa1de9182016-02-25 11:37:38 +00004187 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00004188
4189 bool CanBeNull() const OVERRIDE { return false; }
4190
4191 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4192
4193 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
4194 entrypoint_ = entrypoint;
4195 }
4196
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004197 HLoadClass* GetLoadClass() const {
4198 HInstruction* input = InputAt(0);
4199 if (input->IsClinitCheck()) {
4200 input = input->InputAt(0);
4201 }
4202 DCHECK(input->IsLoadClass());
4203 return input->AsLoadClass();
4204 }
4205
David Brazdil6de19382016-01-08 17:37:10 +00004206 bool IsStringAlloc() const;
4207
4208 DECLARE_INSTRUCTION(NewInstance);
4209
Artem Serovcced8ba2017-07-19 18:18:09 +01004210 protected:
4211 DEFAULT_COPY_CONSTRUCTOR(NewInstance);
4212
David Brazdil6de19382016-01-08 17:37:10 +00004213 private:
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004214 static constexpr size_t kFlagFinalizable = kNumberOfExpressionPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004215 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
4216 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
4217 "Too many packed fields.");
4218
Andreas Gampea5b09a62016-11-17 15:21:22 -08004219 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00004220 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00004221 QuickEntrypointEnum entrypoint_;
David Brazdil6de19382016-01-08 17:37:10 +00004222};
4223
Agi Csaki05f20562015-08-19 14:58:14 -07004224enum IntrinsicNeedsEnvironmentOrCache {
4225 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
4226 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07004227};
4228
Aart Bik5d75afe2015-12-14 11:57:01 -08004229enum IntrinsicSideEffects {
4230 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
4231 kReadSideEffects, // Intrinsic may read heap memory.
4232 kWriteSideEffects, // Intrinsic may write heap memory.
4233 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
4234};
4235
4236enum IntrinsicExceptions {
4237 kNoThrow, // Intrinsic does not throw any exceptions.
4238 kCanThrow // Intrinsic may throw exceptions.
4239};
4240
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004241class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004242 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004243 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004244
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01004245 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004246 SetRawInputAt(index, argument);
4247 }
4248
Roland Levillain3e3d7332015-04-28 11:00:54 +01004249 // Return the number of arguments. This number can be lower than
4250 // the number of inputs returned by InputCount(), as some invoke
4251 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
4252 // inputs at the end of their list of inputs.
4253 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
4254
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004255 DataType::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004256
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004257 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4258
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004259 InvokeType GetInvokeType() const {
4260 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004261 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004262
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01004263 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004264 return intrinsic_;
4265 }
4266
Aart Bik5d75afe2015-12-14 11:57:01 -08004267 void SetIntrinsic(Intrinsics intrinsic,
4268 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
4269 IntrinsicSideEffects side_effects,
4270 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004271
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004272 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00004273 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004274 }
4275
Aart Bikff7d89c2016-11-07 08:49:28 -08004276 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4277
Vladimir Markoa1de9182016-02-25 11:37:38 +00004278 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004279
Aart Bika8b8e9b2018-01-09 11:01:02 -08004280 void SetAlwaysThrows(bool always_throws) { SetPackedFlag<kFlagAlwaysThrows>(always_throws); }
4281
4282 bool AlwaysThrows() const OVERRIDE { return GetPackedFlag<kFlagAlwaysThrows>(); }
4283
Aart Bik71bf7b42016-11-16 10:17:46 -08004284 bool CanBeMoved() const OVERRIDE { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004285
Vladimir Marko372f10e2016-05-17 16:30:10 +01004286 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08004287 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4288 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004289
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004290 uint32_t* GetIntrinsicOptimizations() {
4291 return &intrinsic_optimizations_;
4292 }
4293
4294 const uint32_t* GetIntrinsicOptimizations() const {
4295 return &intrinsic_optimizations_;
4296 }
4297
4298 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4299
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004300 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004301 void SetResolvedMethod(ArtMethod* method) { resolved_method_ = method; }
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004302
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004303 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004304
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004305 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004306 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4307 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004308 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4309 static constexpr size_t kFieldReturnType =
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004310 kFieldInvokeType + kFieldInvokeTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004311 static constexpr size_t kFieldReturnTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004312 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00004313 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
Aart Bika8b8e9b2018-01-09 11:01:02 -08004314 static constexpr size_t kFlagAlwaysThrows = kFlagCanThrow + 1;
4315 static constexpr size_t kNumberOfInvokePackedBits = kFlagAlwaysThrows + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004316 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004317 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004318 using ReturnTypeField = BitField<DataType::Type, kFieldReturnType, kFieldReturnTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004319
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304320 HInvoke(InstructionKind kind,
4321 ArenaAllocator* allocator,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004322 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004323 uint32_t number_of_other_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004324 DataType::Type return_type,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004325 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004326 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004327 ArtMethod* resolved_method,
4328 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004329 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304330 kind,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004331 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4332 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004333 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004334 number_of_arguments + number_of_other_inputs,
4335 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004336 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004337 resolved_method_(resolved_method),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004338 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07004339 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004340 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004341 SetPackedField<ReturnTypeField>(return_type);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004342 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004343 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004344 }
4345
Artem Serovcced8ba2017-07-19 18:18:09 +01004346 DEFAULT_COPY_CONSTRUCTOR(Invoke);
4347
Roland Levillain3e3d7332015-04-28 11:00:54 +01004348 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004349 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004350 const uint32_t dex_method_index_;
4351 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004352
4353 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4354 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004355};
4356
Vladimir Markofcb503c2016-05-18 12:48:17 +01004357class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004358 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004359 HInvokeUnresolved(ArenaAllocator* allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004360 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004361 DataType::Type return_type,
Calin Juravle175dc732015-08-25 15:42:32 +01004362 uint32_t dex_pc,
4363 uint32_t dex_method_index,
4364 InvokeType invoke_type)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304365 : HInvoke(kInvokeUnresolved,
4366 allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004367 number_of_arguments,
4368 0u /* number_of_other_inputs */,
4369 return_type,
4370 dex_pc,
4371 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004372 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01004373 invoke_type) {
4374 }
4375
Artem Serovcced8ba2017-07-19 18:18:09 +01004376 bool IsClonable() const OVERRIDE { return true; }
4377
Calin Juravle175dc732015-08-25 15:42:32 +01004378 DECLARE_INSTRUCTION(InvokeUnresolved);
4379
Artem Serovcced8ba2017-07-19 18:18:09 +01004380 protected:
4381 DEFAULT_COPY_CONSTRUCTOR(InvokeUnresolved);
Calin Juravle175dc732015-08-25 15:42:32 +01004382};
4383
Orion Hodsonac141392017-01-13 11:53:47 +00004384class HInvokePolymorphic FINAL : public HInvoke {
4385 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004386 HInvokePolymorphic(ArenaAllocator* allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004387 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004388 DataType::Type return_type,
Orion Hodsonac141392017-01-13 11:53:47 +00004389 uint32_t dex_pc,
4390 uint32_t dex_method_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304391 : HInvoke(kInvokePolymorphic,
4392 allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004393 number_of_arguments,
4394 0u /* number_of_other_inputs */,
4395 return_type,
4396 dex_pc,
4397 dex_method_index,
4398 nullptr,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304399 kVirtual) {
4400 }
Orion Hodsonac141392017-01-13 11:53:47 +00004401
Artem Serovcced8ba2017-07-19 18:18:09 +01004402 bool IsClonable() const OVERRIDE { return true; }
4403
Orion Hodsonac141392017-01-13 11:53:47 +00004404 DECLARE_INSTRUCTION(InvokePolymorphic);
4405
Artem Serovcced8ba2017-07-19 18:18:09 +01004406 protected:
4407 DEFAULT_COPY_CONSTRUCTOR(InvokePolymorphic);
Orion Hodsonac141392017-01-13 11:53:47 +00004408};
4409
Vladimir Markofcb503c2016-05-18 12:48:17 +01004410class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004411 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004412 // Requirements of this method call regarding the class
4413 // initialization (clinit) check of its declaring class.
4414 enum class ClinitCheckRequirement {
4415 kNone, // Class already initialized.
4416 kExplicit, // Static call having explicit clinit check as last input.
4417 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004418 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004419 };
4420
Vladimir Marko58155012015-08-19 12:49:41 +00004421 // Determines how to load the target ArtMethod*.
4422 enum class MethodLoadKind {
4423 // Use a String init ArtMethod* loaded from Thread entrypoints.
4424 kStringInit,
4425
4426 // Use the method's own ArtMethod* loaded by the register allocator.
4427 kRecursive,
4428
Vladimir Marko65979462017-05-19 17:25:12 +01004429 // Use PC-relative boot image ArtMethod* address that will be known at link time.
4430 // Used for boot image methods referenced by boot image code.
4431 kBootImageLinkTimePcRelative,
4432
Vladimir Marko58155012015-08-19 12:49:41 +00004433 // Use ArtMethod* at a known address, embed the direct address in the code.
4434 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
4435 kDirectAddress,
4436
Vladimir Markob066d432018-01-03 13:14:37 +00004437 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
4438 // Used for app->boot calls with relocatable image.
4439 kBootImageRelRo,
4440
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004441 // Load from an entry in the .bss section using a PC-relative load.
4442 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
4443 kBssEntry,
Vladimir Marko58155012015-08-19 12:49:41 +00004444
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004445 // Make a runtime call to resolve and call the method. This is the last-resort-kind
4446 // used when other kinds are unimplemented on a particular architecture.
4447 kRuntimeCall,
Vladimir Marko58155012015-08-19 12:49:41 +00004448 };
4449
4450 // Determines the location of the code pointer.
4451 enum class CodePtrLocation {
4452 // Recursive call, use local PC-relative call instruction.
4453 kCallSelf,
4454
Vladimir Marko58155012015-08-19 12:49:41 +00004455 // Use code pointer from the ArtMethod*.
4456 // Used when we don't know the target code. This is also the last-resort-kind used when
4457 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4458 kCallArtMethod,
4459 };
4460
4461 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004462 MethodLoadKind method_load_kind;
4463 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004464 // The method load data holds
4465 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4466 // Note that there are multiple string init methods, each having its own offset.
4467 // - the method address for kDirectAddress
Vladimir Markodc151b22015-10-15 18:02:30 +01004468 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004469 };
4470
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004471 HInvokeStaticOrDirect(ArenaAllocator* allocator,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004472 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004473 DataType::Type return_type,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004474 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004475 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004476 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004477 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004478 InvokeType invoke_type,
4479 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004480 ClinitCheckRequirement clinit_check_requirement)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304481 : HInvoke(kInvokeStaticOrDirect,
4482 allocator,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004483 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004484 // There is potentially one extra argument for the HCurrentMethod node, and
4485 // potentially one other if the clinit check is explicit, and potentially
4486 // one other if the method is a string factory.
4487 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004488 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004489 return_type,
4490 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004491 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004492 resolved_method,
4493 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004494 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004495 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004496 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4497 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004498
Artem Serovcced8ba2017-07-19 18:18:09 +01004499 bool IsClonable() const OVERRIDE { return true; }
4500
Vladimir Markodc151b22015-10-15 18:02:30 +01004501 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004502 bool had_current_method_input = HasCurrentMethodInput();
4503 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4504
4505 // Using the current method is the default and once we find a better
4506 // method load kind, we should not go back to using the current method.
4507 DCHECK(had_current_method_input || !needs_current_method_input);
4508
4509 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004510 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4511 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004512 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004513 dispatch_info_ = dispatch_info;
4514 }
4515
Aart Bik6daebeb2017-04-03 14:35:41 -07004516 DispatchInfo GetDispatchInfo() const {
4517 return dispatch_info_;
4518 }
4519
Vladimir Markoc53c0792015-11-19 15:48:33 +00004520 void AddSpecialInput(HInstruction* input) {
4521 // We allow only one special input.
4522 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4523 DCHECK(InputCount() == GetSpecialInputIndex() ||
4524 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4525 InsertInputAt(GetSpecialInputIndex(), input);
4526 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004527
Vladimir Marko372f10e2016-05-17 16:30:10 +01004528 using HInstruction::GetInputRecords; // Keep the const version visible.
4529 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
4530 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4531 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4532 DCHECK(!input_records.empty());
4533 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4534 HInstruction* last_input = input_records.back().GetInstruction();
4535 // Note: `last_input` may be null during arguments setup.
4536 if (last_input != nullptr) {
4537 // `last_input` is the last input of a static invoke marked as having
4538 // an explicit clinit check. It must either be:
4539 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4540 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4541 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4542 }
4543 }
4544 return input_records;
4545 }
4546
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004547 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004548 // We access the method via the dex cache so we can't do an implicit null check.
4549 // TODO: for intrinsics we can generate implicit null checks.
4550 return false;
4551 }
4552
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004553 bool CanBeNull() const OVERRIDE {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004554 return GetPackedField<ReturnTypeField>() == DataType::Type::kReference && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004555 }
4556
Vladimir Markoc53c0792015-11-19 15:48:33 +00004557 // Get the index of the special input, if any.
4558 //
David Brazdil6de19382016-01-08 17:37:10 +00004559 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4560 // method pointer; otherwise there may be one platform-specific special input,
4561 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004562 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004563 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004564
Vladimir Marko58155012015-08-19 12:49:41 +00004565 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4566 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4567 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004568 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004569 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004570 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko65979462017-05-19 17:25:12 +01004571 bool HasPcRelativeMethodLoadKind() const {
4572 return GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markob066d432018-01-03 13:14:37 +00004573 GetMethodLoadKind() == MethodLoadKind::kBootImageRelRo ||
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004574 GetMethodLoadKind() == MethodLoadKind::kBssEntry;
Vladimir Marko65979462017-05-19 17:25:12 +01004575 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004576 bool HasCurrentMethodInput() const {
4577 // This function can be called only after the invoke has been fully initialized by the builder.
4578 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004579 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004580 return true;
4581 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004582 DCHECK(InputCount() == GetSpecialInputIndex() ||
4583 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004584 return false;
4585 }
4586 }
Vladimir Marko58155012015-08-19 12:49:41 +00004587
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004588 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004589 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004590 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004591 }
4592
4593 uint64_t GetMethodAddress() const {
4594 DCHECK(HasMethodAddress());
4595 return dispatch_info_.method_load_data;
4596 }
4597
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004598 const DexFile& GetDexFileForPcRelativeDexCache() const;
4599
Vladimir Markoa1de9182016-02-25 11:37:38 +00004600 ClinitCheckRequirement GetClinitCheckRequirement() const {
4601 return GetPackedField<ClinitCheckRequirementField>();
4602 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004603
Roland Levillain4c0eb422015-04-24 16:43:49 +01004604 // Is this instruction a call to a static method?
4605 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004606 return GetInvokeType() == kStatic;
4607 }
4608
4609 MethodReference GetTargetMethod() const {
4610 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004611 }
4612
Vladimir Markofbb184a2015-11-13 14:47:00 +00004613 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4614 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4615 // instruction; only relevant for static calls with explicit clinit check.
4616 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004617 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004618 size_t last_input_index = inputs_.size() - 1u;
4619 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004620 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004621 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004622 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004623 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004624 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004625 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004626 }
4627
4628 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004629 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004630 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004631 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004632 }
4633
4634 // Is this a call to a static method whose declaring class has an
4635 // implicit intialization check requirement?
4636 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004637 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004638 }
4639
Vladimir Markob554b5a2015-11-06 12:57:55 +00004640 // Does this method load kind need the current method as an input?
4641 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004642 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kRuntimeCall;
Vladimir Markob554b5a2015-11-06 12:57:55 +00004643 }
4644
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004645 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004646
Artem Serovcced8ba2017-07-19 18:18:09 +01004647 protected:
4648 DEFAULT_COPY_CONSTRUCTOR(InvokeStaticOrDirect);
4649
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004650 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004651 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004652 static constexpr size_t kFieldClinitCheckRequirementSize =
4653 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4654 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4655 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4656 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4657 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004658 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4659 kFieldClinitCheckRequirement,
4660 kFieldClinitCheckRequirementSize>;
4661
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004662 // Cached values of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffrayea179f42018-02-08 22:30:18 +00004663 const MethodReference target_method_;
Vladimir Marko58155012015-08-19 12:49:41 +00004664 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004665};
Vladimir Markof64242a2015-12-01 14:58:23 +00004666std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004667std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004668
Vladimir Markofcb503c2016-05-18 12:48:17 +01004669class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004670 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004671 HInvokeVirtual(ArenaAllocator* allocator,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004672 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004673 DataType::Type return_type,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004674 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004675 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004676 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004677 uint32_t vtable_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304678 : HInvoke(kInvokeVirtual,
4679 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004680 number_of_arguments,
4681 0u,
4682 return_type,
4683 dex_pc,
4684 dex_method_index,
4685 resolved_method,
4686 kVirtual),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304687 vtable_index_(vtable_index) {
4688 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004689
Artem Serovcced8ba2017-07-19 18:18:09 +01004690 bool IsClonable() const OVERRIDE { return true; }
4691
Aart Bik71bf7b42016-11-16 10:17:46 -08004692 bool CanBeNull() const OVERRIDE {
4693 switch (GetIntrinsic()) {
4694 case Intrinsics::kThreadCurrentThread:
4695 case Intrinsics::kStringBufferAppend:
4696 case Intrinsics::kStringBufferToString:
4697 case Intrinsics::kStringBuilderAppend:
4698 case Intrinsics::kStringBuilderToString:
4699 return false;
4700 default:
4701 return HInvoke::CanBeNull();
4702 }
4703 }
4704
Calin Juravle641547a2015-04-21 22:08:51 +01004705 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004706 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004707 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004708 }
4709
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004710 uint32_t GetVTableIndex() const { return vtable_index_; }
4711
4712 DECLARE_INSTRUCTION(InvokeVirtual);
4713
Artem Serovcced8ba2017-07-19 18:18:09 +01004714 protected:
4715 DEFAULT_COPY_CONSTRUCTOR(InvokeVirtual);
4716
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004717 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004718 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004719 const uint32_t vtable_index_;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004720};
4721
Vladimir Markofcb503c2016-05-18 12:48:17 +01004722class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004723 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004724 HInvokeInterface(ArenaAllocator* allocator,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004725 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004726 DataType::Type return_type,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004727 uint32_t dex_pc,
4728 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004729 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004730 uint32_t imt_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304731 : HInvoke(kInvokeInterface,
4732 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004733 number_of_arguments,
4734 0u,
4735 return_type,
4736 dex_pc,
4737 dex_method_index,
4738 resolved_method,
4739 kInterface),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304740 imt_index_(imt_index) {
4741 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004742
Artem Serovcced8ba2017-07-19 18:18:09 +01004743 bool IsClonable() const OVERRIDE { return true; }
4744
Calin Juravle641547a2015-04-21 22:08:51 +01004745 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004746 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004747 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004748 }
4749
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004750 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
4751 // The assembly stub currently needs it.
4752 return true;
4753 }
4754
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004755 uint32_t GetImtIndex() const { return imt_index_; }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004756
4757 DECLARE_INSTRUCTION(InvokeInterface);
4758
Artem Serovcced8ba2017-07-19 18:18:09 +01004759 protected:
4760 DEFAULT_COPY_CONSTRUCTOR(InvokeInterface);
4761
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004762 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004763 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004764 const uint32_t imt_index_;
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004765};
4766
Vladimir Markofcb503c2016-05-18 12:48:17 +01004767class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004768 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004769 HNeg(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304770 : HUnaryOperation(kNeg, result_type, input, dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004771 DCHECK_EQ(result_type, DataType::Kind(input->GetType()));
Roland Levillain937e6cd2016-03-22 11:54:37 +00004772 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004773
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004774 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004775
4776 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004777 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004778 }
4779 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004780 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004781 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004782 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4783 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4784 }
4785 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4786 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4787 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004788
Roland Levillain88cb1752014-10-20 16:36:47 +01004789 DECLARE_INSTRUCTION(Neg);
4790
Artem Serovcced8ba2017-07-19 18:18:09 +01004791 protected:
4792 DEFAULT_COPY_CONSTRUCTOR(Neg);
Roland Levillain88cb1752014-10-20 16:36:47 +01004793};
4794
Vladimir Markofcb503c2016-05-18 12:48:17 +01004795class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004796 public:
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004797 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304798 : HExpression(kNewArray, DataType::Type::kReference, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004799 SetRawInputAt(0, cls);
4800 SetRawInputAt(1, length);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004801 }
4802
Artem Serovcced8ba2017-07-19 18:18:09 +01004803 bool IsClonable() const OVERRIDE { return true; }
4804
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004805 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004806 bool NeedsEnvironment() const OVERRIDE { return true; }
4807
Mingyao Yang0c365e62015-03-31 15:09:29 -07004808 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4809 bool CanThrow() const OVERRIDE { return true; }
4810
Calin Juravle10e244f2015-01-26 18:54:32 +00004811 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004812
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004813 HLoadClass* GetLoadClass() const {
4814 DCHECK(InputAt(0)->IsLoadClass());
4815 return InputAt(0)->AsLoadClass();
4816 }
4817
4818 HInstruction* GetLength() const {
4819 return InputAt(1);
4820 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004821
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004822 DECLARE_INSTRUCTION(NewArray);
4823
Artem Serovcced8ba2017-07-19 18:18:09 +01004824 protected:
4825 DEFAULT_COPY_CONSTRUCTOR(NewArray);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004826};
4827
Vladimir Markofcb503c2016-05-18 12:48:17 +01004828class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004829 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004830 HAdd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004831 HInstruction* left,
4832 HInstruction* right,
4833 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304834 : HBinaryOperation(kAdd, result_type, left, right, SideEffects::None(), dex_pc) {
4835 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004836
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004837 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004838
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004839 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004840
4841 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004842 return GetBlock()->GetGraph()->GetIntConstant(
4843 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004844 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004845 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004846 return GetBlock()->GetGraph()->GetLongConstant(
4847 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004848 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004849 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4850 return GetBlock()->GetGraph()->GetFloatConstant(
4851 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4852 }
4853 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4854 return GetBlock()->GetGraph()->GetDoubleConstant(
4855 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4856 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004857
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004858 DECLARE_INSTRUCTION(Add);
4859
Artem Serovcced8ba2017-07-19 18:18:09 +01004860 protected:
4861 DEFAULT_COPY_CONSTRUCTOR(Add);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004862};
4863
Vladimir Markofcb503c2016-05-18 12:48:17 +01004864class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004865 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004866 HSub(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004867 HInstruction* left,
4868 HInstruction* right,
4869 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304870 : HBinaryOperation(kSub, result_type, left, right, SideEffects::None(), dex_pc) {
4871 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004872
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004873 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004874
4875 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004876 return GetBlock()->GetGraph()->GetIntConstant(
4877 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004878 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004879 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004880 return GetBlock()->GetGraph()->GetLongConstant(
4881 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004882 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004883 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4884 return GetBlock()->GetGraph()->GetFloatConstant(
4885 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4886 }
4887 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4888 return GetBlock()->GetGraph()->GetDoubleConstant(
4889 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4890 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004891
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004892 DECLARE_INSTRUCTION(Sub);
4893
Artem Serovcced8ba2017-07-19 18:18:09 +01004894 protected:
4895 DEFAULT_COPY_CONSTRUCTOR(Sub);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004896};
4897
Vladimir Markofcb503c2016-05-18 12:48:17 +01004898class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004899 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004900 HMul(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004901 HInstruction* left,
4902 HInstruction* right,
4903 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304904 : HBinaryOperation(kMul, result_type, left, right, SideEffects::None(), dex_pc) {
4905 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004906
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004907 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004908
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004909 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004910
4911 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004912 return GetBlock()->GetGraph()->GetIntConstant(
4913 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004914 }
4915 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004916 return GetBlock()->GetGraph()->GetLongConstant(
4917 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004918 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004919 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4920 return GetBlock()->GetGraph()->GetFloatConstant(
4921 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4922 }
4923 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4924 return GetBlock()->GetGraph()->GetDoubleConstant(
4925 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4926 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004927
4928 DECLARE_INSTRUCTION(Mul);
4929
Artem Serovcced8ba2017-07-19 18:18:09 +01004930 protected:
4931 DEFAULT_COPY_CONSTRUCTOR(Mul);
Calin Juravle34bacdf2014-10-07 20:23:36 +01004932};
4933
Vladimir Markofcb503c2016-05-18 12:48:17 +01004934class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004935 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004936 HDiv(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004937 HInstruction* left,
4938 HInstruction* right,
4939 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304940 : HBinaryOperation(kDiv, result_type, left, right, SideEffects::None(), dex_pc) {
4941 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004942
Roland Levillain9867bc72015-08-05 10:21:34 +01004943 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004944 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004945 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004946 // Our graph structure ensures we never have 0 for `y` during
4947 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004948 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004949 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004950 return (y == -1) ? -x : x / y;
4951 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004952
Roland Levillain31dd3d62016-02-16 12:21:02 +00004953 template <typename T>
4954 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004955 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004956 return x / y;
4957 }
4958
Roland Levillain9867bc72015-08-05 10:21:34 +01004959 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004960 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004961 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004962 }
4963 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004964 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004965 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4966 }
4967 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4968 return GetBlock()->GetGraph()->GetFloatConstant(
4969 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4970 }
4971 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4972 return GetBlock()->GetGraph()->GetDoubleConstant(
4973 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004974 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004975
4976 DECLARE_INSTRUCTION(Div);
4977
Artem Serovcced8ba2017-07-19 18:18:09 +01004978 protected:
4979 DEFAULT_COPY_CONSTRUCTOR(Div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00004980};
4981
Vladimir Markofcb503c2016-05-18 12:48:17 +01004982class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004983 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004984 HRem(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004985 HInstruction* left,
4986 HInstruction* right,
4987 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304988 : HBinaryOperation(kRem, result_type, left, right, SideEffects::None(), dex_pc) {
4989 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004990
Roland Levillain9867bc72015-08-05 10:21:34 +01004991 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004992 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004993 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004994 // Our graph structure ensures we never have 0 for `y` during
4995 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004996 DCHECK_NE(y, 0);
4997 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4998 return (y == -1) ? 0 : x % y;
4999 }
5000
Roland Levillain31dd3d62016-02-16 12:21:02 +00005001 template <typename T>
5002 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005003 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00005004 return std::fmod(x, y);
5005 }
5006
Roland Levillain9867bc72015-08-05 10:21:34 +01005007 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005008 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005009 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005010 }
5011 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005012 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005013 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005014 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005015 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
5016 return GetBlock()->GetGraph()->GetFloatConstant(
5017 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5018 }
5019 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
5020 return GetBlock()->GetGraph()->GetDoubleConstant(
5021 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5022 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005023
5024 DECLARE_INSTRUCTION(Rem);
5025
Artem Serovcced8ba2017-07-19 18:18:09 +01005026 protected:
5027 DEFAULT_COPY_CONSTRUCTOR(Rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00005028};
5029
Aart Bik1f8d51b2018-02-15 10:42:37 -08005030class HMin FINAL : public HBinaryOperation {
5031 public:
5032 HMin(DataType::Type result_type,
5033 HInstruction* left,
5034 HInstruction* right,
5035 uint32_t dex_pc)
5036 : HBinaryOperation(kMin, result_type, left, right, SideEffects::None(), dex_pc) {}
5037
5038 bool IsCommutative() const OVERRIDE { return true; }
5039
5040 // Evaluation for integral values.
5041 template <typename T> static T ComputeIntegral(T x, T y) {
5042 return (x <= y) ? x : y;
5043 }
5044
5045 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
5046 return GetBlock()->GetGraph()->GetIntConstant(
5047 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5048 }
5049 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
5050 return GetBlock()->GetGraph()->GetLongConstant(
5051 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5052 }
5053 // TODO: Evaluation for floating-point values.
5054 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5055 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { return nullptr; }
5056 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5057 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { return nullptr; }
5058
5059 DECLARE_INSTRUCTION(Min);
5060
5061 protected:
5062 DEFAULT_COPY_CONSTRUCTOR(Min);
5063};
5064
5065class HMax FINAL : public HBinaryOperation {
5066 public:
5067 HMax(DataType::Type result_type,
5068 HInstruction* left,
5069 HInstruction* right,
5070 uint32_t dex_pc)
5071 : HBinaryOperation(kMax, result_type, left, right, SideEffects::None(), dex_pc) {}
5072
5073 bool IsCommutative() const OVERRIDE { return true; }
5074
5075 // Evaluation for integral values.
5076 template <typename T> static T ComputeIntegral(T x, T y) {
5077 return (x >= y) ? x : y;
5078 }
5079
5080 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
5081 return GetBlock()->GetGraph()->GetIntConstant(
5082 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5083 }
5084 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
5085 return GetBlock()->GetGraph()->GetLongConstant(
5086 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5087 }
5088 // TODO: Evaluation for floating-point values.
5089 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5090 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { return nullptr; }
5091 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5092 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { return nullptr; }
5093
5094 DECLARE_INSTRUCTION(Max);
5095
5096 protected:
5097 DEFAULT_COPY_CONSTRUCTOR(Max);
5098};
5099
Aart Bik3dad3412018-02-28 12:01:46 -08005100class HAbs FINAL : public HUnaryOperation {
5101 public:
5102 HAbs(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
5103 : HUnaryOperation(kAbs, result_type, input, dex_pc) {}
5104
5105 // Evaluation for integral values.
5106 template <typename T> static T ComputeIntegral(T x) {
5107 return x < 0 ? -x : x;
5108 }
5109
5110 // Evaluation for floating-point values.
5111 // Note, as a "quality of implementation", rather than pure "spec compliance",
5112 // we require that Math.abs() clears the sign bit (but changes nothing else)
5113 // for all floating-point numbers, including NaN (signaling NaN may become quiet though).
5114 // http://b/30758343
5115 template <typename T, typename S> static T ComputeFP(T x) {
5116 S bits = bit_cast<S, T>(x);
5117 return bit_cast<T, S>(bits & std::numeric_limits<S>::max());
5118 }
5119
5120 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
5121 return GetBlock()->GetGraph()->GetIntConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5122 }
5123 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
5124 return GetBlock()->GetGraph()->GetLongConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5125 }
5126 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
5127 return GetBlock()->GetGraph()->GetFloatConstant(
5128 ComputeFP<float, int32_t>(x->GetValue()), GetDexPc());
5129 }
5130 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
5131 return GetBlock()->GetGraph()->GetDoubleConstant(
5132 ComputeFP<double, int64_t>(x->GetValue()), GetDexPc());
5133 }
5134
5135 DECLARE_INSTRUCTION(Abs);
5136
5137 protected:
5138 DEFAULT_COPY_CONSTRUCTOR(Abs);
5139};
5140
Vladimir Markofcb503c2016-05-18 12:48:17 +01005141class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00005142 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00005143 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
5144 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00005145 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305146 : HExpression(kDivZeroCheck, value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00005147 SetRawInputAt(0, value);
5148 }
5149
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005150 DataType::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
Serguei Katkov8c0676c2015-08-03 13:55:33 +06005151
Calin Juravled0d48522014-11-04 16:40:20 +00005152 bool CanBeMoved() const OVERRIDE { return true; }
5153
Vladimir Marko372f10e2016-05-17 16:30:10 +01005154 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00005155 return true;
5156 }
5157
5158 bool NeedsEnvironment() const OVERRIDE { return true; }
5159 bool CanThrow() const OVERRIDE { return true; }
5160
Calin Juravled0d48522014-11-04 16:40:20 +00005161 DECLARE_INSTRUCTION(DivZeroCheck);
5162
Artem Serovcced8ba2017-07-19 18:18:09 +01005163 protected:
5164 DEFAULT_COPY_CONSTRUCTOR(DivZeroCheck);
Calin Juravled0d48522014-11-04 16:40:20 +00005165};
5166
Vladimir Markofcb503c2016-05-18 12:48:17 +01005167class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005168 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005169 HShl(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005170 HInstruction* value,
5171 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005172 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305173 : HBinaryOperation(kShl, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005174 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5175 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005176 }
5177
Roland Levillain5b5b9312016-03-22 14:57:31 +00005178 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005179 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005180 return value << (distance & max_shift_distance);
5181 }
5182
5183 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005184 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005185 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005186 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005187 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005188 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005189 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005190 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005191 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5192 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5193 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5194 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005195 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005196 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5197 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005198 LOG(FATAL) << DebugName() << " is not defined for float values";
5199 UNREACHABLE();
5200 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005201 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5202 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005203 LOG(FATAL) << DebugName() << " is not defined for double values";
5204 UNREACHABLE();
5205 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005206
5207 DECLARE_INSTRUCTION(Shl);
5208
Artem Serovcced8ba2017-07-19 18:18:09 +01005209 protected:
5210 DEFAULT_COPY_CONSTRUCTOR(Shl);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005211};
5212
Vladimir Markofcb503c2016-05-18 12:48:17 +01005213class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005214 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005215 HShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005216 HInstruction* value,
5217 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005218 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305219 : HBinaryOperation(kShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005220 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5221 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005222 }
5223
Roland Levillain5b5b9312016-03-22 14:57:31 +00005224 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005225 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005226 return value >> (distance & max_shift_distance);
5227 }
5228
5229 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005230 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005231 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005232 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005233 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005234 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005235 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005236 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005237 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5238 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5239 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5240 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005241 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005242 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5243 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005244 LOG(FATAL) << DebugName() << " is not defined for float values";
5245 UNREACHABLE();
5246 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005247 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5248 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005249 LOG(FATAL) << DebugName() << " is not defined for double values";
5250 UNREACHABLE();
5251 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005252
5253 DECLARE_INSTRUCTION(Shr);
5254
Artem Serovcced8ba2017-07-19 18:18:09 +01005255 protected:
5256 DEFAULT_COPY_CONSTRUCTOR(Shr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005257};
5258
Vladimir Markofcb503c2016-05-18 12:48:17 +01005259class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005260 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005261 HUShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005262 HInstruction* value,
5263 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005264 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305265 : HBinaryOperation(kUShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005266 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5267 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00005268 }
5269
Roland Levillain5b5b9312016-03-22 14:57:31 +00005270 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005271 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005272 typedef typename std::make_unsigned<T>::type V;
5273 V ux = static_cast<V>(value);
5274 return static_cast<T>(ux >> (distance & max_shift_distance));
5275 }
5276
5277 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005278 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005279 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005280 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005281 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005282 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005283 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005284 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005285 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5286 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5287 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5288 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00005289 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005290 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5291 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005292 LOG(FATAL) << DebugName() << " is not defined for float values";
5293 UNREACHABLE();
5294 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005295 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5296 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005297 LOG(FATAL) << DebugName() << " is not defined for double values";
5298 UNREACHABLE();
5299 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005300
5301 DECLARE_INSTRUCTION(UShr);
5302
Artem Serovcced8ba2017-07-19 18:18:09 +01005303 protected:
5304 DEFAULT_COPY_CONSTRUCTOR(UShr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005305};
5306
Vladimir Markofcb503c2016-05-18 12:48:17 +01005307class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005308 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005309 HAnd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005310 HInstruction* left,
5311 HInstruction* right,
5312 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305313 : HBinaryOperation(kAnd, result_type, left, right, SideEffects::None(), dex_pc) {
5314 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005315
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005316 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005317
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005318 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005319
5320 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005321 return GetBlock()->GetGraph()->GetIntConstant(
5322 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005323 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005324 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005325 return GetBlock()->GetGraph()->GetLongConstant(
5326 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005327 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005328 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5329 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5330 LOG(FATAL) << DebugName() << " is not defined for float values";
5331 UNREACHABLE();
5332 }
5333 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5334 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5335 LOG(FATAL) << DebugName() << " is not defined for double values";
5336 UNREACHABLE();
5337 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005338
5339 DECLARE_INSTRUCTION(And);
5340
Artem Serovcced8ba2017-07-19 18:18:09 +01005341 protected:
5342 DEFAULT_COPY_CONSTRUCTOR(And);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005343};
5344
Vladimir Markofcb503c2016-05-18 12:48:17 +01005345class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005346 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005347 HOr(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005348 HInstruction* left,
5349 HInstruction* right,
5350 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305351 : HBinaryOperation(kOr, result_type, left, right, SideEffects::None(), dex_pc) {
5352 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005353
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005354 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005355
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005356 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005357
5358 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005359 return GetBlock()->GetGraph()->GetIntConstant(
5360 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005361 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005362 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005363 return GetBlock()->GetGraph()->GetLongConstant(
5364 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005365 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005366 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5367 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5368 LOG(FATAL) << DebugName() << " is not defined for float values";
5369 UNREACHABLE();
5370 }
5371 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5372 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5373 LOG(FATAL) << DebugName() << " is not defined for double values";
5374 UNREACHABLE();
5375 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005376
5377 DECLARE_INSTRUCTION(Or);
5378
Artem Serovcced8ba2017-07-19 18:18:09 +01005379 protected:
5380 DEFAULT_COPY_CONSTRUCTOR(Or);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005381};
5382
Vladimir Markofcb503c2016-05-18 12:48:17 +01005383class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005384 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005385 HXor(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005386 HInstruction* left,
5387 HInstruction* right,
5388 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305389 : HBinaryOperation(kXor, result_type, left, right, SideEffects::None(), dex_pc) {
5390 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005391
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005392 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005393
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005394 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005395
5396 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005397 return GetBlock()->GetGraph()->GetIntConstant(
5398 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005399 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005400 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005401 return GetBlock()->GetGraph()->GetLongConstant(
5402 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005403 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005404 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5405 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5406 LOG(FATAL) << DebugName() << " is not defined for float values";
5407 UNREACHABLE();
5408 }
5409 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5410 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5411 LOG(FATAL) << DebugName() << " is not defined for double values";
5412 UNREACHABLE();
5413 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005414
5415 DECLARE_INSTRUCTION(Xor);
5416
Artem Serovcced8ba2017-07-19 18:18:09 +01005417 protected:
5418 DEFAULT_COPY_CONSTRUCTOR(Xor);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005419};
5420
Vladimir Markofcb503c2016-05-18 12:48:17 +01005421class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005422 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005423 HRor(DataType::Type result_type, HInstruction* value, HInstruction* distance)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305424 : HBinaryOperation(kRor, result_type, value, distance) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005425 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5426 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00005427 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005428
Roland Levillain5b5b9312016-03-22 14:57:31 +00005429 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005430 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005431 typedef typename std::make_unsigned<T>::type V;
5432 V ux = static_cast<V>(value);
5433 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005434 return static_cast<T>(ux);
5435 } else {
5436 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005437 return static_cast<T>(ux >> (distance & max_shift_value)) |
5438 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005439 }
5440 }
5441
Roland Levillain5b5b9312016-03-22 14:57:31 +00005442 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005443 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005444 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005445 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005446 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005447 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005448 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005449 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005450 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5451 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5452 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5453 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005454 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005455 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5456 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005457 LOG(FATAL) << DebugName() << " is not defined for float values";
5458 UNREACHABLE();
5459 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005460 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5461 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005462 LOG(FATAL) << DebugName() << " is not defined for double values";
5463 UNREACHABLE();
5464 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005465
5466 DECLARE_INSTRUCTION(Ror);
5467
Artem Serovcced8ba2017-07-19 18:18:09 +01005468 protected:
5469 DEFAULT_COPY_CONSTRUCTOR(Ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005470};
5471
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005472// The value of a parameter in this method. Its location depends on
5473// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01005474class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005475 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005476 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005477 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005478 uint8_t index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005479 DataType::Type parameter_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005480 bool is_this = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305481 : HExpression(kParameterValue, parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005482 dex_file_(dex_file),
5483 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005484 index_(index) {
5485 SetPackedFlag<kFlagIsThis>(is_this);
5486 SetPackedFlag<kFlagCanBeNull>(!is_this);
5487 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005488
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005489 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005490 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005491 uint8_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07005492 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005493
Vladimir Markoa1de9182016-02-25 11:37:38 +00005494 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
5495 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005496
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005497 DECLARE_INSTRUCTION(ParameterValue);
5498
Artem Serovcced8ba2017-07-19 18:18:09 +01005499 protected:
5500 DEFAULT_COPY_CONSTRUCTOR(ParameterValue);
5501
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005502 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005503 // Whether or not the parameter value corresponds to 'this' argument.
5504 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
5505 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5506 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5507 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5508 "Too many packed fields.");
5509
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005510 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005511 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005512 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005513 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005514 const uint8_t index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005515};
5516
Vladimir Markofcb503c2016-05-18 12:48:17 +01005517class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005518 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005519 HNot(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305520 : HUnaryOperation(kNot, result_type, input, dex_pc) {
5521 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005522
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005523 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005524 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005525 return true;
5526 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005527
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005528 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005529
5530 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005531 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005532 }
5533 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005534 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005535 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005536 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5537 LOG(FATAL) << DebugName() << " is not defined for float values";
5538 UNREACHABLE();
5539 }
5540 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5541 LOG(FATAL) << DebugName() << " is not defined for double values";
5542 UNREACHABLE();
5543 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005544
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005545 DECLARE_INSTRUCTION(Not);
5546
Artem Serovcced8ba2017-07-19 18:18:09 +01005547 protected:
5548 DEFAULT_COPY_CONSTRUCTOR(Not);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005549};
5550
Vladimir Markofcb503c2016-05-18 12:48:17 +01005551class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005552 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005553 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305554 : HUnaryOperation(kBooleanNot, DataType::Type::kBool, input, dex_pc) {
5555 }
David Brazdil66d126e2015-04-03 16:02:44 +01005556
5557 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005558 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01005559 return true;
5560 }
5561
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005562 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005563 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005564 return !x;
5565 }
5566
Roland Levillain9867bc72015-08-05 10:21:34 +01005567 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005568 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005569 }
5570 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5571 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005572 UNREACHABLE();
5573 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005574 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5575 LOG(FATAL) << DebugName() << " is not defined for float values";
5576 UNREACHABLE();
5577 }
5578 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5579 LOG(FATAL) << DebugName() << " is not defined for double values";
5580 UNREACHABLE();
5581 }
David Brazdil66d126e2015-04-03 16:02:44 +01005582
5583 DECLARE_INSTRUCTION(BooleanNot);
5584
Artem Serovcced8ba2017-07-19 18:18:09 +01005585 protected:
5586 DEFAULT_COPY_CONSTRUCTOR(BooleanNot);
David Brazdil66d126e2015-04-03 16:02:44 +01005587};
5588
Vladimir Markofcb503c2016-05-18 12:48:17 +01005589class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005590 public:
5591 // Instantiate a type conversion of `input` to `result_type`.
Vladimir Markoc8fb2112017-10-03 11:37:52 +01005592 HTypeConversion(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305593 : HExpression(kTypeConversion, result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005594 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005595 // Invariant: We should never generate a conversion to a Boolean value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005596 DCHECK_NE(DataType::Type::kBool, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005597 }
5598
5599 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005600 DataType::Type GetInputType() const { return GetInput()->GetType(); }
5601 DataType::Type GetResultType() const { return GetType(); }
Roland Levillaindff1f282014-11-05 14:15:05 +00005602
5603 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005604 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5605 return true;
5606 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005607
Mark Mendelle82549b2015-05-06 10:55:34 -04005608 // Try to statically evaluate the conversion and return a HConstant
5609 // containing the result. If the input cannot be converted, return nullptr.
5610 HConstant* TryStaticEvaluation() const;
5611
Roland Levillaindff1f282014-11-05 14:15:05 +00005612 DECLARE_INSTRUCTION(TypeConversion);
5613
Artem Serovcced8ba2017-07-19 18:18:09 +01005614 protected:
5615 DEFAULT_COPY_CONSTRUCTOR(TypeConversion);
Roland Levillaindff1f282014-11-05 14:15:05 +00005616};
5617
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005618static constexpr uint32_t kNoRegNumber = -1;
5619
Vladimir Markofcb503c2016-05-18 12:48:17 +01005620class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005621 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005622 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
5623 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005624 HNullCheck(HInstruction* value, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305625 : HExpression(kNullCheck, value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005626 SetRawInputAt(0, value);
5627 }
5628
Artem Serovcced8ba2017-07-19 18:18:09 +01005629 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00005630 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005631 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005632 return true;
5633 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005634
Calin Juravle10e244f2015-01-26 18:54:32 +00005635 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005636
Calin Juravle10e244f2015-01-26 18:54:32 +00005637 bool CanThrow() const OVERRIDE { return true; }
5638
5639 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005640
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005641 DECLARE_INSTRUCTION(NullCheck);
5642
Artem Serovcced8ba2017-07-19 18:18:09 +01005643 protected:
5644 DEFAULT_COPY_CONSTRUCTOR(NullCheck);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005645};
5646
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005647// Embeds an ArtField and all the information required by the compiler. We cache
5648// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005649class FieldInfo : public ValueObject {
5650 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005651 FieldInfo(ArtField* field,
5652 MemberOffset field_offset,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005653 DataType::Type field_type,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005654 bool is_volatile,
5655 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005656 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005657 const DexFile& dex_file)
5658 : field_(field),
5659 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005660 field_type_(field_type),
5661 is_volatile_(is_volatile),
5662 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005663 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005664 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005665
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005666 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005667 MemberOffset GetFieldOffset() const { return field_offset_; }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005668 DataType::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005669 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005670 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005671 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005672 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005673
5674 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005675 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005676 const MemberOffset field_offset_;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005677 const DataType::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005678 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005679 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005680 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005681 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005682};
5683
Vladimir Markofcb503c2016-05-18 12:48:17 +01005684class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005685 public:
5686 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005687 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005688 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005689 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005690 bool is_volatile,
5691 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005692 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005693 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005694 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305695 : HExpression(kInstanceFieldGet,
5696 field_type,
5697 SideEffects::FieldReadOfType(field_type, is_volatile),
5698 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005699 field_info_(field,
5700 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005701 field_type,
5702 is_volatile,
5703 field_idx,
5704 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005705 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005706 SetRawInputAt(0, value);
5707 }
5708
Artem Serovcced8ba2017-07-19 18:18:09 +01005709 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005710 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005711
Vladimir Marko372f10e2016-05-17 16:30:10 +01005712 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5713 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005714 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005715 }
5716
Calin Juravle641547a2015-04-21 22:08:51 +01005717 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005718 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005719 }
5720
5721 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005722 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5723 }
5724
Calin Juravle52c48962014-12-16 17:02:57 +00005725 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005726 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005727 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005728 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005729
Vladimir Marko61b92282017-10-11 13:23:17 +01005730 void SetType(DataType::Type new_type) {
5731 DCHECK(DataType::IsIntegralType(GetType()));
5732 DCHECK(DataType::IsIntegralType(new_type));
5733 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5734 SetPackedField<TypeField>(new_type);
5735 }
5736
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005737 DECLARE_INSTRUCTION(InstanceFieldGet);
5738
Artem Serovcced8ba2017-07-19 18:18:09 +01005739 protected:
5740 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldGet);
5741
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005742 private:
5743 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005744};
5745
Vladimir Markofcb503c2016-05-18 12:48:17 +01005746class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005747 public:
5748 HInstanceFieldSet(HInstruction* object,
5749 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005750 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005751 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005752 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005753 bool is_volatile,
5754 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005755 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005756 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005757 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305758 : HTemplateInstruction(kInstanceFieldSet,
5759 SideEffects::FieldWriteOfType(field_type, is_volatile),
5760 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005761 field_info_(field,
5762 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005763 field_type,
5764 is_volatile,
5765 field_idx,
5766 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005767 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005768 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005769 SetRawInputAt(0, object);
5770 SetRawInputAt(1, value);
5771 }
5772
Artem Serovcced8ba2017-07-19 18:18:09 +01005773 bool IsClonable() const OVERRIDE { return true; }
5774
Calin Juravle641547a2015-04-21 22:08:51 +01005775 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005776 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005777 }
5778
Calin Juravle52c48962014-12-16 17:02:57 +00005779 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005780 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005781 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005782 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005783 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005784 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5785 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005786
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005787 DECLARE_INSTRUCTION(InstanceFieldSet);
5788
Artem Serovcced8ba2017-07-19 18:18:09 +01005789 protected:
5790 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldSet);
5791
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005792 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005793 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5794 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5795 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5796 "Too many packed fields.");
5797
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005798 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005799};
5800
Vladimir Markofcb503c2016-05-18 12:48:17 +01005801class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005802 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005803 HArrayGet(HInstruction* array,
5804 HInstruction* index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005805 DataType::Type type,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005806 uint32_t dex_pc)
5807 : HArrayGet(array,
5808 index,
5809 type,
5810 SideEffects::ArrayReadOfType(type),
5811 dex_pc,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305812 /* is_string_char_at */ false) {
5813 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005814
5815 HArrayGet(HInstruction* array,
5816 HInstruction* index,
5817 DataType::Type type,
5818 SideEffects side_effects,
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005819 uint32_t dex_pc,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005820 bool is_string_char_at)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305821 : HExpression(kArrayGet, type, side_effects, dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005822 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005823 SetRawInputAt(0, array);
5824 SetRawInputAt(1, index);
5825 }
5826
Artem Serovcced8ba2017-07-19 18:18:09 +01005827 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005828 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005829 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005830 return true;
5831 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005832 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005833 // TODO: We can be smarter here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01005834 // Currently, unless the array is the result of NewArray, the array access is always
5835 // preceded by some form of null NullCheck necessary for the bounds check, usually
5836 // implicit null check on the ArrayLength input to BoundsCheck or Deoptimize for
5837 // dynamic BCE. There are cases when these could be removed to produce better code.
5838 // If we ever add optimizations to do so we should allow an implicit check here
5839 // (as long as the address falls in the first page).
5840 //
5841 // As an example of such fancy optimization, we could eliminate BoundsCheck for
5842 // a = cond ? new int[1] : null;
5843 // a[0]; // The Phi does not need bounds check for either input.
Calin Juravle77520bc2015-01-12 18:45:46 +00005844 return false;
5845 }
5846
David Brazdil4833f5a2015-12-16 10:37:39 +00005847 bool IsEquivalentOf(HArrayGet* other) const {
5848 bool result = (GetDexPc() == other->GetDexPc());
5849 if (kIsDebugBuild && result) {
5850 DCHECK_EQ(GetBlock(), other->GetBlock());
5851 DCHECK_EQ(GetArray(), other->GetArray());
5852 DCHECK_EQ(GetIndex(), other->GetIndex());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005853 if (DataType::IsIntOrLongType(GetType())) {
5854 DCHECK(DataType::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005855 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005856 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
5857 DCHECK(DataType::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005858 }
5859 }
5860 return result;
5861 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005862
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005863 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5864
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005865 HInstruction* GetArray() const { return InputAt(0); }
5866 HInstruction* GetIndex() const { return InputAt(1); }
5867
Vladimir Marko61b92282017-10-11 13:23:17 +01005868 void SetType(DataType::Type new_type) {
5869 DCHECK(DataType::IsIntegralType(GetType()));
5870 DCHECK(DataType::IsIntegralType(new_type));
5871 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5872 SetPackedField<TypeField>(new_type);
5873 }
5874
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005875 DECLARE_INSTRUCTION(ArrayGet);
5876
Artem Serovcced8ba2017-07-19 18:18:09 +01005877 protected:
5878 DEFAULT_COPY_CONSTRUCTOR(ArrayGet);
5879
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005880 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005881 // We treat a String as an array, creating the HArrayGet from String.charAt()
5882 // intrinsic in the instruction simplifier. We can always determine whether
5883 // a particular HArrayGet is actually a String.charAt() by looking at the type
5884 // of the input but that requires holding the mutator lock, so we prefer to use
5885 // a flag, so that code generators don't need to do the locking.
5886 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
5887 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5888 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5889 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005890};
5891
Vladimir Markofcb503c2016-05-18 12:48:17 +01005892class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005893 public:
5894 HArraySet(HInstruction* array,
5895 HInstruction* index,
5896 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005897 DataType::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005898 uint32_t dex_pc)
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005899 : HArraySet(array,
5900 index,
5901 value,
5902 expected_component_type,
5903 // Make a best guess for side effects now, may be refined during SSA building.
5904 ComputeSideEffects(GetComponentType(value->GetType(), expected_component_type)),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305905 dex_pc) {
5906 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005907
5908 HArraySet(HInstruction* array,
5909 HInstruction* index,
5910 HInstruction* value,
5911 DataType::Type expected_component_type,
5912 SideEffects side_effects,
5913 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305914 : HTemplateInstruction(kArraySet, side_effects, dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005915 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005916 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005917 SetPackedFlag<kFlagValueCanBeNull>(true);
5918 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005919 SetRawInputAt(0, array);
5920 SetRawInputAt(1, index);
5921 SetRawInputAt(2, value);
5922 }
5923
Artem Serovcced8ba2017-07-19 18:18:09 +01005924 bool IsClonable() const OVERRIDE { return true; }
5925
Calin Juravle77520bc2015-01-12 18:45:46 +00005926 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005927 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005928 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005929 }
5930
Mingyao Yang81014cb2015-06-02 03:16:27 -07005931 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005932 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005933
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005934 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005935 // TODO: Same as for ArrayGet.
5936 return false;
5937 }
5938
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005939 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005940 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005941 }
5942
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005943 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005944 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005945 }
5946
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005947 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005948 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005949 }
5950
Vladimir Markoa1de9182016-02-25 11:37:38 +00005951 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5952 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5953 bool StaticTypeOfArrayIsObjectArray() const {
5954 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5955 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005956
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005957 HInstruction* GetArray() const { return InputAt(0); }
5958 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005959 HInstruction* GetValue() const { return InputAt(2); }
5960
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005961 DataType::Type GetComponentType() const {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005962 return GetComponentType(GetValue()->GetType(), GetRawExpectedComponentType());
5963 }
5964
5965 static DataType::Type GetComponentType(DataType::Type value_type,
5966 DataType::Type expected_component_type) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005967 // The Dex format does not type floating point index operations. Since the
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005968 // `expected_component_type` comes from SSA building and can therefore not
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005969 // be correct, we also check what is the value type. If it is a floating
5970 // point type, we must use that type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005971 return ((value_type == DataType::Type::kFloat32) || (value_type == DataType::Type::kFloat64))
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005972 ? value_type
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005973 : expected_component_type;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005974 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005975
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005976 DataType::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005977 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005978 }
5979
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005980 static SideEffects ComputeSideEffects(DataType::Type type) {
5981 return SideEffects::ArrayWriteOfType(type).Union(SideEffectsForArchRuntimeCalls(type));
Aart Bik18b36ab2016-04-13 16:41:35 -07005982 }
5983
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005984 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type value_type) {
5985 return (value_type == DataType::Type::kReference) ? SideEffects::CanTriggerGC()
5986 : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005987 }
5988
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005989 DECLARE_INSTRUCTION(ArraySet);
5990
Artem Serovcced8ba2017-07-19 18:18:09 +01005991 protected:
5992 DEFAULT_COPY_CONSTRUCTOR(ArraySet);
5993
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005994 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005995 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5996 static constexpr size_t kFieldExpectedComponentTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005997 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00005998 static constexpr size_t kFlagNeedsTypeCheck =
5999 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
6000 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006001 // Cached information for the reference_type_info_ so that codegen
6002 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006003 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
6004 static constexpr size_t kNumberOfArraySetPackedBits =
6005 kFlagStaticTypeOfArrayIsObjectArray + 1;
6006 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6007 using ExpectedComponentTypeField =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006008 BitField<DataType::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006009};
6010
Vladimir Markofcb503c2016-05-18 12:48:17 +01006011class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006012 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006013 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306014 : HExpression(kArrayLength, DataType::Type::kInt32, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006015 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006016 // Note that arrays do not change length, so the instruction does not
6017 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006018 SetRawInputAt(0, array);
6019 }
6020
Artem Serovcced8ba2017-07-19 18:18:09 +01006021 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle77520bc2015-01-12 18:45:46 +00006022 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006023 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006024 return true;
6025 }
Calin Juravle641547a2015-04-21 22:08:51 +01006026 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
6027 return obj == InputAt(0);
6028 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006029
Vladimir Markodce016e2016-04-28 13:10:02 +01006030 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
6031
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006032 DECLARE_INSTRUCTION(ArrayLength);
6033
Artem Serovcced8ba2017-07-19 18:18:09 +01006034 protected:
6035 DEFAULT_COPY_CONSTRUCTOR(ArrayLength);
6036
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006037 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01006038 // We treat a String as an array, creating the HArrayLength from String.length()
6039 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
6040 // determine whether a particular HArrayLength is actually a String.length() by
6041 // looking at the type of the input but that requires holding the mutator lock, so
6042 // we prefer to use a flag, so that code generators don't need to do the locking.
6043 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
6044 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
6045 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6046 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006047};
6048
Vladimir Markofcb503c2016-05-18 12:48:17 +01006049class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006050 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00006051 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
6052 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006053 HBoundsCheck(HInstruction* index,
6054 HInstruction* length,
6055 uint32_t dex_pc,
Vladimir Marko0259c242017-12-04 11:27:47 +00006056 bool is_string_char_at = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306057 : HExpression(kBoundsCheck, index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006058 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(index->GetType()));
Vladimir Marko0259c242017-12-04 11:27:47 +00006059 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006060 SetRawInputAt(0, index);
6061 SetRawInputAt(1, length);
6062 }
6063
Artem Serovcced8ba2017-07-19 18:18:09 +01006064 bool IsClonable() const OVERRIDE { return true; }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006065 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006066 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006067 return true;
6068 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006069
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006070 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006071
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006072 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01006073
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00006074 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006075
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006076 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006077
6078 DECLARE_INSTRUCTION(BoundsCheck);
6079
Artem Serovcced8ba2017-07-19 18:18:09 +01006080 protected:
6081 DEFAULT_COPY_CONSTRUCTOR(BoundsCheck);
6082
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006083 private:
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00006084 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006085};
6086
Vladimir Markofcb503c2016-05-18 12:48:17 +01006087class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006088 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00006089 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306090 : HTemplateInstruction(kSuspendCheck, SideEffects::CanTriggerGC(), dex_pc),
6091 slow_path_(nullptr) {
6092 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006093
Artem Serovcced8ba2017-07-19 18:18:09 +01006094 bool IsClonable() const OVERRIDE { return true; }
6095
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006096 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006097 return true;
6098 }
6099
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006100 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
6101 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006102
6103 DECLARE_INSTRUCTION(SuspendCheck);
6104
Artem Serovcced8ba2017-07-19 18:18:09 +01006105 protected:
6106 DEFAULT_COPY_CONSTRUCTOR(SuspendCheck);
6107
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006108 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006109 // Only used for code generation, in order to share the same slow path between back edges
6110 // of a same loop.
6111 SlowPathCode* slow_path_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006112};
6113
David Srbecky0cf44932015-12-09 14:09:59 +00006114// Pseudo-instruction which provides the native debugger with mapping information.
6115// It ensures that we can generate line number and local variables at this point.
6116class HNativeDebugInfo : public HTemplateInstruction<0> {
6117 public:
6118 explicit HNativeDebugInfo(uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306119 : HTemplateInstruction<0>(kNativeDebugInfo, SideEffects::None(), dex_pc) {
6120 }
David Srbecky0cf44932015-12-09 14:09:59 +00006121
6122 bool NeedsEnvironment() const OVERRIDE {
6123 return true;
6124 }
6125
6126 DECLARE_INSTRUCTION(NativeDebugInfo);
6127
Artem Serovcced8ba2017-07-19 18:18:09 +01006128 protected:
6129 DEFAULT_COPY_CONSTRUCTOR(NativeDebugInfo);
David Srbecky0cf44932015-12-09 14:09:59 +00006130};
6131
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006132/**
6133 * Instruction to load a Class object.
6134 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006135class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006136 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006137 // Determines how to load the Class.
6138 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006139 // We cannot load this class. See HSharpening::SharpenLoadClass.
6140 kInvalid = -1,
6141
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006142 // Use the Class* from the method's own ArtMethod*.
6143 kReferrersClass,
6144
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006145 // Use PC-relative boot image Class* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006146 // Used for boot image classes referenced by boot image code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006147 kBootImageLinkTimePcRelative,
6148
6149 // Use a known boot image Class* address, embedded in the code by the codegen.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006150 // Used for boot image classes referenced by apps in JIT- and AOT-compiled code (non-PIC).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006151 kBootImageAddress,
6152
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006153 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
6154 // Used for boot image classes referenced by apps in AOT-compiled code (PIC).
6155 kBootImageRelRo,
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006156
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006157 // Load from an entry in the .bss section using a PC-relative load.
6158 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
6159 kBssEntry,
6160
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006161 // Load from the root table associated with the JIT compiled method.
6162 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006163
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006164 // Load using a simple runtime call. This is the fall-back load kind when
6165 // the codegen is unable to use another appropriate kind.
6166 kRuntimeCall,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006167
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006168 kLast = kRuntimeCall
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006169 };
6170
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006171 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08006172 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006173 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006174 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006175 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01006176 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00006177 bool needs_access_check)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306178 : HInstruction(kLoadClass, SideEffectsForArchRuntimeCalls(), dex_pc),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006179 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006180 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006181 dex_file_(dex_file),
Vladimir Marko175e7862018-03-27 09:03:13 +00006182 klass_(klass) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01006183 // Referrers class should not need access check. We never inline unverified
6184 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006185 DCHECK(!is_referrers_class || !needs_access_check);
6186
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006187 SetPackedField<LoadKindField>(
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006188 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kRuntimeCall);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006189 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00006190 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006191 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Marko175e7862018-03-27 09:03:13 +00006192 SetPackedFlag<kFlagValidLoadedClassRTI>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006193 }
6194
Artem Serovcced8ba2017-07-19 18:18:09 +01006195 bool IsClonable() const OVERRIDE { return true; }
6196
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006197 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006198
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006199 LoadKind GetLoadKind() const {
6200 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006201 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006202
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006203 bool HasPcRelativeLoadKind() const {
6204 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6205 GetLoadKind() == LoadKind::kBootImageRelRo ||
6206 GetLoadKind() == LoadKind::kBssEntry;
6207 }
6208
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006209 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006210
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006211 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006212
Andreas Gampea5b09a62016-11-17 15:21:22 -08006213 size_t ComputeHashCode() const OVERRIDE { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006214
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01006215 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006216
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006217 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006218 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006219 }
6220
Calin Juravle0ba218d2015-05-19 18:46:01 +01006221 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00006222 // The entrypoint the code generator is going to call does not do
6223 // clinit of the class.
6224 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00006225 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006226 }
6227
6228 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00006229 return NeedsAccessCheck() ||
6230 MustGenerateClinitCheck() ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006231 GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006232 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01006233 }
6234
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006235 bool CanThrow() const OVERRIDE {
Vladimir Marko41559982017-01-06 14:04:23 +00006236 return NeedsAccessCheck() ||
6237 MustGenerateClinitCheck() ||
6238 // If the class is in the boot image, the lookup in the runtime call cannot throw.
6239 // This keeps CanThrow() consistent between non-PIC (using kBootImageAddress) and
6240 // PIC and subsequently avoids a DCE behavior dependency on the PIC option.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006241 ((GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006242 GetLoadKind() == LoadKind::kBssEntry) &&
6243 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006244 }
6245
Calin Juravleacf735c2015-02-12 15:25:22 +00006246 ReferenceTypeInfo GetLoadedClassRTI() {
Vladimir Marko175e7862018-03-27 09:03:13 +00006247 if (GetPackedFlag<kFlagValidLoadedClassRTI>()) {
6248 // Note: The is_exact flag from the return value should not be used.
6249 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact */ true);
6250 } else {
6251 return ReferenceTypeInfo::CreateInvalid();
6252 }
Calin Juravleacf735c2015-02-12 15:25:22 +00006253 }
6254
Vladimir Marko175e7862018-03-27 09:03:13 +00006255 // Loaded class RTI is marked as valid by RTP if the klass_ is admissible.
6256 void SetValidLoadedClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
6257 DCHECK(klass_ != nullptr);
6258 SetPackedFlag<kFlagValidLoadedClassRTI>(true);
Calin Juravleacf735c2015-02-12 15:25:22 +00006259 }
6260
Andreas Gampea5b09a62016-11-17 15:21:22 -08006261 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006262 const DexFile& GetDexFile() const { return dex_file_; }
6263
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006264 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006265 return GetLoadKind() == LoadKind::kRuntimeCall;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006266 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00006267
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006268 static SideEffects SideEffectsForArchRuntimeCalls() {
6269 return SideEffects::CanTriggerGC();
6270 }
6271
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006272 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006273 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006274 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006275 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006276
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006277 void MarkInBootImage() {
6278 SetPackedFlag<kFlagIsInBootImage>(true);
6279 }
6280
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006281 void AddSpecialInput(HInstruction* special_input);
6282
6283 using HInstruction::GetInputRecords; // Keep the const version visible.
6284 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
6285 return ArrayRef<HUserRecord<HInstruction*>>(
6286 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6287 }
6288
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006289 DataType::Type GetType() const OVERRIDE {
6290 return DataType::Type::kReference;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006291 }
6292
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006293 Handle<mirror::Class> GetClass() const {
6294 return klass_;
6295 }
6296
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006297 DECLARE_INSTRUCTION(LoadClass);
6298
Artem Serovcced8ba2017-07-19 18:18:09 +01006299 protected:
6300 DEFAULT_COPY_CONSTRUCTOR(LoadClass);
6301
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006302 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006303 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00006304 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006305 // Whether this instruction must generate the initialization check.
6306 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006307 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006308 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
6309 static constexpr size_t kFieldLoadKindSize =
6310 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
Vladimir Marko175e7862018-03-27 09:03:13 +00006311 static constexpr size_t kFlagValidLoadedClassRTI = kFieldLoadKind + kFieldLoadKindSize;
6312 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagValidLoadedClassRTI + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006313 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006314 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
6315
6316 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006317 return load_kind == LoadKind::kReferrersClass ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006318 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006319 load_kind == LoadKind::kBssEntry ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006320 load_kind == LoadKind::kRuntimeCall;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006321 }
6322
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006323 void SetLoadKindInternal(LoadKind load_kind);
6324
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006325 // The special input is the HCurrentMethod for kRuntimeCall or kReferrersClass.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006326 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006327 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006328 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006329
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006330 // A type index and dex file where the class can be accessed. The dex file can be:
6331 // - The compiling method's dex file if the class is defined there too.
6332 // - The compiling method's dex file if the class is referenced there.
6333 // - The dex file where the class is defined. When the load kind can only be
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006334 // kBssEntry or kRuntimeCall, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08006335 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006336 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006337
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006338 Handle<mirror::Class> klass_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006339};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006340std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
6341
6342// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006343inline void HLoadClass::SetLoadKind(LoadKind load_kind) {
6344 // The load kind should be determined before inserting the instruction to the graph.
6345 DCHECK(GetBlock() == nullptr);
6346 DCHECK(GetEnvironment() == nullptr);
6347 SetPackedField<LoadKindField>(load_kind);
6348 if (load_kind != LoadKind::kRuntimeCall && load_kind != LoadKind::kReferrersClass) {
6349 special_input_ = HUserRecord<HInstruction*>(nullptr);
6350 }
6351 if (!NeedsEnvironment()) {
6352 SetSideEffects(SideEffects::None());
6353 }
6354}
6355
6356// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006357inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006358 // The special input is used for PC-relative loads on some architectures,
6359 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006360 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006361 GetLoadKind() == LoadKind::kBootImageAddress ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006362 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006363 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006364 DCHECK(special_input_.GetInstruction() == nullptr);
6365 special_input_ = HUserRecord<HInstruction*>(special_input);
6366 special_input->AddUseAt(this, 0);
6367}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006368
Vladimir Marko372f10e2016-05-17 16:30:10 +01006369class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006370 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006371 // Determines how to load the String.
6372 enum class LoadKind {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006373 // Use PC-relative boot image String* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006374 // Used for boot image strings referenced by boot image code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006375 kBootImageLinkTimePcRelative,
6376
6377 // Use a known boot image String* address, embedded in the code by the codegen.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006378 // Used for boot image strings referenced by apps in JIT- and AOT-compiled code (non-PIC).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006379 kBootImageAddress,
6380
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006381 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
6382 // Used for boot image strings referenced by apps in AOT-compiled code (PIC).
6383 kBootImageRelRo,
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006384
Vladimir Markoaad75c62016-10-03 08:46:48 +00006385 // Load from an entry in the .bss section using a PC-relative load.
6386 // Used for strings outside boot image when .bss is accessible with a PC-relative load.
6387 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006388
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006389 // Load from the root table associated with the JIT compiled method.
6390 kJitTableAddress,
6391
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006392 // Load using a simple runtime call. This is the fall-back load kind when
6393 // the codegen is unable to use another appropriate kind.
6394 kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006395
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006396 kLast = kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006397 };
6398
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006399 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006400 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006401 const DexFile& dex_file,
6402 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306403 : HInstruction(kLoadString, SideEffectsForArchRuntimeCalls(), dex_pc),
Vladimir Marko372f10e2016-05-17 16:30:10 +01006404 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006405 string_index_(string_index),
6406 dex_file_(dex_file) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006407 SetPackedField<LoadKindField>(LoadKind::kRuntimeCall);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006408 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006409
Artem Serovcced8ba2017-07-19 18:18:09 +01006410 bool IsClonable() const OVERRIDE { return true; }
6411
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006412 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006413
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006414 LoadKind GetLoadKind() const {
6415 return GetPackedField<LoadKindField>();
6416 }
6417
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006418 bool HasPcRelativeLoadKind() const {
6419 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6420 GetLoadKind() == LoadKind::kBootImageRelRo ||
6421 GetLoadKind() == LoadKind::kBssEntry;
6422 }
6423
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006424 const DexFile& GetDexFile() const {
6425 return dex_file_;
6426 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006427
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006428 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006429 return string_index_;
6430 }
6431
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006432 Handle<mirror::String> GetString() const {
6433 return string_;
6434 }
6435
6436 void SetString(Handle<mirror::String> str) {
6437 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006438 }
6439
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006440 bool CanBeMoved() const OVERRIDE { return true; }
6441
Vladimir Marko372f10e2016-05-17 16:30:10 +01006442 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006443
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006444 size_t ComputeHashCode() const OVERRIDE { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006445
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006446 // Will call the runtime if we need to load the string through
6447 // the dex cache and the string is not guaranteed to be there yet.
6448 bool NeedsEnvironment() const OVERRIDE {
6449 LoadKind load_kind = GetLoadKind();
Vladimir Marko764d4542017-05-16 10:31:41 +01006450 if (load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006451 load_kind == LoadKind::kBootImageAddress ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006452 load_kind == LoadKind::kBootImageRelRo ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006453 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006454 return false;
6455 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006456 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006457 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006458
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006459 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006460 return GetLoadKind() == LoadKind::kRuntimeCall;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006461 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006462
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07006463 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006464 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006465
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006466 static SideEffects SideEffectsForArchRuntimeCalls() {
6467 return SideEffects::CanTriggerGC();
6468 }
6469
Vladimir Marko372f10e2016-05-17 16:30:10 +01006470 void AddSpecialInput(HInstruction* special_input);
6471
6472 using HInstruction::GetInputRecords; // Keep the const version visible.
6473 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
6474 return ArrayRef<HUserRecord<HInstruction*>>(
6475 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006476 }
6477
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006478 DataType::Type GetType() const OVERRIDE {
6479 return DataType::Type::kReference;
Vladimir Marko372f10e2016-05-17 16:30:10 +01006480 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006481
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006482 DECLARE_INSTRUCTION(LoadString);
6483
Artem Serovcced8ba2017-07-19 18:18:09 +01006484 protected:
6485 DEFAULT_COPY_CONSTRUCTOR(LoadString);
6486
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006487 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006488 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006489 static constexpr size_t kFieldLoadKindSize =
6490 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
6491 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006492 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006493 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006494
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006495 void SetLoadKindInternal(LoadKind load_kind);
6496
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006497 // The special input is the HCurrentMethod for kRuntimeCall.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006498 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00006499 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01006500 HUserRecord<HInstruction*> special_input_;
6501
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006502 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006503 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006504
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006505 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006506};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006507std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
6508
6509// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006510inline void HLoadString::SetLoadKind(LoadKind load_kind) {
6511 // The load kind should be determined before inserting the instruction to the graph.
6512 DCHECK(GetBlock() == nullptr);
6513 DCHECK(GetEnvironment() == nullptr);
6514 DCHECK_EQ(GetLoadKind(), LoadKind::kRuntimeCall);
6515 SetPackedField<LoadKindField>(load_kind);
6516 if (load_kind != LoadKind::kRuntimeCall) {
6517 special_input_ = HUserRecord<HInstruction*>(nullptr);
6518 }
6519 if (!NeedsEnvironment()) {
6520 SetSideEffects(SideEffects::None());
6521 }
6522}
6523
6524// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006525inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006526 // The special input is used for PC-relative loads on some architectures,
6527 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006528 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006529 GetLoadKind() == LoadKind::kBootImageAddress ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006530 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006531 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01006532 // HLoadString::GetInputRecords() returns an empty array at this point,
6533 // so use the GetInputRecords() from the base class to set the input record.
6534 DCHECK(special_input_.GetInstruction() == nullptr);
6535 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006536 special_input->AddUseAt(this, 0);
6537}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006538
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006539/**
6540 * Performs an initialization check on its Class object input.
6541 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01006542class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006543 public:
Roland Levillain3887c462015-08-12 18:15:42 +01006544 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07006545 : HExpression(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306546 kClinitCheck,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006547 DataType::Type::kReference,
Mingyao Yang217eb062017-12-11 15:20:07 -08006548 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006549 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006550 SetRawInputAt(0, constant);
6551 }
6552
Artem Serovcced8ba2017-07-19 18:18:09 +01006553 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006554 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006555 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006556 return true;
6557 }
6558
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006559 bool NeedsEnvironment() const OVERRIDE {
6560 // May call runtime to initialize the class.
6561 return true;
6562 }
6563
Nicolas Geoffray729645a2015-11-19 13:29:02 +00006564 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006565
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006566 HLoadClass* GetLoadClass() const {
6567 DCHECK(InputAt(0)->IsLoadClass());
6568 return InputAt(0)->AsLoadClass();
6569 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006570
6571 DECLARE_INSTRUCTION(ClinitCheck);
6572
Artem Serovcced8ba2017-07-19 18:18:09 +01006573
6574 protected:
6575 DEFAULT_COPY_CONSTRUCTOR(ClinitCheck);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006576};
6577
Vladimir Markofcb503c2016-05-18 12:48:17 +01006578class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006579 public:
6580 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006581 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006582 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006583 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006584 bool is_volatile,
6585 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006586 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006587 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006588 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306589 : HExpression(kStaticFieldGet,
6590 field_type,
6591 SideEffects::FieldReadOfType(field_type, is_volatile),
6592 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006593 field_info_(field,
6594 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006595 field_type,
6596 is_volatile,
6597 field_idx,
6598 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006599 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006600 SetRawInputAt(0, cls);
6601 }
6602
Calin Juravle52c48962014-12-16 17:02:57 +00006603
Artem Serovcced8ba2017-07-19 18:18:09 +01006604 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle10c9cbe2014-12-19 10:50:19 +00006605 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006606
Vladimir Marko372f10e2016-05-17 16:30:10 +01006607 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
6608 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006609 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006610 }
6611
6612 size_t ComputeHashCode() const OVERRIDE {
6613 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6614 }
6615
Calin Juravle52c48962014-12-16 17:02:57 +00006616 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006617 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006618 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006619 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006620
Vladimir Marko61b92282017-10-11 13:23:17 +01006621 void SetType(DataType::Type new_type) {
6622 DCHECK(DataType::IsIntegralType(GetType()));
6623 DCHECK(DataType::IsIntegralType(new_type));
6624 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6625 SetPackedField<TypeField>(new_type);
6626 }
6627
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006628 DECLARE_INSTRUCTION(StaticFieldGet);
6629
Artem Serovcced8ba2017-07-19 18:18:09 +01006630 protected:
6631 DEFAULT_COPY_CONSTRUCTOR(StaticFieldGet);
6632
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006633 private:
6634 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006635};
6636
Vladimir Markofcb503c2016-05-18 12:48:17 +01006637class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006638 public:
6639 HStaticFieldSet(HInstruction* cls,
6640 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006641 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006642 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006643 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006644 bool is_volatile,
6645 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006646 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006647 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006648 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306649 : HTemplateInstruction(kStaticFieldSet,
6650 SideEffects::FieldWriteOfType(field_type, is_volatile),
6651 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006652 field_info_(field,
6653 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006654 field_type,
6655 is_volatile,
6656 field_idx,
6657 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006658 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006659 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006660 SetRawInputAt(0, cls);
6661 SetRawInputAt(1, value);
6662 }
6663
Artem Serovcced8ba2017-07-19 18:18:09 +01006664 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle52c48962014-12-16 17:02:57 +00006665 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006666 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006667 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006668 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006669
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006670 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006671 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6672 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006673
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006674 DECLARE_INSTRUCTION(StaticFieldSet);
6675
Artem Serovcced8ba2017-07-19 18:18:09 +01006676 protected:
6677 DEFAULT_COPY_CONSTRUCTOR(StaticFieldSet);
6678
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006679 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006680 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6681 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6682 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6683 "Too many packed fields.");
6684
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006685 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006686};
6687
Vladimir Markofcb503c2016-05-18 12:48:17 +01006688class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006689 public:
6690 HUnresolvedInstanceFieldGet(HInstruction* obj,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006691 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006692 uint32_t field_index,
6693 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306694 : HExpression(kUnresolvedInstanceFieldGet,
6695 field_type,
6696 SideEffects::AllExceptGCDependency(),
6697 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006698 field_index_(field_index) {
6699 SetRawInputAt(0, obj);
6700 }
6701
Artem Serovcced8ba2017-07-19 18:18:09 +01006702 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006703 bool NeedsEnvironment() const OVERRIDE { return true; }
6704 bool CanThrow() const OVERRIDE { return true; }
6705
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006706 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006707 uint32_t GetFieldIndex() const { return field_index_; }
6708
6709 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6710
Artem Serovcced8ba2017-07-19 18:18:09 +01006711 protected:
6712 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldGet);
6713
Calin Juravlee460d1d2015-09-29 04:52:17 +01006714 private:
6715 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006716};
6717
Vladimir Markofcb503c2016-05-18 12:48:17 +01006718class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006719 public:
6720 HUnresolvedInstanceFieldSet(HInstruction* obj,
6721 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006722 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006723 uint32_t field_index,
6724 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306725 : HTemplateInstruction(kUnresolvedInstanceFieldSet,
6726 SideEffects::AllExceptGCDependency(),
6727 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006728 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006729 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006730 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006731 SetRawInputAt(0, obj);
6732 SetRawInputAt(1, value);
6733 }
6734
Artem Serovcced8ba2017-07-19 18:18:09 +01006735 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006736 bool NeedsEnvironment() const OVERRIDE { return true; }
6737 bool CanThrow() const OVERRIDE { return true; }
6738
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006739 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006740 uint32_t GetFieldIndex() const { return field_index_; }
6741
6742 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6743
Artem Serovcced8ba2017-07-19 18:18:09 +01006744 protected:
6745 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldSet);
6746
Calin Juravlee460d1d2015-09-29 04:52:17 +01006747 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006748 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6749 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006750 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006751 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6752 kFieldFieldType + kFieldFieldTypeSize;
6753 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6754 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006755 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006756
Calin Juravlee460d1d2015-09-29 04:52:17 +01006757 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006758};
6759
Vladimir Markofcb503c2016-05-18 12:48:17 +01006760class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006761 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006762 HUnresolvedStaticFieldGet(DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006763 uint32_t field_index,
6764 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306765 : HExpression(kUnresolvedStaticFieldGet,
6766 field_type,
6767 SideEffects::AllExceptGCDependency(),
6768 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006769 field_index_(field_index) {
6770 }
6771
Artem Serovcced8ba2017-07-19 18:18:09 +01006772 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006773 bool NeedsEnvironment() const OVERRIDE { return true; }
6774 bool CanThrow() const OVERRIDE { return true; }
6775
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006776 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006777 uint32_t GetFieldIndex() const { return field_index_; }
6778
6779 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6780
Artem Serovcced8ba2017-07-19 18:18:09 +01006781 protected:
6782 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldGet);
6783
Calin Juravlee460d1d2015-09-29 04:52:17 +01006784 private:
6785 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006786};
6787
Vladimir Markofcb503c2016-05-18 12:48:17 +01006788class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006789 public:
6790 HUnresolvedStaticFieldSet(HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006791 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006792 uint32_t field_index,
6793 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306794 : HTemplateInstruction(kUnresolvedStaticFieldSet,
6795 SideEffects::AllExceptGCDependency(),
6796 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006797 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006798 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006799 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006800 SetRawInputAt(0, value);
6801 }
6802
Artem Serovcced8ba2017-07-19 18:18:09 +01006803 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006804 bool NeedsEnvironment() const OVERRIDE { return true; }
6805 bool CanThrow() const OVERRIDE { return true; }
6806
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006807 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006808 uint32_t GetFieldIndex() const { return field_index_; }
6809
6810 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6811
Artem Serovcced8ba2017-07-19 18:18:09 +01006812 protected:
6813 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldSet);
6814
Calin Juravlee460d1d2015-09-29 04:52:17 +01006815 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006816 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6817 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006818 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006819 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6820 kFieldFieldType + kFieldFieldTypeSize;
6821 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6822 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006823 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006824
Calin Juravlee460d1d2015-09-29 04:52:17 +01006825 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006826};
6827
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006828// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006829class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006830 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006831 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306832 : HExpression(kLoadException, DataType::Type::kReference, SideEffects::None(), dex_pc) {
6833 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006834
David Brazdilbbd733e2015-08-18 17:48:17 +01006835 bool CanBeNull() const OVERRIDE { return false; }
6836
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006837 DECLARE_INSTRUCTION(LoadException);
6838
Artem Serovcced8ba2017-07-19 18:18:09 +01006839 protected:
6840 DEFAULT_COPY_CONSTRUCTOR(LoadException);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006841};
6842
David Brazdilcb1c0552015-08-04 16:22:25 +01006843// Implicit part of move-exception which clears thread-local exception storage.
6844// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006845class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006846 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006847 explicit HClearException(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306848 : HTemplateInstruction(kClearException, SideEffects::AllWrites(), dex_pc) {
6849 }
David Brazdilcb1c0552015-08-04 16:22:25 +01006850
6851 DECLARE_INSTRUCTION(ClearException);
6852
Artem Serovcced8ba2017-07-19 18:18:09 +01006853 protected:
6854 DEFAULT_COPY_CONSTRUCTOR(ClearException);
David Brazdilcb1c0552015-08-04 16:22:25 +01006855};
6856
Vladimir Markofcb503c2016-05-18 12:48:17 +01006857class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006858 public:
6859 HThrow(HInstruction* exception, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306860 : HTemplateInstruction(kThrow, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006861 SetRawInputAt(0, exception);
6862 }
6863
6864 bool IsControlFlow() const OVERRIDE { return true; }
6865
6866 bool NeedsEnvironment() const OVERRIDE { return true; }
6867
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006868 bool CanThrow() const OVERRIDE { return true; }
6869
Aart Bika8b8e9b2018-01-09 11:01:02 -08006870 bool AlwaysThrows() const OVERRIDE { return true; }
6871
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006872 DECLARE_INSTRUCTION(Throw);
6873
Artem Serovcced8ba2017-07-19 18:18:09 +01006874 protected:
6875 DEFAULT_COPY_CONSTRUCTOR(Throw);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006876};
6877
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006878/**
6879 * Implementation strategies for the code generator of a HInstanceOf
6880 * or `HCheckCast`.
6881 */
6882enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006883 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006884 kExactCheck, // Can do a single class compare.
6885 kClassHierarchyCheck, // Can just walk the super class chain.
6886 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6887 kInterfaceCheck, // No optimization yet when checking against an interface.
6888 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006889 kArrayCheck, // No optimization yet when checking against a generic array.
Vladimir Marko175e7862018-03-27 09:03:13 +00006890 kBitstringCheck, // Compare the type check bitstring.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006891 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006892};
6893
Roland Levillain86503782016-02-11 19:07:30 +00006894std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6895
Vladimir Marko175e7862018-03-27 09:03:13 +00006896// Note: HTypeCheckInstruction is just a helper class, not an abstract instruction with an
6897// `IsTypeCheckInstruction()`. (New virtual methods in the HInstruction class have a high cost.)
6898class HTypeCheckInstruction : public HVariableInputSizeInstruction {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006899 public:
Vladimir Marko175e7862018-03-27 09:03:13 +00006900 HTypeCheckInstruction(InstructionKind kind,
6901 HInstruction* object,
6902 HInstruction* target_class_or_null,
6903 TypeCheckKind check_kind,
6904 Handle<mirror::Class> klass,
6905 uint32_t dex_pc,
6906 ArenaAllocator* allocator,
6907 HIntConstant* bitstring_path_to_root,
6908 HIntConstant* bitstring_mask,
6909 SideEffects side_effects)
6910 : HVariableInputSizeInstruction(
6911 kind,
6912 side_effects,
6913 dex_pc,
6914 allocator,
6915 /* number_of_inputs */ check_kind == TypeCheckKind::kBitstringCheck ? 4u : 2u,
6916 kArenaAllocTypeCheckInputs),
6917 klass_(klass) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006918 SetPackedField<TypeCheckKindField>(check_kind);
6919 SetPackedFlag<kFlagMustDoNullCheck>(true);
Vladimir Marko175e7862018-03-27 09:03:13 +00006920 SetPackedFlag<kFlagValidTargetClassRTI>(false);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006921 SetRawInputAt(0, object);
Vladimir Marko175e7862018-03-27 09:03:13 +00006922 SetRawInputAt(1, target_class_or_null);
6923 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_path_to_root != nullptr);
6924 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_mask != nullptr);
6925 if (check_kind == TypeCheckKind::kBitstringCheck) {
6926 DCHECK(target_class_or_null->IsNullConstant());
6927 SetRawInputAt(2, bitstring_path_to_root);
6928 SetRawInputAt(3, bitstring_mask);
6929 } else {
6930 DCHECK(target_class_or_null->IsLoadClass());
6931 }
Vladimir Marko87584542017-12-12 17:47:52 +00006932 }
6933
6934 HLoadClass* GetTargetClass() const {
Vladimir Marko175e7862018-03-27 09:03:13 +00006935 DCHECK_NE(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
Vladimir Marko87584542017-12-12 17:47:52 +00006936 HInstruction* load_class = InputAt(1);
6937 DCHECK(load_class->IsLoadClass());
6938 return load_class->AsLoadClass();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006939 }
6940
Vladimir Marko175e7862018-03-27 09:03:13 +00006941 uint32_t GetBitstringPathToRoot() const {
6942 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
6943 HInstruction* path_to_root = InputAt(2);
6944 DCHECK(path_to_root->IsIntConstant());
6945 return static_cast<uint32_t>(path_to_root->AsIntConstant()->GetValue());
6946 }
6947
6948 uint32_t GetBitstringMask() const {
6949 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
6950 HInstruction* mask = InputAt(3);
6951 DCHECK(mask->IsIntConstant());
6952 return static_cast<uint32_t>(mask->AsIntConstant()->GetValue());
6953 }
6954
Artem Serovcced8ba2017-07-19 18:18:09 +01006955 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006956 bool CanBeMoved() const OVERRIDE { return true; }
6957
Vladimir Marko175e7862018-03-27 09:03:13 +00006958 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
6959 DCHECK(other->IsInstanceOf() || other->IsCheckCast()) << other->DebugName();
6960 return GetPackedFields() == down_cast<const HTypeCheckInstruction*>(other)->GetPackedFields();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006961 }
6962
Andreas Gampe3fbd3ad2018-03-26 21:14:46 +00006963 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6964 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6965 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6966 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
6967
Vladimir Marko175e7862018-03-27 09:03:13 +00006968 ReferenceTypeInfo GetTargetClassRTI() {
6969 if (GetPackedFlag<kFlagValidTargetClassRTI>()) {
6970 // Note: The is_exact flag from the return value should not be used.
6971 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact */ true);
6972 } else {
6973 return ReferenceTypeInfo::CreateInvalid();
6974 }
6975 }
6976
6977 // Target class RTI is marked as valid by RTP if the klass_ is admissible.
6978 void SetValidTargetClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
6979 DCHECK(klass_ != nullptr);
6980 SetPackedFlag<kFlagValidTargetClassRTI>(true);
6981 }
6982
6983 Handle<mirror::Class> GetClass() const {
6984 return klass_;
6985 }
6986
6987 protected:
6988 DEFAULT_COPY_CONSTRUCTOR(TypeCheckInstruction);
6989
6990 private:
6991 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6992 static constexpr size_t kFieldTypeCheckKindSize =
6993 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6994 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6995 static constexpr size_t kFlagValidTargetClassRTI = kFlagMustDoNullCheck + 1;
6996 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagValidTargetClassRTI + 1;
6997 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6998 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
6999
7000 Handle<mirror::Class> klass_;
7001};
7002
7003class HInstanceOf FINAL : public HTypeCheckInstruction {
7004 public:
7005 HInstanceOf(HInstruction* object,
7006 HInstruction* target_class_or_null,
7007 TypeCheckKind check_kind,
7008 Handle<mirror::Class> klass,
7009 uint32_t dex_pc,
7010 ArenaAllocator* allocator,
7011 HIntConstant* bitstring_path_to_root,
7012 HIntConstant* bitstring_mask)
7013 : HTypeCheckInstruction(kInstanceOf,
7014 object,
7015 target_class_or_null,
7016 check_kind,
7017 klass,
7018 dex_pc,
7019 allocator,
7020 bitstring_path_to_root,
7021 bitstring_mask,
7022 SideEffectsForArchRuntimeCalls(check_kind)) {}
7023
7024 DataType::Type GetType() const OVERRIDE { return DataType::Type::kBool; }
7025
7026 bool NeedsEnvironment() const OVERRIDE {
7027 return CanCallRuntime(GetTypeCheckKind());
7028 }
7029
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007030 static bool CanCallRuntime(TypeCheckKind check_kind) {
7031 // Mips currently does runtime calls for any other checks.
7032 return check_kind != TypeCheckKind::kExactCheck;
7033 }
7034
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007035 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007036 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01007037 }
7038
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007039 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007040
Artem Serovcced8ba2017-07-19 18:18:09 +01007041 protected:
7042 DEFAULT_COPY_CONSTRUCTOR(InstanceOf);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007043};
7044
Vladimir Markofcb503c2016-05-18 12:48:17 +01007045class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00007046 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07007047 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307048 : HExpression(kBoundType, DataType::Type::kReference, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00007049 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
7050 SetPackedFlag<kFlagUpperCanBeNull>(true);
7051 SetPackedFlag<kFlagCanBeNull>(true);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007052 DCHECK_EQ(input->GetType(), DataType::Type::kReference);
Calin Juravleb1498f62015-02-16 13:13:29 +00007053 SetRawInputAt(0, input);
7054 }
7055
Artem Serovcced8ba2017-07-19 18:18:09 +01007056 bool IsClonable() const OVERRIDE { return true; }
7057
David Brazdilf5552582015-12-27 13:36:12 +00007058 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007059 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00007060 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00007061 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007062
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007063 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007064 DCHECK(GetUpperCanBeNull() || !can_be_null);
7065 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007066 }
7067
Vladimir Markoa1de9182016-02-25 11:37:38 +00007068 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007069
Calin Juravleb1498f62015-02-16 13:13:29 +00007070 DECLARE_INSTRUCTION(BoundType);
7071
Artem Serovcced8ba2017-07-19 18:18:09 +01007072 protected:
7073 DEFAULT_COPY_CONSTRUCTOR(BoundType);
7074
Calin Juravleb1498f62015-02-16 13:13:29 +00007075 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007076 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
7077 // is false then CanBeNull() cannot be true).
7078 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
7079 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
7080 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
7081 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7082
Calin Juravleb1498f62015-02-16 13:13:29 +00007083 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007084 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
7085 // It is used to bound the type in cases like:
7086 // if (x instanceof ClassX) {
7087 // // uper_bound_ will be ClassX
7088 // }
David Brazdilf5552582015-12-27 13:36:12 +00007089 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00007090};
7091
Vladimir Marko175e7862018-03-27 09:03:13 +00007092class HCheckCast FINAL : public HTypeCheckInstruction {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007093 public:
7094 HCheckCast(HInstruction* object,
Vladimir Marko175e7862018-03-27 09:03:13 +00007095 HInstruction* target_class_or_null,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007096 TypeCheckKind check_kind,
Vladimir Marko175e7862018-03-27 09:03:13 +00007097 Handle<mirror::Class> klass,
7098 uint32_t dex_pc,
7099 ArenaAllocator* allocator,
7100 HIntConstant* bitstring_path_to_root,
7101 HIntConstant* bitstring_mask)
7102 : HTypeCheckInstruction(kCheckCast,
7103 object,
7104 target_class_or_null,
7105 check_kind,
7106 klass,
7107 dex_pc,
7108 allocator,
7109 bitstring_path_to_root,
7110 bitstring_mask,
7111 SideEffects::CanTriggerGC()) {}
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007112
7113 bool NeedsEnvironment() const OVERRIDE {
7114 // Instruction may throw a CheckCastError.
7115 return true;
7116 }
7117
7118 bool CanThrow() const OVERRIDE { return true; }
7119
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007120 DECLARE_INSTRUCTION(CheckCast);
7121
Artem Serovcced8ba2017-07-19 18:18:09 +01007122 protected:
7123 DEFAULT_COPY_CONSTRUCTOR(CheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007124};
7125
Andreas Gampe26de38b2016-07-27 17:53:11 -07007126/**
7127 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
7128 * @details We define the combined barrier types that are actually required
7129 * by the Java Memory Model, rather than using exactly the terminology from
7130 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
7131 * primitives. Note that the JSR-133 cookbook generally does not deal with
7132 * store atomicity issues, and the recipes there are not always entirely sufficient.
7133 * The current recipe is as follows:
7134 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
7135 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
7136 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
7137 * -# Use StoreStore barrier after all stores but before return from any constructor whose
7138 * class has final fields.
7139 * -# Use NTStoreStore to order non-temporal stores with respect to all later
7140 * store-to-memory instructions. Only generated together with non-temporal stores.
7141 */
7142enum MemBarrierKind {
7143 kAnyStore,
7144 kLoadAny,
7145 kStoreStore,
7146 kAnyAny,
7147 kNTStoreStore,
7148 kLastBarrierKind = kNTStoreStore
7149};
7150std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
7151
Vladimir Markofcb503c2016-05-18 12:48:17 +01007152class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01007153 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007154 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07007155 : HTemplateInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307156 kMemoryBarrier,
7157 SideEffects::AllWritesAndReads(), // Assume write/read on all fields/arrays.
7158 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007159 SetPackedField<BarrierKindField>(barrier_kind);
7160 }
Calin Juravle27df7582015-04-17 19:12:31 +01007161
Artem Serovcced8ba2017-07-19 18:18:09 +01007162 bool IsClonable() const OVERRIDE { return true; }
7163
Vladimir Markoa1de9182016-02-25 11:37:38 +00007164 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01007165
7166 DECLARE_INSTRUCTION(MemoryBarrier);
7167
Artem Serovcced8ba2017-07-19 18:18:09 +01007168 protected:
7169 DEFAULT_COPY_CONSTRUCTOR(MemoryBarrier);
7170
Calin Juravle27df7582015-04-17 19:12:31 +01007171 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007172 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
7173 static constexpr size_t kFieldBarrierKindSize =
7174 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
7175 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
7176 kFieldBarrierKind + kFieldBarrierKindSize;
7177 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
7178 "Too many packed fields.");
7179 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01007180};
7181
Igor Murashkind01745e2017-04-05 16:40:31 -07007182// A constructor fence orders all prior stores to fields that could be accessed via a final field of
7183// the specified object(s), with respect to any subsequent store that might "publish"
7184// (i.e. make visible) the specified object to another thread.
7185//
7186// JLS 17.5.1 "Semantics of final fields" states that a freeze action happens
7187// for all final fields (that were set) at the end of the invoked constructor.
7188//
7189// The constructor fence models the freeze actions for the final fields of an object
7190// being constructed (semantically at the end of the constructor). Constructor fences
7191// have a per-object affinity; two separate objects being constructed get two separate
7192// constructor fences.
7193//
7194// (Note: that if calling a super-constructor or forwarding to another constructor,
7195// the freezes would happen at the end of *that* constructor being invoked).
7196//
7197// The memory model guarantees that when the object being constructed is "published" after
7198// constructor completion (i.e. escapes the current thread via a store), then any final field
7199// writes must be observable on other threads (once they observe that publication).
7200//
7201// Further, anything written before the freeze, and read by dereferencing through the final field,
7202// must also be visible (so final object field could itself have an object with non-final fields;
7203// yet the freeze must also extend to them).
7204//
7205// Constructor example:
7206//
7207// class HasFinal {
7208// final int field; Optimizing IR for <init>()V:
7209// HasFinal() {
7210// field = 123; HInstanceFieldSet(this, HasFinal.field, 123)
7211// // freeze(this.field); HConstructorFence(this)
7212// } HReturn
7213// }
7214//
7215// HConstructorFence can serve double duty as a fence for new-instance/new-array allocations of
7216// already-initialized classes; in that case the allocation must act as a "default-initializer"
7217// of the object which effectively writes the class pointer "final field".
7218//
7219// For example, we can model default-initialiation as roughly the equivalent of the following:
7220//
7221// class Object {
7222// private final Class header;
7223// }
7224//
7225// Java code: Optimizing IR:
7226//
7227// T new_instance<T>() {
7228// Object obj = allocate_memory(T.class.size); obj = HInvoke(art_quick_alloc_object, T)
7229// obj.header = T.class; // header write is done by above call.
7230// // freeze(obj.header) HConstructorFence(obj)
7231// return (T)obj;
7232// }
7233//
7234// See also:
7235// * CompilerDriver::RequiresConstructorBarrier
7236// * QuasiAtomic::ThreadFenceForConstructor
7237//
7238class HConstructorFence FINAL : public HVariableInputSizeInstruction {
7239 // A fence has variable inputs because the inputs can be removed
7240 // after prepare_for_register_allocation phase.
7241 // (TODO: In the future a fence could freeze multiple objects
7242 // after merging two fences together.)
7243 public:
7244 // `fence_object` is the reference that needs to be protected for correct publication.
7245 //
7246 // It makes sense in the following situations:
7247 // * <init> constructors, it's the "this" parameter (i.e. HParameterValue, s.t. IsThis() == true).
7248 // * new-instance-like instructions, it's the return value (i.e. HNewInstance).
7249 //
7250 // After construction the `fence_object` becomes the 0th input.
7251 // This is not an input in a real sense, but just a convenient place to stash the information
7252 // about the associated object.
7253 HConstructorFence(HInstruction* fence_object,
7254 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007255 ArenaAllocator* allocator)
Igor Murashkind01745e2017-04-05 16:40:31 -07007256 // We strongly suspect there is not a more accurate way to describe the fine-grained reordering
7257 // constraints described in the class header. We claim that these SideEffects constraints
7258 // enforce a superset of the real constraints.
7259 //
7260 // The ordering described above is conservatively modeled with SideEffects as follows:
7261 //
7262 // * To prevent reordering of the publication stores:
7263 // ----> "Reads of objects" is the initial SideEffect.
7264 // * For every primitive final field store in the constructor:
7265 // ----> Union that field's type as a read (e.g. "Read of T") into the SideEffect.
7266 // * If there are any stores to reference final fields in the constructor:
7267 // ----> Use a more conservative "AllReads" SideEffect because any stores to any references
7268 // that are reachable from `fence_object` also need to be prevented for reordering
7269 // (and we do not want to do alias analysis to figure out what those stores are).
7270 //
7271 // In the implementation, this initially starts out as an "all reads" side effect; this is an
7272 // even more conservative approach than the one described above, and prevents all of the
7273 // above reordering without analyzing any of the instructions in the constructor.
7274 //
7275 // If in a later phase we discover that there are no writes to reference final fields,
7276 // we can refine the side effect to a smaller set of type reads (see above constraints).
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307277 : HVariableInputSizeInstruction(kConstructorFence,
7278 SideEffects::AllReads(),
Igor Murashkind01745e2017-04-05 16:40:31 -07007279 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007280 allocator,
Igor Murashkind01745e2017-04-05 16:40:31 -07007281 /* number_of_inputs */ 1,
7282 kArenaAllocConstructorFenceInputs) {
7283 DCHECK(fence_object != nullptr);
7284 SetRawInputAt(0, fence_object);
7285 }
7286
7287 // The object associated with this constructor fence.
7288 //
7289 // (Note: This will be null after the prepare_for_register_allocation phase,
7290 // as all constructor fence inputs are removed there).
7291 HInstruction* GetFenceObject() const {
7292 return InputAt(0);
7293 }
7294
7295 // Find all the HConstructorFence uses (`fence_use`) for `this` and:
7296 // - Delete `fence_use` from `this`'s use list.
7297 // - Delete `this` from `fence_use`'s inputs list.
7298 // - If the `fence_use` is dead, remove it from the graph.
7299 //
7300 // A fence is considered dead once it no longer has any uses
7301 // and all of the inputs are dead.
7302 //
7303 // This must *not* be called during/after prepare_for_register_allocation,
7304 // because that removes all the inputs to the fences but the fence is actually
7305 // still considered live.
Igor Murashkin6ef45672017-08-08 13:59:55 -07007306 //
7307 // Returns how many HConstructorFence instructions were removed from graph.
7308 static size_t RemoveConstructorFences(HInstruction* instruction);
Igor Murashkind01745e2017-04-05 16:40:31 -07007309
Igor Murashkindd018df2017-08-09 10:38:31 -07007310 // Combine all inputs of `this` and `other` instruction and remove
7311 // `other` from the graph.
7312 //
7313 // Inputs are unique after the merge.
7314 //
7315 // Requirement: `this` must not be the same as `other.
7316 void Merge(HConstructorFence* other);
7317
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007318 // Check if this constructor fence is protecting
7319 // an HNewInstance or HNewArray that is also the immediate
7320 // predecessor of `this`.
7321 //
Igor Murashkindd018df2017-08-09 10:38:31 -07007322 // If `ignore_inputs` is true, then the immediate predecessor doesn't need
7323 // to be one of the inputs of `this`.
7324 //
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007325 // Returns the associated HNewArray or HNewInstance,
7326 // or null otherwise.
Igor Murashkindd018df2017-08-09 10:38:31 -07007327 HInstruction* GetAssociatedAllocation(bool ignore_inputs = false);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007328
Igor Murashkind01745e2017-04-05 16:40:31 -07007329 DECLARE_INSTRUCTION(ConstructorFence);
7330
Artem Serovcced8ba2017-07-19 18:18:09 +01007331 protected:
7332 DEFAULT_COPY_CONSTRUCTOR(ConstructorFence);
Igor Murashkind01745e2017-04-05 16:40:31 -07007333};
7334
Vladimir Markofcb503c2016-05-18 12:48:17 +01007335class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007336 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007337 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007338 kEnter,
7339 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00007340 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007341 };
7342
7343 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01007344 : HTemplateInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307345 kMonitorOperation,
Vladimir Markoa1de9182016-02-25 11:37:38 +00007346 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
7347 dex_pc) {
7348 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007349 SetRawInputAt(0, object);
7350 }
7351
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007352 // Instruction may go into runtime, so we need an environment.
7353 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00007354
7355 bool CanThrow() const OVERRIDE {
7356 // Verifier guarantees that monitor-exit cannot throw.
7357 // This is important because it allows the HGraphBuilder to remove
7358 // a dead throw-catch loop generated for `synchronized` blocks/methods.
7359 return IsEnter();
7360 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007361
Vladimir Markoa1de9182016-02-25 11:37:38 +00007362 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
7363 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007364
7365 DECLARE_INSTRUCTION(MonitorOperation);
7366
Artem Serovcced8ba2017-07-19 18:18:09 +01007367 protected:
7368 DEFAULT_COPY_CONSTRUCTOR(MonitorOperation);
7369
Calin Juravle52c48962014-12-16 17:02:57 +00007370 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007371 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
7372 static constexpr size_t kFieldOperationKindSize =
7373 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
7374 static constexpr size_t kNumberOfMonitorOperationPackedBits =
7375 kFieldOperationKind + kFieldOperationKindSize;
7376 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7377 "Too many packed fields.");
7378 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007379};
7380
Vladimir Markofcb503c2016-05-18 12:48:17 +01007381class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00007382 public:
7383 HSelect(HInstruction* condition,
7384 HInstruction* true_value,
7385 HInstruction* false_value,
7386 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307387 : HExpression(kSelect, HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
David Brazdil74eb1b22015-12-14 11:44:01 +00007388 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
7389
7390 // First input must be `true_value` or `false_value` to allow codegens to
7391 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
7392 // that architectures which implement HSelect as a conditional move also
7393 // will not need to invert the condition.
7394 SetRawInputAt(0, false_value);
7395 SetRawInputAt(1, true_value);
7396 SetRawInputAt(2, condition);
7397 }
7398
Artem Serovcced8ba2017-07-19 18:18:09 +01007399 bool IsClonable() const OVERRIDE { return true; }
David Brazdil74eb1b22015-12-14 11:44:01 +00007400 HInstruction* GetFalseValue() const { return InputAt(0); }
7401 HInstruction* GetTrueValue() const { return InputAt(1); }
7402 HInstruction* GetCondition() const { return InputAt(2); }
7403
7404 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01007405 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
7406 return true;
7407 }
David Brazdil74eb1b22015-12-14 11:44:01 +00007408
7409 bool CanBeNull() const OVERRIDE {
7410 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
7411 }
7412
7413 DECLARE_INSTRUCTION(Select);
7414
Artem Serovcced8ba2017-07-19 18:18:09 +01007415 protected:
7416 DEFAULT_COPY_CONSTRUCTOR(Select);
David Brazdil74eb1b22015-12-14 11:44:01 +00007417};
7418
Vladimir Markof9f64412015-09-02 14:05:49 +01007419class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007420 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01007421 MoveOperands(Location source,
7422 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007423 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007424 HInstruction* instruction)
7425 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007426
7427 Location GetSource() const { return source_; }
7428 Location GetDestination() const { return destination_; }
7429
7430 void SetSource(Location value) { source_ = value; }
7431 void SetDestination(Location value) { destination_ = value; }
7432
7433 // The parallel move resolver marks moves as "in-progress" by clearing the
7434 // destination (but not the source).
7435 Location MarkPending() {
7436 DCHECK(!IsPending());
7437 Location dest = destination_;
7438 destination_ = Location::NoLocation();
7439 return dest;
7440 }
7441
7442 void ClearPending(Location dest) {
7443 DCHECK(IsPending());
7444 destination_ = dest;
7445 }
7446
7447 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00007448 DCHECK(source_.IsValid() || destination_.IsInvalid());
7449 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007450 }
7451
7452 // True if this blocks a move from the given location.
7453 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08007454 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007455 }
7456
7457 // A move is redundant if it's been eliminated, if its source and
7458 // destination are the same, or if its destination is unneeded.
7459 bool IsRedundant() const {
7460 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
7461 }
7462
7463 // We clear both operands to indicate move that's been eliminated.
7464 void Eliminate() {
7465 source_ = destination_ = Location::NoLocation();
7466 }
7467
7468 bool IsEliminated() const {
7469 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
7470 return source_.IsInvalid();
7471 }
7472
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007473 DataType::Type GetType() const { return type_; }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007474
Nicolas Geoffray90218252015-04-15 11:56:51 +01007475 bool Is64BitMove() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007476 return DataType::Is64BitType(type_);
Nicolas Geoffray90218252015-04-15 11:56:51 +01007477 }
7478
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007479 HInstruction* GetInstruction() const { return instruction_; }
7480
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007481 private:
7482 Location source_;
7483 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01007484 // The type this move is for.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007485 DataType::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007486 // The instruction this move is assocatied with. Null when this move is
7487 // for moving an input in the expected locations of user (including a phi user).
7488 // This is only used in debug mode, to ensure we do not connect interval siblings
7489 // in the same parallel move.
7490 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007491};
7492
Roland Levillainc9285912015-12-18 10:38:42 +00007493std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
7494
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007495static constexpr size_t kDefaultNumberOfMoves = 4;
7496
Vladimir Markofcb503c2016-05-18 12:48:17 +01007497class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007498 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007499 explicit HParallelMove(ArenaAllocator* allocator, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307500 : HTemplateInstruction(kParallelMove, SideEffects::None(), dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007501 moves_(allocator->Adapter(kArenaAllocMoveOperands)) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007502 moves_.reserve(kDefaultNumberOfMoves);
7503 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007504
Nicolas Geoffray90218252015-04-15 11:56:51 +01007505 void AddMove(Location source,
7506 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007507 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007508 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00007509 DCHECK(source.IsValid());
7510 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007511 if (kIsDebugBuild) {
7512 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007513 for (const MoveOperands& move : moves_) {
7514 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007515 // Special case the situation where the move is for the spill slot
7516 // of the instruction.
7517 if ((GetPrevious() == instruction)
7518 || ((GetPrevious() == nullptr)
7519 && instruction->IsPhi()
7520 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007521 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007522 << "Doing parallel moves for the same instruction.";
7523 } else {
7524 DCHECK(false) << "Doing parallel moves for the same instruction.";
7525 }
7526 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00007527 }
7528 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007529 for (const MoveOperands& move : moves_) {
7530 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007531 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01007532 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007533 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007534 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007535 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007536 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007537 }
7538
Vladimir Marko225b6462015-09-28 12:17:40 +01007539 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007540 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007541 }
7542
Vladimir Marko225b6462015-09-28 12:17:40 +01007543 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007544
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01007545 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007546
Artem Serovcced8ba2017-07-19 18:18:09 +01007547 protected:
7548 DEFAULT_COPY_CONSTRUCTOR(ParallelMove);
7549
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007550 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01007551 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007552};
7553
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007554// This instruction computes an intermediate address pointing in the 'middle' of an object. The
7555// result pointer cannot be handled by GC, so extra care is taken to make sure that this value is
7556// never used across anything that can trigger GC.
7557// The result of this instruction is not a pointer in the sense of `DataType::Type::kreference`.
7558// So we represent it by the type `DataType::Type::kInt`.
7559class HIntermediateAddress FINAL : public HExpression<2> {
7560 public:
7561 HIntermediateAddress(HInstruction* base_address, HInstruction* offset, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307562 : HExpression(kIntermediateAddress,
7563 DataType::Type::kInt32,
7564 SideEffects::DependsOnGC(),
7565 dex_pc) {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007566 DCHECK_EQ(DataType::Size(DataType::Type::kInt32),
7567 DataType::Size(DataType::Type::kReference))
7568 << "kPrimInt and kPrimNot have different sizes.";
7569 SetRawInputAt(0, base_address);
7570 SetRawInputAt(1, offset);
7571 }
7572
Artem Serovcced8ba2017-07-19 18:18:09 +01007573 bool IsClonable() const OVERRIDE { return true; }
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007574 bool CanBeMoved() const OVERRIDE { return true; }
7575 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
7576 return true;
7577 }
7578 bool IsActualObject() const OVERRIDE { return false; }
7579
7580 HInstruction* GetBaseAddress() const { return InputAt(0); }
7581 HInstruction* GetOffset() const { return InputAt(1); }
7582
7583 DECLARE_INSTRUCTION(IntermediateAddress);
7584
Artem Serovcced8ba2017-07-19 18:18:09 +01007585 protected:
7586 DEFAULT_COPY_CONSTRUCTOR(IntermediateAddress);
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007587};
7588
7589
Mark Mendell0616ae02015-04-17 12:49:27 -04007590} // namespace art
7591
Aart Bikf8f5a162017-02-06 15:35:29 -08007592#include "nodes_vector.h"
7593
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007594#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
7595#include "nodes_shared.h"
7596#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007597#ifdef ART_ENABLE_CODEGEN_mips
7598#include "nodes_mips.h"
7599#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04007600#ifdef ART_ENABLE_CODEGEN_x86
7601#include "nodes_x86.h"
7602#endif
7603
7604namespace art {
7605
Igor Murashkin6ef45672017-08-08 13:59:55 -07007606class OptimizingCompilerStats;
7607
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007608class HGraphVisitor : public ValueObject {
7609 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007610 explicit HGraphVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7611 : stats_(stats),
7612 graph_(graph) {}
Dave Allison20dfc792014-06-16 20:44:29 -07007613 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007614
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007615 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007616 virtual void VisitBasicBlock(HBasicBlock* block);
7617
Roland Levillain633021e2014-10-01 14:12:25 +01007618 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007619 void VisitInsertionOrder();
7620
Roland Levillain633021e2014-10-01 14:12:25 +01007621 // Visit the graph following dominator tree reverse post-order.
7622 void VisitReversePostOrder();
7623
Nicolas Geoffray787c3072014-03-17 10:20:19 +00007624 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007625
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007626 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007627#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007628 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
7629
7630 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7631
7632#undef DECLARE_VISIT_INSTRUCTION
7633
Igor Murashkin6ef45672017-08-08 13:59:55 -07007634 protected:
7635 OptimizingCompilerStats* stats_;
7636
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007637 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07007638 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007639
7640 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
7641};
7642
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007643class HGraphDelegateVisitor : public HGraphVisitor {
7644 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007645 explicit HGraphDelegateVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7646 : HGraphVisitor(graph, stats) {}
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007647 virtual ~HGraphDelegateVisitor() {}
7648
7649 // Visit functions that delegate to to super class.
7650#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00007651 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007652
7653 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7654
7655#undef DECLARE_VISIT_INSTRUCTION
7656
7657 private:
7658 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
7659};
7660
Artem Serovcced8ba2017-07-19 18:18:09 +01007661// Create a clone of the instruction, insert it into the graph; replace the old one with a new
7662// and remove the old instruction.
7663HInstruction* ReplaceInstrOrPhiByClone(HInstruction* instr);
7664
7665// Create a clone for each clonable instructions/phis and replace the original with the clone.
7666//
7667// Used for testing individual instruction cloner.
7668class CloneAndReplaceInstructionVisitor : public HGraphDelegateVisitor {
7669 public:
7670 explicit CloneAndReplaceInstructionVisitor(HGraph* graph)
Artem Serov7f4aff62017-06-21 17:02:18 +01007671 : HGraphDelegateVisitor(graph), instr_replaced_by_clones_count_(0) {}
Artem Serovcced8ba2017-07-19 18:18:09 +01007672
7673 void VisitInstruction(HInstruction* instruction) OVERRIDE {
7674 if (instruction->IsClonable()) {
7675 ReplaceInstrOrPhiByClone(instruction);
Artem Serov7f4aff62017-06-21 17:02:18 +01007676 instr_replaced_by_clones_count_++;
Artem Serovcced8ba2017-07-19 18:18:09 +01007677 }
7678 }
7679
Artem Serov7f4aff62017-06-21 17:02:18 +01007680 size_t GetInstrReplacedByClonesCount() const { return instr_replaced_by_clones_count_; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007681
7682 private:
Artem Serov7f4aff62017-06-21 17:02:18 +01007683 size_t instr_replaced_by_clones_count_;
Artem Serovcced8ba2017-07-19 18:18:09 +01007684
7685 DISALLOW_COPY_AND_ASSIGN(CloneAndReplaceInstructionVisitor);
7686};
7687
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007688// Iterator over the blocks that art part of the loop. Includes blocks part
7689// of an inner loop. The order in which the blocks are iterated is on their
7690// block id.
7691class HBlocksInLoopIterator : public ValueObject {
7692 public:
7693 explicit HBlocksInLoopIterator(const HLoopInformation& info)
7694 : blocks_in_loop_(info.GetBlocks()),
7695 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
7696 index_(0) {
7697 if (!blocks_in_loop_.IsBitSet(index_)) {
7698 Advance();
7699 }
7700 }
7701
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007702 bool Done() const { return index_ == blocks_.size(); }
7703 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007704 void Advance() {
7705 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007706 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007707 if (blocks_in_loop_.IsBitSet(index_)) {
7708 break;
7709 }
7710 }
7711 }
7712
7713 private:
7714 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007715 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007716 size_t index_;
7717
7718 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
7719};
7720
Mingyao Yang3584bce2015-05-19 16:01:59 -07007721// Iterator over the blocks that art part of the loop. Includes blocks part
7722// of an inner loop. The order in which the blocks are iterated is reverse
7723// post order.
7724class HBlocksInLoopReversePostOrderIterator : public ValueObject {
7725 public:
7726 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
7727 : blocks_in_loop_(info.GetBlocks()),
7728 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
7729 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007730 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007731 Advance();
7732 }
7733 }
7734
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007735 bool Done() const { return index_ == blocks_.size(); }
7736 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07007737 void Advance() {
7738 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007739 for (size_t e = blocks_.size(); index_ < e; ++index_) {
7740 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007741 break;
7742 }
7743 }
7744 }
7745
7746 private:
7747 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007748 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07007749 size_t index_;
7750
7751 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
7752};
7753
Aart Bikf3e61ee2017-04-12 17:09:20 -07007754// Returns int64_t value of a properly typed constant.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007755inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00007756 if (constant->IsIntConstant()) {
7757 return constant->AsIntConstant()->GetValue();
7758 } else if (constant->IsLongConstant()) {
7759 return constant->AsLongConstant()->GetValue();
7760 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00007761 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00007762 return 0;
7763 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007764}
7765
Aart Bikf3e61ee2017-04-12 17:09:20 -07007766// Returns true iff instruction is an integral constant (and sets value on success).
7767inline bool IsInt64AndGet(HInstruction* instruction, /*out*/ int64_t* value) {
7768 if (instruction->IsIntConstant()) {
7769 *value = instruction->AsIntConstant()->GetValue();
7770 return true;
7771 } else if (instruction->IsLongConstant()) {
7772 *value = instruction->AsLongConstant()->GetValue();
7773 return true;
7774 } else if (instruction->IsNullConstant()) {
7775 *value = 0;
7776 return true;
7777 }
7778 return false;
7779}
7780
Aart Bik0148de42017-09-05 09:25:01 -07007781// Returns true iff instruction is the given integral constant.
7782inline bool IsInt64Value(HInstruction* instruction, int64_t value) {
7783 int64_t val = 0;
7784 return IsInt64AndGet(instruction, &val) && val == value;
7785}
7786
7787// Returns true iff instruction is a zero bit pattern.
7788inline bool IsZeroBitPattern(HInstruction* instruction) {
7789 return instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern();
7790}
7791
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007792#define INSTRUCTION_TYPE_CHECK(type, super) \
7793 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
7794 inline const H##type* HInstruction::As##type() const { \
7795 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
7796 } \
7797 inline H##type* HInstruction::As##type() { \
7798 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
7799 }
7800
7801 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
7802#undef INSTRUCTION_TYPE_CHECK
7803
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00007804// Create space in `blocks` for adding `number_of_new_blocks` entries
7805// starting at location `at`. Blocks after `at` are moved accordingly.
7806inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
7807 size_t number_of_new_blocks,
7808 size_t after) {
7809 DCHECK_LT(after, blocks->size());
7810 size_t old_size = blocks->size();
7811 size_t new_size = old_size + number_of_new_blocks;
7812 blocks->resize(new_size);
7813 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
7814}
7815
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00007816/*
7817 * Hunt "under the hood" of array lengths (leading to array references),
7818 * null checks (also leading to array references), and new arrays
7819 * (leading to the actual length). This makes it more likely related
7820 * instructions become actually comparable.
7821 */
7822inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
7823 while (instruction->IsArrayLength() ||
7824 instruction->IsNullCheck() ||
7825 instruction->IsNewArray()) {
7826 instruction = instruction->IsNewArray()
7827 ? instruction->AsNewArray()->GetLength()
7828 : instruction->InputAt(0);
7829 }
7830 return instruction;
7831}
7832
Artem Serov21c7e6f2017-07-27 16:04:42 +01007833void RemoveEnvironmentUses(HInstruction* instruction);
7834bool HasEnvironmentUsedByOthers(HInstruction* instruction);
7835void ResetEnvironmentInputRecords(HInstruction* instruction);
7836
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007837} // namespace art
7838
7839#endif // ART_COMPILER_OPTIMIZING_NODES_H_