blob: 3433e917963f8e232892a11b35afad68d7f87253 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
Vladimir Marko60584552015-09-03 13:35:12 +000020#include <algorithm>
Vladimir Markof9f64412015-09-02 14:05:49 +010021#include <array>
Roland Levillain9867bc72015-08-05 10:21:34 +010022#include <type_traits>
23
Mathieu Chartiere5d80f82015-10-15 17:47:48 -070024#include "base/arena_bit_vector.h"
David Brazdil8d5b8b22015-03-24 10:51:52 +000025#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080026#include "base/arena_object.h"
David Brazdild9c90372016-09-14 16:53:55 +010027#include "base/array_ref.h"
Vladimir Marko2c45bc92016-10-25 16:54:12 +010028#include "base/iteration_range.h"
David Sehrc431b9d2018-03-02 12:01:51 -080029#include "base/quasi_atomic.h"
Vladimir Marko60584552015-09-03 13:35:12 +000030#include "base/stl_util.h"
David Brazdild9c90372016-09-14 16:53:55 +010031#include "base/transform_array_ref.h"
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010032#include "data_type.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070033#include "deoptimization_kind.h"
David Sehr9e734c72018-01-04 17:56:19 -080034#include "dex/dex_file.h"
35#include "dex/dex_file_types.h"
David Sehr8c0961f2018-01-23 16:11:38 -080036#include "dex/invoke_type.h"
David Sehr312f3b22018-03-19 08:39:26 -070037#include "dex/method_reference.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000038#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000039#include "handle.h"
40#include "handle_scope.h"
Nicolas Geoffray762869d2016-07-15 15:28:35 +010041#include "intrinsics_enum.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010042#include "locations.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000043#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010044#include "offsets.h"
Vladimir Marko46817b82016-03-29 12:21:58 +010045#include "utils/intrusive_forward_list.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000046
47namespace art {
48
Vladimir Markoca6fff82017-10-03 14:49:14 +010049class ArenaStack;
David Brazdil1abb4192015-02-17 18:33:36 +000050class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000051class HBasicBlock;
Igor Murashkind01745e2017-04-05 16:40:31 -070052class HConstructorFence;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010053class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000054class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010055class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000056class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000057class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000058class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000059class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000060class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000061class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000062class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000063class HNullConstant;
Igor Murashkind01745e2017-04-05 16:40:31 -070064class HParameterValue;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010065class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010066class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010067class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010068class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000069class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010070class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000071class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000072
Mathieu Chartier736b5602015-09-02 14:54:11 -070073namespace mirror {
74class DexCache;
75} // namespace mirror
76
Nicolas Geoffray818f2102014-02-18 16:43:35 +000077static const int kDefaultNumberOfBlocks = 8;
78static const int kDefaultNumberOfSuccessors = 2;
79static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010080static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010081static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000082static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000083
Roland Levillain5b5b9312016-03-22 14:57:31 +000084// The maximum (meaningful) distance (31) that can be used in an integer shift/rotate operation.
85static constexpr int32_t kMaxIntShiftDistance = 0x1f;
86// The maximum (meaningful) distance (63) that can be used in a long shift/rotate operation.
87static constexpr int32_t kMaxLongShiftDistance = 0x3f;
Calin Juravle9aec02f2014-11-18 23:06:35 +000088
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010089static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070090static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010091
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010092static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
93
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060094static constexpr uint32_t kNoDexPc = -1;
95
Vladimir Markodbb7f5b2016-03-30 13:23:58 +010096inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
97 // For the purposes of the compiler, the dex files must actually be the same object
98 // if we want to safely treat them as the same. This is especially important for JIT
99 // as custom class loaders can open the same underlying file (or memory) multiple
100 // times and provide different class resolution but no two class loaders should ever
101 // use the same DexFile object - doing so is an unsupported hack that can lead to
102 // all sorts of weird failures.
103 return &lhs == &rhs;
104}
105
Dave Allison20dfc792014-06-16 20:44:29 -0700106enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -0700107 // All types.
108 kCondEQ, // ==
109 kCondNE, // !=
110 // Signed integers and floating-point numbers.
111 kCondLT, // <
112 kCondLE, // <=
113 kCondGT, // >
114 kCondGE, // >=
115 // Unsigned integers.
116 kCondB, // <
117 kCondBE, // <=
118 kCondA, // >
119 kCondAE, // >=
Scott Wakeling2c76e062016-08-31 09:48:54 +0100120 // First and last aliases.
121 kCondFirst = kCondEQ,
122 kCondLast = kCondAE,
Dave Allison20dfc792014-06-16 20:44:29 -0700123};
124
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000125enum GraphAnalysisResult {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000126 kAnalysisSkipped,
David Brazdilbadd8262016-02-02 16:28:56 +0000127 kAnalysisInvalidBytecode,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000128 kAnalysisFailThrowCatchLoop,
129 kAnalysisFailAmbiguousArrayOp,
130 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000131};
132
Andreas Gampe9186ced2016-12-12 14:28:21 -0800133template <typename T>
134static inline typename std::make_unsigned<T>::type MakeUnsigned(T x) {
135 return static_cast<typename std::make_unsigned<T>::type>(x);
136}
137
Vladimir Markof9f64412015-09-02 14:05:49 +0100138class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100139 public:
140 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
141
142 void AddInstruction(HInstruction* instruction);
143 void RemoveInstruction(HInstruction* instruction);
144
David Brazdilc3d743f2015-04-22 13:40:50 +0100145 // Insert `instruction` before/after an existing instruction `cursor`.
146 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
147 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
148
Roland Levillain6b469232014-09-25 10:10:38 +0100149 // Return true if this list contains `instruction`.
150 bool Contains(HInstruction* instruction) const;
151
Roland Levillainccc07a92014-09-16 14:48:16 +0100152 // Return true if `instruction1` is found before `instruction2` in
153 // this instruction list and false otherwise. Abort if none
154 // of these instructions is found.
155 bool FoundBefore(const HInstruction* instruction1,
156 const HInstruction* instruction2) const;
157
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000158 bool IsEmpty() const { return first_instruction_ == nullptr; }
159 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
160
161 // Update the block of all instructions to be `block`.
162 void SetBlockOfInstructions(HBasicBlock* block) const;
163
164 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000165 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000166 void Add(const HInstructionList& instruction_list);
167
David Brazdil2d7352b2015-04-20 14:52:42 +0100168 // Return the number of instructions in the list. This is an expensive operation.
169 size_t CountSize() const;
170
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100171 private:
172 HInstruction* first_instruction_;
173 HInstruction* last_instruction_;
174
175 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000176 friend class HGraph;
177 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100178 friend class HInstructionIterator;
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +0000179 friend class HInstructionIteratorHandleChanges;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100180 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100181
182 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
183};
184
David Brazdil4833f5a2015-12-16 10:37:39 +0000185class ReferenceTypeInfo : ValueObject {
186 public:
187 typedef Handle<mirror::Class> TypeHandle;
188
Vladimir Markoa1de9182016-02-25 11:37:38 +0000189 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
190
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700191 static ReferenceTypeInfo Create(TypeHandle type_handle) REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil94ab38f2016-06-21 17:48:19 +0100192 return Create(type_handle, type_handle->CannotBeAssignedFromOtherTypes());
193 }
194
Vladimir Markoa1de9182016-02-25 11:37:38 +0000195 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000196 return ReferenceTypeInfo(type_handle, is_exact);
197 }
198
199 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
200
Vladimir Markof39745e2016-01-26 12:16:55 +0000201 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000202 return handle.GetReference() != nullptr;
203 }
204
Vladimir Marko456307a2016-04-19 14:12:13 +0000205 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000206 return IsValidHandle(type_handle_);
207 }
208
209 bool IsExact() const { return is_exact_; }
210
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700211 bool IsObjectClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000212 DCHECK(IsValid());
213 return GetTypeHandle()->IsObjectClass();
214 }
215
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700216 bool IsStringClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000217 DCHECK(IsValid());
218 return GetTypeHandle()->IsStringClass();
219 }
220
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700221 bool IsObjectArray() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000222 DCHECK(IsValid());
223 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
224 }
225
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700226 bool IsInterface() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000227 DCHECK(IsValid());
228 return GetTypeHandle()->IsInterface();
229 }
230
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700231 bool IsArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000232 DCHECK(IsValid());
233 return GetTypeHandle()->IsArrayClass();
234 }
235
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700236 bool IsPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000237 DCHECK(IsValid());
238 return GetTypeHandle()->IsPrimitiveArray();
239 }
240
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700241 bool IsNonPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000242 DCHECK(IsValid());
243 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
244 }
245
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700246 bool CanArrayHold(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000247 DCHECK(IsValid());
248 if (!IsExact()) return false;
249 if (!IsArrayClass()) return false;
250 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
251 }
252
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700253 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000254 DCHECK(IsValid());
255 if (!IsExact()) return false;
256 if (!IsArrayClass()) return false;
257 if (!rti.IsArrayClass()) return false;
258 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
259 rti.GetTypeHandle()->GetComponentType());
260 }
261
262 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
263
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700264 bool IsSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000265 DCHECK(IsValid());
266 DCHECK(rti.IsValid());
267 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
268 }
269
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700270 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000271 DCHECK(IsValid());
272 DCHECK(rti.IsValid());
273 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
274 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
275 }
276
277 // Returns true if the type information provide the same amount of details.
278 // Note that it does not mean that the instructions have the same actual type
279 // (because the type can be the result of a merge).
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700280 bool IsEqual(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000281 if (!IsValid() && !rti.IsValid()) {
282 // Invalid types are equal.
283 return true;
284 }
285 if (!IsValid() || !rti.IsValid()) {
286 // One is valid, the other not.
287 return false;
288 }
289 return IsExact() == rti.IsExact()
290 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
291 }
292
293 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000294 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
295 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
296 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000297
298 // The class of the object.
299 TypeHandle type_handle_;
300 // Whether or not the type is exact or a superclass of the actual type.
301 // Whether or not we have any information about this type.
302 bool is_exact_;
303};
304
305std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
306
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000307// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100308class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000309 public:
Vladimir Markoca6fff82017-10-03 14:49:14 +0100310 HGraph(ArenaAllocator* allocator,
311 ArenaStack* arena_stack,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100312 const DexFile& dex_file,
313 uint32_t method_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700314 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100315 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100316 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000317 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100318 int start_instruction_id = 0)
Vladimir Markoca6fff82017-10-03 14:49:14 +0100319 : allocator_(allocator),
320 arena_stack_(arena_stack),
321 blocks_(allocator->Adapter(kArenaAllocBlockList)),
322 reverse_post_order_(allocator->Adapter(kArenaAllocReversePostOrder)),
323 linear_order_(allocator->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700324 entry_block_(nullptr),
325 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100326 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100327 number_of_vregs_(0),
328 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000329 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400330 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000331 has_try_catch_(false),
Aart Bikb13c65b2017-03-21 20:14:07 -0700332 has_simd_(false),
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800333 has_loops_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000334 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000335 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000336 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100337 dex_file_(dex_file),
338 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100339 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100340 in_ssa_form_(false),
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800341 number_of_cha_guards_(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700342 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000343 cached_null_constant_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100344 cached_int_constants_(std::less<int32_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
345 cached_float_constants_(std::less<int32_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
346 cached_long_constants_(std::less<int64_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
347 cached_double_constants_(std::less<int64_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000348 cached_current_method_(nullptr),
Nicolas Geoffray53fec082017-03-27 12:56:16 +0100349 art_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000350 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
Mingyao Yang063fc772016-08-02 11:02:54 -0700351 osr_(osr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100352 cha_single_implementation_list_(allocator->Adapter(kArenaAllocCHA)) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100353 blocks_.reserve(kDefaultNumberOfBlocks);
354 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000355
David Brazdilbadd8262016-02-02 16:28:56 +0000356 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700357 void InitializeInexactObjectRTI(VariableSizedHandleScope* handles);
David Brazdilbadd8262016-02-02 16:28:56 +0000358
Vladimir Markoca6fff82017-10-03 14:49:14 +0100359 ArenaAllocator* GetAllocator() const { return allocator_; }
360 ArenaStack* GetArenaStack() const { return arena_stack_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100361 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
362
David Brazdil69ba7b72015-06-23 18:27:30 +0100363 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000364 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100365
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000366 HBasicBlock* GetEntryBlock() const { return entry_block_; }
367 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100368 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000369
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000370 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
371 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000372
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000373 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100374
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100375 void ComputeDominanceInformation();
376 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000377 void ClearLoopInformation();
378 void FindBackEdges(ArenaBitVector* visited);
379 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100380 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100381 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000382
David Brazdil4833f5a2015-12-16 10:37:39 +0000383 // Analyze all natural loops in this graph. Returns a code specifying that it
384 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000385 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000386 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100387
David Brazdilffee3d32015-07-06 11:48:53 +0100388 // Iterate over blocks to compute try block membership. Needs reverse post
389 // order and loop information.
390 void ComputeTryBlockInformation();
391
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000392 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000393 // Returns the instruction to replace the invoke expression or null if the
394 // invoke is for a void method. Note that the caller is responsible for replacing
395 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000396 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000397
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000398 // Update the loop and try membership of `block`, which was spawned from `reference`.
399 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
400 // should be the new back edge.
401 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
402 HBasicBlock* reference,
403 bool replace_if_back_edge);
404
Mingyao Yang3584bce2015-05-19 16:01:59 -0700405 // Need to add a couple of blocks to test if the loop body is entered and
406 // put deoptimization instructions, etc.
407 void TransformLoopHeaderForBCE(HBasicBlock* header);
408
Aart Bikf8f5a162017-02-06 15:35:29 -0800409 // Adds a new loop directly after the loop with the given header and exit.
410 // Returns the new preheader.
411 HBasicBlock* TransformLoopForVectorization(HBasicBlock* header,
412 HBasicBlock* body,
413 HBasicBlock* exit);
414
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000415 // Removes `block` from the graph. Assumes `block` has been disconnected from
416 // other blocks and has no instructions or phis.
417 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000418
David Brazdilfc6a86a2015-06-26 10:33:45 +0000419 // Splits the edge between `block` and `successor` while preserving the
420 // indices in the predecessor/successor lists. If there are multiple edges
421 // between the blocks, the lowest indices are used.
422 // Returns the new block which is empty and has the same dex pc as `successor`.
423 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
424
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100425 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
Artem Serovc73ee372017-07-31 15:08:40 +0100426 void OrderLoopHeaderPredecessors(HBasicBlock* header);
Artem Serov09faaea2017-12-07 14:36:01 +0000427
428 // Transform a loop into a format with a single preheader.
429 //
430 // Each phi in the header should be split: original one in the header should only hold
431 // inputs reachable from the back edges and a single input from the preheader. The newly created
432 // phi in the preheader should collate the inputs from the original multiple incoming blocks.
433 //
434 // Loops in the graph typically have a single preheader, so this method is used to "repair" loops
435 // that no longer have this property.
436 void TransformLoopToSinglePreheaderFormat(HBasicBlock* header);
437
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100438 void SimplifyLoop(HBasicBlock* header);
439
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000440 int32_t GetNextInstructionId() {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100441 CHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000442 return current_instruction_id_++;
443 }
444
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000445 int32_t GetCurrentInstructionId() const {
446 return current_instruction_id_;
447 }
448
449 void SetCurrentInstructionId(int32_t id) {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100450 CHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000451 current_instruction_id_ = id;
452 }
453
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100454 uint16_t GetMaximumNumberOfOutVRegs() const {
455 return maximum_number_of_out_vregs_;
456 }
457
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000458 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
459 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100460 }
461
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100462 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
463 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
464 }
465
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000466 void UpdateTemporariesVRegSlots(size_t slots) {
467 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100468 }
469
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000470 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100471 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000472 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100473 }
474
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100475 void SetNumberOfVRegs(uint16_t number_of_vregs) {
476 number_of_vregs_ = number_of_vregs;
477 }
478
479 uint16_t GetNumberOfVRegs() const {
480 return number_of_vregs_;
481 }
482
483 void SetNumberOfInVRegs(uint16_t value) {
484 number_of_in_vregs_ = value;
485 }
486
David Brazdildee58d62016-04-07 09:54:26 +0000487 uint16_t GetNumberOfInVRegs() const {
488 return number_of_in_vregs_;
489 }
490
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100491 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100492 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100493 return number_of_vregs_ - number_of_in_vregs_;
494 }
495
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100496 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100497 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100498 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100499
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100500 ArrayRef<HBasicBlock* const> GetReversePostOrderSkipEntryBlock() {
501 DCHECK(GetReversePostOrder()[0] == entry_block_);
502 return ArrayRef<HBasicBlock* const>(GetReversePostOrder()).SubArray(1);
503 }
504
505 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetPostOrder() const {
506 return ReverseRange(GetReversePostOrder());
507 }
508
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100509 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100510 return linear_order_;
511 }
512
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100513 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetLinearPostOrder() const {
514 return ReverseRange(GetLinearOrder());
515 }
516
Mark Mendell1152c922015-04-24 17:06:35 -0400517 bool HasBoundsChecks() const {
518 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800519 }
520
Mark Mendell1152c922015-04-24 17:06:35 -0400521 void SetHasBoundsChecks(bool value) {
522 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800523 }
524
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000525 bool IsDebuggable() const { return debuggable_; }
526
David Brazdil8d5b8b22015-03-24 10:51:52 +0000527 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000528 // already, it is created and inserted into the graph. This method is only for
529 // integral types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100530 HConstant* GetConstant(DataType::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000531
532 // TODO: This is problematic for the consistency of reference type propagation
533 // because it can be created anytime after the pass and thus it will be left
534 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600535 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000536
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600537 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
538 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000539 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600540 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
541 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000542 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600543 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
544 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000545 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600546 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
547 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000548 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000549
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100550 HCurrentMethod* GetCurrentMethod();
551
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100552 const DexFile& GetDexFile() const {
553 return dex_file_;
554 }
555
556 uint32_t GetMethodIdx() const {
557 return method_idx_;
558 }
559
Igor Murashkind01745e2017-04-05 16:40:31 -0700560 // Get the method name (without the signature), e.g. "<init>"
561 const char* GetMethodName() const;
562
563 // Get the pretty method name (class + name + optionally signature).
564 std::string PrettyMethod(bool with_signature = true) const;
565
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100566 InvokeType GetInvokeType() const {
567 return invoke_type_;
568 }
569
Mark Mendellc4701932015-04-10 13:18:51 -0400570 InstructionSet GetInstructionSet() const {
571 return instruction_set_;
572 }
573
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000574 bool IsCompilingOsr() const { return osr_; }
575
Mingyao Yang063fc772016-08-02 11:02:54 -0700576 ArenaSet<ArtMethod*>& GetCHASingleImplementationList() {
577 return cha_single_implementation_list_;
578 }
579
580 void AddCHASingleImplementationDependency(ArtMethod* method) {
581 cha_single_implementation_list_.insert(method);
582 }
583
584 bool HasShouldDeoptimizeFlag() const {
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800585 return number_of_cha_guards_ != 0;
Mingyao Yang063fc772016-08-02 11:02:54 -0700586 }
587
David Brazdil77a48ae2015-09-15 12:34:04 +0000588 bool HasTryCatch() const { return has_try_catch_; }
589 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100590
Aart Bikb13c65b2017-03-21 20:14:07 -0700591 bool HasSIMD() const { return has_simd_; }
592 void SetHasSIMD(bool value) { has_simd_ = value; }
593
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800594 bool HasLoops() const { return has_loops_; }
595 void SetHasLoops(bool value) { has_loops_ = value; }
596
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000597 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
598 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
599
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100600 ArtMethod* GetArtMethod() const { return art_method_; }
601 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
602
Roland Levillain5e8d5f02016-10-18 18:03:43 +0100603 // Returns an instruction with the opposite Boolean value from 'cond'.
Mark Mendellf6529172015-11-17 11:16:56 -0500604 // The instruction has been inserted into the graph, either as a constant, or
605 // before cursor.
606 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
607
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000608 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
609
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800610 uint32_t GetNumberOfCHAGuards() { return number_of_cha_guards_; }
611 void SetNumberOfCHAGuards(uint32_t num) { number_of_cha_guards_ = num; }
612 void IncrementNumberOfCHAGuards() { number_of_cha_guards_++; }
613
David Brazdil2d7352b2015-04-20 14:52:42 +0100614 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000615 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100616 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000617
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000618 template <class InstructionType, typename ValueType>
619 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600620 ArenaSafeMap<ValueType, InstructionType*>* cache,
621 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000622 // Try to find an existing constant of the given value.
623 InstructionType* constant = nullptr;
624 auto cached_constant = cache->find(value);
625 if (cached_constant != cache->end()) {
626 constant = cached_constant->second;
627 }
628
629 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100630 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000631 if (constant == nullptr || constant->GetBlock() == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100632 constant = new (allocator_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000633 cache->Overwrite(value, constant);
634 InsertConstant(constant);
635 }
636 return constant;
637 }
638
David Brazdil8d5b8b22015-03-24 10:51:52 +0000639 void InsertConstant(HConstant* instruction);
640
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000641 // Cache a float constant into the graph. This method should only be
642 // called by the SsaBuilder when creating "equivalent" instructions.
643 void CacheFloatConstant(HFloatConstant* constant);
644
645 // See CacheFloatConstant comment.
646 void CacheDoubleConstant(HDoubleConstant* constant);
647
Vladimir Markoca6fff82017-10-03 14:49:14 +0100648 ArenaAllocator* const allocator_;
649 ArenaStack* const arena_stack_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000650
651 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100652 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000653
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100654 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100655 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000656
Aart Bik281c6812016-08-26 11:31:48 -0700657 // List of blocks to perform a linear order tree traversal. Unlike the reverse
658 // post order, this order is not incrementally kept up-to-date.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100659 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100660
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000661 HBasicBlock* entry_block_;
662 HBasicBlock* exit_block_;
663
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100664 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100665 uint16_t maximum_number_of_out_vregs_;
666
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100667 // The number of virtual registers in this method. Contains the parameters.
668 uint16_t number_of_vregs_;
669
670 // The number of virtual registers used by parameters of this method.
671 uint16_t number_of_in_vregs_;
672
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000673 // Number of vreg size slots that the temporaries use (used in baseline compiler).
674 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100675
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800676 // Flag whether there are bounds checks in the graph. We can skip
677 // BCE if it's false. It's only best effort to keep it up to date in
678 // the presence of code elimination so there might be false positives.
Mark Mendell1152c922015-04-24 17:06:35 -0400679 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800680
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800681 // Flag whether there are try/catch blocks in the graph. We will skip
682 // try/catch-related passes if it's false. It's only best effort to keep
683 // it up to date in the presence of code elimination so there might be
684 // false positives.
David Brazdil77a48ae2015-09-15 12:34:04 +0000685 bool has_try_catch_;
686
Aart Bikb13c65b2017-03-21 20:14:07 -0700687 // Flag whether SIMD instructions appear in the graph. If true, the
688 // code generators may have to be more careful spilling the wider
689 // contents of SIMD registers.
690 bool has_simd_;
691
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800692 // Flag whether there are any loops in the graph. We can skip loop
693 // optimization if it's false. It's only best effort to keep it up
694 // to date in the presence of code elimination so there might be false
695 // positives.
696 bool has_loops_;
697
698 // Flag whether there are any irreducible loops in the graph. It's only
699 // best effort to keep it up to date in the presence of code elimination
700 // so there might be false positives.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000701 bool has_irreducible_loops_;
702
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000703 // Indicates whether the graph should be compiled in a way that
704 // ensures full debuggability. If false, we can apply more
705 // aggressive optimizations that may limit the level of debugging.
706 const bool debuggable_;
707
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000708 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000709 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000710
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100711 // The dex file from which the method is from.
712 const DexFile& dex_file_;
713
714 // The method index in the dex file.
715 const uint32_t method_idx_;
716
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100717 // If inlined, this encodes how the callee is being invoked.
718 const InvokeType invoke_type_;
719
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100720 // Whether the graph has been transformed to SSA form. Only used
721 // in debug mode to ensure we are not using properties only valid
722 // for non-SSA form (like the number of temporaries).
723 bool in_ssa_form_;
724
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800725 // Number of CHA guards in the graph. Used to short-circuit the
726 // CHA guard optimization pass when there is no CHA guard left.
727 uint32_t number_of_cha_guards_;
728
Mathieu Chartiere401d142015-04-22 13:56:20 -0700729 const InstructionSet instruction_set_;
730
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000731 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000732 HNullConstant* cached_null_constant_;
733 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000734 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000735 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000736 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000737
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100738 HCurrentMethod* cached_current_method_;
739
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100740 // The ArtMethod this graph is for. Note that for AOT, it may be null,
741 // for example for methods whose declaring class could not be resolved
742 // (such as when the superclass could not be found).
743 ArtMethod* art_method_;
744
David Brazdil4833f5a2015-12-16 10:37:39 +0000745 // Keep the RTI of inexact Object to avoid having to pass stack handle
746 // collection pointer to passes which may create NullConstant.
747 ReferenceTypeInfo inexact_object_rti_;
748
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000749 // Whether we are compiling this graph for on stack replacement: this will
750 // make all loops seen as irreducible and emit special stack maps to mark
751 // compiled code entries which the interpreter can directly jump to.
752 const bool osr_;
753
Mingyao Yang063fc772016-08-02 11:02:54 -0700754 // List of methods that are assumed to have single implementation.
755 ArenaSet<ArtMethod*> cha_single_implementation_list_;
756
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000757 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100758 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000759 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000760 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000761 DISALLOW_COPY_AND_ASSIGN(HGraph);
762};
763
Vladimir Markof9f64412015-09-02 14:05:49 +0100764class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000765 public:
766 HLoopInformation(HBasicBlock* header, HGraph* graph)
767 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100768 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000769 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100770 contains_irreducible_loop_(false),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100771 back_edges_(graph->GetAllocator()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100772 // Make bit vector growable, as the number of blocks may change.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100773 blocks_(graph->GetAllocator(),
774 graph->GetBlocks().size(),
775 true,
776 kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100777 back_edges_.reserve(kDefaultNumberOfBackEdges);
778 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100779
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000780 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100781 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000782
783 void Dump(std::ostream& os);
784
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100785 HBasicBlock* GetHeader() const {
786 return header_;
787 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000788
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000789 void SetHeader(HBasicBlock* block) {
790 header_ = block;
791 }
792
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100793 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
794 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
795 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
796
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000797 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100798 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000799 }
800
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100801 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100802 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100803 }
804
David Brazdil46e2a392015-03-16 17:31:52 +0000805 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100806 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100807 }
808
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000809 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100810 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000811 }
812
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100813 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100814
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100815 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100816 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100817 }
818
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100819 // Returns the lifetime position of the back edge that has the
820 // greatest lifetime position.
821 size_t GetLifetimeEnd() const;
822
823 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100824 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100825 }
826
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000827 // Finds blocks that are part of this loop.
828 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100829
Artem Serov7f4aff62017-06-21 17:02:18 +0100830 // Updates blocks population of the loop and all of its outer' ones recursively after the
831 // population of the inner loop is updated.
832 void PopulateInnerLoopUpwards(HLoopInformation* inner_loop);
833
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100834 // Returns whether this loop information contains `block`.
835 // Note that this loop information *must* be populated before entering this function.
836 bool Contains(const HBasicBlock& block) const;
837
838 // Returns whether this loop information is an inner loop of `other`.
839 // Note that `other` *must* be populated before entering this function.
840 bool IsIn(const HLoopInformation& other) const;
841
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800842 // Returns true if instruction is not defined within this loop.
843 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700844
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100845 const ArenaBitVector& GetBlocks() const { return blocks_; }
846
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000847 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000848 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000849
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000850 void ClearAllBlocks() {
851 blocks_.ClearAllBits();
852 }
853
David Brazdil3f4a5222016-05-06 12:46:21 +0100854 bool HasBackEdgeNotDominatedByHeader() const;
855
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100856 bool IsPopulated() const {
857 return blocks_.GetHighestBitSet() != -1;
858 }
859
Anton Shaminf89381f2016-05-16 16:44:13 +0600860 bool DominatesAllBackEdges(HBasicBlock* block);
861
David Sehrc757dec2016-11-04 15:48:34 -0700862 bool HasExitEdge() const;
863
Artem Serov7f4aff62017-06-21 17:02:18 +0100864 // Resets back edge and blocks-in-loop data.
865 void ResetBasicBlockData() {
866 back_edges_.clear();
867 ClearAllBlocks();
868 }
869
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000870 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100871 // Internal recursive implementation of `Populate`.
872 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100873 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100874
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000875 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100876 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000877 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100878 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100879 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100880 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000881
882 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
883};
884
David Brazdilec16f792015-08-19 15:04:01 +0100885// Stores try/catch information for basic blocks.
886// Note that HGraph is constructed so that catch blocks cannot simultaneously
887// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100888class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100889 public:
890 // Try block information constructor.
891 explicit TryCatchInformation(const HTryBoundary& try_entry)
892 : try_entry_(&try_entry),
893 catch_dex_file_(nullptr),
894 catch_type_index_(DexFile::kDexNoIndex16) {
895 DCHECK(try_entry_ != nullptr);
896 }
897
898 // Catch block information constructor.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800899 TryCatchInformation(dex::TypeIndex catch_type_index, const DexFile& dex_file)
David Brazdilec16f792015-08-19 15:04:01 +0100900 : try_entry_(nullptr),
901 catch_dex_file_(&dex_file),
902 catch_type_index_(catch_type_index) {}
903
904 bool IsTryBlock() const { return try_entry_ != nullptr; }
905
906 const HTryBoundary& GetTryEntry() const {
907 DCHECK(IsTryBlock());
908 return *try_entry_;
909 }
910
911 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
912
913 bool IsCatchAllTypeIndex() const {
914 DCHECK(IsCatchBlock());
Andreas Gampea5b09a62016-11-17 15:21:22 -0800915 return !catch_type_index_.IsValid();
David Brazdilec16f792015-08-19 15:04:01 +0100916 }
917
Andreas Gampea5b09a62016-11-17 15:21:22 -0800918 dex::TypeIndex GetCatchTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +0100919 DCHECK(IsCatchBlock());
920 return catch_type_index_;
921 }
922
923 const DexFile& GetCatchDexFile() const {
924 DCHECK(IsCatchBlock());
925 return *catch_dex_file_;
926 }
927
928 private:
929 // One of possibly several TryBoundary instructions entering the block's try.
930 // Only set for try blocks.
931 const HTryBoundary* try_entry_;
932
933 // Exception type information. Only set for catch blocks.
934 const DexFile* catch_dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800935 const dex::TypeIndex catch_type_index_;
David Brazdilec16f792015-08-19 15:04:01 +0100936};
937
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100938static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100939static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100940
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000941// A block in a method. Contains the list of instructions represented
942// as a double linked list. Each block knows its predecessors and
943// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100944
Vladimir Markof9f64412015-09-02 14:05:49 +0100945class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000946 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700947 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000948 : graph_(graph),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100949 predecessors_(graph->GetAllocator()->Adapter(kArenaAllocPredecessors)),
950 successors_(graph->GetAllocator()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000951 loop_information_(nullptr),
952 dominator_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100953 dominated_blocks_(graph->GetAllocator()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100954 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100955 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100956 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000957 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000958 try_catch_information_(nullptr) {
959 predecessors_.reserve(kDefaultNumberOfPredecessors);
960 successors_.reserve(kDefaultNumberOfSuccessors);
961 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
962 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000963
Vladimir Marko60584552015-09-03 13:35:12 +0000964 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100965 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000966 }
967
Vladimir Marko60584552015-09-03 13:35:12 +0000968 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100969 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000970 }
971
David Brazdild26a4112015-11-10 11:07:31 +0000972 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
973 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
974
Vladimir Marko60584552015-09-03 13:35:12 +0000975 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
976 return ContainsElement(successors_, block, start_from);
977 }
978
979 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100980 return dominated_blocks_;
981 }
982
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100983 bool IsEntryBlock() const {
984 return graph_->GetEntryBlock() == this;
985 }
986
987 bool IsExitBlock() const {
988 return graph_->GetExitBlock() == this;
989 }
990
David Brazdil46e2a392015-03-16 17:31:52 +0000991 bool IsSingleGoto() const;
Mads Ager16e52892017-07-14 13:11:37 +0200992 bool IsSingleReturn() const;
Mingyao Yang46721ef2017-10-05 14:45:17 -0700993 bool IsSingleReturnOrReturnVoidAllowingPhis() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000994 bool IsSingleTryBoundary() const;
995
996 // Returns true if this block emits nothing but a jump.
997 bool IsSingleJump() const {
998 HLoopInformation* loop_info = GetLoopInformation();
999 return (IsSingleGoto() || IsSingleTryBoundary())
1000 // Back edges generate a suspend check.
1001 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
1002 }
David Brazdil46e2a392015-03-16 17:31:52 +00001003
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001004 void AddBackEdge(HBasicBlock* back_edge) {
1005 if (loop_information_ == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001006 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001007 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001008 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001009 loop_information_->AddBackEdge(back_edge);
1010 }
1011
Artem Serov7f4aff62017-06-21 17:02:18 +01001012 // Registers a back edge; if the block was not a loop header before the call associates a newly
1013 // created loop info with it.
1014 //
1015 // Used in SuperblockCloner to preserve LoopInformation object instead of reseting loop
1016 // info for all blocks during back edges recalculation.
1017 void AddBackEdgeWhileUpdating(HBasicBlock* back_edge) {
1018 if (loop_information_ == nullptr || loop_information_->GetHeader() != this) {
1019 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
1020 }
1021 loop_information_->AddBackEdge(back_edge);
1022 }
1023
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001024 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001025 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001026
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001027 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001028 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001029 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001030
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001031 HBasicBlock* GetDominator() const { return dominator_; }
1032 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +00001033 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
1034
1035 void RemoveDominatedBlock(HBasicBlock* block) {
1036 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +01001037 }
Vladimir Marko60584552015-09-03 13:35:12 +00001038
1039 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
1040 ReplaceElement(dominated_blocks_, existing, new_block);
1041 }
1042
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001043 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001044
1045 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001046 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001047 }
1048
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001049 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
1050 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +01001051 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001052 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +01001053 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
1054 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001055
Nicolas Geoffray09aa1472016-01-19 10:52:54 +00001056 HInstruction* GetFirstInstructionDisregardMoves() const;
1057
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001058 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001059 successors_.push_back(block);
1060 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001061 }
1062
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001063 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
1064 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001065 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001066 new_block->predecessors_.push_back(this);
1067 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001068 }
1069
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001070 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
1071 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001072 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001073 new_block->successors_.push_back(this);
1074 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001075 }
1076
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001077 // Insert `this` between `predecessor` and `successor. This method
1078 // preserves the indicies, and will update the first edge found between
1079 // `predecessor` and `successor`.
1080 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
1081 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001082 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +00001083 successor->predecessors_[predecessor_index] = this;
1084 predecessor->successors_[successor_index] = this;
1085 successors_.push_back(successor);
1086 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001087 }
1088
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001089 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001090 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001091 }
1092
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001093 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001094 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001095 }
1096
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001097 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001098 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001099 }
1100
1101 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001102 predecessors_.push_back(block);
1103 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001104 }
1105
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001106 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001107 DCHECK_EQ(predecessors_.size(), 2u);
1108 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001109 }
1110
David Brazdil769c9e52015-04-27 13:54:09 +01001111 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001112 DCHECK_EQ(successors_.size(), 2u);
1113 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +01001114 }
1115
David Brazdilfc6a86a2015-06-26 10:33:45 +00001116 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001117 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001118 }
1119
David Brazdilfc6a86a2015-06-26 10:33:45 +00001120 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001121 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001122 }
1123
David Brazdilfc6a86a2015-06-26 10:33:45 +00001124 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001125 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001126 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001127 }
1128
1129 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001130 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001131 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001132 }
1133
1134 // Returns whether the first occurrence of `predecessor` in the list of
1135 // predecessors is at index `idx`.
1136 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001137 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001138 return GetPredecessorIndexOf(predecessor) == idx;
1139 }
1140
David Brazdild7558da2015-09-22 13:04:14 +01001141 // Create a new block between this block and its predecessors. The new block
1142 // is added to the graph, all predecessor edges are relinked to it and an edge
1143 // is created to `this`. Returns the new empty block. Reverse post order or
1144 // loop and try/catch information are not updated.
1145 HBasicBlock* CreateImmediateDominator();
1146
David Brazdilfc6a86a2015-06-26 10:33:45 +00001147 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001148 // created, latter block. Note that this method will add the block to the
1149 // graph, create a Goto at the end of the former block and will create an edge
1150 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001151 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001152 HBasicBlock* SplitBefore(HInstruction* cursor);
1153
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001154 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001155 // created block. Note that this method just updates raw block information,
1156 // like predecessors, successors, dominators, and instruction list. It does not
1157 // update the graph, reverse post order, loop information, nor make sure the
1158 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001159 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1160
1161 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1162 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001163
1164 // Merge `other` at the end of `this`. Successors and dominated blocks of
1165 // `other` are changed to be successors and dominated blocks of `this`. Note
1166 // that this method does not update the graph, reverse post order, loop
1167 // information, nor make sure the blocks are consistent (for example ending
1168 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001169 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001170
1171 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1172 // of `this` are moved to `other`.
1173 // Note that this method does not update the graph, reverse post order, loop
1174 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001175 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001176 void ReplaceWith(HBasicBlock* other);
1177
Aart Bik6b69e0a2017-01-11 10:20:43 -08001178 // Merges the instructions of `other` at the end of `this`.
1179 void MergeInstructionsWith(HBasicBlock* other);
1180
David Brazdil2d7352b2015-04-20 14:52:42 +01001181 // Merge `other` at the end of `this`. This method updates loops, reverse post
1182 // order, links to predecessors, successors, dominators and deletes the block
1183 // from the graph. The two blocks must be successive, i.e. `this` the only
1184 // predecessor of `other` and vice versa.
1185 void MergeWith(HBasicBlock* other);
1186
1187 // Disconnects `this` from all its predecessors, successors and dominator,
1188 // removes it from all loops it is included in and eventually from the graph.
1189 // The block must not dominate any other block. Predecessors and successors
1190 // are safely updated.
1191 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001192
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001193 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001194 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001195 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001196 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Artem Serovcced8ba2017-07-19 18:18:09 +01001197 // Replace phi `initial` with `replacement` within this block.
1198 void ReplaceAndRemovePhiWith(HPhi* initial, HPhi* replacement);
Roland Levillainccc07a92014-09-16 14:48:16 +01001199 // Replace instruction `initial` with `replacement` within this block.
1200 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1201 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001202 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001203 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001204 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1205 // instruction list. With 'ensure_safety' set to true, it verifies that the
1206 // instruction is not in use and removes it from the use lists of its inputs.
1207 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1208 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001209 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001210
1211 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001212 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001213 }
1214
Roland Levillain6b879dd2014-09-22 17:13:44 +01001215 bool IsLoopPreHeaderFirstPredecessor() const {
1216 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001217 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001218 }
1219
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001220 bool IsFirstPredecessorBackEdge() const {
1221 DCHECK(IsLoopHeader());
1222 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1223 }
1224
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001225 HLoopInformation* GetLoopInformation() const {
1226 return loop_information_;
1227 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001228
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001229 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001230 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001231 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001232 void SetInLoop(HLoopInformation* info) {
1233 if (IsLoopHeader()) {
1234 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001235 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001236 loop_information_ = info;
1237 } else if (loop_information_->Contains(*info->GetHeader())) {
1238 // Block is currently part of an outer loop. Make it part of this inner loop.
1239 // Note that a non loop header having a loop information means this loop information
1240 // has already been populated
1241 loop_information_ = info;
1242 } else {
1243 // Block is part of an inner loop. Do not update the loop information.
1244 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1245 // at this point, because this method is being called while populating `info`.
1246 }
1247 }
1248
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001249 // Raw update of the loop information.
1250 void SetLoopInformation(HLoopInformation* info) {
1251 loop_information_ = info;
1252 }
1253
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001254 bool IsInLoop() const { return loop_information_ != nullptr; }
1255
David Brazdilec16f792015-08-19 15:04:01 +01001256 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1257
1258 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1259 try_catch_information_ = try_catch_information;
1260 }
1261
1262 bool IsTryBlock() const {
1263 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1264 }
1265
1266 bool IsCatchBlock() const {
1267 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1268 }
David Brazdilffee3d32015-07-06 11:48:53 +01001269
1270 // Returns the try entry that this block's successors should have. They will
1271 // be in the same try, unless the block ends in a try boundary. In that case,
1272 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001273 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001274
Aart Bik75ff2c92018-04-21 01:28:11 +00001275 bool HasThrowingInstructions() const;
1276
David Brazdila4b8c212015-05-07 09:59:30 +01001277 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001278 bool Dominates(HBasicBlock* block) const;
1279
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001280 size_t GetLifetimeStart() const { return lifetime_start_; }
1281 size_t GetLifetimeEnd() const { return lifetime_end_; }
1282
1283 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1284 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1285
David Brazdil8d5b8b22015-03-24 10:51:52 +00001286 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001287 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001288 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001289 bool HasSinglePhi() const;
1290
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001291 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001292 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001293 ArenaVector<HBasicBlock*> predecessors_;
1294 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001295 HInstructionList instructions_;
1296 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001297 HLoopInformation* loop_information_;
1298 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001299 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001300 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001301 // The dex program counter of the first instruction of this block.
1302 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001303 size_t lifetime_start_;
1304 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001305 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001306
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001307 friend class HGraph;
1308 friend class HInstruction;
1309
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001310 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1311};
1312
David Brazdilb2bd1c52015-03-25 11:17:37 +00001313// Iterates over the LoopInformation of all loops which contain 'block'
1314// from the innermost to the outermost.
1315class HLoopInformationOutwardIterator : public ValueObject {
1316 public:
1317 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1318 : current_(block.GetLoopInformation()) {}
1319
1320 bool Done() const { return current_ == nullptr; }
1321
1322 void Advance() {
1323 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001324 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001325 }
1326
1327 HLoopInformation* Current() const {
1328 DCHECK(!Done());
1329 return current_;
1330 }
1331
1332 private:
1333 HLoopInformation* current_;
1334
1335 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1336};
1337
Alexandre Ramesef20f712015-06-09 10:29:30 +01001338#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001339 M(Above, Condition) \
1340 M(AboveOrEqual, Condition) \
Aart Bik3dad3412018-02-28 12:01:46 -08001341 M(Abs, UnaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001342 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001343 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001344 M(ArrayGet, Instruction) \
1345 M(ArrayLength, Instruction) \
1346 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001347 M(Below, Condition) \
1348 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001349 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001350 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001351 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001352 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001353 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001354 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001355 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001356 M(Compare, BinaryOperation) \
Igor Murashkind01745e2017-04-05 16:40:31 -07001357 M(ConstructorFence, Instruction) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001358 M(CurrentMethod, Instruction) \
Mingyao Yang063fc772016-08-02 11:02:54 -07001359 M(ShouldDeoptimizeFlag, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001360 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001361 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001362 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001363 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001364 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001365 M(Exit, Instruction) \
1366 M(FloatConstant, Constant) \
1367 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001368 M(GreaterThan, Condition) \
1369 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001370 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001371 M(InstanceFieldGet, Instruction) \
1372 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001373 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001374 M(IntConstant, Constant) \
xueliang.zhonge0eb4832017-10-30 13:43:14 +00001375 M(IntermediateAddress, Instruction) \
Calin Juravle175dc732015-08-25 15:42:32 +01001376 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001377 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001378 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001379 M(InvokeVirtual, Invoke) \
Orion Hodsonac141392017-01-13 11:53:47 +00001380 M(InvokePolymorphic, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001381 M(LessThan, Condition) \
1382 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001383 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001384 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001385 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001386 M(LongConstant, Constant) \
Aart Bik1f8d51b2018-02-15 10:42:37 -08001387 M(Max, Instruction) \
Calin Juravle27df7582015-04-17 19:12:31 +01001388 M(MemoryBarrier, Instruction) \
Aart Bik1f8d51b2018-02-15 10:42:37 -08001389 M(Min, BinaryOperation) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001390 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001391 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001392 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001393 M(Neg, UnaryOperation) \
1394 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001395 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001396 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001397 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001398 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001399 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001400 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001401 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001402 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001403 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001404 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001405 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001406 M(Return, Instruction) \
1407 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001408 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001409 M(Shl, BinaryOperation) \
1410 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001411 M(StaticFieldGet, Instruction) \
1412 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001413 M(UnresolvedInstanceFieldGet, Instruction) \
1414 M(UnresolvedInstanceFieldSet, Instruction) \
1415 M(UnresolvedStaticFieldGet, Instruction) \
1416 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001417 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001418 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001419 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001420 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001421 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001422 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001423 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001424 M(Xor, BinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001425 M(VecReplicateScalar, VecUnaryOperation) \
Aart Bik0148de42017-09-05 09:25:01 -07001426 M(VecExtractScalar, VecUnaryOperation) \
1427 M(VecReduce, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001428 M(VecCnv, VecUnaryOperation) \
1429 M(VecNeg, VecUnaryOperation) \
Aart Bik6daebeb2017-04-03 14:35:41 -07001430 M(VecAbs, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001431 M(VecNot, VecUnaryOperation) \
1432 M(VecAdd, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001433 M(VecHalvingAdd, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001434 M(VecSub, VecBinaryOperation) \
1435 M(VecMul, VecBinaryOperation) \
1436 M(VecDiv, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001437 M(VecMin, VecBinaryOperation) \
1438 M(VecMax, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001439 M(VecAnd, VecBinaryOperation) \
1440 M(VecAndNot, VecBinaryOperation) \
1441 M(VecOr, VecBinaryOperation) \
1442 M(VecXor, VecBinaryOperation) \
Aart Bik29aa0822018-03-08 11:28:00 -08001443 M(VecSaturationAdd, VecBinaryOperation) \
1444 M(VecSaturationSub, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001445 M(VecShl, VecBinaryOperation) \
1446 M(VecShr, VecBinaryOperation) \
1447 M(VecUShr, VecBinaryOperation) \
Aart Bik8de59162017-04-21 09:42:01 -07001448 M(VecSetScalars, VecOperation) \
Artem Serovf34dd202017-04-10 17:41:46 +01001449 M(VecMultiplyAccumulate, VecOperation) \
Aart Bikdbbac8f2017-09-01 13:06:08 -07001450 M(VecSADAccumulate, VecOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001451 M(VecLoad, VecMemoryOperation) \
1452 M(VecStore, VecMemoryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001453
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001454/*
1455 * Instructions, shared across several (not all) architectures.
1456 */
1457#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1458#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1459#else
1460#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001461 M(BitwiseNegatedRight, Instruction) \
Anton Kirilov74234da2017-01-13 14:42:47 +00001462 M(DataProcWithShifterOp, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001463 M(MultiplyAccumulate, Instruction) \
Artem Serove1811ed2017-04-27 16:50:47 +01001464 M(IntermediateAddressIndex, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001465#endif
1466
Alexandre Ramesef20f712015-06-09 10:29:30 +01001467#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1468
1469#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1470
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001471#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001472#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001473#else
1474#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1475 M(MipsComputeBaseMethodAddress, Instruction) \
Lena Djokica2901602017-09-21 13:50:52 +02001476 M(MipsPackedSwitch, Instruction) \
1477 M(IntermediateArrayAddressIndex, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001478#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001479
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001480#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1481
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001482#ifndef ART_ENABLE_CODEGEN_x86
1483#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1484#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001485#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1486 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001487 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001488 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001489 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001490#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001491
1492#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1493
1494#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1495 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001496 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001497 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1498 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001499 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001500 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001501 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1502 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1503
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001504#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1505 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001506 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001507 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001508 M(BinaryOperation, Instruction) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001509 M(Invoke, Instruction) \
1510 M(VecOperation, Instruction) \
1511 M(VecUnaryOperation, VecOperation) \
1512 M(VecBinaryOperation, VecOperation) \
1513 M(VecMemoryOperation, VecOperation)
Dave Allison20dfc792014-06-16 20:44:29 -07001514
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001515#define FOR_EACH_INSTRUCTION(M) \
1516 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1517 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1518
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001519#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001520FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1521#undef FORWARD_DECLARATION
1522
Vladimir Marko372f10e2016-05-17 16:30:10 +01001523#define DECLARE_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001524 private: \
1525 H##type& operator=(const H##type&) = delete; \
1526 public: \
Vladimir Marko372f10e2016-05-17 16:30:10 +01001527 const char* DebugName() const OVERRIDE { return #type; } \
Artem Serovcced8ba2017-07-19 18:18:09 +01001528 HInstruction* Clone(ArenaAllocator* arena) const OVERRIDE { \
1529 DCHECK(IsClonable()); \
1530 return new (arena) H##type(*this->As##type()); \
1531 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001532 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001533
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001534#define DECLARE_ABSTRACT_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001535 private: \
1536 H##type& operator=(const H##type&) = delete; \
1537 public: \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001538 bool Is##type() const { return As##type() != nullptr; } \
1539 const H##type* As##type() const { return this; } \
1540 H##type* As##type() { return this; }
1541
Artem Serovcced8ba2017-07-19 18:18:09 +01001542#define DEFAULT_COPY_CONSTRUCTOR(type) \
1543 explicit H##type(const H##type& other) = default;
1544
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001545template <typename T>
Vladimir Marko82b07402017-03-01 19:02:04 +00001546class HUseListNode : public ArenaObject<kArenaAllocUseListNode>,
1547 public IntrusiveForwardListNode<HUseListNode<T>> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001548 public:
Igor Murashkind01745e2017-04-05 16:40:31 -07001549 // Get the instruction which has this use as one of the inputs.
David Brazdiled596192015-01-23 10:39:45 +00001550 T GetUser() const { return user_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001551 // Get the position of the input record that this use corresponds to.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001552 size_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001553 // Set the position of the input record that this use corresponds to.
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001554 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001555
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001556 private:
David Brazdiled596192015-01-23 10:39:45 +00001557 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001558 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001559
1560 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001561 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001562
Vladimir Marko46817b82016-03-29 12:21:58 +01001563 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001564
1565 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1566};
1567
David Brazdiled596192015-01-23 10:39:45 +00001568template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001569using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001570
David Brazdil1abb4192015-02-17 18:33:36 +00001571// This class is used by HEnvironment and HInstruction classes to record the
1572// instructions they use and pointers to the corresponding HUseListNodes kept
1573// by the used instructions.
1574template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001575class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001576 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001577 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1578 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001579
Vladimir Marko46817b82016-03-29 12:21:58 +01001580 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1581 : HUserRecord(old_record.instruction_, before_use_node) {}
1582 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1583 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001584 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001585 }
1586
1587 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001588 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1589 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001590
1591 private:
1592 // Instruction used by the user.
1593 HInstruction* instruction_;
1594
Vladimir Marko46817b82016-03-29 12:21:58 +01001595 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1596 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001597};
1598
Vladimir Markoe9004912016-06-16 16:50:52 +01001599// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1600// This is used for HInstruction::GetInputs() to return a container wrapper providing
1601// HInstruction* values even though the underlying container has HUserRecord<>s.
1602struct HInputExtractor {
1603 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1604 return record.GetInstruction();
1605 }
1606 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1607 return record.GetInstruction();
1608 }
1609};
1610
1611using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1612using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1613
Aart Bik854a02b2015-07-14 16:07:00 -07001614/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001615 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001616 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001617 * For write/read dependences on fields/arrays, the dependence analysis uses
1618 * type disambiguation (e.g. a float field write cannot modify the value of an
1619 * integer field read) and the access type (e.g. a reference array write cannot
1620 * modify the value of a reference field read [although it may modify the
1621 * reference fetch prior to reading the field, which is represented by its own
1622 * write/read dependence]). The analysis makes conservative points-to
1623 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1624 * the same, and any reference read depends on any reference read without
1625 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001626 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001627 * The internal representation uses 38-bit and is described in the table below.
1628 * The first line indicates the side effect, and for field/array accesses the
1629 * second line indicates the type of the access (in the order of the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001630 * DataType::Type enum).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001631 * The two numbered lines below indicate the bit position in the bitfield (read
1632 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001633 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001634 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1635 * +-------------+---------+---------+--------------+---------+---------+
1636 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1637 * | 3 |333333322|222222221| 1 |111111110|000000000|
1638 * | 7 |654321098|765432109| 8 |765432109|876543210|
1639 *
1640 * Note that, to ease the implementation, 'changes' bits are least significant
1641 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001642 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001643class SideEffects : public ValueObject {
1644 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001645 SideEffects() : flags_(0) {}
1646
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001647 static SideEffects None() {
1648 return SideEffects(0);
1649 }
1650
1651 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001652 return SideEffects(kAllChangeBits | kAllDependOnBits);
1653 }
1654
1655 static SideEffects AllChanges() {
1656 return SideEffects(kAllChangeBits);
1657 }
1658
1659 static SideEffects AllDependencies() {
1660 return SideEffects(kAllDependOnBits);
1661 }
1662
1663 static SideEffects AllExceptGCDependency() {
1664 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1665 }
1666
1667 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001668 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001669 }
1670
Aart Bik34c3ba92015-07-20 14:08:59 -07001671 static SideEffects AllWrites() {
1672 return SideEffects(kAllWrites);
1673 }
1674
1675 static SideEffects AllReads() {
1676 return SideEffects(kAllReads);
1677 }
1678
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001679 static SideEffects FieldWriteOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001680 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001681 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001682 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001683 }
1684
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001685 static SideEffects ArrayWriteOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001686 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001687 }
1688
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001689 static SideEffects FieldReadOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001690 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001691 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001692 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001693 }
1694
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001695 static SideEffects ArrayReadOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001696 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001697 }
1698
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001699 static SideEffects CanTriggerGC() {
1700 return SideEffects(1ULL << kCanTriggerGCBit);
1701 }
1702
1703 static SideEffects DependsOnGC() {
1704 return SideEffects(1ULL << kDependsOnGCBit);
1705 }
1706
Aart Bik854a02b2015-07-14 16:07:00 -07001707 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001708 SideEffects Union(SideEffects other) const {
1709 return SideEffects(flags_ | other.flags_);
1710 }
1711
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001712 SideEffects Exclusion(SideEffects other) const {
1713 return SideEffects(flags_ & ~other.flags_);
1714 }
1715
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001716 void Add(SideEffects other) {
1717 flags_ |= other.flags_;
1718 }
1719
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001720 bool Includes(SideEffects other) const {
1721 return (other.flags_ & flags_) == other.flags_;
1722 }
1723
1724 bool HasSideEffects() const {
1725 return (flags_ & kAllChangeBits);
1726 }
1727
1728 bool HasDependencies() const {
1729 return (flags_ & kAllDependOnBits);
1730 }
1731
1732 // Returns true if there are no side effects or dependencies.
1733 bool DoesNothing() const {
1734 return flags_ == 0;
1735 }
1736
Aart Bik854a02b2015-07-14 16:07:00 -07001737 // Returns true if something is written.
1738 bool DoesAnyWrite() const {
1739 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001740 }
1741
Aart Bik854a02b2015-07-14 16:07:00 -07001742 // Returns true if something is read.
1743 bool DoesAnyRead() const {
1744 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001745 }
1746
Aart Bik854a02b2015-07-14 16:07:00 -07001747 // Returns true if potentially everything is written and read
1748 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001749 bool DoesAllReadWrite() const {
1750 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1751 }
1752
Aart Bik854a02b2015-07-14 16:07:00 -07001753 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001754 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001755 }
1756
Roland Levillain0d5a2812015-11-13 10:07:31 +00001757 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001758 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001759 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1760 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001761 }
1762
1763 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001764 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001765 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001766 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001767 for (int s = kLastBit; s >= 0; s--) {
1768 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1769 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1770 // This is a bit for the GC side effect.
1771 if (current_bit_is_set) {
1772 flags += "GC";
1773 }
Aart Bik854a02b2015-07-14 16:07:00 -07001774 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001775 } else {
1776 // This is a bit for the array/field analysis.
1777 // The underscore character stands for the 'can trigger GC' bit.
1778 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1779 if (current_bit_is_set) {
1780 flags += kDebug[s];
1781 }
1782 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1783 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1784 flags += "|";
1785 }
1786 }
Aart Bik854a02b2015-07-14 16:07:00 -07001787 }
1788 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001789 }
1790
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001791 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001792
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001793 private:
1794 static constexpr int kFieldArrayAnalysisBits = 9;
1795
1796 static constexpr int kFieldWriteOffset = 0;
1797 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1798 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1799 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1800
1801 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1802
1803 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1804 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1805 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1806 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1807
1808 static constexpr int kLastBit = kDependsOnGCBit;
1809 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1810
1811 // Aliases.
1812
1813 static_assert(kChangeBits == kDependOnBits,
1814 "the 'change' bits should match the 'depend on' bits.");
1815
1816 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1817 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1818 static constexpr uint64_t kAllWrites =
1819 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1820 static constexpr uint64_t kAllReads =
1821 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001822
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001823 // Translates type to bit flag. The type must correspond to a Java type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001824 static uint64_t TypeFlag(DataType::Type type, int offset) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001825 int shift;
1826 switch (type) {
1827 case DataType::Type::kReference: shift = 0; break;
1828 case DataType::Type::kBool: shift = 1; break;
1829 case DataType::Type::kInt8: shift = 2; break;
1830 case DataType::Type::kUint16: shift = 3; break;
1831 case DataType::Type::kInt16: shift = 4; break;
1832 case DataType::Type::kInt32: shift = 5; break;
1833 case DataType::Type::kInt64: shift = 6; break;
1834 case DataType::Type::kFloat32: shift = 7; break;
1835 case DataType::Type::kFloat64: shift = 8; break;
1836 default:
1837 LOG(FATAL) << "Unexpected data type " << type;
1838 UNREACHABLE();
1839 }
Aart Bik854a02b2015-07-14 16:07:00 -07001840 DCHECK_LE(kFieldWriteOffset, shift);
1841 DCHECK_LT(shift, kArrayWriteOffset);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001842 return UINT64_C(1) << (shift + offset);
Aart Bik854a02b2015-07-14 16:07:00 -07001843 }
1844
1845 // Private constructor on direct flags value.
1846 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1847
1848 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001849};
1850
David Brazdiled596192015-01-23 10:39:45 +00001851// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001852class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001853 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001854 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
Mingyao Yang01b47b02017-02-03 12:09:57 -08001855 size_t number_of_vregs,
1856 ArtMethod* method,
1857 uint32_t dex_pc,
1858 HInstruction* holder)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001859 : vregs_(number_of_vregs, allocator->Adapter(kArenaAllocEnvironmentVRegs)),
1860 locations_(allocator->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001861 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001862 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001863 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001864 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001865 }
1866
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001867 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
1868 const HEnvironment& to_copy,
1869 HInstruction* holder)
1870 : HEnvironment(allocator,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001871 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001872 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001873 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001874 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001875
Vladimir Markoec32f642017-06-02 10:51:55 +01001876 void AllocateLocations() {
1877 DCHECK(locations_.empty());
1878 locations_.resize(vregs_.size());
1879 }
1880
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001881 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001882 if (parent_ != nullptr) {
1883 parent_->SetAndCopyParentChain(allocator, parent);
1884 } else {
1885 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1886 parent_->CopyFrom(parent);
1887 if (parent->GetParent() != nullptr) {
1888 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1889 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001890 }
David Brazdiled596192015-01-23 10:39:45 +00001891 }
1892
Vladimir Marko69d310e2017-10-09 14:12:23 +01001893 void CopyFrom(ArrayRef<HInstruction* const> locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001894 void CopyFrom(HEnvironment* environment);
1895
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001896 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1897 // input to the loop phi instead. This is for inserting instructions that
1898 // require an environment (like HDeoptimization) in the loop pre-header.
1899 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001900
1901 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001902 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001903 }
1904
1905 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001906 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001907 }
1908
David Brazdil1abb4192015-02-17 18:33:36 +00001909 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001910
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001911 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001912
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001913 HEnvironment* GetParent() const { return parent_; }
1914
1915 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001916 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001917 }
1918
1919 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001920 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001921 }
1922
1923 uint32_t GetDexPc() const {
1924 return dex_pc_;
1925 }
1926
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001927 ArtMethod* GetMethod() const {
1928 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001929 }
1930
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001931 HInstruction* GetHolder() const {
1932 return holder_;
1933 }
1934
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001935
1936 bool IsFromInlinedInvoke() const {
1937 return GetParent() != nullptr;
1938 }
1939
David Brazdiled596192015-01-23 10:39:45 +00001940 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001941 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1942 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001943 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001944 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001945 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00001946
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001947 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001948 HInstruction* const holder_;
1949
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001950 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001951
1952 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1953};
1954
Vladimir Markof9f64412015-09-02 14:05:49 +01001955class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001956 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05301957#define DECLARE_KIND(type, super) k##type,
1958 enum InstructionKind {
1959 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1960 kLastInstructionKind
1961 };
1962#undef DECLARE_KIND
1963
1964 HInstruction(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01001965 : HInstruction(kind, DataType::Type::kVoid, side_effects, dex_pc) {}
1966
1967 HInstruction(InstructionKind kind, DataType::Type type, SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001968 : previous_(nullptr),
1969 next_(nullptr),
1970 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001971 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001972 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001973 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001974 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001975 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001976 locations_(nullptr),
1977 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001978 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001979 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001980 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05301981 SetPackedField<InstructionKindField>(kind);
Vladimir Markobd785672018-05-03 17:09:09 +01001982 SetPackedField<TypeField>(type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001983 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1984 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001985
Dave Allison20dfc792014-06-16 20:44:29 -07001986 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001987
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001988
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001989 HInstruction* GetNext() const { return next_; }
1990 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001991
Calin Juravle77520bc2015-01-12 18:45:46 +00001992 HInstruction* GetNextDisregardingMoves() const;
1993 HInstruction* GetPreviousDisregardingMoves() const;
1994
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001995 HBasicBlock* GetBlock() const { return block_; }
Vladimir Markoca6fff82017-10-03 14:49:14 +01001996 ArenaAllocator* GetAllocator() const { return block_->GetGraph()->GetAllocator(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001997 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001998 bool IsInBlock() const { return block_ != nullptr; }
1999 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00002000 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
2001 bool IsIrreducibleLoopHeaderPhi() const {
2002 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
2003 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002004
Vladimir Marko372f10e2016-05-17 16:30:10 +01002005 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
2006
2007 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
2008 // One virtual method is enough, just const_cast<> and then re-add the const.
2009 return ArrayRef<const HUserRecord<HInstruction*>>(
2010 const_cast<HInstruction*>(this)->GetInputRecords());
2011 }
2012
Vladimir Markoe9004912016-06-16 16:50:52 +01002013 HInputsRef GetInputs() {
2014 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002015 }
2016
Vladimir Markoe9004912016-06-16 16:50:52 +01002017 HConstInputsRef GetInputs() const {
2018 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002019 }
2020
2021 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00002022 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002023
Aart Bik2767f4b2016-10-28 15:03:53 -07002024 bool HasInput(HInstruction* input) const {
2025 for (const HInstruction* i : GetInputs()) {
2026 if (i == input) {
2027 return true;
2028 }
2029 }
2030 return false;
2031 }
2032
Vladimir Marko372f10e2016-05-17 16:30:10 +01002033 void SetRawInputAt(size_t index, HInstruction* input) {
2034 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
2035 }
2036
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002037 virtual void Accept(HGraphVisitor* visitor) = 0;
2038 virtual const char* DebugName() const = 0;
2039
Vladimir Markobd785672018-05-03 17:09:09 +01002040 DataType::Type GetType() const {
2041 return TypeField::Decode(GetPackedFields());
2042 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002043
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002044 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002045
2046 uint32_t GetDexPc() const { return dex_pc_; }
2047
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002048 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01002049
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002050 // Can the instruction throw?
2051 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
2052 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01002053 virtual bool CanThrow() const { return false; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08002054
2055 // Does the instruction always throw an exception unconditionally?
2056 virtual bool AlwaysThrows() const { return false; }
2057
David Brazdilec16f792015-08-19 15:04:01 +01002058 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002059
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002060 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002061 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002062
Calin Juravle10e244f2015-01-26 18:54:32 +00002063 // Does not apply for all instructions, but having this at top level greatly
2064 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00002065 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002066 virtual bool CanBeNull() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002067 DCHECK_EQ(GetType(), DataType::Type::kReference) << "CanBeNull only applies to reference types";
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002068 return true;
2069 }
Calin Juravle10e244f2015-01-26 18:54:32 +00002070
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002071 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01002072 return false;
2073 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002074
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002075 virtual bool IsActualObject() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002076 return GetType() == DataType::Type::kReference;
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002077 }
2078
Calin Juravle2e768302015-07-28 14:41:11 +00002079 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00002080
Calin Juravle61d544b2015-02-23 16:46:57 +00002081 ReferenceTypeInfo GetReferenceTypeInfo() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002082 DCHECK_EQ(GetType(), DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002083 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00002084 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00002085 }
Calin Juravleacf735c2015-02-12 15:25:22 +00002086
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002087 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00002088 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002089 // Note: fixup_end remains valid across push_front().
2090 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
2091 HUseListNode<HInstruction*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002092 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HInstruction*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002093 uses_.push_front(*new_node);
2094 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002095 }
2096
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002097 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01002098 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002099 // Note: env_fixup_end remains valid across push_front().
2100 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
2101 HUseListNode<HEnvironment*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002102 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HEnvironment*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002103 env_uses_.push_front(*new_node);
2104 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002105 }
2106
David Brazdil1abb4192015-02-17 18:33:36 +00002107 void RemoveAsUserOfInput(size_t input) {
2108 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002109 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2110 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2111 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00002112 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002113
Vladimir Marko372f10e2016-05-17 16:30:10 +01002114 void RemoveAsUserOfAllInputs() {
2115 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2116 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2117 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2118 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
2119 }
2120 }
2121
David Brazdil1abb4192015-02-17 18:33:36 +00002122 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2123 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002124
Vladimir Marko46817b82016-03-29 12:21:58 +01002125 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2126 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2127 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002128 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002129 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002130 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002131
Aart Bikcc42be02016-10-20 16:14:16 -07002132 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002133 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002134 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002135 !CanThrow() &&
2136 !IsSuspendCheck() &&
2137 !IsControlFlow() &&
2138 !IsNativeDebugInfo() &&
2139 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002140 // If we added an explicit barrier then we should keep it.
Igor Murashkind01745e2017-04-05 16:40:31 -07002141 !IsMemoryBarrier() &&
2142 !IsConstructorFence();
Aart Bik482095d2016-10-10 15:39:10 -07002143 }
2144
Aart Bikcc42be02016-10-20 16:14:16 -07002145 bool IsDeadAndRemovable() const {
2146 return IsRemovable() && !HasUses();
2147 }
2148
Roland Levillain6c82d402014-10-13 16:10:27 +01002149 // Does this instruction strictly dominate `other_instruction`?
2150 // Returns false if this instruction and `other_instruction` are the same.
2151 // Aborts if this instruction and `other_instruction` are both phis.
2152 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002153
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002154 int GetId() const { return id_; }
2155 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002156
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002157 int GetSsaIndex() const { return ssa_index_; }
2158 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2159 bool HasSsaIndex() const { return ssa_index_ != -1; }
2160
2161 bool HasEnvironment() const { return environment_ != nullptr; }
2162 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002163 // Set the `environment_` field. Raw because this method does not
2164 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002165 void SetRawEnvironment(HEnvironment* environment) {
2166 DCHECK(environment_ == nullptr);
2167 DCHECK_EQ(environment->GetHolder(), this);
2168 environment_ = environment;
2169 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002170
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002171 void InsertRawEnvironment(HEnvironment* environment) {
2172 DCHECK(environment_ != nullptr);
2173 DCHECK_EQ(environment->GetHolder(), this);
2174 DCHECK(environment->GetParent() == nullptr);
2175 environment->parent_ = environment_;
2176 environment_ = environment;
2177 }
2178
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002179 void RemoveEnvironment();
2180
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002181 // Set the environment of this instruction, copying it from `environment`. While
2182 // copying, the uses lists are being updated.
2183 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002184 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002185 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002186 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002187 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002188 if (environment->GetParent() != nullptr) {
2189 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2190 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002191 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002192
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002193 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2194 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002195 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002196 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002197 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002198 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002199 if (environment->GetParent() != nullptr) {
2200 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2201 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002202 }
2203
Nicolas Geoffray39468442014-09-02 15:17:15 +01002204 // Returns the number of entries in the environment. Typically, that is the
2205 // number of dex registers in a method. It could be more in case of inlining.
2206 size_t EnvironmentSize() const;
2207
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002208 LocationSummary* GetLocations() const { return locations_; }
2209 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002210
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002211 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002212 void ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002213 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002214
Alexandre Rames188d4312015-04-09 18:30:21 +01002215 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2216 // uses of this instruction by `other` are *not* updated.
2217 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2218 ReplaceWith(other);
2219 other->ReplaceInput(this, use_index);
2220 }
2221
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002222 // Move `this` instruction before `cursor`
2223 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002224
Vladimir Markofb337ea2015-11-25 15:25:10 +00002225 // Move `this` before its first user and out of any loops. If there is no
2226 // out-of-loop user that dominates all other users, move the instruction
2227 // to the end of the out-of-loop common dominator of the user's blocks.
2228 //
2229 // This can be used only on non-throwing instructions with no side effects that
2230 // have at least one use but no environment uses.
2231 void MoveBeforeFirstUserAndOutOfLoops();
2232
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002233#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002234 bool Is##type() const; \
2235 const H##type* As##type() const; \
2236 H##type* As##type();
2237
2238 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2239#undef INSTRUCTION_TYPE_CHECK
2240
2241#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002242 bool Is##type() const { return (As##type() != nullptr); } \
2243 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002244 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002245 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002246#undef INSTRUCTION_TYPE_CHECK
2247
Artem Serovcced8ba2017-07-19 18:18:09 +01002248 // Return a clone of the instruction if it is clonable (shallow copy by default, custom copy
2249 // if a custom copy-constructor is provided for a particular type). If IsClonable() is false for
2250 // the instruction then the behaviour of this function is undefined.
2251 //
2252 // Note: It is semantically valid to create a clone of the instruction only until
2253 // prepare_for_register_allocator phase as lifetime, intervals and codegen info are not
2254 // copied.
2255 //
2256 // Note: HEnvironment and some other fields are not copied and are set to default values, see
2257 // 'explicit HInstruction(const HInstruction& other)' for details.
2258 virtual HInstruction* Clone(ArenaAllocator* arena ATTRIBUTE_UNUSED) const {
2259 LOG(FATAL) << "Cloning is not implemented for the instruction " <<
2260 DebugName() << " " << GetId();
2261 UNREACHABLE();
2262 }
2263
2264 // Return whether instruction can be cloned (copied).
2265 virtual bool IsClonable() const { return false; }
2266
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002267 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002268 // TODO: this method is used by LICM and GVN with possibly different
2269 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002270 virtual bool CanBeMoved() const { return false; }
2271
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002272 // Returns whether any data encoded in the two instructions is equal.
2273 // This method does not look at the inputs. Both instructions must be
2274 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002275 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002276 return false;
2277 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002278
2279 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002280 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002281 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002282 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002283
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002284 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2285 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2286 // the virtual function because the __attribute__((__pure__)) doesn't really
2287 // apply the strong requirement for virtual functions, preventing optimizations.
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302288 InstructionKind GetKind() const { return GetPackedField<InstructionKindField>(); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002289
2290 virtual size_t ComputeHashCode() const {
2291 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002292 for (const HInstruction* input : GetInputs()) {
2293 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002294 }
2295 return result;
2296 }
2297
2298 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002299 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002300 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002301
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002302 size_t GetLifetimePosition() const { return lifetime_position_; }
2303 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2304 LiveInterval* GetLiveInterval() const { return live_interval_; }
2305 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2306 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2307
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002308 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2309
2310 // Returns whether the code generation of the instruction will require to have access
2311 // to the current method. Such instructions are:
2312 // (1): Instructions that require an environment, as calling the runtime requires
2313 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002314 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2315 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002316 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002317 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002318 }
2319
Vladimir Markodc151b22015-10-15 18:02:30 +01002320 // Returns whether the code generation of the instruction will require to have access
2321 // to the dex cache of the current method's declaring class via the current method.
2322 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002323
Mark Mendellc4701932015-04-10 13:18:51 -04002324 // Does this instruction have any use in an environment before
2325 // control flow hits 'other'?
2326 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2327
2328 // Remove all references to environment uses of this instruction.
2329 // The caller must ensure that this is safe to do.
2330 void RemoveEnvironmentUsers();
2331
Vladimir Markoa1de9182016-02-25 11:37:38 +00002332 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2333 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002334
David Brazdil1abb4192015-02-17 18:33:36 +00002335 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002336 // If set, the machine code for this instruction is assumed to be generated by
2337 // its users. Used by liveness analysis to compute use positions accordingly.
2338 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2339 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302340 static constexpr size_t kFieldInstructionKind = kFlagReferenceTypeIsExact + 1;
2341 static constexpr size_t kFieldInstructionKindSize =
2342 MinimumBitsToStore(static_cast<size_t>(InstructionKind::kLastInstructionKind - 1));
Vladimir Markobd785672018-05-03 17:09:09 +01002343 static constexpr size_t kFieldType =
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302344 kFieldInstructionKind + kFieldInstructionKindSize;
Vladimir Markobd785672018-05-03 17:09:09 +01002345 static constexpr size_t kFieldTypeSize =
2346 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
2347 static constexpr size_t kNumberOfGenericPackedBits = kFieldType + kFieldTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00002348 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2349
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302350 static_assert(kNumberOfGenericPackedBits <= kMaxNumberOfPackedBits,
2351 "Too many generic packed fields");
2352
Vladimir Markobd785672018-05-03 17:09:09 +01002353 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
2354
Vladimir Marko372f10e2016-05-17 16:30:10 +01002355 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2356 return GetInputRecords()[i];
2357 }
2358
2359 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2360 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2361 input_records[index] = input;
2362 }
David Brazdil1abb4192015-02-17 18:33:36 +00002363
Vladimir Markoa1de9182016-02-25 11:37:38 +00002364 uint32_t GetPackedFields() const {
2365 return packed_fields_;
2366 }
2367
2368 template <size_t flag>
2369 bool GetPackedFlag() const {
2370 return (packed_fields_ & (1u << flag)) != 0u;
2371 }
2372
2373 template <size_t flag>
2374 void SetPackedFlag(bool value = true) {
2375 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2376 }
2377
2378 template <typename BitFieldType>
2379 typename BitFieldType::value_type GetPackedField() const {
2380 return BitFieldType::Decode(packed_fields_);
2381 }
2382
2383 template <typename BitFieldType>
2384 void SetPackedField(typename BitFieldType::value_type value) {
2385 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2386 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2387 }
2388
Artem Serovcced8ba2017-07-19 18:18:09 +01002389 // Copy construction for the instruction (used for Clone function).
2390 //
2391 // Fields (e.g. lifetime, intervals and codegen info) associated with phases starting from
2392 // prepare_for_register_allocator are not copied (set to default values).
2393 //
2394 // Copy constructors must be provided for every HInstruction type; default copy constructor is
2395 // fine for most of them. However for some of the instructions a custom copy constructor must be
2396 // specified (when instruction has non-trivially copyable fields and must have a special behaviour
2397 // for copying them).
2398 explicit HInstruction(const HInstruction& other)
2399 : previous_(nullptr),
2400 next_(nullptr),
2401 block_(nullptr),
2402 dex_pc_(other.dex_pc_),
2403 id_(-1),
2404 ssa_index_(-1),
2405 packed_fields_(other.packed_fields_),
2406 environment_(nullptr),
2407 locations_(nullptr),
2408 live_interval_(nullptr),
2409 lifetime_position_(kNoLifetime),
2410 side_effects_(other.side_effects_),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302411 reference_type_handle_(other.reference_type_handle_) {
2412 }
Artem Serovcced8ba2017-07-19 18:18:09 +01002413
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002414 private:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302415 using InstructionKindField =
2416 BitField<InstructionKind, kFieldInstructionKind, kFieldInstructionKindSize>;
2417
Vladimir Marko46817b82016-03-29 12:21:58 +01002418 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2419 auto before_use_node = uses_.before_begin();
2420 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2421 HInstruction* user = use_node->GetUser();
2422 size_t input_index = use_node->GetIndex();
2423 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2424 before_use_node = use_node;
2425 }
2426 }
2427
2428 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2429 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2430 if (next != uses_.end()) {
2431 HInstruction* next_user = next->GetUser();
2432 size_t next_index = next->GetIndex();
2433 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2434 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2435 }
2436 }
2437
2438 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2439 auto before_env_use_node = env_uses_.before_begin();
2440 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2441 HEnvironment* user = env_use_node->GetUser();
2442 size_t input_index = env_use_node->GetIndex();
2443 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2444 before_env_use_node = env_use_node;
2445 }
2446 }
2447
2448 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2449 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2450 if (next != env_uses_.end()) {
2451 HEnvironment* next_user = next->GetUser();
2452 size_t next_index = next->GetIndex();
2453 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2454 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2455 }
2456 }
David Brazdil1abb4192015-02-17 18:33:36 +00002457
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002458 HInstruction* previous_;
2459 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002460 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002461 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002462
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002463 // An instruction gets an id when it is added to the graph.
2464 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002465 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002466 int id_;
2467
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002468 // When doing liveness analysis, instructions that have uses get an SSA index.
2469 int ssa_index_;
2470
Vladimir Markoa1de9182016-02-25 11:37:38 +00002471 // Packed fields.
2472 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002473
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002474 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002475 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002476
2477 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002478 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002479
Nicolas Geoffray39468442014-09-02 15:17:15 +01002480 // The environment associated with this instruction. Not null if the instruction
2481 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002482 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002483
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002484 // Set by the code generator.
2485 LocationSummary* locations_;
2486
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002487 // Set by the liveness analysis.
2488 LiveInterval* live_interval_;
2489
2490 // Set by the liveness analysis, this is the position in a linear
2491 // order of blocks where this instruction's live interval start.
2492 size_t lifetime_position_;
2493
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002494 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002495
Vladimir Markoa1de9182016-02-25 11:37:38 +00002496 // The reference handle part of the reference type info.
2497 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002498 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002499 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002500
David Brazdil1abb4192015-02-17 18:33:36 +00002501 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002502 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002503 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002504 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002505 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002506};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002507std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002508
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002509// Iterates over the instructions, while preserving the next instruction
2510// in case the current instruction gets removed from the list by the user
2511// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002512class HInstructionIterator : public ValueObject {
2513 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002514 explicit HInstructionIterator(const HInstructionList& instructions)
2515 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002516 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002517 }
2518
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002519 bool Done() const { return instruction_ == nullptr; }
2520 HInstruction* Current() const { return instruction_; }
2521 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002522 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002523 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002524 }
2525
2526 private:
2527 HInstruction* instruction_;
2528 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002529
2530 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002531};
2532
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002533// Iterates over the instructions without saving the next instruction,
2534// therefore handling changes in the graph potentially made by the user
2535// of this iterator.
2536class HInstructionIteratorHandleChanges : public ValueObject {
2537 public:
2538 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2539 : instruction_(instructions.first_instruction_) {
2540 }
2541
2542 bool Done() const { return instruction_ == nullptr; }
2543 HInstruction* Current() const { return instruction_; }
2544 void Advance() {
2545 instruction_ = instruction_->GetNext();
2546 }
2547
2548 private:
2549 HInstruction* instruction_;
2550
2551 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2552};
2553
2554
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002555class HBackwardInstructionIterator : public ValueObject {
2556 public:
2557 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2558 : instruction_(instructions.last_instruction_) {
2559 next_ = Done() ? nullptr : instruction_->GetPrevious();
2560 }
2561
2562 bool Done() const { return instruction_ == nullptr; }
2563 HInstruction* Current() const { return instruction_; }
2564 void Advance() {
2565 instruction_ = next_;
2566 next_ = Done() ? nullptr : instruction_->GetPrevious();
2567 }
2568
2569 private:
2570 HInstruction* instruction_;
2571 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002572
2573 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002574};
2575
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002576class HVariableInputSizeInstruction : public HInstruction {
2577 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002578 using HInstruction::GetInputRecords; // Keep the const version visible.
2579 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
2580 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2581 }
2582
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002583 void AddInput(HInstruction* input);
2584 void InsertInputAt(size_t index, HInstruction* input);
2585 void RemoveInputAt(size_t index);
2586
Igor Murashkind01745e2017-04-05 16:40:31 -07002587 // Removes all the inputs.
2588 // Also removes this instructions from each input's use list
2589 // (for non-environment uses only).
2590 void RemoveAllInputs();
2591
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002592 protected:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302593 HVariableInputSizeInstruction(InstructionKind inst_kind,
2594 SideEffects side_effects,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002595 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002596 ArenaAllocator* allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002597 size_t number_of_inputs,
2598 ArenaAllocKind kind)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302599 : HInstruction(inst_kind, side_effects, dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002600 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Vladimir Markobd785672018-05-03 17:09:09 +01002601 HVariableInputSizeInstruction(InstructionKind inst_kind,
2602 DataType::Type type,
2603 SideEffects side_effects,
2604 uint32_t dex_pc,
2605 ArenaAllocator* allocator,
2606 size_t number_of_inputs,
2607 ArenaAllocKind kind)
2608 : HInstruction(inst_kind, type, side_effects, dex_pc),
2609 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002610
Artem Serovcced8ba2017-07-19 18:18:09 +01002611 DEFAULT_COPY_CONSTRUCTOR(VariableInputSizeInstruction);
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002612
Artem Serovcced8ba2017-07-19 18:18:09 +01002613 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002614};
2615
Vladimir Markof9f64412015-09-02 14:05:49 +01002616template<size_t N>
Vladimir Markobd785672018-05-03 17:09:09 +01002617class HExpression : public HInstruction {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002618 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002619 HExpression<N>(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302620 : HInstruction(kind, side_effects, dex_pc), inputs_() {}
Vladimir Markobd785672018-05-03 17:09:09 +01002621 HExpression<N>(InstructionKind kind,
2622 DataType::Type type,
2623 SideEffects side_effects,
2624 uint32_t dex_pc)
2625 : HInstruction(kind, type, side_effects, dex_pc), inputs_() {}
2626 virtual ~HExpression() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002627
Vladimir Marko372f10e2016-05-17 16:30:10 +01002628 using HInstruction::GetInputRecords; // Keep the const version visible.
2629 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2630 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002631 }
2632
Artem Serovcced8ba2017-07-19 18:18:09 +01002633 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002634 DEFAULT_COPY_CONSTRUCTOR(Expression<N>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002635
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002636 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002637 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002638
2639 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002640};
2641
Vladimir Markobd785672018-05-03 17:09:09 +01002642// HExpression specialization for N=0.
Vladimir Markof9f64412015-09-02 14:05:49 +01002643template<>
Vladimir Markobd785672018-05-03 17:09:09 +01002644class HExpression<0> : public HInstruction {
Vladimir Markof9f64412015-09-02 14:05:49 +01002645 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002646 using HInstruction::HInstruction;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002647
Vladimir Markobd785672018-05-03 17:09:09 +01002648 virtual ~HExpression() {}
Vladimir Markof9f64412015-09-02 14:05:49 +01002649
Vladimir Marko372f10e2016-05-17 16:30:10 +01002650 using HInstruction::GetInputRecords; // Keep the const version visible.
2651 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2652 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002653 }
2654
Artem Serovcced8ba2017-07-19 18:18:09 +01002655 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002656 DEFAULT_COPY_CONSTRUCTOR(Expression<0>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002657
Vladimir Markof9f64412015-09-02 14:05:49 +01002658 private:
2659 friend class SsaBuilder;
2660};
2661
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002662// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2663// instruction that branches to the exit block.
Vladimir Markobd785672018-05-03 17:09:09 +01002664class HReturnVoid FINAL : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002665 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002666 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002667 : HExpression(kReturnVoid, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302668 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002669
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002670 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002671
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002672 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002673
Artem Serovcced8ba2017-07-19 18:18:09 +01002674 protected:
2675 DEFAULT_COPY_CONSTRUCTOR(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002676};
2677
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002678// Represents dex's RETURN opcodes. A HReturn is a control flow
2679// instruction that branches to the exit block.
Vladimir Markobd785672018-05-03 17:09:09 +01002680class HReturn FINAL : public HExpression<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002681 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002682 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002683 : HExpression(kReturn, SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002684 SetRawInputAt(0, value);
2685 }
2686
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002687 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002688
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002689 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002690
Artem Serovcced8ba2017-07-19 18:18:09 +01002691 protected:
2692 DEFAULT_COPY_CONSTRUCTOR(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002693};
2694
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002695class HPhi FINAL : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002696 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002697 HPhi(ArenaAllocator* allocator,
David Brazdildee58d62016-04-07 09:54:26 +00002698 uint32_t reg_number,
2699 size_t number_of_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002700 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +00002701 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002702 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302703 kPhi,
Vladimir Markobd785672018-05-03 17:09:09 +01002704 ToPhiType(type),
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002705 SideEffects::None(),
2706 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002707 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002708 number_of_inputs,
2709 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002710 reg_number_(reg_number) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002711 DCHECK_NE(GetType(), DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +00002712 // Phis are constructed live and marked dead if conflicting or unused.
2713 // Individual steps of SsaBuilder should assume that if a phi has been
2714 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2715 SetPackedFlag<kFlagIsLive>(true);
2716 SetPackedFlag<kFlagCanBeNull>(true);
2717 }
2718
Artem Serovcced8ba2017-07-19 18:18:09 +01002719 bool IsClonable() const OVERRIDE { return true; }
2720
David Brazdildee58d62016-04-07 09:54:26 +00002721 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002722 static DataType::Type ToPhiType(DataType::Type type) {
2723 return DataType::Kind(type);
David Brazdildee58d62016-04-07 09:54:26 +00002724 }
2725
2726 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2727
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002728 void SetType(DataType::Type new_type) {
David Brazdildee58d62016-04-07 09:54:26 +00002729 // Make sure that only valid type changes occur. The following are allowed:
2730 // (1) int -> float/ref (primitive type propagation),
2731 // (2) long -> double (primitive type propagation).
2732 DCHECK(GetType() == new_type ||
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002733 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kFloat32) ||
2734 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kReference) ||
2735 (GetType() == DataType::Type::kInt64 && new_type == DataType::Type::kFloat64));
David Brazdildee58d62016-04-07 09:54:26 +00002736 SetPackedField<TypeField>(new_type);
2737 }
2738
2739 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2740 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2741
2742 uint32_t GetRegNumber() const { return reg_number_; }
2743
2744 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2745 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2746 bool IsDead() const { return !IsLive(); }
2747 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2748
Vladimir Markoe9004912016-06-16 16:50:52 +01002749 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002750 return other != nullptr
2751 && other->IsPhi()
2752 && other->AsPhi()->GetBlock() == GetBlock()
2753 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2754 }
2755
Nicolas Geoffrayf57c1ae2017-06-28 17:40:18 +01002756 bool HasEquivalentPhi() const {
2757 if (GetPrevious() != nullptr && GetPrevious()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2758 return true;
2759 }
2760 if (GetNext() != nullptr && GetNext()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2761 return true;
2762 }
2763 return false;
2764 }
2765
David Brazdildee58d62016-04-07 09:54:26 +00002766 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2767 // An equivalent phi is a phi having the same dex register and type.
2768 // It assumes that phis with the same dex register are adjacent.
2769 HPhi* GetNextEquivalentPhiWithSameType() {
2770 HInstruction* next = GetNext();
2771 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2772 if (next->GetType() == GetType()) {
2773 return next->AsPhi();
2774 }
2775 next = next->GetNext();
2776 }
2777 return nullptr;
2778 }
2779
2780 DECLARE_INSTRUCTION(Phi);
2781
Artem Serovcced8ba2017-07-19 18:18:09 +01002782 protected:
2783 DEFAULT_COPY_CONSTRUCTOR(Phi);
2784
David Brazdildee58d62016-04-07 09:54:26 +00002785 private:
Vladimir Markobd785672018-05-03 17:09:09 +01002786 static constexpr size_t kFlagIsLive = HInstruction::kNumberOfGenericPackedBits;
David Brazdildee58d62016-04-07 09:54:26 +00002787 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2788 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2789 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
David Brazdildee58d62016-04-07 09:54:26 +00002790
David Brazdildee58d62016-04-07 09:54:26 +00002791 const uint32_t reg_number_;
David Brazdildee58d62016-04-07 09:54:26 +00002792};
2793
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002794// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002795// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002796// exit block.
Vladimir Markobd785672018-05-03 17:09:09 +01002797class HExit FINAL : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002798 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302799 explicit HExit(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002800 : HExpression(kExit, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302801 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002802
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002803 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002804
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002805 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002806
Artem Serovcced8ba2017-07-19 18:18:09 +01002807 protected:
2808 DEFAULT_COPY_CONSTRUCTOR(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002809};
2810
2811// Jumps from one block to another.
Vladimir Markobd785672018-05-03 17:09:09 +01002812class HGoto FINAL : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002813 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302814 explicit HGoto(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002815 : HExpression(kGoto, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302816 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002817
Artem Serovcced8ba2017-07-19 18:18:09 +01002818 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002819 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002820
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002821 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002822 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002823 }
2824
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002825 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002826
Artem Serovcced8ba2017-07-19 18:18:09 +01002827 protected:
2828 DEFAULT_COPY_CONSTRUCTOR(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002829};
2830
Roland Levillain9867bc72015-08-05 10:21:34 +01002831class HConstant : public HExpression<0> {
2832 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302833 explicit HConstant(InstructionKind kind, DataType::Type type, uint32_t dex_pc = kNoDexPc)
2834 : HExpression(kind, type, SideEffects::None(), dex_pc) {
2835 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002836
2837 bool CanBeMoved() const OVERRIDE { return true; }
2838
Roland Levillain1a653882016-03-18 18:05:57 +00002839 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002840 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002841 // Is this constant 0 in the arithmetic sense?
2842 virtual bool IsArithmeticZero() const { return false; }
2843 // Is this constant a 0-bit pattern?
2844 virtual bool IsZeroBitPattern() const { return false; }
2845 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002846 virtual bool IsOne() const { return false; }
2847
David Brazdil77a48ae2015-09-15 12:34:04 +00002848 virtual uint64_t GetValueAsUint64() const = 0;
2849
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002850 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002851
Artem Serovcced8ba2017-07-19 18:18:09 +01002852 protected:
2853 DEFAULT_COPY_CONSTRUCTOR(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002854};
2855
Vladimir Markofcb503c2016-05-18 12:48:17 +01002856class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002857 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002858 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002859 return true;
2860 }
2861
David Brazdil77a48ae2015-09-15 12:34:04 +00002862 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2863
Roland Levillain9867bc72015-08-05 10:21:34 +01002864 size_t ComputeHashCode() const OVERRIDE { return 0; }
2865
Roland Levillain1a653882016-03-18 18:05:57 +00002866 // The null constant representation is a 0-bit pattern.
2867 virtual bool IsZeroBitPattern() const { return true; }
2868
Roland Levillain9867bc72015-08-05 10:21:34 +01002869 DECLARE_INSTRUCTION(NullConstant);
2870
Artem Serovcced8ba2017-07-19 18:18:09 +01002871 protected:
2872 DEFAULT_COPY_CONSTRUCTOR(NullConstant);
2873
Roland Levillain9867bc72015-08-05 10:21:34 +01002874 private:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002875 explicit HNullConstant(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302876 : HConstant(kNullConstant, DataType::Type::kReference, dex_pc) {
2877 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002878
2879 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01002880};
2881
2882// Constants of the type int. Those can be from Dex instructions, or
2883// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002884class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002885 public:
2886 int32_t GetValue() const { return value_; }
2887
David Brazdil9f389d42015-10-01 14:32:56 +01002888 uint64_t GetValueAsUint64() const OVERRIDE {
2889 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2890 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002891
Vladimir Marko372f10e2016-05-17 16:30:10 +01002892 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002893 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002894 return other->AsIntConstant()->value_ == value_;
2895 }
2896
2897 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2898
2899 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002900 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2901 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002902 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2903
Roland Levillain1a653882016-03-18 18:05:57 +00002904 // Integer constants are used to encode Boolean values as well,
2905 // where 1 means true and 0 means false.
2906 bool IsTrue() const { return GetValue() == 1; }
2907 bool IsFalse() const { return GetValue() == 0; }
2908
Roland Levillain9867bc72015-08-05 10:21:34 +01002909 DECLARE_INSTRUCTION(IntConstant);
2910
Artem Serovcced8ba2017-07-19 18:18:09 +01002911 protected:
2912 DEFAULT_COPY_CONSTRUCTOR(IntConstant);
2913
Roland Levillain9867bc72015-08-05 10:21:34 +01002914 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002915 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302916 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc), value_(value) {
2917 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002918 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302919 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc),
2920 value_(value ? 1 : 0) {
2921 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002922
2923 const int32_t value_;
2924
2925 friend class HGraph;
2926 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2927 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Roland Levillain9867bc72015-08-05 10:21:34 +01002928};
2929
Vladimir Markofcb503c2016-05-18 12:48:17 +01002930class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002931 public:
2932 int64_t GetValue() const { return value_; }
2933
David Brazdil77a48ae2015-09-15 12:34:04 +00002934 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2935
Vladimir Marko372f10e2016-05-17 16:30:10 +01002936 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002937 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002938 return other->AsLongConstant()->value_ == value_;
2939 }
2940
2941 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2942
2943 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002944 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2945 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002946 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2947
2948 DECLARE_INSTRUCTION(LongConstant);
2949
Artem Serovcced8ba2017-07-19 18:18:09 +01002950 protected:
2951 DEFAULT_COPY_CONSTRUCTOR(LongConstant);
2952
Roland Levillain9867bc72015-08-05 10:21:34 +01002953 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002954 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302955 : HConstant(kLongConstant, DataType::Type::kInt64, dex_pc),
2956 value_(value) {
2957 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002958
2959 const int64_t value_;
2960
2961 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01002962};
Dave Allison20dfc792014-06-16 20:44:29 -07002963
Vladimir Markofcb503c2016-05-18 12:48:17 +01002964class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002965 public:
2966 float GetValue() const { return value_; }
2967
2968 uint64_t GetValueAsUint64() const OVERRIDE {
2969 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2970 }
2971
Vladimir Marko372f10e2016-05-17 16:30:10 +01002972 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002973 DCHECK(other->IsFloatConstant()) << other->DebugName();
2974 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2975 }
2976
2977 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2978
2979 bool IsMinusOne() const OVERRIDE {
2980 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2981 }
Roland Levillain1a653882016-03-18 18:05:57 +00002982 bool IsArithmeticZero() const OVERRIDE {
2983 return std::fpclassify(value_) == FP_ZERO;
2984 }
2985 bool IsArithmeticPositiveZero() const {
2986 return IsArithmeticZero() && !std::signbit(value_);
2987 }
2988 bool IsArithmeticNegativeZero() const {
2989 return IsArithmeticZero() && std::signbit(value_);
2990 }
2991 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002992 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002993 }
2994 bool IsOne() const OVERRIDE {
2995 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2996 }
2997 bool IsNaN() const {
2998 return std::isnan(value_);
2999 }
3000
3001 DECLARE_INSTRUCTION(FloatConstant);
3002
Artem Serovcced8ba2017-07-19 18:18:09 +01003003 protected:
3004 DEFAULT_COPY_CONSTRUCTOR(FloatConstant);
3005
Roland Levillain31dd3d62016-02-16 12:21:02 +00003006 private:
3007 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303008 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3009 value_(value) {
3010 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003011 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303012 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3013 value_(bit_cast<float, int32_t>(value)) {
3014 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003015
3016 const float value_;
3017
3018 // Only the SsaBuilder and HGraph can create floating-point constants.
3019 friend class SsaBuilder;
3020 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003021};
3022
Vladimir Markofcb503c2016-05-18 12:48:17 +01003023class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003024 public:
3025 double GetValue() const { return value_; }
3026
3027 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
3028
Vladimir Marko372f10e2016-05-17 16:30:10 +01003029 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003030 DCHECK(other->IsDoubleConstant()) << other->DebugName();
3031 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
3032 }
3033
3034 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
3035
3036 bool IsMinusOne() const OVERRIDE {
3037 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
3038 }
Roland Levillain1a653882016-03-18 18:05:57 +00003039 bool IsArithmeticZero() const OVERRIDE {
3040 return std::fpclassify(value_) == FP_ZERO;
3041 }
3042 bool IsArithmeticPositiveZero() const {
3043 return IsArithmeticZero() && !std::signbit(value_);
3044 }
3045 bool IsArithmeticNegativeZero() const {
3046 return IsArithmeticZero() && std::signbit(value_);
3047 }
3048 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003049 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00003050 }
3051 bool IsOne() const OVERRIDE {
3052 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3053 }
3054 bool IsNaN() const {
3055 return std::isnan(value_);
3056 }
3057
3058 DECLARE_INSTRUCTION(DoubleConstant);
3059
Artem Serovcced8ba2017-07-19 18:18:09 +01003060 protected:
3061 DEFAULT_COPY_CONSTRUCTOR(DoubleConstant);
3062
Roland Levillain31dd3d62016-02-16 12:21:02 +00003063 private:
3064 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303065 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3066 value_(value) {
3067 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003068 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303069 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3070 value_(bit_cast<double, int64_t>(value)) {
3071 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003072
3073 const double value_;
3074
3075 // Only the SsaBuilder and HGraph can create floating-point constants.
3076 friend class SsaBuilder;
3077 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003078};
3079
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003080// Conditional branch. A block ending with an HIf instruction must have
3081// two successors.
Vladimir Markobd785672018-05-03 17:09:09 +01003082class HIf FINAL : public HExpression<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003083 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003084 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003085 : HExpression(kIf, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003086 SetRawInputAt(0, input);
3087 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003088
Artem Serovcced8ba2017-07-19 18:18:09 +01003089 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003090 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003091
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003092 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003093 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003094 }
3095
3096 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003097 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003098 }
3099
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003100 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003101
Artem Serovcced8ba2017-07-19 18:18:09 +01003102 protected:
3103 DEFAULT_COPY_CONSTRUCTOR(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003104};
3105
David Brazdilfc6a86a2015-06-26 10:33:45 +00003106
3107// Abstract instruction which marks the beginning and/or end of a try block and
3108// links it to the respective exception handlers. Behaves the same as a Goto in
3109// non-exceptional control flow.
3110// Normal-flow successor is stored at index zero, exception handlers under
3111// higher indices in no particular order.
Vladimir Markobd785672018-05-03 17:09:09 +01003112class HTryBoundary FINAL : public HExpression<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003113 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003114 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01003115 kEntry,
3116 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003117 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01003118 };
3119
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003120 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003121 : HExpression(kTryBoundary, SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003122 SetPackedField<BoundaryKindField>(kind);
3123 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003124
3125 bool IsControlFlow() const OVERRIDE { return true; }
3126
3127 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01003128 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003129
David Brazdild26a4112015-11-10 11:07:31 +00003130 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
3131 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
3132 }
3133
David Brazdilfc6a86a2015-06-26 10:33:45 +00003134 // Returns whether `handler` is among its exception handlers (non-zero index
3135 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01003136 bool HasExceptionHandler(const HBasicBlock& handler) const {
3137 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00003138 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00003139 }
3140
3141 // If not present already, adds `handler` to its block's list of exception
3142 // handlers.
3143 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01003144 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003145 GetBlock()->AddSuccessor(handler);
3146 }
3147 }
3148
Vladimir Markoa1de9182016-02-25 11:37:38 +00003149 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
3150 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003151
David Brazdilffee3d32015-07-06 11:48:53 +01003152 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
3153
David Brazdilfc6a86a2015-06-26 10:33:45 +00003154 DECLARE_INSTRUCTION(TryBoundary);
3155
Artem Serovcced8ba2017-07-19 18:18:09 +01003156 protected:
3157 DEFAULT_COPY_CONSTRUCTOR(TryBoundary);
3158
David Brazdilfc6a86a2015-06-26 10:33:45 +00003159 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003160 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
3161 static constexpr size_t kFieldBoundaryKindSize =
3162 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
3163 static constexpr size_t kNumberOfTryBoundaryPackedBits =
3164 kFieldBoundaryKind + kFieldBoundaryKindSize;
3165 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
3166 "Too many packed fields.");
3167 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00003168};
3169
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003170// Deoptimize to interpreter, upon checking a condition.
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003171class HDeoptimize FINAL : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003172 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003173 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
3174 // across.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003175 HDeoptimize(ArenaAllocator* allocator,
3176 HInstruction* cond,
3177 DeoptimizationKind kind,
3178 uint32_t dex_pc)
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003179 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303180 kDeoptimize,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003181 SideEffects::All(),
3182 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003183 allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003184 /* number_of_inputs */ 1,
3185 kArenaAllocMisc) {
3186 SetPackedFlag<kFieldCanBeMoved>(false);
3187 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003188 SetRawInputAt(0, cond);
3189 }
3190
Artem Serovcced8ba2017-07-19 18:18:09 +01003191 bool IsClonable() const OVERRIDE { return true; }
3192
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003193 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
3194 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
3195 // instead of `guard`.
3196 // We set CanTriggerGC to prevent any intermediate address to be live
3197 // at the point of the `HDeoptimize`.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003198 HDeoptimize(ArenaAllocator* allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003199 HInstruction* cond,
3200 HInstruction* guard,
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003201 DeoptimizationKind kind,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003202 uint32_t dex_pc)
3203 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303204 kDeoptimize,
Vladimir Markobd785672018-05-03 17:09:09 +01003205 guard->GetType(),
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003206 SideEffects::CanTriggerGC(),
3207 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003208 allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003209 /* number_of_inputs */ 2,
3210 kArenaAllocMisc) {
3211 SetPackedFlag<kFieldCanBeMoved>(true);
3212 SetPackedField<DeoptimizeKindField>(kind);
3213 SetRawInputAt(0, cond);
3214 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003215 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003216
3217 bool CanBeMoved() const OVERRIDE { return GetPackedFlag<kFieldCanBeMoved>(); }
3218
3219 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
3220 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
3221 }
3222
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003223 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003224
Aart Bik75ff2c92018-04-21 01:28:11 +00003225 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003226
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003227 DeoptimizationKind GetDeoptimizationKind() const { return GetPackedField<DeoptimizeKindField>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003228
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003229 bool GuardsAnInput() const {
3230 return InputCount() == 2;
3231 }
3232
3233 HInstruction* GuardedInput() const {
3234 DCHECK(GuardsAnInput());
3235 return InputAt(1);
3236 }
3237
3238 void RemoveGuard() {
3239 RemoveInputAt(1);
3240 }
3241
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003242 DECLARE_INSTRUCTION(Deoptimize);
3243
Artem Serovcced8ba2017-07-19 18:18:09 +01003244 protected:
3245 DEFAULT_COPY_CONSTRUCTOR(Deoptimize);
3246
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003247 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003248 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3249 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3250 static constexpr size_t kFieldDeoptimizeKindSize =
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003251 MinimumBitsToStore(static_cast<size_t>(DeoptimizationKind::kLast));
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003252 static constexpr size_t kNumberOfDeoptimizePackedBits =
3253 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3254 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3255 "Too many packed fields.");
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003256 using DeoptimizeKindField =
3257 BitField<DeoptimizationKind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003258};
3259
Mingyao Yang063fc772016-08-02 11:02:54 -07003260// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3261// The compiled code checks this flag value in a guard before devirtualized call and
3262// if it's true, starts to do deoptimization.
3263// It has a 4-byte slot on stack.
3264// TODO: allocate a register for this flag.
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003265class HShouldDeoptimizeFlag FINAL : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003266 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003267 // CHA guards are only optimized in a separate pass and it has no side effects
3268 // with regard to other passes.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003269 HShouldDeoptimizeFlag(ArenaAllocator* allocator, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303270 : HVariableInputSizeInstruction(kShouldDeoptimizeFlag,
Vladimir Markobd785672018-05-03 17:09:09 +01003271 DataType::Type::kInt32,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303272 SideEffects::None(),
3273 dex_pc,
3274 allocator,
3275 0,
3276 kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003277 }
3278
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003279 // We do all CHA guard elimination/motion in a single pass, after which there is no
3280 // further guard elimination/motion since a guard might have been used for justification
3281 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3282 // to avoid other optimizations trying to move it.
Mingyao Yang063fc772016-08-02 11:02:54 -07003283 bool CanBeMoved() const OVERRIDE { return false; }
3284
3285 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3286
Artem Serovcced8ba2017-07-19 18:18:09 +01003287 protected:
3288 DEFAULT_COPY_CONSTRUCTOR(ShouldDeoptimizeFlag);
Mingyao Yang063fc772016-08-02 11:02:54 -07003289};
3290
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003291// Represents the ArtMethod that was passed as a first argument to
3292// the method. It is used by instructions that depend on it, like
3293// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003294class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003295 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003296 explicit HCurrentMethod(DataType::Type type, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303297 : HExpression(kCurrentMethod, type, SideEffects::None(), dex_pc) {
3298 }
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003299
3300 DECLARE_INSTRUCTION(CurrentMethod);
3301
Artem Serovcced8ba2017-07-19 18:18:09 +01003302 protected:
3303 DEFAULT_COPY_CONSTRUCTOR(CurrentMethod);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003304};
3305
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003306// Fetches an ArtMethod from the virtual table or the interface method table
3307// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003308class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003309 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003310 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003311 kVTable,
3312 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003313 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003314 };
3315 HClassTableGet(HInstruction* cls,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003316 DataType::Type type,
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003317 TableKind kind,
3318 size_t index,
3319 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303320 : HExpression(kClassTableGet, type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003321 index_(index) {
3322 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003323 SetRawInputAt(0, cls);
3324 }
3325
Artem Serovcced8ba2017-07-19 18:18:09 +01003326 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003327 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003328 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003329 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003330 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003331 }
3332
Vladimir Markoa1de9182016-02-25 11:37:38 +00003333 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003334 size_t GetIndex() const { return index_; }
3335
3336 DECLARE_INSTRUCTION(ClassTableGet);
3337
Artem Serovcced8ba2017-07-19 18:18:09 +01003338 protected:
3339 DEFAULT_COPY_CONSTRUCTOR(ClassTableGet);
3340
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003341 private:
Vladimir Markobd785672018-05-03 17:09:09 +01003342 static constexpr size_t kFieldTableKind = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003343 static constexpr size_t kFieldTableKindSize =
3344 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3345 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3346 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3347 "Too many packed fields.");
3348 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3349
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003350 // The index of the ArtMethod in the table.
3351 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003352};
3353
Mark Mendellfe57faa2015-09-18 09:26:15 -04003354// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3355// have one successor for each entry in the switch table, and the final successor
3356// will be the block containing the next Dex opcode.
Vladimir Markobd785672018-05-03 17:09:09 +01003357class HPackedSwitch FINAL : public HExpression<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003358 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003359 HPackedSwitch(int32_t start_value,
3360 uint32_t num_entries,
3361 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003362 uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003363 : HExpression(kPackedSwitch, SideEffects::None(), dex_pc),
Mark Mendellfe57faa2015-09-18 09:26:15 -04003364 start_value_(start_value),
3365 num_entries_(num_entries) {
3366 SetRawInputAt(0, input);
3367 }
3368
Artem Serovcced8ba2017-07-19 18:18:09 +01003369 bool IsClonable() const OVERRIDE { return true; }
3370
Mark Mendellfe57faa2015-09-18 09:26:15 -04003371 bool IsControlFlow() const OVERRIDE { return true; }
3372
3373 int32_t GetStartValue() const { return start_value_; }
3374
Vladimir Marko211c2112015-09-24 16:52:33 +01003375 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003376
3377 HBasicBlock* GetDefaultBlock() const {
3378 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003379 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003380 }
3381 DECLARE_INSTRUCTION(PackedSwitch);
3382
Artem Serovcced8ba2017-07-19 18:18:09 +01003383 protected:
3384 DEFAULT_COPY_CONSTRUCTOR(PackedSwitch);
3385
Mark Mendellfe57faa2015-09-18 09:26:15 -04003386 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003387 const int32_t start_value_;
3388 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003389};
3390
Roland Levillain88cb1752014-10-20 16:36:47 +01003391class HUnaryOperation : public HExpression<1> {
3392 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303393 HUnaryOperation(InstructionKind kind,
3394 DataType::Type result_type,
3395 HInstruction* input,
3396 uint32_t dex_pc = kNoDexPc)
3397 : HExpression(kind, result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003398 SetRawInputAt(0, input);
3399 }
3400
Artem Serovcced8ba2017-07-19 18:18:09 +01003401 // All of the UnaryOperation instructions are clonable.
3402 bool IsClonable() const OVERRIDE { return true; }
3403
Roland Levillain88cb1752014-10-20 16:36:47 +01003404 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003405 DataType::Type GetResultType() const { return GetType(); }
Roland Levillain88cb1752014-10-20 16:36:47 +01003406
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003407 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003408 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003409 return true;
3410 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003411
Roland Levillain31dd3d62016-02-16 12:21:02 +00003412 // Try to statically evaluate `this` and return a HConstant
3413 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003414 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003415 HConstant* TryStaticEvaluation() const;
3416
3417 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003418 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3419 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003420 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3421 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003422
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003423 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003424
Artem Serovcced8ba2017-07-19 18:18:09 +01003425 protected:
3426 DEFAULT_COPY_CONSTRUCTOR(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003427};
3428
Dave Allison20dfc792014-06-16 20:44:29 -07003429class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003430 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303431 HBinaryOperation(InstructionKind kind,
3432 DataType::Type result_type,
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003433 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003434 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003435 SideEffects side_effects = SideEffects::None(),
3436 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303437 : HExpression(kind, result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003438 SetRawInputAt(0, left);
3439 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003440 }
3441
Artem Serovcced8ba2017-07-19 18:18:09 +01003442 // All of the BinaryOperation instructions are clonable.
3443 bool IsClonable() const OVERRIDE { return true; }
3444
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003445 HInstruction* GetLeft() const { return InputAt(0); }
3446 HInstruction* GetRight() const { return InputAt(1); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003447 DataType::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003448
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003449 virtual bool IsCommutative() const { return false; }
3450
3451 // Put constant on the right.
3452 // Returns whether order is changed.
3453 bool OrderInputsWithConstantOnTheRight() {
3454 HInstruction* left = InputAt(0);
3455 HInstruction* right = InputAt(1);
3456 if (left->IsConstant() && !right->IsConstant()) {
3457 ReplaceInput(right, 0);
3458 ReplaceInput(left, 1);
3459 return true;
3460 }
3461 return false;
3462 }
3463
3464 // Order inputs by instruction id, but favor constant on the right side.
3465 // This helps GVN for commutative ops.
3466 void OrderInputs() {
3467 DCHECK(IsCommutative());
3468 HInstruction* left = InputAt(0);
3469 HInstruction* right = InputAt(1);
3470 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3471 return;
3472 }
3473 if (OrderInputsWithConstantOnTheRight()) {
3474 return;
3475 }
3476 // Order according to instruction id.
3477 if (left->GetId() > right->GetId()) {
3478 ReplaceInput(right, 0);
3479 ReplaceInput(left, 1);
3480 }
3481 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003482
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003483 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003484 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003485 return true;
3486 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003487
Roland Levillain31dd3d62016-02-16 12:21:02 +00003488 // Try to statically evaluate `this` and return a HConstant
3489 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003490 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003491 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003492
3493 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003494 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3495 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003496 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3497 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003498 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003499 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3500 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003501 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3502 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003503 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3504 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003505 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003506 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3507 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003508
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003509 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003510 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003511 HConstant* GetConstantRight() const;
3512
3513 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003514 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003515 HInstruction* GetLeastConstantLeft() const;
3516
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003517 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003518
Artem Serovcced8ba2017-07-19 18:18:09 +01003519 protected:
3520 DEFAULT_COPY_CONSTRUCTOR(BinaryOperation);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003521};
3522
Mark Mendellc4701932015-04-10 13:18:51 -04003523// The comparison bias applies for floating point operations and indicates how NaN
3524// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003525enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003526 kNoBias, // bias is not applicable (i.e. for long operation)
3527 kGtBias, // return 1 for NaN comparisons
3528 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003529 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003530};
3531
Roland Levillain31dd3d62016-02-16 12:21:02 +00003532std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3533
Dave Allison20dfc792014-06-16 20:44:29 -07003534class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003535 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303536 HCondition(InstructionKind kind,
3537 HInstruction* first,
3538 HInstruction* second,
3539 uint32_t dex_pc = kNoDexPc)
3540 : HBinaryOperation(kind,
3541 DataType::Type::kBool,
3542 first,
3543 second,
3544 SideEffects::None(),
3545 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003546 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3547 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003548
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003549 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003550 // `instruction`, and disregard moves in between.
3551 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003552
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003553 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003554
3555 virtual IfCondition GetCondition() const = 0;
3556
Mark Mendellc4701932015-04-10 13:18:51 -04003557 virtual IfCondition GetOppositeCondition() const = 0;
3558
Vladimir Markoa1de9182016-02-25 11:37:38 +00003559 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003560 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3561
Vladimir Markoa1de9182016-02-25 11:37:38 +00003562 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3563 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003564
Vladimir Marko372f10e2016-05-17 16:30:10 +01003565 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003566 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003567 }
3568
Roland Levillain4fa13f62015-07-06 18:11:54 +01003569 bool IsFPConditionTrueIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003570 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003571 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003572 if (if_cond == kCondNE) {
3573 return true;
3574 } else if (if_cond == kCondEQ) {
3575 return false;
3576 }
3577 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003578 }
3579
3580 bool IsFPConditionFalseIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003581 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003582 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003583 if (if_cond == kCondEQ) {
3584 return true;
3585 } else if (if_cond == kCondNE) {
3586 return false;
3587 }
3588 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003589 }
3590
Roland Levillain31dd3d62016-02-16 12:21:02 +00003591 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003592 // Needed if we merge a HCompare into a HCondition.
Vladimir Markobd785672018-05-03 17:09:09 +01003593 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003594 static constexpr size_t kFieldComparisonBiasSize =
3595 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3596 static constexpr size_t kNumberOfConditionPackedBits =
3597 kFieldComparisonBias + kFieldComparisonBiasSize;
3598 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3599 using ComparisonBiasField =
3600 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3601
Roland Levillain31dd3d62016-02-16 12:21:02 +00003602 template <typename T>
3603 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3604
3605 template <typename T>
3606 int32_t CompareFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003607 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003608 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3609 // Handle the bias.
3610 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3611 }
3612
3613 // Return an integer constant containing the result of a condition evaluated at compile time.
3614 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3615 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3616 }
3617
Artem Serovcced8ba2017-07-19 18:18:09 +01003618 DEFAULT_COPY_CONSTRUCTOR(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003619};
3620
3621// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003622class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003623 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003624 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303625 : HCondition(kEqual, first, second, dex_pc) {
3626 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003627
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003628 bool IsCommutative() const OVERRIDE { return true; }
3629
Vladimir Marko9e23df52015-11-10 17:14:35 +00003630 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3631 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003632 return MakeConstantCondition(true, GetDexPc());
3633 }
3634 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3635 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3636 }
3637 // In the following Evaluate methods, a HCompare instruction has
3638 // been merged into this HEqual instruction; evaluate it as
3639 // `Compare(x, y) == 0`.
3640 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3641 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3642 GetDexPc());
3643 }
3644 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3645 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3646 }
3647 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3648 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003649 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003650
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003651 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003652
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003653 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003654 return kCondEQ;
3655 }
3656
Mark Mendellc4701932015-04-10 13:18:51 -04003657 IfCondition GetOppositeCondition() const OVERRIDE {
3658 return kCondNE;
3659 }
3660
Artem Serovcced8ba2017-07-19 18:18:09 +01003661 protected:
3662 DEFAULT_COPY_CONSTRUCTOR(Equal);
3663
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003664 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003665 template <typename T> static bool Compute(T x, T y) { return x == y; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003666};
3667
Vladimir Markofcb503c2016-05-18 12:48:17 +01003668class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003669 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303670 HNotEqual(HInstruction* first, HInstruction* second,
3671 uint32_t dex_pc = kNoDexPc)
3672 : HCondition(kNotEqual, first, second, dex_pc) {
3673 }
Dave Allison20dfc792014-06-16 20:44:29 -07003674
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003675 bool IsCommutative() const OVERRIDE { return true; }
3676
Vladimir Marko9e23df52015-11-10 17:14:35 +00003677 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3678 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003679 return MakeConstantCondition(false, GetDexPc());
3680 }
3681 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3682 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3683 }
3684 // In the following Evaluate methods, a HCompare instruction has
3685 // been merged into this HNotEqual instruction; evaluate it as
3686 // `Compare(x, y) != 0`.
3687 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3688 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3689 }
3690 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3691 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3692 }
3693 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3694 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003695 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003696
Dave Allison20dfc792014-06-16 20:44:29 -07003697 DECLARE_INSTRUCTION(NotEqual);
3698
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003699 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003700 return kCondNE;
3701 }
3702
Mark Mendellc4701932015-04-10 13:18:51 -04003703 IfCondition GetOppositeCondition() const OVERRIDE {
3704 return kCondEQ;
3705 }
3706
Artem Serovcced8ba2017-07-19 18:18:09 +01003707 protected:
3708 DEFAULT_COPY_CONSTRUCTOR(NotEqual);
3709
Dave Allison20dfc792014-06-16 20:44:29 -07003710 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003711 template <typename T> static bool Compute(T x, T y) { return x != y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003712};
3713
Vladimir Markofcb503c2016-05-18 12:48:17 +01003714class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003715 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303716 HLessThan(HInstruction* first, HInstruction* second,
3717 uint32_t dex_pc = kNoDexPc)
3718 : HCondition(kLessThan, first, second, dex_pc) {
3719 }
Dave Allison20dfc792014-06-16 20:44:29 -07003720
Roland Levillain9867bc72015-08-05 10:21:34 +01003721 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003722 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003723 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003724 // In the following Evaluate methods, a HCompare instruction has
3725 // been merged into this HLessThan instruction; evaluate it as
3726 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003727 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003728 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3729 }
3730 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3731 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3732 }
3733 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3734 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003735 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003736
Dave Allison20dfc792014-06-16 20:44:29 -07003737 DECLARE_INSTRUCTION(LessThan);
3738
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003739 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003740 return kCondLT;
3741 }
3742
Mark Mendellc4701932015-04-10 13:18:51 -04003743 IfCondition GetOppositeCondition() const OVERRIDE {
3744 return kCondGE;
3745 }
3746
Artem Serovcced8ba2017-07-19 18:18:09 +01003747 protected:
3748 DEFAULT_COPY_CONSTRUCTOR(LessThan);
3749
Dave Allison20dfc792014-06-16 20:44:29 -07003750 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003751 template <typename T> static bool Compute(T x, T y) { return x < y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003752};
3753
Vladimir Markofcb503c2016-05-18 12:48:17 +01003754class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003755 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303756 HLessThanOrEqual(HInstruction* first, HInstruction* second,
3757 uint32_t dex_pc = kNoDexPc)
3758 : HCondition(kLessThanOrEqual, first, second, dex_pc) {
3759 }
Dave Allison20dfc792014-06-16 20:44:29 -07003760
Roland Levillain9867bc72015-08-05 10:21:34 +01003761 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003762 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003763 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003764 // In the following Evaluate methods, a HCompare instruction has
3765 // been merged into this HLessThanOrEqual instruction; evaluate it as
3766 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003767 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003768 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3769 }
3770 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3771 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3772 }
3773 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3774 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003775 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003776
Dave Allison20dfc792014-06-16 20:44:29 -07003777 DECLARE_INSTRUCTION(LessThanOrEqual);
3778
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003779 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003780 return kCondLE;
3781 }
3782
Mark Mendellc4701932015-04-10 13:18:51 -04003783 IfCondition GetOppositeCondition() const OVERRIDE {
3784 return kCondGT;
3785 }
3786
Artem Serovcced8ba2017-07-19 18:18:09 +01003787 protected:
3788 DEFAULT_COPY_CONSTRUCTOR(LessThanOrEqual);
3789
Dave Allison20dfc792014-06-16 20:44:29 -07003790 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003791 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003792};
3793
Vladimir Markofcb503c2016-05-18 12:48:17 +01003794class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003795 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003796 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303797 : HCondition(kGreaterThan, first, second, dex_pc) {
3798 }
Dave Allison20dfc792014-06-16 20:44:29 -07003799
Roland Levillain9867bc72015-08-05 10:21:34 +01003800 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003801 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003802 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003803 // In the following Evaluate methods, a HCompare instruction has
3804 // been merged into this HGreaterThan instruction; evaluate it as
3805 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003806 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003807 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3808 }
3809 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3810 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3811 }
3812 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3813 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003814 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003815
Dave Allison20dfc792014-06-16 20:44:29 -07003816 DECLARE_INSTRUCTION(GreaterThan);
3817
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003818 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003819 return kCondGT;
3820 }
3821
Mark Mendellc4701932015-04-10 13:18:51 -04003822 IfCondition GetOppositeCondition() const OVERRIDE {
3823 return kCondLE;
3824 }
3825
Artem Serovcced8ba2017-07-19 18:18:09 +01003826 protected:
3827 DEFAULT_COPY_CONSTRUCTOR(GreaterThan);
3828
Dave Allison20dfc792014-06-16 20:44:29 -07003829 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003830 template <typename T> static bool Compute(T x, T y) { return x > y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003831};
3832
Vladimir Markofcb503c2016-05-18 12:48:17 +01003833class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003834 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003835 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303836 : HCondition(kGreaterThanOrEqual, first, second, dex_pc) {
3837 }
Dave Allison20dfc792014-06-16 20:44:29 -07003838
Roland Levillain9867bc72015-08-05 10:21:34 +01003839 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003840 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003841 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003842 // In the following Evaluate methods, a HCompare instruction has
3843 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3844 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003845 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003846 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3847 }
3848 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3849 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3850 }
3851 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3852 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003853 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003854
Dave Allison20dfc792014-06-16 20:44:29 -07003855 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3856
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003857 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003858 return kCondGE;
3859 }
3860
Mark Mendellc4701932015-04-10 13:18:51 -04003861 IfCondition GetOppositeCondition() const OVERRIDE {
3862 return kCondLT;
3863 }
3864
Artem Serovcced8ba2017-07-19 18:18:09 +01003865 protected:
3866 DEFAULT_COPY_CONSTRUCTOR(GreaterThanOrEqual);
3867
Dave Allison20dfc792014-06-16 20:44:29 -07003868 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003869 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003870};
3871
Vladimir Markofcb503c2016-05-18 12:48:17 +01003872class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003873 public:
3874 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303875 : HCondition(kBelow, first, second, dex_pc) {
3876 }
Aart Bike9f37602015-10-09 11:15:55 -07003877
3878 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003879 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003880 }
3881 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003882 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3883 }
3884 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3885 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3886 LOG(FATAL) << DebugName() << " is not defined for float values";
3887 UNREACHABLE();
3888 }
3889 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3890 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3891 LOG(FATAL) << DebugName() << " is not defined for double values";
3892 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003893 }
3894
3895 DECLARE_INSTRUCTION(Below);
3896
3897 IfCondition GetCondition() const OVERRIDE {
3898 return kCondB;
3899 }
3900
3901 IfCondition GetOppositeCondition() const OVERRIDE {
3902 return kCondAE;
3903 }
3904
Artem Serovcced8ba2017-07-19 18:18:09 +01003905 protected:
3906 DEFAULT_COPY_CONSTRUCTOR(Below);
3907
Aart Bike9f37602015-10-09 11:15:55 -07003908 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003909 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003910 return MakeUnsigned(x) < MakeUnsigned(y);
3911 }
Aart Bike9f37602015-10-09 11:15:55 -07003912};
3913
Vladimir Markofcb503c2016-05-18 12:48:17 +01003914class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003915 public:
3916 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303917 : HCondition(kBelowOrEqual, first, second, dex_pc) {
3918 }
Aart Bike9f37602015-10-09 11:15:55 -07003919
3920 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003921 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003922 }
3923 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003924 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3925 }
3926 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3927 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3928 LOG(FATAL) << DebugName() << " is not defined for float values";
3929 UNREACHABLE();
3930 }
3931 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3932 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3933 LOG(FATAL) << DebugName() << " is not defined for double values";
3934 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003935 }
3936
3937 DECLARE_INSTRUCTION(BelowOrEqual);
3938
3939 IfCondition GetCondition() const OVERRIDE {
3940 return kCondBE;
3941 }
3942
3943 IfCondition GetOppositeCondition() const OVERRIDE {
3944 return kCondA;
3945 }
3946
Artem Serovcced8ba2017-07-19 18:18:09 +01003947 protected:
3948 DEFAULT_COPY_CONSTRUCTOR(BelowOrEqual);
3949
Aart Bike9f37602015-10-09 11:15:55 -07003950 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003951 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003952 return MakeUnsigned(x) <= MakeUnsigned(y);
3953 }
Aart Bike9f37602015-10-09 11:15:55 -07003954};
3955
Vladimir Markofcb503c2016-05-18 12:48:17 +01003956class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003957 public:
3958 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303959 : HCondition(kAbove, first, second, dex_pc) {
3960 }
Aart Bike9f37602015-10-09 11:15:55 -07003961
3962 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003963 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003964 }
3965 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003966 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3967 }
3968 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3969 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3970 LOG(FATAL) << DebugName() << " is not defined for float values";
3971 UNREACHABLE();
3972 }
3973 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3974 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3975 LOG(FATAL) << DebugName() << " is not defined for double values";
3976 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003977 }
3978
3979 DECLARE_INSTRUCTION(Above);
3980
3981 IfCondition GetCondition() const OVERRIDE {
3982 return kCondA;
3983 }
3984
3985 IfCondition GetOppositeCondition() const OVERRIDE {
3986 return kCondBE;
3987 }
3988
Artem Serovcced8ba2017-07-19 18:18:09 +01003989 protected:
3990 DEFAULT_COPY_CONSTRUCTOR(Above);
3991
Aart Bike9f37602015-10-09 11:15:55 -07003992 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003993 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003994 return MakeUnsigned(x) > MakeUnsigned(y);
3995 }
Aart Bike9f37602015-10-09 11:15:55 -07003996};
3997
Vladimir Markofcb503c2016-05-18 12:48:17 +01003998class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003999 public:
4000 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304001 : HCondition(kAboveOrEqual, first, second, dex_pc) {
4002 }
Aart Bike9f37602015-10-09 11:15:55 -07004003
4004 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004005 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004006 }
4007 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004008 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4009 }
4010 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4011 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4012 LOG(FATAL) << DebugName() << " is not defined for float values";
4013 UNREACHABLE();
4014 }
4015 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4016 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4017 LOG(FATAL) << DebugName() << " is not defined for double values";
4018 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004019 }
4020
4021 DECLARE_INSTRUCTION(AboveOrEqual);
4022
4023 IfCondition GetCondition() const OVERRIDE {
4024 return kCondAE;
4025 }
4026
4027 IfCondition GetOppositeCondition() const OVERRIDE {
4028 return kCondB;
4029 }
4030
Artem Serovcced8ba2017-07-19 18:18:09 +01004031 protected:
4032 DEFAULT_COPY_CONSTRUCTOR(AboveOrEqual);
4033
Aart Bike9f37602015-10-09 11:15:55 -07004034 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004035 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004036 return MakeUnsigned(x) >= MakeUnsigned(y);
4037 }
Aart Bike9f37602015-10-09 11:15:55 -07004038};
Dave Allison20dfc792014-06-16 20:44:29 -07004039
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004040// Instruction to check how two inputs compare to each other.
4041// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01004042class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004043 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00004044 // Note that `comparison_type` is the type of comparison performed
4045 // between the comparison's inputs, not the type of the instantiated
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004046 // HCompare instruction (which is always DataType::Type::kInt).
4047 HCompare(DataType::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004048 HInstruction* first,
4049 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04004050 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004051 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304052 : HBinaryOperation(kCompare,
4053 DataType::Type::kInt32,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004054 first,
4055 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00004056 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004057 dex_pc) {
4058 SetPackedField<ComparisonBiasField>(bias);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004059 DCHECK_EQ(comparison_type, DataType::Kind(first->GetType()));
4060 DCHECK_EQ(comparison_type, DataType::Kind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004061 }
4062
Roland Levillain9867bc72015-08-05 10:21:34 +01004063 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004064 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
4065
4066 template <typename T>
4067 int32_t ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004068 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004069 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
4070 // Handle the bias.
4071 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
4072 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004073
Roland Levillain9867bc72015-08-05 10:21:34 +01004074 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004075 // Note that there is no "cmp-int" Dex instruction so we shouldn't
4076 // reach this code path when processing a freshly built HIR
4077 // graph. However HCompare integer instructions can be synthesized
4078 // by the instruction simplifier to implement IntegerCompare and
4079 // IntegerSignum intrinsics, so we have to handle this case.
4080 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004081 }
4082 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004083 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4084 }
4085 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4086 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4087 }
4088 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4089 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01004090 }
4091
Vladimir Marko372f10e2016-05-17 16:30:10 +01004092 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004093 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00004094 }
4095
Vladimir Markoa1de9182016-02-25 11:37:38 +00004096 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04004097
Roland Levillain31dd3d62016-02-16 12:21:02 +00004098 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00004099 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00004100 bool IsGtBias() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004101 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004102 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00004103 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004104
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004105 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type type ATTRIBUTE_UNUSED) {
Roland Levillain1693a1f2016-03-15 14:57:31 +00004106 // Comparisons do not require a runtime call in any back end.
4107 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004108 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004109
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004110 DECLARE_INSTRUCTION(Compare);
4111
Roland Levillain31dd3d62016-02-16 12:21:02 +00004112 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01004113 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004114 static constexpr size_t kFieldComparisonBiasSize =
4115 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
4116 static constexpr size_t kNumberOfComparePackedBits =
4117 kFieldComparisonBias + kFieldComparisonBiasSize;
4118 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4119 using ComparisonBiasField =
4120 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
4121
Roland Levillain31dd3d62016-02-16 12:21:02 +00004122 // Return an integer constant containing the result of a comparison evaluated at compile time.
4123 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
4124 DCHECK(value == -1 || value == 0 || value == 1) << value;
4125 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
4126 }
4127
Artem Serovcced8ba2017-07-19 18:18:09 +01004128 DEFAULT_COPY_CONSTRUCTOR(Compare);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004129};
4130
Nicolas Geoffray2b615ba2017-01-06 14:40:07 +00004131class HNewInstance FINAL : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00004132 public:
4133 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00004134 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004135 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00004136 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00004137 bool finalizable,
4138 QuickEntrypointEnum entrypoint)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304139 : HExpression(kNewInstance,
4140 DataType::Type::kReference,
4141 SideEffects::CanTriggerGC(),
4142 dex_pc),
David Brazdil6de19382016-01-08 17:37:10 +00004143 type_index_(type_index),
4144 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00004145 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004146 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00004147 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00004148 }
4149
Artem Serovcced8ba2017-07-19 18:18:09 +01004150 bool IsClonable() const OVERRIDE { return true; }
4151
Andreas Gampea5b09a62016-11-17 15:21:22 -08004152 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00004153 const DexFile& GetDexFile() const { return dex_file_; }
4154
4155 // Calls runtime so needs an environment.
4156 bool NeedsEnvironment() const OVERRIDE { return true; }
4157
Mingyao Yang062157f2016-03-02 10:15:36 -08004158 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
4159 bool CanThrow() const OVERRIDE { return true; }
4160
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004161 bool NeedsChecks() const {
4162 return entrypoint_ == kQuickAllocObjectWithChecks;
4163 }
David Brazdil6de19382016-01-08 17:37:10 +00004164
Vladimir Markoa1de9182016-02-25 11:37:38 +00004165 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00004166
4167 bool CanBeNull() const OVERRIDE { return false; }
4168
4169 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4170
4171 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
4172 entrypoint_ = entrypoint;
4173 }
4174
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004175 HLoadClass* GetLoadClass() const {
4176 HInstruction* input = InputAt(0);
4177 if (input->IsClinitCheck()) {
4178 input = input->InputAt(0);
4179 }
4180 DCHECK(input->IsLoadClass());
4181 return input->AsLoadClass();
4182 }
4183
David Brazdil6de19382016-01-08 17:37:10 +00004184 bool IsStringAlloc() const;
4185
4186 DECLARE_INSTRUCTION(NewInstance);
4187
Artem Serovcced8ba2017-07-19 18:18:09 +01004188 protected:
4189 DEFAULT_COPY_CONSTRUCTOR(NewInstance);
4190
David Brazdil6de19382016-01-08 17:37:10 +00004191 private:
Vladimir Markobd785672018-05-03 17:09:09 +01004192 static constexpr size_t kFlagFinalizable = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004193 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
4194 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
4195 "Too many packed fields.");
4196
Andreas Gampea5b09a62016-11-17 15:21:22 -08004197 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00004198 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00004199 QuickEntrypointEnum entrypoint_;
David Brazdil6de19382016-01-08 17:37:10 +00004200};
4201
Agi Csaki05f20562015-08-19 14:58:14 -07004202enum IntrinsicNeedsEnvironmentOrCache {
4203 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
4204 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07004205};
4206
Aart Bik5d75afe2015-12-14 11:57:01 -08004207enum IntrinsicSideEffects {
4208 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
4209 kReadSideEffects, // Intrinsic may read heap memory.
4210 kWriteSideEffects, // Intrinsic may write heap memory.
4211 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
4212};
4213
4214enum IntrinsicExceptions {
4215 kNoThrow, // Intrinsic does not throw any exceptions.
4216 kCanThrow // Intrinsic may throw exceptions.
4217};
4218
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004219class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004220 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004221 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004222
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01004223 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004224 SetRawInputAt(index, argument);
4225 }
4226
Roland Levillain3e3d7332015-04-28 11:00:54 +01004227 // Return the number of arguments. This number can be lower than
4228 // the number of inputs returned by InputCount(), as some invoke
4229 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
4230 // inputs at the end of their list of inputs.
4231 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
4232
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004233 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4234
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004235 InvokeType GetInvokeType() const {
4236 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004237 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004238
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01004239 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004240 return intrinsic_;
4241 }
4242
Aart Bik5d75afe2015-12-14 11:57:01 -08004243 void SetIntrinsic(Intrinsics intrinsic,
4244 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
4245 IntrinsicSideEffects side_effects,
4246 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004247
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004248 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00004249 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004250 }
4251
Aart Bikff7d89c2016-11-07 08:49:28 -08004252 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4253
Vladimir Markoa1de9182016-02-25 11:37:38 +00004254 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004255
Aart Bika8b8e9b2018-01-09 11:01:02 -08004256 void SetAlwaysThrows(bool always_throws) { SetPackedFlag<kFlagAlwaysThrows>(always_throws); }
4257
4258 bool AlwaysThrows() const OVERRIDE { return GetPackedFlag<kFlagAlwaysThrows>(); }
4259
Aart Bik71bf7b42016-11-16 10:17:46 -08004260 bool CanBeMoved() const OVERRIDE { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004261
Vladimir Marko372f10e2016-05-17 16:30:10 +01004262 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08004263 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4264 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004265
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004266 uint32_t* GetIntrinsicOptimizations() {
4267 return &intrinsic_optimizations_;
4268 }
4269
4270 const uint32_t* GetIntrinsicOptimizations() const {
4271 return &intrinsic_optimizations_;
4272 }
4273
4274 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4275
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004276 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004277 void SetResolvedMethod(ArtMethod* method) { resolved_method_ = method; }
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004278
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004279 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004280
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004281 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004282 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4283 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004284 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
Vladimir Markobd785672018-05-03 17:09:09 +01004285 static constexpr size_t kFlagCanThrow = kFieldInvokeType + kFieldInvokeTypeSize;
Aart Bika8b8e9b2018-01-09 11:01:02 -08004286 static constexpr size_t kFlagAlwaysThrows = kFlagCanThrow + 1;
4287 static constexpr size_t kNumberOfInvokePackedBits = kFlagAlwaysThrows + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004288 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004289 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004290
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304291 HInvoke(InstructionKind kind,
4292 ArenaAllocator* allocator,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004293 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004294 uint32_t number_of_other_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004295 DataType::Type return_type,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004296 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004297 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004298 ArtMethod* resolved_method,
4299 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004300 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304301 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01004302 return_type,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004303 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4304 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004305 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004306 number_of_arguments + number_of_other_inputs,
4307 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004308 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004309 resolved_method_(resolved_method),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004310 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07004311 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004312 intrinsic_optimizations_(0) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004313 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004314 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004315 }
4316
Artem Serovcced8ba2017-07-19 18:18:09 +01004317 DEFAULT_COPY_CONSTRUCTOR(Invoke);
4318
Roland Levillain3e3d7332015-04-28 11:00:54 +01004319 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004320 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004321 const uint32_t dex_method_index_;
4322 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004323
4324 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4325 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004326};
4327
Vladimir Markofcb503c2016-05-18 12:48:17 +01004328class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004329 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004330 HInvokeUnresolved(ArenaAllocator* allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004331 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004332 DataType::Type return_type,
Calin Juravle175dc732015-08-25 15:42:32 +01004333 uint32_t dex_pc,
4334 uint32_t dex_method_index,
4335 InvokeType invoke_type)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304336 : HInvoke(kInvokeUnresolved,
4337 allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004338 number_of_arguments,
4339 0u /* number_of_other_inputs */,
4340 return_type,
4341 dex_pc,
4342 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004343 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01004344 invoke_type) {
4345 }
4346
Artem Serovcced8ba2017-07-19 18:18:09 +01004347 bool IsClonable() const OVERRIDE { return true; }
4348
Calin Juravle175dc732015-08-25 15:42:32 +01004349 DECLARE_INSTRUCTION(InvokeUnresolved);
4350
Artem Serovcced8ba2017-07-19 18:18:09 +01004351 protected:
4352 DEFAULT_COPY_CONSTRUCTOR(InvokeUnresolved);
Calin Juravle175dc732015-08-25 15:42:32 +01004353};
4354
Orion Hodsonac141392017-01-13 11:53:47 +00004355class HInvokePolymorphic FINAL : public HInvoke {
4356 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004357 HInvokePolymorphic(ArenaAllocator* allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004358 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004359 DataType::Type return_type,
Orion Hodsonac141392017-01-13 11:53:47 +00004360 uint32_t dex_pc,
4361 uint32_t dex_method_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304362 : HInvoke(kInvokePolymorphic,
4363 allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004364 number_of_arguments,
4365 0u /* number_of_other_inputs */,
4366 return_type,
4367 dex_pc,
4368 dex_method_index,
4369 nullptr,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304370 kVirtual) {
4371 }
Orion Hodsonac141392017-01-13 11:53:47 +00004372
Artem Serovcced8ba2017-07-19 18:18:09 +01004373 bool IsClonable() const OVERRIDE { return true; }
4374
Orion Hodsonac141392017-01-13 11:53:47 +00004375 DECLARE_INSTRUCTION(InvokePolymorphic);
4376
Artem Serovcced8ba2017-07-19 18:18:09 +01004377 protected:
4378 DEFAULT_COPY_CONSTRUCTOR(InvokePolymorphic);
Orion Hodsonac141392017-01-13 11:53:47 +00004379};
4380
Vladimir Markofcb503c2016-05-18 12:48:17 +01004381class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004382 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004383 // Requirements of this method call regarding the class
4384 // initialization (clinit) check of its declaring class.
4385 enum class ClinitCheckRequirement {
4386 kNone, // Class already initialized.
4387 kExplicit, // Static call having explicit clinit check as last input.
4388 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004389 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004390 };
4391
Vladimir Marko58155012015-08-19 12:49:41 +00004392 // Determines how to load the target ArtMethod*.
4393 enum class MethodLoadKind {
4394 // Use a String init ArtMethod* loaded from Thread entrypoints.
4395 kStringInit,
4396
4397 // Use the method's own ArtMethod* loaded by the register allocator.
4398 kRecursive,
4399
Vladimir Marko65979462017-05-19 17:25:12 +01004400 // Use PC-relative boot image ArtMethod* address that will be known at link time.
4401 // Used for boot image methods referenced by boot image code.
4402 kBootImageLinkTimePcRelative,
4403
Vladimir Marko58155012015-08-19 12:49:41 +00004404 // Use ArtMethod* at a known address, embed the direct address in the code.
4405 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
4406 kDirectAddress,
4407
Vladimir Markob066d432018-01-03 13:14:37 +00004408 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
4409 // Used for app->boot calls with relocatable image.
4410 kBootImageRelRo,
4411
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004412 // Load from an entry in the .bss section using a PC-relative load.
4413 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
4414 kBssEntry,
Vladimir Marko58155012015-08-19 12:49:41 +00004415
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004416 // Make a runtime call to resolve and call the method. This is the last-resort-kind
4417 // used when other kinds are unimplemented on a particular architecture.
4418 kRuntimeCall,
Vladimir Marko58155012015-08-19 12:49:41 +00004419 };
4420
4421 // Determines the location of the code pointer.
4422 enum class CodePtrLocation {
4423 // Recursive call, use local PC-relative call instruction.
4424 kCallSelf,
4425
Vladimir Marko58155012015-08-19 12:49:41 +00004426 // Use code pointer from the ArtMethod*.
4427 // Used when we don't know the target code. This is also the last-resort-kind used when
4428 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4429 kCallArtMethod,
4430 };
4431
4432 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004433 MethodLoadKind method_load_kind;
4434 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004435 // The method load data holds
4436 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4437 // Note that there are multiple string init methods, each having its own offset.
4438 // - the method address for kDirectAddress
Vladimir Markodc151b22015-10-15 18:02:30 +01004439 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004440 };
4441
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004442 HInvokeStaticOrDirect(ArenaAllocator* allocator,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004443 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004444 DataType::Type return_type,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004445 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004446 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004447 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004448 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004449 InvokeType invoke_type,
4450 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004451 ClinitCheckRequirement clinit_check_requirement)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304452 : HInvoke(kInvokeStaticOrDirect,
4453 allocator,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004454 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004455 // There is potentially one extra argument for the HCurrentMethod node, and
4456 // potentially one other if the clinit check is explicit, and potentially
4457 // one other if the method is a string factory.
4458 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004459 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004460 return_type,
4461 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004462 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004463 resolved_method,
4464 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004465 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004466 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004467 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4468 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004469
Artem Serovcced8ba2017-07-19 18:18:09 +01004470 bool IsClonable() const OVERRIDE { return true; }
4471
Vladimir Markodc151b22015-10-15 18:02:30 +01004472 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004473 bool had_current_method_input = HasCurrentMethodInput();
4474 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4475
4476 // Using the current method is the default and once we find a better
4477 // method load kind, we should not go back to using the current method.
4478 DCHECK(had_current_method_input || !needs_current_method_input);
4479
4480 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004481 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4482 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004483 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004484 dispatch_info_ = dispatch_info;
4485 }
4486
Aart Bik6daebeb2017-04-03 14:35:41 -07004487 DispatchInfo GetDispatchInfo() const {
4488 return dispatch_info_;
4489 }
4490
Vladimir Markoc53c0792015-11-19 15:48:33 +00004491 void AddSpecialInput(HInstruction* input) {
4492 // We allow only one special input.
4493 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4494 DCHECK(InputCount() == GetSpecialInputIndex() ||
4495 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4496 InsertInputAt(GetSpecialInputIndex(), input);
4497 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004498
Vladimir Marko372f10e2016-05-17 16:30:10 +01004499 using HInstruction::GetInputRecords; // Keep the const version visible.
4500 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
4501 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4502 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4503 DCHECK(!input_records.empty());
4504 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4505 HInstruction* last_input = input_records.back().GetInstruction();
4506 // Note: `last_input` may be null during arguments setup.
4507 if (last_input != nullptr) {
4508 // `last_input` is the last input of a static invoke marked as having
4509 // an explicit clinit check. It must either be:
4510 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4511 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4512 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4513 }
4514 }
4515 return input_records;
4516 }
4517
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004518 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004519 // We access the method via the dex cache so we can't do an implicit null check.
4520 // TODO: for intrinsics we can generate implicit null checks.
4521 return false;
4522 }
4523
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004524 bool CanBeNull() const OVERRIDE {
Vladimir Markobd785672018-05-03 17:09:09 +01004525 return GetType() == DataType::Type::kReference && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004526 }
4527
Vladimir Markoc53c0792015-11-19 15:48:33 +00004528 // Get the index of the special input, if any.
4529 //
David Brazdil6de19382016-01-08 17:37:10 +00004530 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4531 // method pointer; otherwise there may be one platform-specific special input,
4532 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004533 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004534 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004535
Vladimir Marko58155012015-08-19 12:49:41 +00004536 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4537 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4538 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004539 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004540 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004541 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko65979462017-05-19 17:25:12 +01004542 bool HasPcRelativeMethodLoadKind() const {
4543 return GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markob066d432018-01-03 13:14:37 +00004544 GetMethodLoadKind() == MethodLoadKind::kBootImageRelRo ||
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004545 GetMethodLoadKind() == MethodLoadKind::kBssEntry;
Vladimir Marko65979462017-05-19 17:25:12 +01004546 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004547 bool HasCurrentMethodInput() const {
4548 // This function can be called only after the invoke has been fully initialized by the builder.
4549 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004550 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004551 return true;
4552 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004553 DCHECK(InputCount() == GetSpecialInputIndex() ||
4554 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004555 return false;
4556 }
4557 }
Vladimir Marko58155012015-08-19 12:49:41 +00004558
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004559 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004560 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004561 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004562 }
4563
4564 uint64_t GetMethodAddress() const {
4565 DCHECK(HasMethodAddress());
4566 return dispatch_info_.method_load_data;
4567 }
4568
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004569 const DexFile& GetDexFileForPcRelativeDexCache() const;
4570
Vladimir Markoa1de9182016-02-25 11:37:38 +00004571 ClinitCheckRequirement GetClinitCheckRequirement() const {
4572 return GetPackedField<ClinitCheckRequirementField>();
4573 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004574
Roland Levillain4c0eb422015-04-24 16:43:49 +01004575 // Is this instruction a call to a static method?
4576 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004577 return GetInvokeType() == kStatic;
4578 }
4579
4580 MethodReference GetTargetMethod() const {
4581 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004582 }
4583
Vladimir Markofbb184a2015-11-13 14:47:00 +00004584 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4585 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4586 // instruction; only relevant for static calls with explicit clinit check.
4587 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004588 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004589 size_t last_input_index = inputs_.size() - 1u;
4590 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004591 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004592 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004593 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004594 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004595 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004596 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004597 }
4598
4599 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004600 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004601 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004602 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004603 }
4604
4605 // Is this a call to a static method whose declaring class has an
4606 // implicit intialization check requirement?
4607 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004608 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004609 }
4610
Vladimir Markob554b5a2015-11-06 12:57:55 +00004611 // Does this method load kind need the current method as an input?
4612 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004613 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kRuntimeCall;
Vladimir Markob554b5a2015-11-06 12:57:55 +00004614 }
4615
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004616 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004617
Artem Serovcced8ba2017-07-19 18:18:09 +01004618 protected:
4619 DEFAULT_COPY_CONSTRUCTOR(InvokeStaticOrDirect);
4620
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004621 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004622 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004623 static constexpr size_t kFieldClinitCheckRequirementSize =
4624 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4625 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4626 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4627 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4628 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004629 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4630 kFieldClinitCheckRequirement,
4631 kFieldClinitCheckRequirementSize>;
4632
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004633 // Cached values of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffrayea179f42018-02-08 22:30:18 +00004634 const MethodReference target_method_;
Vladimir Marko58155012015-08-19 12:49:41 +00004635 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004636};
Vladimir Markof64242a2015-12-01 14:58:23 +00004637std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004638std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004639
Vladimir Markofcb503c2016-05-18 12:48:17 +01004640class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004641 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004642 HInvokeVirtual(ArenaAllocator* allocator,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004643 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004644 DataType::Type return_type,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004645 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004646 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004647 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004648 uint32_t vtable_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304649 : HInvoke(kInvokeVirtual,
4650 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004651 number_of_arguments,
4652 0u,
4653 return_type,
4654 dex_pc,
4655 dex_method_index,
4656 resolved_method,
4657 kVirtual),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304658 vtable_index_(vtable_index) {
4659 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004660
Artem Serovcced8ba2017-07-19 18:18:09 +01004661 bool IsClonable() const OVERRIDE { return true; }
4662
Aart Bik71bf7b42016-11-16 10:17:46 -08004663 bool CanBeNull() const OVERRIDE {
4664 switch (GetIntrinsic()) {
4665 case Intrinsics::kThreadCurrentThread:
4666 case Intrinsics::kStringBufferAppend:
4667 case Intrinsics::kStringBufferToString:
4668 case Intrinsics::kStringBuilderAppend:
4669 case Intrinsics::kStringBuilderToString:
4670 return false;
4671 default:
4672 return HInvoke::CanBeNull();
4673 }
4674 }
4675
Calin Juravle641547a2015-04-21 22:08:51 +01004676 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004677 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004678 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004679 }
4680
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004681 uint32_t GetVTableIndex() const { return vtable_index_; }
4682
4683 DECLARE_INSTRUCTION(InvokeVirtual);
4684
Artem Serovcced8ba2017-07-19 18:18:09 +01004685 protected:
4686 DEFAULT_COPY_CONSTRUCTOR(InvokeVirtual);
4687
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004688 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004689 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004690 const uint32_t vtable_index_;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004691};
4692
Vladimir Markofcb503c2016-05-18 12:48:17 +01004693class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004694 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004695 HInvokeInterface(ArenaAllocator* allocator,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004696 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004697 DataType::Type return_type,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004698 uint32_t dex_pc,
4699 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004700 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004701 uint32_t imt_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304702 : HInvoke(kInvokeInterface,
4703 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004704 number_of_arguments,
4705 0u,
4706 return_type,
4707 dex_pc,
4708 dex_method_index,
4709 resolved_method,
4710 kInterface),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304711 imt_index_(imt_index) {
4712 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004713
Artem Serovcced8ba2017-07-19 18:18:09 +01004714 bool IsClonable() const OVERRIDE { return true; }
4715
Calin Juravle641547a2015-04-21 22:08:51 +01004716 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004717 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004718 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004719 }
4720
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004721 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
4722 // The assembly stub currently needs it.
4723 return true;
4724 }
4725
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004726 uint32_t GetImtIndex() const { return imt_index_; }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004727
4728 DECLARE_INSTRUCTION(InvokeInterface);
4729
Artem Serovcced8ba2017-07-19 18:18:09 +01004730 protected:
4731 DEFAULT_COPY_CONSTRUCTOR(InvokeInterface);
4732
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004733 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004734 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004735 const uint32_t imt_index_;
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004736};
4737
Vladimir Markofcb503c2016-05-18 12:48:17 +01004738class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004739 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004740 HNeg(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304741 : HUnaryOperation(kNeg, result_type, input, dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004742 DCHECK_EQ(result_type, DataType::Kind(input->GetType()));
Roland Levillain937e6cd2016-03-22 11:54:37 +00004743 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004744
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004745 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004746
4747 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004748 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004749 }
4750 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004751 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004752 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004753 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4754 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4755 }
4756 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4757 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4758 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004759
Roland Levillain88cb1752014-10-20 16:36:47 +01004760 DECLARE_INSTRUCTION(Neg);
4761
Artem Serovcced8ba2017-07-19 18:18:09 +01004762 protected:
4763 DEFAULT_COPY_CONSTRUCTOR(Neg);
Roland Levillain88cb1752014-10-20 16:36:47 +01004764};
4765
Vladimir Markofcb503c2016-05-18 12:48:17 +01004766class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004767 public:
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004768 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304769 : HExpression(kNewArray, DataType::Type::kReference, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004770 SetRawInputAt(0, cls);
4771 SetRawInputAt(1, length);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004772 }
4773
Artem Serovcced8ba2017-07-19 18:18:09 +01004774 bool IsClonable() const OVERRIDE { return true; }
4775
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004776 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004777 bool NeedsEnvironment() const OVERRIDE { return true; }
4778
Mingyao Yang0c365e62015-03-31 15:09:29 -07004779 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4780 bool CanThrow() const OVERRIDE { return true; }
4781
Calin Juravle10e244f2015-01-26 18:54:32 +00004782 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004783
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004784 HLoadClass* GetLoadClass() const {
4785 DCHECK(InputAt(0)->IsLoadClass());
4786 return InputAt(0)->AsLoadClass();
4787 }
4788
4789 HInstruction* GetLength() const {
4790 return InputAt(1);
4791 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004792
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004793 DECLARE_INSTRUCTION(NewArray);
4794
Artem Serovcced8ba2017-07-19 18:18:09 +01004795 protected:
4796 DEFAULT_COPY_CONSTRUCTOR(NewArray);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004797};
4798
Vladimir Markofcb503c2016-05-18 12:48:17 +01004799class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004800 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004801 HAdd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004802 HInstruction* left,
4803 HInstruction* right,
4804 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304805 : HBinaryOperation(kAdd, result_type, left, right, SideEffects::None(), dex_pc) {
4806 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004807
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004808 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004809
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004810 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004811
4812 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004813 return GetBlock()->GetGraph()->GetIntConstant(
4814 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004815 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004816 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004817 return GetBlock()->GetGraph()->GetLongConstant(
4818 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004819 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004820 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4821 return GetBlock()->GetGraph()->GetFloatConstant(
4822 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4823 }
4824 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4825 return GetBlock()->GetGraph()->GetDoubleConstant(
4826 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4827 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004828
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004829 DECLARE_INSTRUCTION(Add);
4830
Artem Serovcced8ba2017-07-19 18:18:09 +01004831 protected:
4832 DEFAULT_COPY_CONSTRUCTOR(Add);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004833};
4834
Vladimir Markofcb503c2016-05-18 12:48:17 +01004835class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004836 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004837 HSub(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004838 HInstruction* left,
4839 HInstruction* right,
4840 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304841 : HBinaryOperation(kSub, result_type, left, right, SideEffects::None(), dex_pc) {
4842 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004843
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004844 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004845
4846 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004847 return GetBlock()->GetGraph()->GetIntConstant(
4848 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004849 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004850 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004851 return GetBlock()->GetGraph()->GetLongConstant(
4852 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004853 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004854 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4855 return GetBlock()->GetGraph()->GetFloatConstant(
4856 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4857 }
4858 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4859 return GetBlock()->GetGraph()->GetDoubleConstant(
4860 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4861 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004862
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004863 DECLARE_INSTRUCTION(Sub);
4864
Artem Serovcced8ba2017-07-19 18:18:09 +01004865 protected:
4866 DEFAULT_COPY_CONSTRUCTOR(Sub);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004867};
4868
Vladimir Markofcb503c2016-05-18 12:48:17 +01004869class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004870 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004871 HMul(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004872 HInstruction* left,
4873 HInstruction* right,
4874 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304875 : HBinaryOperation(kMul, result_type, left, right, SideEffects::None(), dex_pc) {
4876 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004877
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004878 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004879
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004880 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004881
4882 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004883 return GetBlock()->GetGraph()->GetIntConstant(
4884 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004885 }
4886 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004887 return GetBlock()->GetGraph()->GetLongConstant(
4888 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004889 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004890 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4891 return GetBlock()->GetGraph()->GetFloatConstant(
4892 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4893 }
4894 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4895 return GetBlock()->GetGraph()->GetDoubleConstant(
4896 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4897 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004898
4899 DECLARE_INSTRUCTION(Mul);
4900
Artem Serovcced8ba2017-07-19 18:18:09 +01004901 protected:
4902 DEFAULT_COPY_CONSTRUCTOR(Mul);
Calin Juravle34bacdf2014-10-07 20:23:36 +01004903};
4904
Vladimir Markofcb503c2016-05-18 12:48:17 +01004905class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004906 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004907 HDiv(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004908 HInstruction* left,
4909 HInstruction* right,
4910 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304911 : HBinaryOperation(kDiv, result_type, left, right, SideEffects::None(), dex_pc) {
4912 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004913
Roland Levillain9867bc72015-08-05 10:21:34 +01004914 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004915 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004916 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004917 // Our graph structure ensures we never have 0 for `y` during
4918 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004919 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004920 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004921 return (y == -1) ? -x : x / y;
4922 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004923
Roland Levillain31dd3d62016-02-16 12:21:02 +00004924 template <typename T>
4925 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004926 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004927 return x / y;
4928 }
4929
Roland Levillain9867bc72015-08-05 10:21:34 +01004930 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004931 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004932 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004933 }
4934 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004935 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004936 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4937 }
4938 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4939 return GetBlock()->GetGraph()->GetFloatConstant(
4940 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4941 }
4942 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4943 return GetBlock()->GetGraph()->GetDoubleConstant(
4944 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004945 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004946
4947 DECLARE_INSTRUCTION(Div);
4948
Artem Serovcced8ba2017-07-19 18:18:09 +01004949 protected:
4950 DEFAULT_COPY_CONSTRUCTOR(Div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00004951};
4952
Vladimir Markofcb503c2016-05-18 12:48:17 +01004953class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004954 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004955 HRem(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004956 HInstruction* left,
4957 HInstruction* right,
4958 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304959 : HBinaryOperation(kRem, result_type, left, right, SideEffects::None(), dex_pc) {
4960 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004961
Roland Levillain9867bc72015-08-05 10:21:34 +01004962 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004963 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004964 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004965 // Our graph structure ensures we never have 0 for `y` during
4966 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004967 DCHECK_NE(y, 0);
4968 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4969 return (y == -1) ? 0 : x % y;
4970 }
4971
Roland Levillain31dd3d62016-02-16 12:21:02 +00004972 template <typename T>
4973 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004974 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004975 return std::fmod(x, y);
4976 }
4977
Roland Levillain9867bc72015-08-05 10:21:34 +01004978 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004979 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004980 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004981 }
4982 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004983 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004984 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004985 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004986 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4987 return GetBlock()->GetGraph()->GetFloatConstant(
4988 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4989 }
4990 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4991 return GetBlock()->GetGraph()->GetDoubleConstant(
4992 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4993 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004994
4995 DECLARE_INSTRUCTION(Rem);
4996
Artem Serovcced8ba2017-07-19 18:18:09 +01004997 protected:
4998 DEFAULT_COPY_CONSTRUCTOR(Rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00004999};
5000
Aart Bik1f8d51b2018-02-15 10:42:37 -08005001class HMin FINAL : public HBinaryOperation {
5002 public:
5003 HMin(DataType::Type result_type,
5004 HInstruction* left,
5005 HInstruction* right,
5006 uint32_t dex_pc)
5007 : HBinaryOperation(kMin, result_type, left, right, SideEffects::None(), dex_pc) {}
5008
5009 bool IsCommutative() const OVERRIDE { return true; }
5010
5011 // Evaluation for integral values.
5012 template <typename T> static T ComputeIntegral(T x, T y) {
5013 return (x <= y) ? x : y;
5014 }
5015
5016 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
5017 return GetBlock()->GetGraph()->GetIntConstant(
5018 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5019 }
5020 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
5021 return GetBlock()->GetGraph()->GetLongConstant(
5022 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5023 }
5024 // TODO: Evaluation for floating-point values.
5025 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5026 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { return nullptr; }
5027 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5028 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { return nullptr; }
5029
5030 DECLARE_INSTRUCTION(Min);
5031
5032 protected:
5033 DEFAULT_COPY_CONSTRUCTOR(Min);
5034};
5035
5036class HMax FINAL : public HBinaryOperation {
5037 public:
5038 HMax(DataType::Type result_type,
5039 HInstruction* left,
5040 HInstruction* right,
5041 uint32_t dex_pc)
5042 : HBinaryOperation(kMax, result_type, left, right, SideEffects::None(), dex_pc) {}
5043
5044 bool IsCommutative() const OVERRIDE { return true; }
5045
5046 // Evaluation for integral values.
5047 template <typename T> static T ComputeIntegral(T x, T y) {
5048 return (x >= y) ? x : y;
5049 }
5050
5051 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
5052 return GetBlock()->GetGraph()->GetIntConstant(
5053 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5054 }
5055 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
5056 return GetBlock()->GetGraph()->GetLongConstant(
5057 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5058 }
5059 // TODO: Evaluation for floating-point values.
5060 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5061 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { return nullptr; }
5062 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5063 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { return nullptr; }
5064
5065 DECLARE_INSTRUCTION(Max);
5066
5067 protected:
5068 DEFAULT_COPY_CONSTRUCTOR(Max);
5069};
5070
Aart Bik3dad3412018-02-28 12:01:46 -08005071class HAbs FINAL : public HUnaryOperation {
5072 public:
5073 HAbs(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
5074 : HUnaryOperation(kAbs, result_type, input, dex_pc) {}
5075
5076 // Evaluation for integral values.
5077 template <typename T> static T ComputeIntegral(T x) {
5078 return x < 0 ? -x : x;
5079 }
5080
5081 // Evaluation for floating-point values.
5082 // Note, as a "quality of implementation", rather than pure "spec compliance",
5083 // we require that Math.abs() clears the sign bit (but changes nothing else)
5084 // for all floating-point numbers, including NaN (signaling NaN may become quiet though).
5085 // http://b/30758343
5086 template <typename T, typename S> static T ComputeFP(T x) {
5087 S bits = bit_cast<S, T>(x);
5088 return bit_cast<T, S>(bits & std::numeric_limits<S>::max());
5089 }
5090
5091 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
5092 return GetBlock()->GetGraph()->GetIntConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5093 }
5094 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
5095 return GetBlock()->GetGraph()->GetLongConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5096 }
5097 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
5098 return GetBlock()->GetGraph()->GetFloatConstant(
5099 ComputeFP<float, int32_t>(x->GetValue()), GetDexPc());
5100 }
5101 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
5102 return GetBlock()->GetGraph()->GetDoubleConstant(
5103 ComputeFP<double, int64_t>(x->GetValue()), GetDexPc());
5104 }
5105
5106 DECLARE_INSTRUCTION(Abs);
5107
5108 protected:
5109 DEFAULT_COPY_CONSTRUCTOR(Abs);
5110};
5111
Vladimir Markofcb503c2016-05-18 12:48:17 +01005112class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00005113 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00005114 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
5115 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00005116 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305117 : HExpression(kDivZeroCheck, value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00005118 SetRawInputAt(0, value);
5119 }
5120
5121 bool CanBeMoved() const OVERRIDE { return true; }
5122
Vladimir Marko372f10e2016-05-17 16:30:10 +01005123 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00005124 return true;
5125 }
5126
5127 bool NeedsEnvironment() const OVERRIDE { return true; }
5128 bool CanThrow() const OVERRIDE { return true; }
5129
Calin Juravled0d48522014-11-04 16:40:20 +00005130 DECLARE_INSTRUCTION(DivZeroCheck);
5131
Artem Serovcced8ba2017-07-19 18:18:09 +01005132 protected:
5133 DEFAULT_COPY_CONSTRUCTOR(DivZeroCheck);
Calin Juravled0d48522014-11-04 16:40:20 +00005134};
5135
Vladimir Markofcb503c2016-05-18 12:48:17 +01005136class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005137 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005138 HShl(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005139 HInstruction* value,
5140 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005141 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305142 : HBinaryOperation(kShl, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005143 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5144 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005145 }
5146
Roland Levillain5b5b9312016-03-22 14:57:31 +00005147 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005148 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005149 return value << (distance & max_shift_distance);
5150 }
5151
5152 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005153 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005154 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005155 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005156 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005157 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005158 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005159 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005160 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5161 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5162 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5163 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005164 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005165 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5166 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005167 LOG(FATAL) << DebugName() << " is not defined for float values";
5168 UNREACHABLE();
5169 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005170 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5171 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005172 LOG(FATAL) << DebugName() << " is not defined for double values";
5173 UNREACHABLE();
5174 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005175
5176 DECLARE_INSTRUCTION(Shl);
5177
Artem Serovcced8ba2017-07-19 18:18:09 +01005178 protected:
5179 DEFAULT_COPY_CONSTRUCTOR(Shl);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005180};
5181
Vladimir Markofcb503c2016-05-18 12:48:17 +01005182class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005183 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005184 HShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005185 HInstruction* value,
5186 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005187 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305188 : HBinaryOperation(kShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005189 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5190 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005191 }
5192
Roland Levillain5b5b9312016-03-22 14:57:31 +00005193 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005194 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005195 return value >> (distance & max_shift_distance);
5196 }
5197
5198 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005199 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005200 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005201 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005202 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005203 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005204 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005205 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005206 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5207 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5208 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5209 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005210 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005211 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5212 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005213 LOG(FATAL) << DebugName() << " is not defined for float values";
5214 UNREACHABLE();
5215 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005216 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5217 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005218 LOG(FATAL) << DebugName() << " is not defined for double values";
5219 UNREACHABLE();
5220 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005221
5222 DECLARE_INSTRUCTION(Shr);
5223
Artem Serovcced8ba2017-07-19 18:18:09 +01005224 protected:
5225 DEFAULT_COPY_CONSTRUCTOR(Shr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005226};
5227
Vladimir Markofcb503c2016-05-18 12:48:17 +01005228class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005229 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005230 HUShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005231 HInstruction* value,
5232 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005233 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305234 : HBinaryOperation(kUShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005235 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5236 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00005237 }
5238
Roland Levillain5b5b9312016-03-22 14:57:31 +00005239 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005240 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005241 typedef typename std::make_unsigned<T>::type V;
5242 V ux = static_cast<V>(value);
5243 return static_cast<T>(ux >> (distance & max_shift_distance));
5244 }
5245
5246 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005247 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005248 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005249 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005250 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005251 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005252 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005253 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005254 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5255 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5256 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5257 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00005258 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005259 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5260 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005261 LOG(FATAL) << DebugName() << " is not defined for float values";
5262 UNREACHABLE();
5263 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005264 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5265 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005266 LOG(FATAL) << DebugName() << " is not defined for double values";
5267 UNREACHABLE();
5268 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005269
5270 DECLARE_INSTRUCTION(UShr);
5271
Artem Serovcced8ba2017-07-19 18:18:09 +01005272 protected:
5273 DEFAULT_COPY_CONSTRUCTOR(UShr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005274};
5275
Vladimir Markofcb503c2016-05-18 12:48:17 +01005276class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005277 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005278 HAnd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005279 HInstruction* left,
5280 HInstruction* right,
5281 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305282 : HBinaryOperation(kAnd, result_type, left, right, SideEffects::None(), dex_pc) {
5283 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005284
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005285 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005286
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005287 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005288
5289 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005290 return GetBlock()->GetGraph()->GetIntConstant(
5291 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005292 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005293 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005294 return GetBlock()->GetGraph()->GetLongConstant(
5295 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005296 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005297 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5298 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5299 LOG(FATAL) << DebugName() << " is not defined for float values";
5300 UNREACHABLE();
5301 }
5302 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5303 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5304 LOG(FATAL) << DebugName() << " is not defined for double values";
5305 UNREACHABLE();
5306 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005307
5308 DECLARE_INSTRUCTION(And);
5309
Artem Serovcced8ba2017-07-19 18:18:09 +01005310 protected:
5311 DEFAULT_COPY_CONSTRUCTOR(And);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005312};
5313
Vladimir Markofcb503c2016-05-18 12:48:17 +01005314class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005315 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005316 HOr(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005317 HInstruction* left,
5318 HInstruction* right,
5319 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305320 : HBinaryOperation(kOr, result_type, left, right, SideEffects::None(), dex_pc) {
5321 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005322
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005323 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005324
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005325 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005326
5327 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005328 return GetBlock()->GetGraph()->GetIntConstant(
5329 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005330 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005331 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005332 return GetBlock()->GetGraph()->GetLongConstant(
5333 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005334 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005335 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5336 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5337 LOG(FATAL) << DebugName() << " is not defined for float values";
5338 UNREACHABLE();
5339 }
5340 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5341 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5342 LOG(FATAL) << DebugName() << " is not defined for double values";
5343 UNREACHABLE();
5344 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005345
5346 DECLARE_INSTRUCTION(Or);
5347
Artem Serovcced8ba2017-07-19 18:18:09 +01005348 protected:
5349 DEFAULT_COPY_CONSTRUCTOR(Or);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005350};
5351
Vladimir Markofcb503c2016-05-18 12:48:17 +01005352class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005353 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005354 HXor(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005355 HInstruction* left,
5356 HInstruction* right,
5357 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305358 : HBinaryOperation(kXor, result_type, left, right, SideEffects::None(), dex_pc) {
5359 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005360
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005361 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005362
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005363 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005364
5365 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005366 return GetBlock()->GetGraph()->GetIntConstant(
5367 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005368 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005369 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005370 return GetBlock()->GetGraph()->GetLongConstant(
5371 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005372 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005373 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5374 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5375 LOG(FATAL) << DebugName() << " is not defined for float values";
5376 UNREACHABLE();
5377 }
5378 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5379 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5380 LOG(FATAL) << DebugName() << " is not defined for double values";
5381 UNREACHABLE();
5382 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005383
5384 DECLARE_INSTRUCTION(Xor);
5385
Artem Serovcced8ba2017-07-19 18:18:09 +01005386 protected:
5387 DEFAULT_COPY_CONSTRUCTOR(Xor);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005388};
5389
Vladimir Markofcb503c2016-05-18 12:48:17 +01005390class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005391 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005392 HRor(DataType::Type result_type, HInstruction* value, HInstruction* distance)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305393 : HBinaryOperation(kRor, result_type, value, distance) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005394 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5395 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00005396 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005397
Roland Levillain5b5b9312016-03-22 14:57:31 +00005398 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005399 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005400 typedef typename std::make_unsigned<T>::type V;
5401 V ux = static_cast<V>(value);
5402 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005403 return static_cast<T>(ux);
5404 } else {
5405 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005406 return static_cast<T>(ux >> (distance & max_shift_value)) |
5407 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005408 }
5409 }
5410
Roland Levillain5b5b9312016-03-22 14:57:31 +00005411 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005412 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005413 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005414 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005415 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005416 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005417 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005418 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005419 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5420 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5421 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5422 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005423 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005424 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5425 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005426 LOG(FATAL) << DebugName() << " is not defined for float values";
5427 UNREACHABLE();
5428 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005429 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5430 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005431 LOG(FATAL) << DebugName() << " is not defined for double values";
5432 UNREACHABLE();
5433 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005434
5435 DECLARE_INSTRUCTION(Ror);
5436
Artem Serovcced8ba2017-07-19 18:18:09 +01005437 protected:
5438 DEFAULT_COPY_CONSTRUCTOR(Ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005439};
5440
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005441// The value of a parameter in this method. Its location depends on
5442// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01005443class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005444 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005445 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005446 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005447 uint8_t index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005448 DataType::Type parameter_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005449 bool is_this = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305450 : HExpression(kParameterValue, parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005451 dex_file_(dex_file),
5452 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005453 index_(index) {
5454 SetPackedFlag<kFlagIsThis>(is_this);
5455 SetPackedFlag<kFlagCanBeNull>(!is_this);
5456 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005457
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005458 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005459 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005460 uint8_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07005461 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005462
Vladimir Markoa1de9182016-02-25 11:37:38 +00005463 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
5464 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005465
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005466 DECLARE_INSTRUCTION(ParameterValue);
5467
Artem Serovcced8ba2017-07-19 18:18:09 +01005468 protected:
5469 DEFAULT_COPY_CONSTRUCTOR(ParameterValue);
5470
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005471 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005472 // Whether or not the parameter value corresponds to 'this' argument.
Vladimir Markobd785672018-05-03 17:09:09 +01005473 static constexpr size_t kFlagIsThis = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005474 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5475 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5476 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5477 "Too many packed fields.");
5478
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005479 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005480 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005481 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005482 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005483 const uint8_t index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005484};
5485
Vladimir Markofcb503c2016-05-18 12:48:17 +01005486class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005487 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005488 HNot(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305489 : HUnaryOperation(kNot, result_type, input, dex_pc) {
5490 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005491
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005492 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005493 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005494 return true;
5495 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005496
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005497 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005498
5499 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005500 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005501 }
5502 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005503 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005504 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005505 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5506 LOG(FATAL) << DebugName() << " is not defined for float values";
5507 UNREACHABLE();
5508 }
5509 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5510 LOG(FATAL) << DebugName() << " is not defined for double values";
5511 UNREACHABLE();
5512 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005513
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005514 DECLARE_INSTRUCTION(Not);
5515
Artem Serovcced8ba2017-07-19 18:18:09 +01005516 protected:
5517 DEFAULT_COPY_CONSTRUCTOR(Not);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005518};
5519
Vladimir Markofcb503c2016-05-18 12:48:17 +01005520class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005521 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005522 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305523 : HUnaryOperation(kBooleanNot, DataType::Type::kBool, input, dex_pc) {
5524 }
David Brazdil66d126e2015-04-03 16:02:44 +01005525
5526 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005527 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01005528 return true;
5529 }
5530
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005531 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005532 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005533 return !x;
5534 }
5535
Roland Levillain9867bc72015-08-05 10:21:34 +01005536 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005537 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005538 }
5539 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5540 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005541 UNREACHABLE();
5542 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005543 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5544 LOG(FATAL) << DebugName() << " is not defined for float values";
5545 UNREACHABLE();
5546 }
5547 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5548 LOG(FATAL) << DebugName() << " is not defined for double values";
5549 UNREACHABLE();
5550 }
David Brazdil66d126e2015-04-03 16:02:44 +01005551
5552 DECLARE_INSTRUCTION(BooleanNot);
5553
Artem Serovcced8ba2017-07-19 18:18:09 +01005554 protected:
5555 DEFAULT_COPY_CONSTRUCTOR(BooleanNot);
David Brazdil66d126e2015-04-03 16:02:44 +01005556};
5557
Vladimir Markofcb503c2016-05-18 12:48:17 +01005558class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005559 public:
5560 // Instantiate a type conversion of `input` to `result_type`.
Vladimir Markoc8fb2112017-10-03 11:37:52 +01005561 HTypeConversion(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305562 : HExpression(kTypeConversion, result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005563 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005564 // Invariant: We should never generate a conversion to a Boolean value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005565 DCHECK_NE(DataType::Type::kBool, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005566 }
5567
5568 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005569 DataType::Type GetInputType() const { return GetInput()->GetType(); }
5570 DataType::Type GetResultType() const { return GetType(); }
Roland Levillaindff1f282014-11-05 14:15:05 +00005571
5572 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005573 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5574 return true;
5575 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005576
Mark Mendelle82549b2015-05-06 10:55:34 -04005577 // Try to statically evaluate the conversion and return a HConstant
5578 // containing the result. If the input cannot be converted, return nullptr.
5579 HConstant* TryStaticEvaluation() const;
5580
Roland Levillaindff1f282014-11-05 14:15:05 +00005581 DECLARE_INSTRUCTION(TypeConversion);
5582
Artem Serovcced8ba2017-07-19 18:18:09 +01005583 protected:
5584 DEFAULT_COPY_CONSTRUCTOR(TypeConversion);
Roland Levillaindff1f282014-11-05 14:15:05 +00005585};
5586
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005587static constexpr uint32_t kNoRegNumber = -1;
5588
Vladimir Markofcb503c2016-05-18 12:48:17 +01005589class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005590 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005591 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
5592 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005593 HNullCheck(HInstruction* value, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305594 : HExpression(kNullCheck, value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005595 SetRawInputAt(0, value);
5596 }
5597
Artem Serovcced8ba2017-07-19 18:18:09 +01005598 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00005599 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005600 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005601 return true;
5602 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005603
Calin Juravle10e244f2015-01-26 18:54:32 +00005604 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005605
Calin Juravle10e244f2015-01-26 18:54:32 +00005606 bool CanThrow() const OVERRIDE { return true; }
5607
5608 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005609
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005610 DECLARE_INSTRUCTION(NullCheck);
5611
Artem Serovcced8ba2017-07-19 18:18:09 +01005612 protected:
5613 DEFAULT_COPY_CONSTRUCTOR(NullCheck);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005614};
5615
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005616// Embeds an ArtField and all the information required by the compiler. We cache
5617// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005618class FieldInfo : public ValueObject {
5619 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005620 FieldInfo(ArtField* field,
5621 MemberOffset field_offset,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005622 DataType::Type field_type,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005623 bool is_volatile,
5624 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005625 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005626 const DexFile& dex_file)
5627 : field_(field),
5628 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005629 field_type_(field_type),
5630 is_volatile_(is_volatile),
5631 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005632 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005633 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005634
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005635 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005636 MemberOffset GetFieldOffset() const { return field_offset_; }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005637 DataType::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005638 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005639 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005640 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005641 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005642
5643 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005644 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005645 const MemberOffset field_offset_;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005646 const DataType::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005647 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005648 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005649 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005650 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005651};
5652
Vladimir Markofcb503c2016-05-18 12:48:17 +01005653class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005654 public:
5655 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005656 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005657 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005658 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005659 bool is_volatile,
5660 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005661 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005662 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005663 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305664 : HExpression(kInstanceFieldGet,
5665 field_type,
5666 SideEffects::FieldReadOfType(field_type, is_volatile),
5667 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005668 field_info_(field,
5669 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005670 field_type,
5671 is_volatile,
5672 field_idx,
5673 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005674 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005675 SetRawInputAt(0, value);
5676 }
5677
Artem Serovcced8ba2017-07-19 18:18:09 +01005678 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005679 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005680
Vladimir Marko372f10e2016-05-17 16:30:10 +01005681 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5682 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005683 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005684 }
5685
Calin Juravle641547a2015-04-21 22:08:51 +01005686 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005687 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005688 }
5689
5690 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005691 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5692 }
5693
Calin Juravle52c48962014-12-16 17:02:57 +00005694 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005695 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005696 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005697 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005698
Vladimir Marko61b92282017-10-11 13:23:17 +01005699 void SetType(DataType::Type new_type) {
5700 DCHECK(DataType::IsIntegralType(GetType()));
5701 DCHECK(DataType::IsIntegralType(new_type));
5702 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5703 SetPackedField<TypeField>(new_type);
5704 }
5705
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005706 DECLARE_INSTRUCTION(InstanceFieldGet);
5707
Artem Serovcced8ba2017-07-19 18:18:09 +01005708 protected:
5709 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldGet);
5710
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005711 private:
5712 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005713};
5714
Vladimir Markobd785672018-05-03 17:09:09 +01005715class HInstanceFieldSet FINAL : public HExpression<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005716 public:
5717 HInstanceFieldSet(HInstruction* object,
5718 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005719 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005720 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005721 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005722 bool is_volatile,
5723 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005724 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005725 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005726 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01005727 : HExpression(kInstanceFieldSet,
5728 SideEffects::FieldWriteOfType(field_type, is_volatile),
5729 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005730 field_info_(field,
5731 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005732 field_type,
5733 is_volatile,
5734 field_idx,
5735 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005736 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005737 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005738 SetRawInputAt(0, object);
5739 SetRawInputAt(1, value);
5740 }
5741
Artem Serovcced8ba2017-07-19 18:18:09 +01005742 bool IsClonable() const OVERRIDE { return true; }
5743
Calin Juravle641547a2015-04-21 22:08:51 +01005744 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005745 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005746 }
5747
Calin Juravle52c48962014-12-16 17:02:57 +00005748 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005749 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005750 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005751 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005752 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005753 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5754 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005755
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005756 DECLARE_INSTRUCTION(InstanceFieldSet);
5757
Artem Serovcced8ba2017-07-19 18:18:09 +01005758 protected:
5759 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldSet);
5760
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005761 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005762 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5763 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5764 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5765 "Too many packed fields.");
5766
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005767 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005768};
5769
Vladimir Markofcb503c2016-05-18 12:48:17 +01005770class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005771 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005772 HArrayGet(HInstruction* array,
5773 HInstruction* index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005774 DataType::Type type,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005775 uint32_t dex_pc)
5776 : HArrayGet(array,
5777 index,
5778 type,
5779 SideEffects::ArrayReadOfType(type),
5780 dex_pc,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305781 /* is_string_char_at */ false) {
5782 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005783
5784 HArrayGet(HInstruction* array,
5785 HInstruction* index,
5786 DataType::Type type,
5787 SideEffects side_effects,
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005788 uint32_t dex_pc,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005789 bool is_string_char_at)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305790 : HExpression(kArrayGet, type, side_effects, dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005791 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005792 SetRawInputAt(0, array);
5793 SetRawInputAt(1, index);
5794 }
5795
Artem Serovcced8ba2017-07-19 18:18:09 +01005796 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005797 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005798 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005799 return true;
5800 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005801 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005802 // TODO: We can be smarter here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01005803 // Currently, unless the array is the result of NewArray, the array access is always
5804 // preceded by some form of null NullCheck necessary for the bounds check, usually
5805 // implicit null check on the ArrayLength input to BoundsCheck or Deoptimize for
5806 // dynamic BCE. There are cases when these could be removed to produce better code.
5807 // If we ever add optimizations to do so we should allow an implicit check here
5808 // (as long as the address falls in the first page).
5809 //
5810 // As an example of such fancy optimization, we could eliminate BoundsCheck for
5811 // a = cond ? new int[1] : null;
5812 // a[0]; // The Phi does not need bounds check for either input.
Calin Juravle77520bc2015-01-12 18:45:46 +00005813 return false;
5814 }
5815
David Brazdil4833f5a2015-12-16 10:37:39 +00005816 bool IsEquivalentOf(HArrayGet* other) const {
5817 bool result = (GetDexPc() == other->GetDexPc());
5818 if (kIsDebugBuild && result) {
5819 DCHECK_EQ(GetBlock(), other->GetBlock());
5820 DCHECK_EQ(GetArray(), other->GetArray());
5821 DCHECK_EQ(GetIndex(), other->GetIndex());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005822 if (DataType::IsIntOrLongType(GetType())) {
5823 DCHECK(DataType::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005824 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005825 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
5826 DCHECK(DataType::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005827 }
5828 }
5829 return result;
5830 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005831
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005832 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5833
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005834 HInstruction* GetArray() const { return InputAt(0); }
5835 HInstruction* GetIndex() const { return InputAt(1); }
5836
Vladimir Marko61b92282017-10-11 13:23:17 +01005837 void SetType(DataType::Type new_type) {
5838 DCHECK(DataType::IsIntegralType(GetType()));
5839 DCHECK(DataType::IsIntegralType(new_type));
5840 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5841 SetPackedField<TypeField>(new_type);
5842 }
5843
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005844 DECLARE_INSTRUCTION(ArrayGet);
5845
Artem Serovcced8ba2017-07-19 18:18:09 +01005846 protected:
5847 DEFAULT_COPY_CONSTRUCTOR(ArrayGet);
5848
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005849 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005850 // We treat a String as an array, creating the HArrayGet from String.charAt()
5851 // intrinsic in the instruction simplifier. We can always determine whether
5852 // a particular HArrayGet is actually a String.charAt() by looking at the type
5853 // of the input but that requires holding the mutator lock, so we prefer to use
5854 // a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01005855 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005856 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5857 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5858 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005859};
5860
Vladimir Markobd785672018-05-03 17:09:09 +01005861class HArraySet FINAL : public HExpression<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005862 public:
5863 HArraySet(HInstruction* array,
5864 HInstruction* index,
5865 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005866 DataType::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005867 uint32_t dex_pc)
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005868 : HArraySet(array,
5869 index,
5870 value,
5871 expected_component_type,
5872 // Make a best guess for side effects now, may be refined during SSA building.
5873 ComputeSideEffects(GetComponentType(value->GetType(), expected_component_type)),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305874 dex_pc) {
5875 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005876
5877 HArraySet(HInstruction* array,
5878 HInstruction* index,
5879 HInstruction* value,
5880 DataType::Type expected_component_type,
5881 SideEffects side_effects,
5882 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01005883 : HExpression(kArraySet, side_effects, dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005884 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005885 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005886 SetPackedFlag<kFlagValueCanBeNull>(true);
5887 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005888 SetRawInputAt(0, array);
5889 SetRawInputAt(1, index);
5890 SetRawInputAt(2, value);
5891 }
5892
Artem Serovcced8ba2017-07-19 18:18:09 +01005893 bool IsClonable() const OVERRIDE { return true; }
5894
Calin Juravle77520bc2015-01-12 18:45:46 +00005895 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005896 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005897 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005898 }
5899
Mingyao Yang81014cb2015-06-02 03:16:27 -07005900 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005901 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005902
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005903 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005904 // TODO: Same as for ArrayGet.
5905 return false;
5906 }
5907
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005908 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005909 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005910 }
5911
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005912 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005913 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005914 }
5915
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005916 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005917 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005918 }
5919
Vladimir Markoa1de9182016-02-25 11:37:38 +00005920 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5921 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5922 bool StaticTypeOfArrayIsObjectArray() const {
5923 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5924 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005925
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005926 HInstruction* GetArray() const { return InputAt(0); }
5927 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005928 HInstruction* GetValue() const { return InputAt(2); }
5929
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005930 DataType::Type GetComponentType() const {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005931 return GetComponentType(GetValue()->GetType(), GetRawExpectedComponentType());
5932 }
5933
5934 static DataType::Type GetComponentType(DataType::Type value_type,
5935 DataType::Type expected_component_type) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005936 // The Dex format does not type floating point index operations. Since the
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005937 // `expected_component_type` comes from SSA building and can therefore not
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005938 // be correct, we also check what is the value type. If it is a floating
5939 // point type, we must use that type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005940 return ((value_type == DataType::Type::kFloat32) || (value_type == DataType::Type::kFloat64))
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005941 ? value_type
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005942 : expected_component_type;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005943 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005944
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005945 DataType::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005946 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005947 }
5948
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005949 static SideEffects ComputeSideEffects(DataType::Type type) {
5950 return SideEffects::ArrayWriteOfType(type).Union(SideEffectsForArchRuntimeCalls(type));
Aart Bik18b36ab2016-04-13 16:41:35 -07005951 }
5952
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005953 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type value_type) {
5954 return (value_type == DataType::Type::kReference) ? SideEffects::CanTriggerGC()
5955 : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005956 }
5957
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005958 DECLARE_INSTRUCTION(ArraySet);
5959
Artem Serovcced8ba2017-07-19 18:18:09 +01005960 protected:
5961 DEFAULT_COPY_CONSTRUCTOR(ArraySet);
5962
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005963 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005964 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5965 static constexpr size_t kFieldExpectedComponentTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005966 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00005967 static constexpr size_t kFlagNeedsTypeCheck =
5968 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5969 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005970 // Cached information for the reference_type_info_ so that codegen
5971 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005972 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5973 static constexpr size_t kNumberOfArraySetPackedBits =
5974 kFlagStaticTypeOfArrayIsObjectArray + 1;
5975 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5976 using ExpectedComponentTypeField =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005977 BitField<DataType::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005978};
5979
Vladimir Markofcb503c2016-05-18 12:48:17 +01005980class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005981 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005982 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305983 : HExpression(kArrayLength, DataType::Type::kInt32, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005984 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005985 // Note that arrays do not change length, so the instruction does not
5986 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005987 SetRawInputAt(0, array);
5988 }
5989
Artem Serovcced8ba2017-07-19 18:18:09 +01005990 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle77520bc2015-01-12 18:45:46 +00005991 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005992 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005993 return true;
5994 }
Calin Juravle641547a2015-04-21 22:08:51 +01005995 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5996 return obj == InputAt(0);
5997 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005998
Vladimir Markodce016e2016-04-28 13:10:02 +01005999 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
6000
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006001 DECLARE_INSTRUCTION(ArrayLength);
6002
Artem Serovcced8ba2017-07-19 18:18:09 +01006003 protected:
6004 DEFAULT_COPY_CONSTRUCTOR(ArrayLength);
6005
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006006 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01006007 // We treat a String as an array, creating the HArrayLength from String.length()
6008 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
6009 // determine whether a particular HArrayLength is actually a String.length() by
6010 // looking at the type of the input but that requires holding the mutator lock, so
6011 // we prefer to use a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01006012 static constexpr size_t kFlagIsStringLength = kNumberOfGenericPackedBits;
Vladimir Markodce016e2016-04-28 13:10:02 +01006013 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
6014 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6015 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006016};
6017
Vladimir Markofcb503c2016-05-18 12:48:17 +01006018class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006019 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00006020 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
6021 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006022 HBoundsCheck(HInstruction* index,
6023 HInstruction* length,
6024 uint32_t dex_pc,
Vladimir Marko0259c242017-12-04 11:27:47 +00006025 bool is_string_char_at = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306026 : HExpression(kBoundsCheck, index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006027 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(index->GetType()));
Vladimir Marko0259c242017-12-04 11:27:47 +00006028 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006029 SetRawInputAt(0, index);
6030 SetRawInputAt(1, length);
6031 }
6032
Artem Serovcced8ba2017-07-19 18:18:09 +01006033 bool IsClonable() const OVERRIDE { return true; }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006034 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006035 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006036 return true;
6037 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006038
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006039 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006040
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006041 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01006042
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00006043 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006044
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006045 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006046
6047 DECLARE_INSTRUCTION(BoundsCheck);
6048
Artem Serovcced8ba2017-07-19 18:18:09 +01006049 protected:
6050 DEFAULT_COPY_CONSTRUCTOR(BoundsCheck);
6051
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006052 private:
Vladimir Markobd785672018-05-03 17:09:09 +01006053 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006054};
6055
Vladimir Markobd785672018-05-03 17:09:09 +01006056class HSuspendCheck FINAL : public HExpression<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006057 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00006058 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01006059 : HExpression(kSuspendCheck, SideEffects::CanTriggerGC(), dex_pc),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306060 slow_path_(nullptr) {
6061 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006062
Artem Serovcced8ba2017-07-19 18:18:09 +01006063 bool IsClonable() const OVERRIDE { return true; }
6064
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006065 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006066 return true;
6067 }
6068
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006069 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
6070 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006071
6072 DECLARE_INSTRUCTION(SuspendCheck);
6073
Artem Serovcced8ba2017-07-19 18:18:09 +01006074 protected:
6075 DEFAULT_COPY_CONSTRUCTOR(SuspendCheck);
6076
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006077 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006078 // Only used for code generation, in order to share the same slow path between back edges
6079 // of a same loop.
6080 SlowPathCode* slow_path_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006081};
6082
David Srbecky0cf44932015-12-09 14:09:59 +00006083// Pseudo-instruction which provides the native debugger with mapping information.
6084// It ensures that we can generate line number and local variables at this point.
Vladimir Markobd785672018-05-03 17:09:09 +01006085class HNativeDebugInfo : public HExpression<0> {
David Srbecky0cf44932015-12-09 14:09:59 +00006086 public:
6087 explicit HNativeDebugInfo(uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006088 : HExpression<0>(kNativeDebugInfo, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306089 }
David Srbecky0cf44932015-12-09 14:09:59 +00006090
6091 bool NeedsEnvironment() const OVERRIDE {
6092 return true;
6093 }
6094
6095 DECLARE_INSTRUCTION(NativeDebugInfo);
6096
Artem Serovcced8ba2017-07-19 18:18:09 +01006097 protected:
6098 DEFAULT_COPY_CONSTRUCTOR(NativeDebugInfo);
David Srbecky0cf44932015-12-09 14:09:59 +00006099};
6100
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006101/**
6102 * Instruction to load a Class object.
6103 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006104class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006105 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006106 // Determines how to load the Class.
6107 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006108 // We cannot load this class. See HSharpening::SharpenLoadClass.
6109 kInvalid = -1,
6110
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006111 // Use the Class* from the method's own ArtMethod*.
6112 kReferrersClass,
6113
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006114 // Use PC-relative boot image Class* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006115 // Used for boot image classes referenced by boot image code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006116 kBootImageLinkTimePcRelative,
6117
6118 // Use a known boot image Class* address, embedded in the code by the codegen.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006119 // Used for boot image classes referenced by apps in JIT- and AOT-compiled code (non-PIC).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006120 kBootImageAddress,
6121
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006122 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
6123 // Used for boot image classes referenced by apps in AOT-compiled code (PIC).
6124 kBootImageRelRo,
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006125
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006126 // Load from an entry in the .bss section using a PC-relative load.
6127 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
6128 kBssEntry,
6129
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006130 // Load from the root table associated with the JIT compiled method.
6131 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006132
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006133 // Load using a simple runtime call. This is the fall-back load kind when
6134 // the codegen is unable to use another appropriate kind.
6135 kRuntimeCall,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006136
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006137 kLast = kRuntimeCall
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006138 };
6139
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006140 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08006141 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006142 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006143 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006144 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01006145 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00006146 bool needs_access_check)
Vladimir Markobd785672018-05-03 17:09:09 +01006147 : HInstruction(kLoadClass,
6148 DataType::Type::kReference,
6149 SideEffectsForArchRuntimeCalls(),
6150 dex_pc),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006151 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006152 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006153 dex_file_(dex_file),
Vladimir Marko175e7862018-03-27 09:03:13 +00006154 klass_(klass) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01006155 // Referrers class should not need access check. We never inline unverified
6156 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006157 DCHECK(!is_referrers_class || !needs_access_check);
6158
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006159 SetPackedField<LoadKindField>(
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006160 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kRuntimeCall);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006161 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00006162 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006163 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Marko175e7862018-03-27 09:03:13 +00006164 SetPackedFlag<kFlagValidLoadedClassRTI>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006165 }
6166
Artem Serovcced8ba2017-07-19 18:18:09 +01006167 bool IsClonable() const OVERRIDE { return true; }
6168
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006169 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006170
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006171 LoadKind GetLoadKind() const {
6172 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006173 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006174
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006175 bool HasPcRelativeLoadKind() const {
6176 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6177 GetLoadKind() == LoadKind::kBootImageRelRo ||
6178 GetLoadKind() == LoadKind::kBssEntry;
6179 }
6180
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006181 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006182
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006183 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006184
Andreas Gampea5b09a62016-11-17 15:21:22 -08006185 size_t ComputeHashCode() const OVERRIDE { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006186
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01006187 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006188
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006189 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006190 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006191 }
6192
Calin Juravle0ba218d2015-05-19 18:46:01 +01006193 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00006194 // The entrypoint the code generator is going to call does not do
6195 // clinit of the class.
6196 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00006197 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006198 }
6199
6200 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00006201 return NeedsAccessCheck() ||
6202 MustGenerateClinitCheck() ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006203 GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006204 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01006205 }
6206
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006207 bool CanThrow() const OVERRIDE {
Vladimir Marko41559982017-01-06 14:04:23 +00006208 return NeedsAccessCheck() ||
6209 MustGenerateClinitCheck() ||
6210 // If the class is in the boot image, the lookup in the runtime call cannot throw.
6211 // This keeps CanThrow() consistent between non-PIC (using kBootImageAddress) and
6212 // PIC and subsequently avoids a DCE behavior dependency on the PIC option.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006213 ((GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006214 GetLoadKind() == LoadKind::kBssEntry) &&
6215 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006216 }
6217
Calin Juravleacf735c2015-02-12 15:25:22 +00006218 ReferenceTypeInfo GetLoadedClassRTI() {
Vladimir Marko175e7862018-03-27 09:03:13 +00006219 if (GetPackedFlag<kFlagValidLoadedClassRTI>()) {
6220 // Note: The is_exact flag from the return value should not be used.
6221 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact */ true);
6222 } else {
6223 return ReferenceTypeInfo::CreateInvalid();
6224 }
Calin Juravleacf735c2015-02-12 15:25:22 +00006225 }
6226
Vladimir Marko175e7862018-03-27 09:03:13 +00006227 // Loaded class RTI is marked as valid by RTP if the klass_ is admissible.
6228 void SetValidLoadedClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
6229 DCHECK(klass_ != nullptr);
6230 SetPackedFlag<kFlagValidLoadedClassRTI>(true);
Calin Juravleacf735c2015-02-12 15:25:22 +00006231 }
6232
Andreas Gampea5b09a62016-11-17 15:21:22 -08006233 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006234 const DexFile& GetDexFile() const { return dex_file_; }
6235
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006236 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006237 return GetLoadKind() == LoadKind::kRuntimeCall;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006238 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00006239
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006240 static SideEffects SideEffectsForArchRuntimeCalls() {
6241 return SideEffects::CanTriggerGC();
6242 }
6243
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006244 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006245 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006246 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006247 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006248
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006249 void MarkInBootImage() {
6250 SetPackedFlag<kFlagIsInBootImage>(true);
6251 }
6252
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006253 void AddSpecialInput(HInstruction* special_input);
6254
6255 using HInstruction::GetInputRecords; // Keep the const version visible.
6256 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
6257 return ArrayRef<HUserRecord<HInstruction*>>(
6258 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6259 }
6260
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006261 Handle<mirror::Class> GetClass() const {
6262 return klass_;
6263 }
6264
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006265 DECLARE_INSTRUCTION(LoadClass);
6266
Artem Serovcced8ba2017-07-19 18:18:09 +01006267 protected:
6268 DEFAULT_COPY_CONSTRUCTOR(LoadClass);
6269
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006270 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006271 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00006272 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006273 // Whether this instruction must generate the initialization check.
6274 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006275 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006276 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
6277 static constexpr size_t kFieldLoadKindSize =
6278 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
Vladimir Marko175e7862018-03-27 09:03:13 +00006279 static constexpr size_t kFlagValidLoadedClassRTI = kFieldLoadKind + kFieldLoadKindSize;
6280 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagValidLoadedClassRTI + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006281 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006282 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
6283
6284 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006285 return load_kind == LoadKind::kReferrersClass ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006286 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006287 load_kind == LoadKind::kBssEntry ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006288 load_kind == LoadKind::kRuntimeCall;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006289 }
6290
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006291 void SetLoadKindInternal(LoadKind load_kind);
6292
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006293 // The special input is the HCurrentMethod for kRuntimeCall or kReferrersClass.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006294 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006295 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006296 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006297
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006298 // A type index and dex file where the class can be accessed. The dex file can be:
6299 // - The compiling method's dex file if the class is defined there too.
6300 // - The compiling method's dex file if the class is referenced there.
6301 // - The dex file where the class is defined. When the load kind can only be
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006302 // kBssEntry or kRuntimeCall, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08006303 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006304 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006305
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006306 Handle<mirror::Class> klass_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006307};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006308std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
6309
6310// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006311inline void HLoadClass::SetLoadKind(LoadKind load_kind) {
6312 // The load kind should be determined before inserting the instruction to the graph.
6313 DCHECK(GetBlock() == nullptr);
6314 DCHECK(GetEnvironment() == nullptr);
6315 SetPackedField<LoadKindField>(load_kind);
6316 if (load_kind != LoadKind::kRuntimeCall && load_kind != LoadKind::kReferrersClass) {
6317 special_input_ = HUserRecord<HInstruction*>(nullptr);
6318 }
6319 if (!NeedsEnvironment()) {
6320 SetSideEffects(SideEffects::None());
6321 }
6322}
6323
6324// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006325inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006326 // The special input is used for PC-relative loads on some architectures,
6327 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006328 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006329 GetLoadKind() == LoadKind::kBootImageAddress ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006330 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006331 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006332 DCHECK(special_input_.GetInstruction() == nullptr);
6333 special_input_ = HUserRecord<HInstruction*>(special_input);
6334 special_input->AddUseAt(this, 0);
6335}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006336
Vladimir Marko372f10e2016-05-17 16:30:10 +01006337class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006338 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006339 // Determines how to load the String.
6340 enum class LoadKind {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006341 // Use PC-relative boot image String* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006342 // Used for boot image strings referenced by boot image code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006343 kBootImageLinkTimePcRelative,
6344
6345 // Use a known boot image String* address, embedded in the code by the codegen.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006346 // Used for boot image strings referenced by apps in JIT- and AOT-compiled code (non-PIC).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006347 kBootImageAddress,
6348
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006349 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
6350 // Used for boot image strings referenced by apps in AOT-compiled code (PIC).
6351 kBootImageRelRo,
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006352
Vladimir Markoaad75c62016-10-03 08:46:48 +00006353 // Load from an entry in the .bss section using a PC-relative load.
6354 // Used for strings outside boot image when .bss is accessible with a PC-relative load.
6355 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006356
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006357 // Load from the root table associated with the JIT compiled method.
6358 kJitTableAddress,
6359
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006360 // Load using a simple runtime call. This is the fall-back load kind when
6361 // the codegen is unable to use another appropriate kind.
6362 kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006363
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006364 kLast = kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006365 };
6366
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006367 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006368 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006369 const DexFile& dex_file,
6370 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006371 : HInstruction(kLoadString,
6372 DataType::Type::kReference,
6373 SideEffectsForArchRuntimeCalls(),
6374 dex_pc),
Vladimir Marko372f10e2016-05-17 16:30:10 +01006375 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006376 string_index_(string_index),
6377 dex_file_(dex_file) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006378 SetPackedField<LoadKindField>(LoadKind::kRuntimeCall);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006379 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006380
Artem Serovcced8ba2017-07-19 18:18:09 +01006381 bool IsClonable() const OVERRIDE { return true; }
6382
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006383 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006384
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006385 LoadKind GetLoadKind() const {
6386 return GetPackedField<LoadKindField>();
6387 }
6388
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006389 bool HasPcRelativeLoadKind() const {
6390 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6391 GetLoadKind() == LoadKind::kBootImageRelRo ||
6392 GetLoadKind() == LoadKind::kBssEntry;
6393 }
6394
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006395 const DexFile& GetDexFile() const {
6396 return dex_file_;
6397 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006398
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006399 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006400 return string_index_;
6401 }
6402
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006403 Handle<mirror::String> GetString() const {
6404 return string_;
6405 }
6406
6407 void SetString(Handle<mirror::String> str) {
6408 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006409 }
6410
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006411 bool CanBeMoved() const OVERRIDE { return true; }
6412
Vladimir Marko372f10e2016-05-17 16:30:10 +01006413 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006414
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006415 size_t ComputeHashCode() const OVERRIDE { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006416
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006417 // Will call the runtime if we need to load the string through
6418 // the dex cache and the string is not guaranteed to be there yet.
6419 bool NeedsEnvironment() const OVERRIDE {
6420 LoadKind load_kind = GetLoadKind();
Vladimir Marko764d4542017-05-16 10:31:41 +01006421 if (load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006422 load_kind == LoadKind::kBootImageAddress ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006423 load_kind == LoadKind::kBootImageRelRo ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006424 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006425 return false;
6426 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006427 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006428 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006429
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006430 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006431 return GetLoadKind() == LoadKind::kRuntimeCall;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006432 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006433
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07006434 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006435 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006436
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006437 static SideEffects SideEffectsForArchRuntimeCalls() {
6438 return SideEffects::CanTriggerGC();
6439 }
6440
Vladimir Marko372f10e2016-05-17 16:30:10 +01006441 void AddSpecialInput(HInstruction* special_input);
6442
6443 using HInstruction::GetInputRecords; // Keep the const version visible.
6444 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
6445 return ArrayRef<HUserRecord<HInstruction*>>(
6446 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006447 }
6448
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006449 DECLARE_INSTRUCTION(LoadString);
6450
Artem Serovcced8ba2017-07-19 18:18:09 +01006451 protected:
6452 DEFAULT_COPY_CONSTRUCTOR(LoadString);
6453
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006454 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006455 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006456 static constexpr size_t kFieldLoadKindSize =
6457 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
6458 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006459 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006460 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006461
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006462 void SetLoadKindInternal(LoadKind load_kind);
6463
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006464 // The special input is the HCurrentMethod for kRuntimeCall.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006465 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00006466 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01006467 HUserRecord<HInstruction*> special_input_;
6468
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006469 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006470 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006471
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006472 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006473};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006474std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
6475
6476// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006477inline void HLoadString::SetLoadKind(LoadKind load_kind) {
6478 // The load kind should be determined before inserting the instruction to the graph.
6479 DCHECK(GetBlock() == nullptr);
6480 DCHECK(GetEnvironment() == nullptr);
6481 DCHECK_EQ(GetLoadKind(), LoadKind::kRuntimeCall);
6482 SetPackedField<LoadKindField>(load_kind);
6483 if (load_kind != LoadKind::kRuntimeCall) {
6484 special_input_ = HUserRecord<HInstruction*>(nullptr);
6485 }
6486 if (!NeedsEnvironment()) {
6487 SetSideEffects(SideEffects::None());
6488 }
6489}
6490
6491// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006492inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006493 // The special input is used for PC-relative loads on some architectures,
6494 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006495 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006496 GetLoadKind() == LoadKind::kBootImageAddress ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006497 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006498 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01006499 // HLoadString::GetInputRecords() returns an empty array at this point,
6500 // so use the GetInputRecords() from the base class to set the input record.
6501 DCHECK(special_input_.GetInstruction() == nullptr);
6502 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006503 special_input->AddUseAt(this, 0);
6504}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006505
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006506/**
6507 * Performs an initialization check on its Class object input.
6508 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01006509class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006510 public:
Roland Levillain3887c462015-08-12 18:15:42 +01006511 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07006512 : HExpression(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306513 kClinitCheck,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006514 DataType::Type::kReference,
Mingyao Yang217eb062017-12-11 15:20:07 -08006515 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006516 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006517 SetRawInputAt(0, constant);
6518 }
6519
Artem Serovcced8ba2017-07-19 18:18:09 +01006520 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006521 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006522 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006523 return true;
6524 }
6525
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006526 bool NeedsEnvironment() const OVERRIDE {
6527 // May call runtime to initialize the class.
6528 return true;
6529 }
6530
Nicolas Geoffray729645a2015-11-19 13:29:02 +00006531 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006532
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006533 HLoadClass* GetLoadClass() const {
6534 DCHECK(InputAt(0)->IsLoadClass());
6535 return InputAt(0)->AsLoadClass();
6536 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006537
6538 DECLARE_INSTRUCTION(ClinitCheck);
6539
Artem Serovcced8ba2017-07-19 18:18:09 +01006540
6541 protected:
6542 DEFAULT_COPY_CONSTRUCTOR(ClinitCheck);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006543};
6544
Vladimir Markofcb503c2016-05-18 12:48:17 +01006545class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006546 public:
6547 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006548 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006549 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006550 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006551 bool is_volatile,
6552 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006553 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006554 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006555 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306556 : HExpression(kStaticFieldGet,
6557 field_type,
6558 SideEffects::FieldReadOfType(field_type, is_volatile),
6559 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006560 field_info_(field,
6561 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006562 field_type,
6563 is_volatile,
6564 field_idx,
6565 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006566 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006567 SetRawInputAt(0, cls);
6568 }
6569
Calin Juravle52c48962014-12-16 17:02:57 +00006570
Artem Serovcced8ba2017-07-19 18:18:09 +01006571 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle10c9cbe2014-12-19 10:50:19 +00006572 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006573
Vladimir Marko372f10e2016-05-17 16:30:10 +01006574 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
6575 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006576 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006577 }
6578
6579 size_t ComputeHashCode() const OVERRIDE {
6580 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6581 }
6582
Calin Juravle52c48962014-12-16 17:02:57 +00006583 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006584 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006585 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006586 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006587
Vladimir Marko61b92282017-10-11 13:23:17 +01006588 void SetType(DataType::Type new_type) {
6589 DCHECK(DataType::IsIntegralType(GetType()));
6590 DCHECK(DataType::IsIntegralType(new_type));
6591 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6592 SetPackedField<TypeField>(new_type);
6593 }
6594
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006595 DECLARE_INSTRUCTION(StaticFieldGet);
6596
Artem Serovcced8ba2017-07-19 18:18:09 +01006597 protected:
6598 DEFAULT_COPY_CONSTRUCTOR(StaticFieldGet);
6599
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006600 private:
6601 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006602};
6603
Vladimir Markobd785672018-05-03 17:09:09 +01006604class HStaticFieldSet FINAL : public HExpression<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006605 public:
6606 HStaticFieldSet(HInstruction* cls,
6607 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006608 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006609 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006610 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006611 bool is_volatile,
6612 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006613 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006614 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006615 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006616 : HExpression(kStaticFieldSet,
6617 SideEffects::FieldWriteOfType(field_type, is_volatile),
6618 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006619 field_info_(field,
6620 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006621 field_type,
6622 is_volatile,
6623 field_idx,
6624 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006625 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006626 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006627 SetRawInputAt(0, cls);
6628 SetRawInputAt(1, value);
6629 }
6630
Artem Serovcced8ba2017-07-19 18:18:09 +01006631 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle52c48962014-12-16 17:02:57 +00006632 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006633 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006634 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006635 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006636
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006637 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006638 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6639 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006640
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006641 DECLARE_INSTRUCTION(StaticFieldSet);
6642
Artem Serovcced8ba2017-07-19 18:18:09 +01006643 protected:
6644 DEFAULT_COPY_CONSTRUCTOR(StaticFieldSet);
6645
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006646 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006647 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6648 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6649 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6650 "Too many packed fields.");
6651
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006652 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006653};
6654
Vladimir Markofcb503c2016-05-18 12:48:17 +01006655class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006656 public:
6657 HUnresolvedInstanceFieldGet(HInstruction* obj,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006658 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006659 uint32_t field_index,
6660 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306661 : HExpression(kUnresolvedInstanceFieldGet,
6662 field_type,
6663 SideEffects::AllExceptGCDependency(),
6664 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006665 field_index_(field_index) {
6666 SetRawInputAt(0, obj);
6667 }
6668
Artem Serovcced8ba2017-07-19 18:18:09 +01006669 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006670 bool NeedsEnvironment() const OVERRIDE { return true; }
6671 bool CanThrow() const OVERRIDE { return true; }
6672
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006673 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006674 uint32_t GetFieldIndex() const { return field_index_; }
6675
6676 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6677
Artem Serovcced8ba2017-07-19 18:18:09 +01006678 protected:
6679 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldGet);
6680
Calin Juravlee460d1d2015-09-29 04:52:17 +01006681 private:
6682 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006683};
6684
Vladimir Markobd785672018-05-03 17:09:09 +01006685class HUnresolvedInstanceFieldSet FINAL : public HExpression<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006686 public:
6687 HUnresolvedInstanceFieldSet(HInstruction* obj,
6688 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006689 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006690 uint32_t field_index,
6691 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006692 : HExpression(kUnresolvedInstanceFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006693 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006694 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006695 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006696 SetRawInputAt(0, obj);
6697 SetRawInputAt(1, value);
6698 }
6699
Artem Serovcced8ba2017-07-19 18:18:09 +01006700 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006701 bool NeedsEnvironment() const OVERRIDE { return true; }
6702 bool CanThrow() const OVERRIDE { return true; }
6703
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006704 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006705 uint32_t GetFieldIndex() const { return field_index_; }
6706
6707 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6708
Artem Serovcced8ba2017-07-19 18:18:09 +01006709 protected:
6710 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldSet);
6711
Calin Juravlee460d1d2015-09-29 04:52:17 +01006712 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006713 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6714 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006715 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006716 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6717 kFieldFieldType + kFieldFieldTypeSize;
6718 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6719 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006720 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006721
Calin Juravlee460d1d2015-09-29 04:52:17 +01006722 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006723};
6724
Vladimir Markofcb503c2016-05-18 12:48:17 +01006725class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006726 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006727 HUnresolvedStaticFieldGet(DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006728 uint32_t field_index,
6729 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306730 : HExpression(kUnresolvedStaticFieldGet,
6731 field_type,
6732 SideEffects::AllExceptGCDependency(),
6733 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006734 field_index_(field_index) {
6735 }
6736
Artem Serovcced8ba2017-07-19 18:18:09 +01006737 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006738 bool NeedsEnvironment() const OVERRIDE { return true; }
6739 bool CanThrow() const OVERRIDE { return true; }
6740
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006741 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006742 uint32_t GetFieldIndex() const { return field_index_; }
6743
6744 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6745
Artem Serovcced8ba2017-07-19 18:18:09 +01006746 protected:
6747 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldGet);
6748
Calin Juravlee460d1d2015-09-29 04:52:17 +01006749 private:
6750 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006751};
6752
Vladimir Markobd785672018-05-03 17:09:09 +01006753class HUnresolvedStaticFieldSet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006754 public:
6755 HUnresolvedStaticFieldSet(HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006756 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006757 uint32_t field_index,
6758 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006759 : HExpression(kUnresolvedStaticFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006760 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006761 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006762 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006763 SetRawInputAt(0, value);
6764 }
6765
Artem Serovcced8ba2017-07-19 18:18:09 +01006766 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006767 bool NeedsEnvironment() const OVERRIDE { return true; }
6768 bool CanThrow() const OVERRIDE { return true; }
6769
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006770 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006771 uint32_t GetFieldIndex() const { return field_index_; }
6772
6773 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6774
Artem Serovcced8ba2017-07-19 18:18:09 +01006775 protected:
6776 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldSet);
6777
Calin Juravlee460d1d2015-09-29 04:52:17 +01006778 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006779 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6780 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006781 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006782 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6783 kFieldFieldType + kFieldFieldTypeSize;
6784 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6785 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006786 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006787
Calin Juravlee460d1d2015-09-29 04:52:17 +01006788 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006789};
6790
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006791// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006792class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006793 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006794 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306795 : HExpression(kLoadException, DataType::Type::kReference, SideEffects::None(), dex_pc) {
6796 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006797
David Brazdilbbd733e2015-08-18 17:48:17 +01006798 bool CanBeNull() const OVERRIDE { return false; }
6799
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006800 DECLARE_INSTRUCTION(LoadException);
6801
Artem Serovcced8ba2017-07-19 18:18:09 +01006802 protected:
6803 DEFAULT_COPY_CONSTRUCTOR(LoadException);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006804};
6805
David Brazdilcb1c0552015-08-04 16:22:25 +01006806// Implicit part of move-exception which clears thread-local exception storage.
6807// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markobd785672018-05-03 17:09:09 +01006808class HClearException FINAL : public HExpression<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006809 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006810 explicit HClearException(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01006811 : HExpression(kClearException, SideEffects::AllWrites(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306812 }
David Brazdilcb1c0552015-08-04 16:22:25 +01006813
6814 DECLARE_INSTRUCTION(ClearException);
6815
Artem Serovcced8ba2017-07-19 18:18:09 +01006816 protected:
6817 DEFAULT_COPY_CONSTRUCTOR(ClearException);
David Brazdilcb1c0552015-08-04 16:22:25 +01006818};
6819
Vladimir Markobd785672018-05-03 17:09:09 +01006820class HThrow FINAL : public HExpression<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006821 public:
6822 HThrow(HInstruction* exception, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006823 : HExpression(kThrow, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006824 SetRawInputAt(0, exception);
6825 }
6826
6827 bool IsControlFlow() const OVERRIDE { return true; }
6828
6829 bool NeedsEnvironment() const OVERRIDE { return true; }
6830
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006831 bool CanThrow() const OVERRIDE { return true; }
6832
Aart Bika8b8e9b2018-01-09 11:01:02 -08006833 bool AlwaysThrows() const OVERRIDE { return true; }
6834
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006835 DECLARE_INSTRUCTION(Throw);
6836
Artem Serovcced8ba2017-07-19 18:18:09 +01006837 protected:
6838 DEFAULT_COPY_CONSTRUCTOR(Throw);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006839};
6840
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006841/**
6842 * Implementation strategies for the code generator of a HInstanceOf
6843 * or `HCheckCast`.
6844 */
6845enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006846 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006847 kExactCheck, // Can do a single class compare.
6848 kClassHierarchyCheck, // Can just walk the super class chain.
6849 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6850 kInterfaceCheck, // No optimization yet when checking against an interface.
6851 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006852 kArrayCheck, // No optimization yet when checking against a generic array.
Vladimir Marko175e7862018-03-27 09:03:13 +00006853 kBitstringCheck, // Compare the type check bitstring.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006854 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006855};
6856
Roland Levillain86503782016-02-11 19:07:30 +00006857std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6858
Vladimir Marko175e7862018-03-27 09:03:13 +00006859// Note: HTypeCheckInstruction is just a helper class, not an abstract instruction with an
6860// `IsTypeCheckInstruction()`. (New virtual methods in the HInstruction class have a high cost.)
6861class HTypeCheckInstruction : public HVariableInputSizeInstruction {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006862 public:
Vladimir Marko175e7862018-03-27 09:03:13 +00006863 HTypeCheckInstruction(InstructionKind kind,
Vladimir Markobd785672018-05-03 17:09:09 +01006864 DataType::Type type,
Vladimir Marko175e7862018-03-27 09:03:13 +00006865 HInstruction* object,
6866 HInstruction* target_class_or_null,
6867 TypeCheckKind check_kind,
6868 Handle<mirror::Class> klass,
6869 uint32_t dex_pc,
6870 ArenaAllocator* allocator,
6871 HIntConstant* bitstring_path_to_root,
6872 HIntConstant* bitstring_mask,
6873 SideEffects side_effects)
6874 : HVariableInputSizeInstruction(
6875 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01006876 type,
Vladimir Marko175e7862018-03-27 09:03:13 +00006877 side_effects,
6878 dex_pc,
6879 allocator,
6880 /* number_of_inputs */ check_kind == TypeCheckKind::kBitstringCheck ? 4u : 2u,
6881 kArenaAllocTypeCheckInputs),
6882 klass_(klass) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006883 SetPackedField<TypeCheckKindField>(check_kind);
6884 SetPackedFlag<kFlagMustDoNullCheck>(true);
Vladimir Marko175e7862018-03-27 09:03:13 +00006885 SetPackedFlag<kFlagValidTargetClassRTI>(false);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006886 SetRawInputAt(0, object);
Vladimir Marko175e7862018-03-27 09:03:13 +00006887 SetRawInputAt(1, target_class_or_null);
6888 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_path_to_root != nullptr);
6889 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_mask != nullptr);
6890 if (check_kind == TypeCheckKind::kBitstringCheck) {
6891 DCHECK(target_class_or_null->IsNullConstant());
6892 SetRawInputAt(2, bitstring_path_to_root);
6893 SetRawInputAt(3, bitstring_mask);
6894 } else {
6895 DCHECK(target_class_or_null->IsLoadClass());
6896 }
Vladimir Marko87584542017-12-12 17:47:52 +00006897 }
6898
6899 HLoadClass* GetTargetClass() const {
Vladimir Marko175e7862018-03-27 09:03:13 +00006900 DCHECK_NE(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
Vladimir Marko87584542017-12-12 17:47:52 +00006901 HInstruction* load_class = InputAt(1);
6902 DCHECK(load_class->IsLoadClass());
6903 return load_class->AsLoadClass();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006904 }
6905
Vladimir Marko175e7862018-03-27 09:03:13 +00006906 uint32_t GetBitstringPathToRoot() const {
6907 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
6908 HInstruction* path_to_root = InputAt(2);
6909 DCHECK(path_to_root->IsIntConstant());
6910 return static_cast<uint32_t>(path_to_root->AsIntConstant()->GetValue());
6911 }
6912
6913 uint32_t GetBitstringMask() const {
6914 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
6915 HInstruction* mask = InputAt(3);
6916 DCHECK(mask->IsIntConstant());
6917 return static_cast<uint32_t>(mask->AsIntConstant()->GetValue());
6918 }
6919
Artem Serovcced8ba2017-07-19 18:18:09 +01006920 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006921 bool CanBeMoved() const OVERRIDE { return true; }
6922
Vladimir Marko175e7862018-03-27 09:03:13 +00006923 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
6924 DCHECK(other->IsInstanceOf() || other->IsCheckCast()) << other->DebugName();
6925 return GetPackedFields() == down_cast<const HTypeCheckInstruction*>(other)->GetPackedFields();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006926 }
6927
Andreas Gampe3fbd3ad2018-03-26 21:14:46 +00006928 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6929 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6930 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6931 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
6932
Vladimir Marko175e7862018-03-27 09:03:13 +00006933 ReferenceTypeInfo GetTargetClassRTI() {
6934 if (GetPackedFlag<kFlagValidTargetClassRTI>()) {
6935 // Note: The is_exact flag from the return value should not be used.
6936 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact */ true);
6937 } else {
6938 return ReferenceTypeInfo::CreateInvalid();
6939 }
6940 }
6941
6942 // Target class RTI is marked as valid by RTP if the klass_ is admissible.
6943 void SetValidTargetClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
6944 DCHECK(klass_ != nullptr);
6945 SetPackedFlag<kFlagValidTargetClassRTI>(true);
6946 }
6947
6948 Handle<mirror::Class> GetClass() const {
6949 return klass_;
6950 }
6951
6952 protected:
6953 DEFAULT_COPY_CONSTRUCTOR(TypeCheckInstruction);
6954
6955 private:
6956 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6957 static constexpr size_t kFieldTypeCheckKindSize =
6958 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6959 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6960 static constexpr size_t kFlagValidTargetClassRTI = kFlagMustDoNullCheck + 1;
6961 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagValidTargetClassRTI + 1;
6962 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6963 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
6964
6965 Handle<mirror::Class> klass_;
6966};
6967
6968class HInstanceOf FINAL : public HTypeCheckInstruction {
6969 public:
6970 HInstanceOf(HInstruction* object,
6971 HInstruction* target_class_or_null,
6972 TypeCheckKind check_kind,
6973 Handle<mirror::Class> klass,
6974 uint32_t dex_pc,
6975 ArenaAllocator* allocator,
6976 HIntConstant* bitstring_path_to_root,
6977 HIntConstant* bitstring_mask)
6978 : HTypeCheckInstruction(kInstanceOf,
Vladimir Markobd785672018-05-03 17:09:09 +01006979 DataType::Type::kBool,
Vladimir Marko175e7862018-03-27 09:03:13 +00006980 object,
6981 target_class_or_null,
6982 check_kind,
6983 klass,
6984 dex_pc,
6985 allocator,
6986 bitstring_path_to_root,
6987 bitstring_mask,
6988 SideEffectsForArchRuntimeCalls(check_kind)) {}
6989
Vladimir Marko175e7862018-03-27 09:03:13 +00006990 bool NeedsEnvironment() const OVERRIDE {
6991 return CanCallRuntime(GetTypeCheckKind());
6992 }
6993
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006994 static bool CanCallRuntime(TypeCheckKind check_kind) {
6995 // Mips currently does runtime calls for any other checks.
6996 return check_kind != TypeCheckKind::kExactCheck;
6997 }
6998
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006999 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007000 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01007001 }
7002
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007003 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007004
Artem Serovcced8ba2017-07-19 18:18:09 +01007005 protected:
7006 DEFAULT_COPY_CONSTRUCTOR(InstanceOf);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007007};
7008
Vladimir Markofcb503c2016-05-18 12:48:17 +01007009class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00007010 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07007011 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307012 : HExpression(kBoundType, DataType::Type::kReference, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00007013 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
7014 SetPackedFlag<kFlagUpperCanBeNull>(true);
7015 SetPackedFlag<kFlagCanBeNull>(true);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007016 DCHECK_EQ(input->GetType(), DataType::Type::kReference);
Calin Juravleb1498f62015-02-16 13:13:29 +00007017 SetRawInputAt(0, input);
7018 }
7019
Artem Serovcced8ba2017-07-19 18:18:09 +01007020 bool IsClonable() const OVERRIDE { return true; }
7021
David Brazdilf5552582015-12-27 13:36:12 +00007022 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007023 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00007024 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00007025 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007026
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007027 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007028 DCHECK(GetUpperCanBeNull() || !can_be_null);
7029 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007030 }
7031
Vladimir Markoa1de9182016-02-25 11:37:38 +00007032 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007033
Calin Juravleb1498f62015-02-16 13:13:29 +00007034 DECLARE_INSTRUCTION(BoundType);
7035
Artem Serovcced8ba2017-07-19 18:18:09 +01007036 protected:
7037 DEFAULT_COPY_CONSTRUCTOR(BoundType);
7038
Calin Juravleb1498f62015-02-16 13:13:29 +00007039 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007040 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
7041 // is false then CanBeNull() cannot be true).
Vladimir Markobd785672018-05-03 17:09:09 +01007042 static constexpr size_t kFlagUpperCanBeNull = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007043 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
7044 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
7045 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7046
Calin Juravleb1498f62015-02-16 13:13:29 +00007047 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007048 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
7049 // It is used to bound the type in cases like:
7050 // if (x instanceof ClassX) {
7051 // // uper_bound_ will be ClassX
7052 // }
David Brazdilf5552582015-12-27 13:36:12 +00007053 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00007054};
7055
Vladimir Marko175e7862018-03-27 09:03:13 +00007056class HCheckCast FINAL : public HTypeCheckInstruction {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007057 public:
7058 HCheckCast(HInstruction* object,
Vladimir Marko175e7862018-03-27 09:03:13 +00007059 HInstruction* target_class_or_null,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007060 TypeCheckKind check_kind,
Vladimir Marko175e7862018-03-27 09:03:13 +00007061 Handle<mirror::Class> klass,
7062 uint32_t dex_pc,
7063 ArenaAllocator* allocator,
7064 HIntConstant* bitstring_path_to_root,
7065 HIntConstant* bitstring_mask)
7066 : HTypeCheckInstruction(kCheckCast,
Vladimir Markobd785672018-05-03 17:09:09 +01007067 DataType::Type::kVoid,
Vladimir Marko175e7862018-03-27 09:03:13 +00007068 object,
7069 target_class_or_null,
7070 check_kind,
7071 klass,
7072 dex_pc,
7073 allocator,
7074 bitstring_path_to_root,
7075 bitstring_mask,
7076 SideEffects::CanTriggerGC()) {}
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007077
7078 bool NeedsEnvironment() const OVERRIDE {
7079 // Instruction may throw a CheckCastError.
7080 return true;
7081 }
7082
7083 bool CanThrow() const OVERRIDE { return true; }
7084
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007085 DECLARE_INSTRUCTION(CheckCast);
7086
Artem Serovcced8ba2017-07-19 18:18:09 +01007087 protected:
7088 DEFAULT_COPY_CONSTRUCTOR(CheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007089};
7090
Andreas Gampe26de38b2016-07-27 17:53:11 -07007091/**
7092 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
7093 * @details We define the combined barrier types that are actually required
7094 * by the Java Memory Model, rather than using exactly the terminology from
7095 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
7096 * primitives. Note that the JSR-133 cookbook generally does not deal with
7097 * store atomicity issues, and the recipes there are not always entirely sufficient.
7098 * The current recipe is as follows:
7099 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
7100 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
7101 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
7102 * -# Use StoreStore barrier after all stores but before return from any constructor whose
7103 * class has final fields.
7104 * -# Use NTStoreStore to order non-temporal stores with respect to all later
7105 * store-to-memory instructions. Only generated together with non-temporal stores.
7106 */
7107enum MemBarrierKind {
7108 kAnyStore,
7109 kLoadAny,
7110 kStoreStore,
7111 kAnyAny,
7112 kNTStoreStore,
7113 kLastBarrierKind = kNTStoreStore
7114};
7115std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
7116
Vladimir Markobd785672018-05-03 17:09:09 +01007117class HMemoryBarrier FINAL : public HExpression<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01007118 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007119 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007120 : HExpression(kMemoryBarrier,
7121 SideEffects::AllWritesAndReads(), // Assume write/read on all fields/arrays.
7122 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007123 SetPackedField<BarrierKindField>(barrier_kind);
7124 }
Calin Juravle27df7582015-04-17 19:12:31 +01007125
Artem Serovcced8ba2017-07-19 18:18:09 +01007126 bool IsClonable() const OVERRIDE { return true; }
7127
Vladimir Markoa1de9182016-02-25 11:37:38 +00007128 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01007129
7130 DECLARE_INSTRUCTION(MemoryBarrier);
7131
Artem Serovcced8ba2017-07-19 18:18:09 +01007132 protected:
7133 DEFAULT_COPY_CONSTRUCTOR(MemoryBarrier);
7134
Calin Juravle27df7582015-04-17 19:12:31 +01007135 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007136 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
7137 static constexpr size_t kFieldBarrierKindSize =
7138 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
7139 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
7140 kFieldBarrierKind + kFieldBarrierKindSize;
7141 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
7142 "Too many packed fields.");
7143 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01007144};
7145
Igor Murashkind01745e2017-04-05 16:40:31 -07007146// A constructor fence orders all prior stores to fields that could be accessed via a final field of
7147// the specified object(s), with respect to any subsequent store that might "publish"
7148// (i.e. make visible) the specified object to another thread.
7149//
7150// JLS 17.5.1 "Semantics of final fields" states that a freeze action happens
7151// for all final fields (that were set) at the end of the invoked constructor.
7152//
7153// The constructor fence models the freeze actions for the final fields of an object
7154// being constructed (semantically at the end of the constructor). Constructor fences
7155// have a per-object affinity; two separate objects being constructed get two separate
7156// constructor fences.
7157//
7158// (Note: that if calling a super-constructor or forwarding to another constructor,
7159// the freezes would happen at the end of *that* constructor being invoked).
7160//
7161// The memory model guarantees that when the object being constructed is "published" after
7162// constructor completion (i.e. escapes the current thread via a store), then any final field
7163// writes must be observable on other threads (once they observe that publication).
7164//
7165// Further, anything written before the freeze, and read by dereferencing through the final field,
7166// must also be visible (so final object field could itself have an object with non-final fields;
7167// yet the freeze must also extend to them).
7168//
7169// Constructor example:
7170//
7171// class HasFinal {
7172// final int field; Optimizing IR for <init>()V:
7173// HasFinal() {
7174// field = 123; HInstanceFieldSet(this, HasFinal.field, 123)
7175// // freeze(this.field); HConstructorFence(this)
7176// } HReturn
7177// }
7178//
7179// HConstructorFence can serve double duty as a fence for new-instance/new-array allocations of
7180// already-initialized classes; in that case the allocation must act as a "default-initializer"
7181// of the object which effectively writes the class pointer "final field".
7182//
7183// For example, we can model default-initialiation as roughly the equivalent of the following:
7184//
7185// class Object {
7186// private final Class header;
7187// }
7188//
7189// Java code: Optimizing IR:
7190//
7191// T new_instance<T>() {
7192// Object obj = allocate_memory(T.class.size); obj = HInvoke(art_quick_alloc_object, T)
7193// obj.header = T.class; // header write is done by above call.
7194// // freeze(obj.header) HConstructorFence(obj)
7195// return (T)obj;
7196// }
7197//
7198// See also:
7199// * CompilerDriver::RequiresConstructorBarrier
7200// * QuasiAtomic::ThreadFenceForConstructor
7201//
7202class HConstructorFence FINAL : public HVariableInputSizeInstruction {
7203 // A fence has variable inputs because the inputs can be removed
7204 // after prepare_for_register_allocation phase.
7205 // (TODO: In the future a fence could freeze multiple objects
7206 // after merging two fences together.)
7207 public:
7208 // `fence_object` is the reference that needs to be protected for correct publication.
7209 //
7210 // It makes sense in the following situations:
7211 // * <init> constructors, it's the "this" parameter (i.e. HParameterValue, s.t. IsThis() == true).
7212 // * new-instance-like instructions, it's the return value (i.e. HNewInstance).
7213 //
7214 // After construction the `fence_object` becomes the 0th input.
7215 // This is not an input in a real sense, but just a convenient place to stash the information
7216 // about the associated object.
7217 HConstructorFence(HInstruction* fence_object,
7218 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007219 ArenaAllocator* allocator)
Igor Murashkind01745e2017-04-05 16:40:31 -07007220 // We strongly suspect there is not a more accurate way to describe the fine-grained reordering
7221 // constraints described in the class header. We claim that these SideEffects constraints
7222 // enforce a superset of the real constraints.
7223 //
7224 // The ordering described above is conservatively modeled with SideEffects as follows:
7225 //
7226 // * To prevent reordering of the publication stores:
7227 // ----> "Reads of objects" is the initial SideEffect.
7228 // * For every primitive final field store in the constructor:
7229 // ----> Union that field's type as a read (e.g. "Read of T") into the SideEffect.
7230 // * If there are any stores to reference final fields in the constructor:
7231 // ----> Use a more conservative "AllReads" SideEffect because any stores to any references
7232 // that are reachable from `fence_object` also need to be prevented for reordering
7233 // (and we do not want to do alias analysis to figure out what those stores are).
7234 //
7235 // In the implementation, this initially starts out as an "all reads" side effect; this is an
7236 // even more conservative approach than the one described above, and prevents all of the
7237 // above reordering without analyzing any of the instructions in the constructor.
7238 //
7239 // If in a later phase we discover that there are no writes to reference final fields,
7240 // we can refine the side effect to a smaller set of type reads (see above constraints).
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307241 : HVariableInputSizeInstruction(kConstructorFence,
7242 SideEffects::AllReads(),
Igor Murashkind01745e2017-04-05 16:40:31 -07007243 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007244 allocator,
Igor Murashkind01745e2017-04-05 16:40:31 -07007245 /* number_of_inputs */ 1,
7246 kArenaAllocConstructorFenceInputs) {
7247 DCHECK(fence_object != nullptr);
7248 SetRawInputAt(0, fence_object);
7249 }
7250
7251 // The object associated with this constructor fence.
7252 //
7253 // (Note: This will be null after the prepare_for_register_allocation phase,
7254 // as all constructor fence inputs are removed there).
7255 HInstruction* GetFenceObject() const {
7256 return InputAt(0);
7257 }
7258
7259 // Find all the HConstructorFence uses (`fence_use`) for `this` and:
7260 // - Delete `fence_use` from `this`'s use list.
7261 // - Delete `this` from `fence_use`'s inputs list.
7262 // - If the `fence_use` is dead, remove it from the graph.
7263 //
7264 // A fence is considered dead once it no longer has any uses
7265 // and all of the inputs are dead.
7266 //
7267 // This must *not* be called during/after prepare_for_register_allocation,
7268 // because that removes all the inputs to the fences but the fence is actually
7269 // still considered live.
Igor Murashkin6ef45672017-08-08 13:59:55 -07007270 //
7271 // Returns how many HConstructorFence instructions were removed from graph.
7272 static size_t RemoveConstructorFences(HInstruction* instruction);
Igor Murashkind01745e2017-04-05 16:40:31 -07007273
Igor Murashkindd018df2017-08-09 10:38:31 -07007274 // Combine all inputs of `this` and `other` instruction and remove
7275 // `other` from the graph.
7276 //
7277 // Inputs are unique after the merge.
7278 //
7279 // Requirement: `this` must not be the same as `other.
7280 void Merge(HConstructorFence* other);
7281
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007282 // Check if this constructor fence is protecting
7283 // an HNewInstance or HNewArray that is also the immediate
7284 // predecessor of `this`.
7285 //
Igor Murashkindd018df2017-08-09 10:38:31 -07007286 // If `ignore_inputs` is true, then the immediate predecessor doesn't need
7287 // to be one of the inputs of `this`.
7288 //
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007289 // Returns the associated HNewArray or HNewInstance,
7290 // or null otherwise.
Igor Murashkindd018df2017-08-09 10:38:31 -07007291 HInstruction* GetAssociatedAllocation(bool ignore_inputs = false);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007292
Igor Murashkind01745e2017-04-05 16:40:31 -07007293 DECLARE_INSTRUCTION(ConstructorFence);
7294
Artem Serovcced8ba2017-07-19 18:18:09 +01007295 protected:
7296 DEFAULT_COPY_CONSTRUCTOR(ConstructorFence);
Igor Murashkind01745e2017-04-05 16:40:31 -07007297};
7298
Vladimir Markobd785672018-05-03 17:09:09 +01007299class HMonitorOperation FINAL : public HExpression<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007300 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007301 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007302 kEnter,
7303 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00007304 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007305 };
7306
7307 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007308 : HExpression(kMonitorOperation,
7309 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
7310 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007311 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007312 SetRawInputAt(0, object);
7313 }
7314
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007315 // Instruction may go into runtime, so we need an environment.
7316 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00007317
7318 bool CanThrow() const OVERRIDE {
7319 // Verifier guarantees that monitor-exit cannot throw.
7320 // This is important because it allows the HGraphBuilder to remove
7321 // a dead throw-catch loop generated for `synchronized` blocks/methods.
7322 return IsEnter();
7323 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007324
Vladimir Markoa1de9182016-02-25 11:37:38 +00007325 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
7326 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007327
7328 DECLARE_INSTRUCTION(MonitorOperation);
7329
Artem Serovcced8ba2017-07-19 18:18:09 +01007330 protected:
7331 DEFAULT_COPY_CONSTRUCTOR(MonitorOperation);
7332
Calin Juravle52c48962014-12-16 17:02:57 +00007333 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007334 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
7335 static constexpr size_t kFieldOperationKindSize =
7336 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
7337 static constexpr size_t kNumberOfMonitorOperationPackedBits =
7338 kFieldOperationKind + kFieldOperationKindSize;
7339 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7340 "Too many packed fields.");
7341 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007342};
7343
Vladimir Markofcb503c2016-05-18 12:48:17 +01007344class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00007345 public:
7346 HSelect(HInstruction* condition,
7347 HInstruction* true_value,
7348 HInstruction* false_value,
7349 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307350 : HExpression(kSelect, HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
David Brazdil74eb1b22015-12-14 11:44:01 +00007351 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
7352
7353 // First input must be `true_value` or `false_value` to allow codegens to
7354 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
7355 // that architectures which implement HSelect as a conditional move also
7356 // will not need to invert the condition.
7357 SetRawInputAt(0, false_value);
7358 SetRawInputAt(1, true_value);
7359 SetRawInputAt(2, condition);
7360 }
7361
Artem Serovcced8ba2017-07-19 18:18:09 +01007362 bool IsClonable() const OVERRIDE { return true; }
David Brazdil74eb1b22015-12-14 11:44:01 +00007363 HInstruction* GetFalseValue() const { return InputAt(0); }
7364 HInstruction* GetTrueValue() const { return InputAt(1); }
7365 HInstruction* GetCondition() const { return InputAt(2); }
7366
7367 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01007368 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
7369 return true;
7370 }
David Brazdil74eb1b22015-12-14 11:44:01 +00007371
7372 bool CanBeNull() const OVERRIDE {
7373 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
7374 }
7375
7376 DECLARE_INSTRUCTION(Select);
7377
Artem Serovcced8ba2017-07-19 18:18:09 +01007378 protected:
7379 DEFAULT_COPY_CONSTRUCTOR(Select);
David Brazdil74eb1b22015-12-14 11:44:01 +00007380};
7381
Vladimir Markof9f64412015-09-02 14:05:49 +01007382class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007383 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01007384 MoveOperands(Location source,
7385 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007386 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007387 HInstruction* instruction)
7388 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007389
7390 Location GetSource() const { return source_; }
7391 Location GetDestination() const { return destination_; }
7392
7393 void SetSource(Location value) { source_ = value; }
7394 void SetDestination(Location value) { destination_ = value; }
7395
7396 // The parallel move resolver marks moves as "in-progress" by clearing the
7397 // destination (but not the source).
7398 Location MarkPending() {
7399 DCHECK(!IsPending());
7400 Location dest = destination_;
7401 destination_ = Location::NoLocation();
7402 return dest;
7403 }
7404
7405 void ClearPending(Location dest) {
7406 DCHECK(IsPending());
7407 destination_ = dest;
7408 }
7409
7410 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00007411 DCHECK(source_.IsValid() || destination_.IsInvalid());
7412 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007413 }
7414
7415 // True if this blocks a move from the given location.
7416 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08007417 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007418 }
7419
7420 // A move is redundant if it's been eliminated, if its source and
7421 // destination are the same, or if its destination is unneeded.
7422 bool IsRedundant() const {
7423 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
7424 }
7425
7426 // We clear both operands to indicate move that's been eliminated.
7427 void Eliminate() {
7428 source_ = destination_ = Location::NoLocation();
7429 }
7430
7431 bool IsEliminated() const {
7432 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
7433 return source_.IsInvalid();
7434 }
7435
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007436 DataType::Type GetType() const { return type_; }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007437
Nicolas Geoffray90218252015-04-15 11:56:51 +01007438 bool Is64BitMove() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007439 return DataType::Is64BitType(type_);
Nicolas Geoffray90218252015-04-15 11:56:51 +01007440 }
7441
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007442 HInstruction* GetInstruction() const { return instruction_; }
7443
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007444 private:
7445 Location source_;
7446 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01007447 // The type this move is for.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007448 DataType::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007449 // The instruction this move is assocatied with. Null when this move is
7450 // for moving an input in the expected locations of user (including a phi user).
7451 // This is only used in debug mode, to ensure we do not connect interval siblings
7452 // in the same parallel move.
7453 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007454};
7455
Roland Levillainc9285912015-12-18 10:38:42 +00007456std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
7457
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007458static constexpr size_t kDefaultNumberOfMoves = 4;
7459
Vladimir Markobd785672018-05-03 17:09:09 +01007460class HParallelMove FINAL : public HExpression<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007461 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007462 explicit HParallelMove(ArenaAllocator* allocator, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007463 : HExpression(kParallelMove, SideEffects::None(), dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007464 moves_(allocator->Adapter(kArenaAllocMoveOperands)) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007465 moves_.reserve(kDefaultNumberOfMoves);
7466 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007467
Nicolas Geoffray90218252015-04-15 11:56:51 +01007468 void AddMove(Location source,
7469 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007470 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007471 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00007472 DCHECK(source.IsValid());
7473 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007474 if (kIsDebugBuild) {
7475 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007476 for (const MoveOperands& move : moves_) {
7477 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007478 // Special case the situation where the move is for the spill slot
7479 // of the instruction.
7480 if ((GetPrevious() == instruction)
7481 || ((GetPrevious() == nullptr)
7482 && instruction->IsPhi()
7483 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007484 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007485 << "Doing parallel moves for the same instruction.";
7486 } else {
7487 DCHECK(false) << "Doing parallel moves for the same instruction.";
7488 }
7489 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00007490 }
7491 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007492 for (const MoveOperands& move : moves_) {
7493 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007494 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01007495 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007496 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007497 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007498 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007499 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007500 }
7501
Vladimir Marko225b6462015-09-28 12:17:40 +01007502 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007503 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007504 }
7505
Vladimir Marko225b6462015-09-28 12:17:40 +01007506 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007507
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01007508 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007509
Artem Serovcced8ba2017-07-19 18:18:09 +01007510 protected:
7511 DEFAULT_COPY_CONSTRUCTOR(ParallelMove);
7512
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007513 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01007514 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007515};
7516
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007517// This instruction computes an intermediate address pointing in the 'middle' of an object. The
7518// result pointer cannot be handled by GC, so extra care is taken to make sure that this value is
7519// never used across anything that can trigger GC.
7520// The result of this instruction is not a pointer in the sense of `DataType::Type::kreference`.
7521// So we represent it by the type `DataType::Type::kInt`.
7522class HIntermediateAddress FINAL : public HExpression<2> {
7523 public:
7524 HIntermediateAddress(HInstruction* base_address, HInstruction* offset, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307525 : HExpression(kIntermediateAddress,
7526 DataType::Type::kInt32,
7527 SideEffects::DependsOnGC(),
7528 dex_pc) {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007529 DCHECK_EQ(DataType::Size(DataType::Type::kInt32),
7530 DataType::Size(DataType::Type::kReference))
7531 << "kPrimInt and kPrimNot have different sizes.";
7532 SetRawInputAt(0, base_address);
7533 SetRawInputAt(1, offset);
7534 }
7535
Artem Serovcced8ba2017-07-19 18:18:09 +01007536 bool IsClonable() const OVERRIDE { return true; }
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007537 bool CanBeMoved() const OVERRIDE { return true; }
7538 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
7539 return true;
7540 }
7541 bool IsActualObject() const OVERRIDE { return false; }
7542
7543 HInstruction* GetBaseAddress() const { return InputAt(0); }
7544 HInstruction* GetOffset() const { return InputAt(1); }
7545
7546 DECLARE_INSTRUCTION(IntermediateAddress);
7547
Artem Serovcced8ba2017-07-19 18:18:09 +01007548 protected:
7549 DEFAULT_COPY_CONSTRUCTOR(IntermediateAddress);
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007550};
7551
7552
Mark Mendell0616ae02015-04-17 12:49:27 -04007553} // namespace art
7554
Aart Bikf8f5a162017-02-06 15:35:29 -08007555#include "nodes_vector.h"
7556
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007557#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
7558#include "nodes_shared.h"
7559#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007560#ifdef ART_ENABLE_CODEGEN_mips
7561#include "nodes_mips.h"
7562#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04007563#ifdef ART_ENABLE_CODEGEN_x86
7564#include "nodes_x86.h"
7565#endif
7566
7567namespace art {
7568
Igor Murashkin6ef45672017-08-08 13:59:55 -07007569class OptimizingCompilerStats;
7570
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007571class HGraphVisitor : public ValueObject {
7572 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007573 explicit HGraphVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7574 : stats_(stats),
7575 graph_(graph) {}
Dave Allison20dfc792014-06-16 20:44:29 -07007576 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007577
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007578 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007579 virtual void VisitBasicBlock(HBasicBlock* block);
7580
Roland Levillain633021e2014-10-01 14:12:25 +01007581 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007582 void VisitInsertionOrder();
7583
Roland Levillain633021e2014-10-01 14:12:25 +01007584 // Visit the graph following dominator tree reverse post-order.
7585 void VisitReversePostOrder();
7586
Nicolas Geoffray787c3072014-03-17 10:20:19 +00007587 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007588
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007589 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007590#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007591 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
7592
7593 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7594
7595#undef DECLARE_VISIT_INSTRUCTION
7596
Igor Murashkin6ef45672017-08-08 13:59:55 -07007597 protected:
7598 OptimizingCompilerStats* stats_;
7599
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007600 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07007601 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007602
7603 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
7604};
7605
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007606class HGraphDelegateVisitor : public HGraphVisitor {
7607 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007608 explicit HGraphDelegateVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7609 : HGraphVisitor(graph, stats) {}
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007610 virtual ~HGraphDelegateVisitor() {}
7611
7612 // Visit functions that delegate to to super class.
7613#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00007614 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007615
7616 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7617
7618#undef DECLARE_VISIT_INSTRUCTION
7619
7620 private:
7621 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
7622};
7623
Artem Serovcced8ba2017-07-19 18:18:09 +01007624// Create a clone of the instruction, insert it into the graph; replace the old one with a new
7625// and remove the old instruction.
7626HInstruction* ReplaceInstrOrPhiByClone(HInstruction* instr);
7627
7628// Create a clone for each clonable instructions/phis and replace the original with the clone.
7629//
7630// Used for testing individual instruction cloner.
7631class CloneAndReplaceInstructionVisitor : public HGraphDelegateVisitor {
7632 public:
7633 explicit CloneAndReplaceInstructionVisitor(HGraph* graph)
Artem Serov7f4aff62017-06-21 17:02:18 +01007634 : HGraphDelegateVisitor(graph), instr_replaced_by_clones_count_(0) {}
Artem Serovcced8ba2017-07-19 18:18:09 +01007635
7636 void VisitInstruction(HInstruction* instruction) OVERRIDE {
7637 if (instruction->IsClonable()) {
7638 ReplaceInstrOrPhiByClone(instruction);
Artem Serov7f4aff62017-06-21 17:02:18 +01007639 instr_replaced_by_clones_count_++;
Artem Serovcced8ba2017-07-19 18:18:09 +01007640 }
7641 }
7642
Artem Serov7f4aff62017-06-21 17:02:18 +01007643 size_t GetInstrReplacedByClonesCount() const { return instr_replaced_by_clones_count_; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007644
7645 private:
Artem Serov7f4aff62017-06-21 17:02:18 +01007646 size_t instr_replaced_by_clones_count_;
Artem Serovcced8ba2017-07-19 18:18:09 +01007647
7648 DISALLOW_COPY_AND_ASSIGN(CloneAndReplaceInstructionVisitor);
7649};
7650
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007651// Iterator over the blocks that art part of the loop. Includes blocks part
7652// of an inner loop. The order in which the blocks are iterated is on their
7653// block id.
7654class HBlocksInLoopIterator : public ValueObject {
7655 public:
7656 explicit HBlocksInLoopIterator(const HLoopInformation& info)
7657 : blocks_in_loop_(info.GetBlocks()),
7658 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
7659 index_(0) {
7660 if (!blocks_in_loop_.IsBitSet(index_)) {
7661 Advance();
7662 }
7663 }
7664
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007665 bool Done() const { return index_ == blocks_.size(); }
7666 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007667 void Advance() {
7668 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007669 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007670 if (blocks_in_loop_.IsBitSet(index_)) {
7671 break;
7672 }
7673 }
7674 }
7675
7676 private:
7677 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007678 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007679 size_t index_;
7680
7681 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
7682};
7683
Mingyao Yang3584bce2015-05-19 16:01:59 -07007684// Iterator over the blocks that art part of the loop. Includes blocks part
7685// of an inner loop. The order in which the blocks are iterated is reverse
7686// post order.
7687class HBlocksInLoopReversePostOrderIterator : public ValueObject {
7688 public:
7689 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
7690 : blocks_in_loop_(info.GetBlocks()),
7691 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
7692 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007693 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007694 Advance();
7695 }
7696 }
7697
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007698 bool Done() const { return index_ == blocks_.size(); }
7699 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07007700 void Advance() {
7701 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007702 for (size_t e = blocks_.size(); index_ < e; ++index_) {
7703 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007704 break;
7705 }
7706 }
7707 }
7708
7709 private:
7710 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007711 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07007712 size_t index_;
7713
7714 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
7715};
7716
Aart Bikf3e61ee2017-04-12 17:09:20 -07007717// Returns int64_t value of a properly typed constant.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007718inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00007719 if (constant->IsIntConstant()) {
7720 return constant->AsIntConstant()->GetValue();
7721 } else if (constant->IsLongConstant()) {
7722 return constant->AsLongConstant()->GetValue();
7723 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00007724 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00007725 return 0;
7726 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007727}
7728
Aart Bikf3e61ee2017-04-12 17:09:20 -07007729// Returns true iff instruction is an integral constant (and sets value on success).
7730inline bool IsInt64AndGet(HInstruction* instruction, /*out*/ int64_t* value) {
7731 if (instruction->IsIntConstant()) {
7732 *value = instruction->AsIntConstant()->GetValue();
7733 return true;
7734 } else if (instruction->IsLongConstant()) {
7735 *value = instruction->AsLongConstant()->GetValue();
7736 return true;
7737 } else if (instruction->IsNullConstant()) {
7738 *value = 0;
7739 return true;
7740 }
7741 return false;
7742}
7743
Aart Bik0148de42017-09-05 09:25:01 -07007744// Returns true iff instruction is the given integral constant.
7745inline bool IsInt64Value(HInstruction* instruction, int64_t value) {
7746 int64_t val = 0;
7747 return IsInt64AndGet(instruction, &val) && val == value;
7748}
7749
7750// Returns true iff instruction is a zero bit pattern.
7751inline bool IsZeroBitPattern(HInstruction* instruction) {
7752 return instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern();
7753}
7754
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007755#define INSTRUCTION_TYPE_CHECK(type, super) \
7756 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
7757 inline const H##type* HInstruction::As##type() const { \
7758 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
7759 } \
7760 inline H##type* HInstruction::As##type() { \
7761 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
7762 }
7763
7764 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
7765#undef INSTRUCTION_TYPE_CHECK
7766
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00007767// Create space in `blocks` for adding `number_of_new_blocks` entries
7768// starting at location `at`. Blocks after `at` are moved accordingly.
7769inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
7770 size_t number_of_new_blocks,
7771 size_t after) {
7772 DCHECK_LT(after, blocks->size());
7773 size_t old_size = blocks->size();
7774 size_t new_size = old_size + number_of_new_blocks;
7775 blocks->resize(new_size);
7776 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
7777}
7778
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00007779/*
7780 * Hunt "under the hood" of array lengths (leading to array references),
7781 * null checks (also leading to array references), and new arrays
7782 * (leading to the actual length). This makes it more likely related
7783 * instructions become actually comparable.
7784 */
7785inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
7786 while (instruction->IsArrayLength() ||
7787 instruction->IsNullCheck() ||
7788 instruction->IsNewArray()) {
7789 instruction = instruction->IsNewArray()
7790 ? instruction->AsNewArray()->GetLength()
7791 : instruction->InputAt(0);
7792 }
7793 return instruction;
7794}
7795
Artem Serov21c7e6f2017-07-27 16:04:42 +01007796void RemoveEnvironmentUses(HInstruction* instruction);
7797bool HasEnvironmentUsedByOthers(HInstruction* instruction);
7798void ResetEnvironmentInputRecords(HInstruction* instruction);
7799
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007800} // namespace art
7801
7802#endif // ART_COMPILER_OPTIMIZING_NODES_H_