blob: fef0c865ae216960a847f2b167bd07591a2af30b [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"
Vladimir Marko60584552015-09-03 13:35:12 +000029#include "base/stl_util.h"
David Brazdild9c90372016-09-14 16:53:55 +010030#include "base/transform_array_ref.h"
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010031#include "data_type.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070032#include "deoptimization_kind.h"
Vladimir Marko87f3fcb2016-04-28 15:52:11 +010033#include "dex_file.h"
Andreas Gampea5b09a62016-11-17 15:21:22 -080034#include "dex_file_types.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000035#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000036#include "handle.h"
37#include "handle_scope.h"
Nicolas Geoffray762869d2016-07-15 15:28:35 +010038#include "intrinsics_enum.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070039#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010040#include "locations.h"
Vladimir Marko58155012015-08-19 12:49:41 +000041#include "method_reference.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000042#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010043#include "offsets.h"
Vladimir Marko46817b82016-03-29 12:21:58 +010044#include "utils/intrusive_forward_list.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000045
46namespace art {
47
David Brazdil1abb4192015-02-17 18:33:36 +000048class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000049class HBasicBlock;
Igor Murashkind01745e2017-04-05 16:40:31 -070050class HConstructorFence;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010051class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000052class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010053class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000054class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000055class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000056class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000057class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000058class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000059class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000060class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000061class HNullConstant;
Igor Murashkind01745e2017-04-05 16:40:31 -070062class HParameterValue;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010063class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010064class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010065class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010066class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000067class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010068class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000069class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000070
Mathieu Chartier736b5602015-09-02 14:54:11 -070071namespace mirror {
72class DexCache;
73} // namespace mirror
74
Nicolas Geoffray818f2102014-02-18 16:43:35 +000075static const int kDefaultNumberOfBlocks = 8;
76static const int kDefaultNumberOfSuccessors = 2;
77static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010078static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010079static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000080static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000081
Roland Levillain5b5b9312016-03-22 14:57:31 +000082// The maximum (meaningful) distance (31) that can be used in an integer shift/rotate operation.
83static constexpr int32_t kMaxIntShiftDistance = 0x1f;
84// The maximum (meaningful) distance (63) that can be used in a long shift/rotate operation.
85static constexpr int32_t kMaxLongShiftDistance = 0x3f;
Calin Juravle9aec02f2014-11-18 23:06:35 +000086
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010087static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070088static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010089
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010090static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
91
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060092static constexpr uint32_t kNoDexPc = -1;
93
Vladimir Markodbb7f5b2016-03-30 13:23:58 +010094inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
95 // For the purposes of the compiler, the dex files must actually be the same object
96 // if we want to safely treat them as the same. This is especially important for JIT
97 // as custom class loaders can open the same underlying file (or memory) multiple
98 // times and provide different class resolution but no two class loaders should ever
99 // use the same DexFile object - doing so is an unsupported hack that can lead to
100 // all sorts of weird failures.
101 return &lhs == &rhs;
102}
103
Dave Allison20dfc792014-06-16 20:44:29 -0700104enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -0700105 // All types.
106 kCondEQ, // ==
107 kCondNE, // !=
108 // Signed integers and floating-point numbers.
109 kCondLT, // <
110 kCondLE, // <=
111 kCondGT, // >
112 kCondGE, // >=
113 // Unsigned integers.
114 kCondB, // <
115 kCondBE, // <=
116 kCondA, // >
117 kCondAE, // >=
Scott Wakeling2c76e062016-08-31 09:48:54 +0100118 // First and last aliases.
119 kCondFirst = kCondEQ,
120 kCondLast = kCondAE,
Dave Allison20dfc792014-06-16 20:44:29 -0700121};
122
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000123enum GraphAnalysisResult {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000124 kAnalysisSkipped,
David Brazdilbadd8262016-02-02 16:28:56 +0000125 kAnalysisInvalidBytecode,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000126 kAnalysisFailThrowCatchLoop,
127 kAnalysisFailAmbiguousArrayOp,
128 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000129};
130
Andreas Gampe9186ced2016-12-12 14:28:21 -0800131template <typename T>
132static inline typename std::make_unsigned<T>::type MakeUnsigned(T x) {
133 return static_cast<typename std::make_unsigned<T>::type>(x);
134}
135
Vladimir Markof9f64412015-09-02 14:05:49 +0100136class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100137 public:
138 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
139
140 void AddInstruction(HInstruction* instruction);
141 void RemoveInstruction(HInstruction* instruction);
142
David Brazdilc3d743f2015-04-22 13:40:50 +0100143 // Insert `instruction` before/after an existing instruction `cursor`.
144 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
145 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
146
Roland Levillain6b469232014-09-25 10:10:38 +0100147 // Return true if this list contains `instruction`.
148 bool Contains(HInstruction* instruction) const;
149
Roland Levillainccc07a92014-09-16 14:48:16 +0100150 // Return true if `instruction1` is found before `instruction2` in
151 // this instruction list and false otherwise. Abort if none
152 // of these instructions is found.
153 bool FoundBefore(const HInstruction* instruction1,
154 const HInstruction* instruction2) const;
155
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000156 bool IsEmpty() const { return first_instruction_ == nullptr; }
157 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
158
159 // Update the block of all instructions to be `block`.
160 void SetBlockOfInstructions(HBasicBlock* block) const;
161
162 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000163 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000164 void Add(const HInstructionList& instruction_list);
165
David Brazdil2d7352b2015-04-20 14:52:42 +0100166 // Return the number of instructions in the list. This is an expensive operation.
167 size_t CountSize() const;
168
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100169 private:
170 HInstruction* first_instruction_;
171 HInstruction* last_instruction_;
172
173 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000174 friend class HGraph;
175 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100176 friend class HInstructionIterator;
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +0000177 friend class HInstructionIteratorHandleChanges;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100178 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100179
180 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
181};
182
David Brazdil4833f5a2015-12-16 10:37:39 +0000183class ReferenceTypeInfo : ValueObject {
184 public:
185 typedef Handle<mirror::Class> TypeHandle;
186
Vladimir Markoa1de9182016-02-25 11:37:38 +0000187 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
188
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700189 static ReferenceTypeInfo Create(TypeHandle type_handle) REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil94ab38f2016-06-21 17:48:19 +0100190 return Create(type_handle, type_handle->CannotBeAssignedFromOtherTypes());
191 }
192
Vladimir Markoa1de9182016-02-25 11:37:38 +0000193 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000194 return ReferenceTypeInfo(type_handle, is_exact);
195 }
196
197 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
198
Vladimir Markof39745e2016-01-26 12:16:55 +0000199 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000200 return handle.GetReference() != nullptr;
201 }
202
Vladimir Marko456307a2016-04-19 14:12:13 +0000203 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000204 return IsValidHandle(type_handle_);
205 }
206
207 bool IsExact() const { return is_exact_; }
208
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700209 bool IsObjectClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000210 DCHECK(IsValid());
211 return GetTypeHandle()->IsObjectClass();
212 }
213
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700214 bool IsStringClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000215 DCHECK(IsValid());
216 return GetTypeHandle()->IsStringClass();
217 }
218
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700219 bool IsObjectArray() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000220 DCHECK(IsValid());
221 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
222 }
223
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700224 bool IsInterface() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000225 DCHECK(IsValid());
226 return GetTypeHandle()->IsInterface();
227 }
228
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700229 bool IsArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000230 DCHECK(IsValid());
231 return GetTypeHandle()->IsArrayClass();
232 }
233
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700234 bool IsPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000235 DCHECK(IsValid());
236 return GetTypeHandle()->IsPrimitiveArray();
237 }
238
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700239 bool IsNonPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000240 DCHECK(IsValid());
241 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
242 }
243
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700244 bool CanArrayHold(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000245 DCHECK(IsValid());
246 if (!IsExact()) return false;
247 if (!IsArrayClass()) return false;
248 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
249 }
250
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700251 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000252 DCHECK(IsValid());
253 if (!IsExact()) return false;
254 if (!IsArrayClass()) return false;
255 if (!rti.IsArrayClass()) return false;
256 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
257 rti.GetTypeHandle()->GetComponentType());
258 }
259
260 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
261
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700262 bool IsSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000263 DCHECK(IsValid());
264 DCHECK(rti.IsValid());
265 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
266 }
267
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700268 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000269 DCHECK(IsValid());
270 DCHECK(rti.IsValid());
271 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
272 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
273 }
274
275 // Returns true if the type information provide the same amount of details.
276 // Note that it does not mean that the instructions have the same actual type
277 // (because the type can be the result of a merge).
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700278 bool IsEqual(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000279 if (!IsValid() && !rti.IsValid()) {
280 // Invalid types are equal.
281 return true;
282 }
283 if (!IsValid() || !rti.IsValid()) {
284 // One is valid, the other not.
285 return false;
286 }
287 return IsExact() == rti.IsExact()
288 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
289 }
290
291 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000292 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
293 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
294 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000295
296 // The class of the object.
297 TypeHandle type_handle_;
298 // Whether or not the type is exact or a superclass of the actual type.
299 // Whether or not we have any information about this type.
300 bool is_exact_;
301};
302
303std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
304
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000305// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100306class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000307 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100308 HGraph(ArenaAllocator* arena,
309 const DexFile& dex_file,
310 uint32_t method_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700311 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100312 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100313 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000314 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100315 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000316 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100317 blocks_(arena->Adapter(kArenaAllocBlockList)),
318 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
319 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700320 entry_block_(nullptr),
321 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100322 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100323 number_of_vregs_(0),
324 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000325 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400326 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000327 has_try_catch_(false),
Aart Bikb13c65b2017-03-21 20:14:07 -0700328 has_simd_(false),
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800329 has_loops_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000330 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000331 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000332 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100333 dex_file_(dex_file),
334 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100335 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100336 in_ssa_form_(false),
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800337 number_of_cha_guards_(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700338 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000339 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100340 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
341 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
342 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
343 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000344 cached_current_method_(nullptr),
Nicolas Geoffray53fec082017-03-27 12:56:16 +0100345 art_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000346 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
Mingyao Yang063fc772016-08-02 11:02:54 -0700347 osr_(osr),
348 cha_single_implementation_list_(arena->Adapter(kArenaAllocCHA)) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100349 blocks_.reserve(kDefaultNumberOfBlocks);
350 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000351
David Brazdilbadd8262016-02-02 16:28:56 +0000352 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700353 void InitializeInexactObjectRTI(VariableSizedHandleScope* handles);
David Brazdilbadd8262016-02-02 16:28:56 +0000354
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000355 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100356 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
357
David Brazdil69ba7b72015-06-23 18:27:30 +0100358 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000359 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100360
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000361 HBasicBlock* GetEntryBlock() const { return entry_block_; }
362 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100363 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000364
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000365 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
366 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000367
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000368 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100369
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100370 void ComputeDominanceInformation();
371 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000372 void ClearLoopInformation();
373 void FindBackEdges(ArenaBitVector* visited);
374 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100375 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100376 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000377
David Brazdil4833f5a2015-12-16 10:37:39 +0000378 // Analyze all natural loops in this graph. Returns a code specifying that it
379 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000380 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000381 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100382
David Brazdilffee3d32015-07-06 11:48:53 +0100383 // Iterate over blocks to compute try block membership. Needs reverse post
384 // order and loop information.
385 void ComputeTryBlockInformation();
386
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000387 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000388 // Returns the instruction to replace the invoke expression or null if the
389 // invoke is for a void method. Note that the caller is responsible for replacing
390 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000391 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000392
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000393 // Update the loop and try membership of `block`, which was spawned from `reference`.
394 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
395 // should be the new back edge.
396 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
397 HBasicBlock* reference,
398 bool replace_if_back_edge);
399
Mingyao Yang3584bce2015-05-19 16:01:59 -0700400 // Need to add a couple of blocks to test if the loop body is entered and
401 // put deoptimization instructions, etc.
402 void TransformLoopHeaderForBCE(HBasicBlock* header);
403
Aart Bikf8f5a162017-02-06 15:35:29 -0800404 // Adds a new loop directly after the loop with the given header and exit.
405 // Returns the new preheader.
406 HBasicBlock* TransformLoopForVectorization(HBasicBlock* header,
407 HBasicBlock* body,
408 HBasicBlock* exit);
409
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000410 // Removes `block` from the graph. Assumes `block` has been disconnected from
411 // other blocks and has no instructions or phis.
412 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000413
David Brazdilfc6a86a2015-06-26 10:33:45 +0000414 // Splits the edge between `block` and `successor` while preserving the
415 // indices in the predecessor/successor lists. If there are multiple edges
416 // between the blocks, the lowest indices are used.
417 // Returns the new block which is empty and has the same dex pc as `successor`.
418 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
419
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100420 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
Artem Serovc73ee372017-07-31 15:08:40 +0100421 void OrderLoopHeaderPredecessors(HBasicBlock* header);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100422 void SimplifyLoop(HBasicBlock* header);
423
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000424 int32_t GetNextInstructionId() {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100425 CHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000426 return current_instruction_id_++;
427 }
428
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000429 int32_t GetCurrentInstructionId() const {
430 return current_instruction_id_;
431 }
432
433 void SetCurrentInstructionId(int32_t id) {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100434 CHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000435 current_instruction_id_ = id;
436 }
437
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100438 uint16_t GetMaximumNumberOfOutVRegs() const {
439 return maximum_number_of_out_vregs_;
440 }
441
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000442 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
443 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100444 }
445
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100446 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
447 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
448 }
449
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000450 void UpdateTemporariesVRegSlots(size_t slots) {
451 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100452 }
453
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000454 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100455 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000456 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100457 }
458
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100459 void SetNumberOfVRegs(uint16_t number_of_vregs) {
460 number_of_vregs_ = number_of_vregs;
461 }
462
463 uint16_t GetNumberOfVRegs() const {
464 return number_of_vregs_;
465 }
466
467 void SetNumberOfInVRegs(uint16_t value) {
468 number_of_in_vregs_ = value;
469 }
470
David Brazdildee58d62016-04-07 09:54:26 +0000471 uint16_t GetNumberOfInVRegs() const {
472 return number_of_in_vregs_;
473 }
474
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100475 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100476 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100477 return number_of_vregs_ - number_of_in_vregs_;
478 }
479
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100480 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100481 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100482 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100483
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100484 ArrayRef<HBasicBlock* const> GetReversePostOrderSkipEntryBlock() {
485 DCHECK(GetReversePostOrder()[0] == entry_block_);
486 return ArrayRef<HBasicBlock* const>(GetReversePostOrder()).SubArray(1);
487 }
488
489 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetPostOrder() const {
490 return ReverseRange(GetReversePostOrder());
491 }
492
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100493 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100494 return linear_order_;
495 }
496
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100497 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetLinearPostOrder() const {
498 return ReverseRange(GetLinearOrder());
499 }
500
Mark Mendell1152c922015-04-24 17:06:35 -0400501 bool HasBoundsChecks() const {
502 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800503 }
504
Mark Mendell1152c922015-04-24 17:06:35 -0400505 void SetHasBoundsChecks(bool value) {
506 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800507 }
508
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000509 bool IsDebuggable() const { return debuggable_; }
510
David Brazdil8d5b8b22015-03-24 10:51:52 +0000511 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000512 // already, it is created and inserted into the graph. This method is only for
513 // integral types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100514 HConstant* GetConstant(DataType::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000515
516 // TODO: This is problematic for the consistency of reference type propagation
517 // because it can be created anytime after the pass and thus it will be left
518 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600519 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000520
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600521 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
522 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000523 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600524 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
525 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000526 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600527 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
528 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000529 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600530 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
531 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000532 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000533
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100534 HCurrentMethod* GetCurrentMethod();
535
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100536 const DexFile& GetDexFile() const {
537 return dex_file_;
538 }
539
540 uint32_t GetMethodIdx() const {
541 return method_idx_;
542 }
543
Igor Murashkind01745e2017-04-05 16:40:31 -0700544 // Get the method name (without the signature), e.g. "<init>"
545 const char* GetMethodName() const;
546
547 // Get the pretty method name (class + name + optionally signature).
548 std::string PrettyMethod(bool with_signature = true) const;
549
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100550 InvokeType GetInvokeType() const {
551 return invoke_type_;
552 }
553
Mark Mendellc4701932015-04-10 13:18:51 -0400554 InstructionSet GetInstructionSet() const {
555 return instruction_set_;
556 }
557
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000558 bool IsCompilingOsr() const { return osr_; }
559
Mingyao Yang063fc772016-08-02 11:02:54 -0700560 ArenaSet<ArtMethod*>& GetCHASingleImplementationList() {
561 return cha_single_implementation_list_;
562 }
563
564 void AddCHASingleImplementationDependency(ArtMethod* method) {
565 cha_single_implementation_list_.insert(method);
566 }
567
568 bool HasShouldDeoptimizeFlag() const {
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800569 return number_of_cha_guards_ != 0;
Mingyao Yang063fc772016-08-02 11:02:54 -0700570 }
571
David Brazdil77a48ae2015-09-15 12:34:04 +0000572 bool HasTryCatch() const { return has_try_catch_; }
573 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100574
Aart Bikb13c65b2017-03-21 20:14:07 -0700575 bool HasSIMD() const { return has_simd_; }
576 void SetHasSIMD(bool value) { has_simd_ = value; }
577
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800578 bool HasLoops() const { return has_loops_; }
579 void SetHasLoops(bool value) { has_loops_ = value; }
580
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000581 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
582 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
583
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100584 ArtMethod* GetArtMethod() const { return art_method_; }
585 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
586
Roland Levillain5e8d5f02016-10-18 18:03:43 +0100587 // Returns an instruction with the opposite Boolean value from 'cond'.
Mark Mendellf6529172015-11-17 11:16:56 -0500588 // The instruction has been inserted into the graph, either as a constant, or
589 // before cursor.
590 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
591
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000592 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
593
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800594 uint32_t GetNumberOfCHAGuards() { return number_of_cha_guards_; }
595 void SetNumberOfCHAGuards(uint32_t num) { number_of_cha_guards_ = num; }
596 void IncrementNumberOfCHAGuards() { number_of_cha_guards_++; }
597
David Brazdil2d7352b2015-04-20 14:52:42 +0100598 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000599 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100600 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000601
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000602 template <class InstructionType, typename ValueType>
603 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600604 ArenaSafeMap<ValueType, InstructionType*>* cache,
605 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000606 // Try to find an existing constant of the given value.
607 InstructionType* constant = nullptr;
608 auto cached_constant = cache->find(value);
609 if (cached_constant != cache->end()) {
610 constant = cached_constant->second;
611 }
612
613 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100614 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000615 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600616 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000617 cache->Overwrite(value, constant);
618 InsertConstant(constant);
619 }
620 return constant;
621 }
622
David Brazdil8d5b8b22015-03-24 10:51:52 +0000623 void InsertConstant(HConstant* instruction);
624
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000625 // Cache a float constant into the graph. This method should only be
626 // called by the SsaBuilder when creating "equivalent" instructions.
627 void CacheFloatConstant(HFloatConstant* constant);
628
629 // See CacheFloatConstant comment.
630 void CacheDoubleConstant(HDoubleConstant* constant);
631
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000632 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000633
634 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100635 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000636
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100637 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100638 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000639
Aart Bik281c6812016-08-26 11:31:48 -0700640 // List of blocks to perform a linear order tree traversal. Unlike the reverse
641 // post order, this order is not incrementally kept up-to-date.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100642 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100643
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000644 HBasicBlock* entry_block_;
645 HBasicBlock* exit_block_;
646
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100647 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100648 uint16_t maximum_number_of_out_vregs_;
649
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100650 // The number of virtual registers in this method. Contains the parameters.
651 uint16_t number_of_vregs_;
652
653 // The number of virtual registers used by parameters of this method.
654 uint16_t number_of_in_vregs_;
655
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000656 // Number of vreg size slots that the temporaries use (used in baseline compiler).
657 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100658
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800659 // Flag whether there are bounds checks in the graph. We can skip
660 // BCE if it's false. It's only best effort to keep it up to date in
661 // the presence of code elimination so there might be false positives.
Mark Mendell1152c922015-04-24 17:06:35 -0400662 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800663
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800664 // Flag whether there are try/catch blocks in the graph. We will skip
665 // try/catch-related passes if it's false. It's only best effort to keep
666 // it up to date in the presence of code elimination so there might be
667 // false positives.
David Brazdil77a48ae2015-09-15 12:34:04 +0000668 bool has_try_catch_;
669
Aart Bikb13c65b2017-03-21 20:14:07 -0700670 // Flag whether SIMD instructions appear in the graph. If true, the
671 // code generators may have to be more careful spilling the wider
672 // contents of SIMD registers.
673 bool has_simd_;
674
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800675 // Flag whether there are any loops in the graph. We can skip loop
676 // optimization if it's false. It's only best effort to keep it up
677 // to date in the presence of code elimination so there might be false
678 // positives.
679 bool has_loops_;
680
681 // Flag whether there are any irreducible loops in the graph. It's only
682 // best effort to keep it up to date in the presence of code elimination
683 // so there might be false positives.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000684 bool has_irreducible_loops_;
685
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000686 // Indicates whether the graph should be compiled in a way that
687 // ensures full debuggability. If false, we can apply more
688 // aggressive optimizations that may limit the level of debugging.
689 const bool debuggable_;
690
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000691 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000692 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000693
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100694 // The dex file from which the method is from.
695 const DexFile& dex_file_;
696
697 // The method index in the dex file.
698 const uint32_t method_idx_;
699
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100700 // If inlined, this encodes how the callee is being invoked.
701 const InvokeType invoke_type_;
702
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100703 // Whether the graph has been transformed to SSA form. Only used
704 // in debug mode to ensure we are not using properties only valid
705 // for non-SSA form (like the number of temporaries).
706 bool in_ssa_form_;
707
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800708 // Number of CHA guards in the graph. Used to short-circuit the
709 // CHA guard optimization pass when there is no CHA guard left.
710 uint32_t number_of_cha_guards_;
711
Mathieu Chartiere401d142015-04-22 13:56:20 -0700712 const InstructionSet instruction_set_;
713
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000714 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000715 HNullConstant* cached_null_constant_;
716 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000717 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000718 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000719 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000720
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100721 HCurrentMethod* cached_current_method_;
722
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100723 // The ArtMethod this graph is for. Note that for AOT, it may be null,
724 // for example for methods whose declaring class could not be resolved
725 // (such as when the superclass could not be found).
726 ArtMethod* art_method_;
727
David Brazdil4833f5a2015-12-16 10:37:39 +0000728 // Keep the RTI of inexact Object to avoid having to pass stack handle
729 // collection pointer to passes which may create NullConstant.
730 ReferenceTypeInfo inexact_object_rti_;
731
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000732 // Whether we are compiling this graph for on stack replacement: this will
733 // make all loops seen as irreducible and emit special stack maps to mark
734 // compiled code entries which the interpreter can directly jump to.
735 const bool osr_;
736
Mingyao Yang063fc772016-08-02 11:02:54 -0700737 // List of methods that are assumed to have single implementation.
738 ArenaSet<ArtMethod*> cha_single_implementation_list_;
739
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000740 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100741 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000742 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000743 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000744 DISALLOW_COPY_AND_ASSIGN(HGraph);
745};
746
Vladimir Markof9f64412015-09-02 14:05:49 +0100747class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000748 public:
749 HLoopInformation(HBasicBlock* header, HGraph* graph)
750 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100751 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000752 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100753 contains_irreducible_loop_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100754 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100755 // Make bit vector growable, as the number of blocks may change.
Vladimir Markof6a35de2016-03-21 12:01:50 +0000756 blocks_(graph->GetArena(), graph->GetBlocks().size(), true, kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100757 back_edges_.reserve(kDefaultNumberOfBackEdges);
758 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100759
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000760 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100761 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000762
763 void Dump(std::ostream& os);
764
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100765 HBasicBlock* GetHeader() const {
766 return header_;
767 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000768
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000769 void SetHeader(HBasicBlock* block) {
770 header_ = block;
771 }
772
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100773 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
774 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
775 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
776
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000777 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100778 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000779 }
780
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100781 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100782 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100783 }
784
David Brazdil46e2a392015-03-16 17:31:52 +0000785 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100786 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100787 }
788
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000789 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100790 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000791 }
792
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100793 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100794
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100795 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100796 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100797 }
798
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100799 // Returns the lifetime position of the back edge that has the
800 // greatest lifetime position.
801 size_t GetLifetimeEnd() const;
802
803 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100804 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100805 }
806
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000807 // Finds blocks that are part of this loop.
808 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100809
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100810 // Returns whether this loop information contains `block`.
811 // Note that this loop information *must* be populated before entering this function.
812 bool Contains(const HBasicBlock& block) const;
813
814 // Returns whether this loop information is an inner loop of `other`.
815 // Note that `other` *must* be populated before entering this function.
816 bool IsIn(const HLoopInformation& other) const;
817
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800818 // Returns true if instruction is not defined within this loop.
819 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700820
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100821 const ArenaBitVector& GetBlocks() const { return blocks_; }
822
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000823 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000824 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000825
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000826 void ClearAllBlocks() {
827 blocks_.ClearAllBits();
828 }
829
David Brazdil3f4a5222016-05-06 12:46:21 +0100830 bool HasBackEdgeNotDominatedByHeader() const;
831
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100832 bool IsPopulated() const {
833 return blocks_.GetHighestBitSet() != -1;
834 }
835
Anton Shaminf89381f2016-05-16 16:44:13 +0600836 bool DominatesAllBackEdges(HBasicBlock* block);
837
David Sehrc757dec2016-11-04 15:48:34 -0700838 bool HasExitEdge() const;
839
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000840 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100841 // Internal recursive implementation of `Populate`.
842 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100843 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100844
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000845 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100846 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000847 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100848 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100849 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100850 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000851
852 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
853};
854
David Brazdilec16f792015-08-19 15:04:01 +0100855// Stores try/catch information for basic blocks.
856// Note that HGraph is constructed so that catch blocks cannot simultaneously
857// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100858class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100859 public:
860 // Try block information constructor.
861 explicit TryCatchInformation(const HTryBoundary& try_entry)
862 : try_entry_(&try_entry),
863 catch_dex_file_(nullptr),
864 catch_type_index_(DexFile::kDexNoIndex16) {
865 DCHECK(try_entry_ != nullptr);
866 }
867
868 // Catch block information constructor.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800869 TryCatchInformation(dex::TypeIndex catch_type_index, const DexFile& dex_file)
David Brazdilec16f792015-08-19 15:04:01 +0100870 : try_entry_(nullptr),
871 catch_dex_file_(&dex_file),
872 catch_type_index_(catch_type_index) {}
873
874 bool IsTryBlock() const { return try_entry_ != nullptr; }
875
876 const HTryBoundary& GetTryEntry() const {
877 DCHECK(IsTryBlock());
878 return *try_entry_;
879 }
880
881 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
882
883 bool IsCatchAllTypeIndex() const {
884 DCHECK(IsCatchBlock());
Andreas Gampea5b09a62016-11-17 15:21:22 -0800885 return !catch_type_index_.IsValid();
David Brazdilec16f792015-08-19 15:04:01 +0100886 }
887
Andreas Gampea5b09a62016-11-17 15:21:22 -0800888 dex::TypeIndex GetCatchTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +0100889 DCHECK(IsCatchBlock());
890 return catch_type_index_;
891 }
892
893 const DexFile& GetCatchDexFile() const {
894 DCHECK(IsCatchBlock());
895 return *catch_dex_file_;
896 }
897
898 private:
899 // One of possibly several TryBoundary instructions entering the block's try.
900 // Only set for try blocks.
901 const HTryBoundary* try_entry_;
902
903 // Exception type information. Only set for catch blocks.
904 const DexFile* catch_dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800905 const dex::TypeIndex catch_type_index_;
David Brazdilec16f792015-08-19 15:04:01 +0100906};
907
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100908static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100909static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100910
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000911// A block in a method. Contains the list of instructions represented
912// as a double linked list. Each block knows its predecessors and
913// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100914
Vladimir Markof9f64412015-09-02 14:05:49 +0100915class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000916 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700917 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000918 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000919 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
920 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000921 loop_information_(nullptr),
922 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000923 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100924 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100925 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100926 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000927 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000928 try_catch_information_(nullptr) {
929 predecessors_.reserve(kDefaultNumberOfPredecessors);
930 successors_.reserve(kDefaultNumberOfSuccessors);
931 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
932 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000933
Vladimir Marko60584552015-09-03 13:35:12 +0000934 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100935 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000936 }
937
Vladimir Marko60584552015-09-03 13:35:12 +0000938 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100939 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000940 }
941
David Brazdild26a4112015-11-10 11:07:31 +0000942 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
943 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
944
Vladimir Marko60584552015-09-03 13:35:12 +0000945 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
946 return ContainsElement(successors_, block, start_from);
947 }
948
949 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100950 return dominated_blocks_;
951 }
952
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100953 bool IsEntryBlock() const {
954 return graph_->GetEntryBlock() == this;
955 }
956
957 bool IsExitBlock() const {
958 return graph_->GetExitBlock() == this;
959 }
960
David Brazdil46e2a392015-03-16 17:31:52 +0000961 bool IsSingleGoto() const;
Mads Ager16e52892017-07-14 13:11:37 +0200962 bool IsSingleReturn() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000963 bool IsSingleTryBoundary() const;
964
965 // Returns true if this block emits nothing but a jump.
966 bool IsSingleJump() const {
967 HLoopInformation* loop_info = GetLoopInformation();
968 return (IsSingleGoto() || IsSingleTryBoundary())
969 // Back edges generate a suspend check.
970 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
971 }
David Brazdil46e2a392015-03-16 17:31:52 +0000972
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000973 void AddBackEdge(HBasicBlock* back_edge) {
974 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000975 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000976 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100977 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000978 loop_information_->AddBackEdge(back_edge);
979 }
980
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000981 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000982 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000983
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100984 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000985 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600986 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000987
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000988 HBasicBlock* GetDominator() const { return dominator_; }
989 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000990 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
991
992 void RemoveDominatedBlock(HBasicBlock* block) {
993 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100994 }
Vladimir Marko60584552015-09-03 13:35:12 +0000995
996 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
997 ReplaceElement(dominated_blocks_, existing, new_block);
998 }
999
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001000 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001001
1002 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001003 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001004 }
1005
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001006 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
1007 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +01001008 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001009 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +01001010 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
1011 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001012
Nicolas Geoffray09aa1472016-01-19 10:52:54 +00001013 HInstruction* GetFirstInstructionDisregardMoves() const;
1014
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001015 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001016 successors_.push_back(block);
1017 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001018 }
1019
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001020 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
1021 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001022 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001023 new_block->predecessors_.push_back(this);
1024 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001025 }
1026
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001027 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
1028 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001029 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001030 new_block->successors_.push_back(this);
1031 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001032 }
1033
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001034 // Insert `this` between `predecessor` and `successor. This method
1035 // preserves the indicies, and will update the first edge found between
1036 // `predecessor` and `successor`.
1037 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
1038 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001039 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +00001040 successor->predecessors_[predecessor_index] = this;
1041 predecessor->successors_[successor_index] = this;
1042 successors_.push_back(successor);
1043 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001044 }
1045
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001046 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001047 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001048 }
1049
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001050 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001051 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001052 }
1053
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001054 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001055 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001056 }
1057
1058 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001059 predecessors_.push_back(block);
1060 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001061 }
1062
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001063 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001064 DCHECK_EQ(predecessors_.size(), 2u);
1065 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001066 }
1067
David Brazdil769c9e52015-04-27 13:54:09 +01001068 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001069 DCHECK_EQ(successors_.size(), 2u);
1070 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +01001071 }
1072
David Brazdilfc6a86a2015-06-26 10:33:45 +00001073 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001074 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001075 }
1076
David Brazdilfc6a86a2015-06-26 10:33:45 +00001077 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001078 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001079 }
1080
David Brazdilfc6a86a2015-06-26 10:33:45 +00001081 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001082 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001083 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001084 }
1085
1086 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001087 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001088 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001089 }
1090
1091 // Returns whether the first occurrence of `predecessor` in the list of
1092 // predecessors is at index `idx`.
1093 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001094 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001095 return GetPredecessorIndexOf(predecessor) == idx;
1096 }
1097
David Brazdild7558da2015-09-22 13:04:14 +01001098 // Create a new block between this block and its predecessors. The new block
1099 // is added to the graph, all predecessor edges are relinked to it and an edge
1100 // is created to `this`. Returns the new empty block. Reverse post order or
1101 // loop and try/catch information are not updated.
1102 HBasicBlock* CreateImmediateDominator();
1103
David Brazdilfc6a86a2015-06-26 10:33:45 +00001104 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001105 // created, latter block. Note that this method will add the block to the
1106 // graph, create a Goto at the end of the former block and will create an edge
1107 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001108 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001109 HBasicBlock* SplitBefore(HInstruction* cursor);
1110
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001111 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001112 // created block. Note that this method just updates raw block information,
1113 // like predecessors, successors, dominators, and instruction list. It does not
1114 // update the graph, reverse post order, loop information, nor make sure the
1115 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001116 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1117
1118 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1119 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001120
1121 // Merge `other` at the end of `this`. Successors and dominated blocks of
1122 // `other` are changed to be successors and dominated blocks of `this`. Note
1123 // that this method does not update the graph, reverse post order, loop
1124 // information, nor make sure the blocks are consistent (for example ending
1125 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001126 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001127
1128 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1129 // of `this` are moved to `other`.
1130 // Note that this method does not update the graph, reverse post order, loop
1131 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001132 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001133 void ReplaceWith(HBasicBlock* other);
1134
Aart Bik6b69e0a2017-01-11 10:20:43 -08001135 // Merges the instructions of `other` at the end of `this`.
1136 void MergeInstructionsWith(HBasicBlock* other);
1137
David Brazdil2d7352b2015-04-20 14:52:42 +01001138 // Merge `other` at the end of `this`. This method updates loops, reverse post
1139 // order, links to predecessors, successors, dominators and deletes the block
1140 // from the graph. The two blocks must be successive, i.e. `this` the only
1141 // predecessor of `other` and vice versa.
1142 void MergeWith(HBasicBlock* other);
1143
1144 // Disconnects `this` from all its predecessors, successors and dominator,
1145 // removes it from all loops it is included in and eventually from the graph.
1146 // The block must not dominate any other block. Predecessors and successors
1147 // are safely updated.
1148 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001149
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001150 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001151 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001152 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001153 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001154 // Replace instruction `initial` with `replacement` within this block.
1155 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1156 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001157 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001158 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001159 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1160 // instruction list. With 'ensure_safety' set to true, it verifies that the
1161 // instruction is not in use and removes it from the use lists of its inputs.
1162 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1163 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001164 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001165
1166 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001167 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001168 }
1169
Roland Levillain6b879dd2014-09-22 17:13:44 +01001170 bool IsLoopPreHeaderFirstPredecessor() const {
1171 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001172 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001173 }
1174
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001175 bool IsFirstPredecessorBackEdge() const {
1176 DCHECK(IsLoopHeader());
1177 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1178 }
1179
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001180 HLoopInformation* GetLoopInformation() const {
1181 return loop_information_;
1182 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001183
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001184 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001185 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001186 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001187 void SetInLoop(HLoopInformation* info) {
1188 if (IsLoopHeader()) {
1189 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001190 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001191 loop_information_ = info;
1192 } else if (loop_information_->Contains(*info->GetHeader())) {
1193 // Block is currently part of an outer loop. Make it part of this inner loop.
1194 // Note that a non loop header having a loop information means this loop information
1195 // has already been populated
1196 loop_information_ = info;
1197 } else {
1198 // Block is part of an inner loop. Do not update the loop information.
1199 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1200 // at this point, because this method is being called while populating `info`.
1201 }
1202 }
1203
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001204 // Raw update of the loop information.
1205 void SetLoopInformation(HLoopInformation* info) {
1206 loop_information_ = info;
1207 }
1208
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001209 bool IsInLoop() const { return loop_information_ != nullptr; }
1210
David Brazdilec16f792015-08-19 15:04:01 +01001211 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1212
1213 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1214 try_catch_information_ = try_catch_information;
1215 }
1216
1217 bool IsTryBlock() const {
1218 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1219 }
1220
1221 bool IsCatchBlock() const {
1222 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1223 }
David Brazdilffee3d32015-07-06 11:48:53 +01001224
1225 // Returns the try entry that this block's successors should have. They will
1226 // be in the same try, unless the block ends in a try boundary. In that case,
1227 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001228 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001229
David Brazdild7558da2015-09-22 13:04:14 +01001230 bool HasThrowingInstructions() const;
1231
David Brazdila4b8c212015-05-07 09:59:30 +01001232 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001233 bool Dominates(HBasicBlock* block) const;
1234
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001235 size_t GetLifetimeStart() const { return lifetime_start_; }
1236 size_t GetLifetimeEnd() const { return lifetime_end_; }
1237
1238 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1239 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1240
David Brazdil8d5b8b22015-03-24 10:51:52 +00001241 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001242 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001243 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001244 bool HasSinglePhi() const;
1245
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001246 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001247 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001248 ArenaVector<HBasicBlock*> predecessors_;
1249 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001250 HInstructionList instructions_;
1251 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001252 HLoopInformation* loop_information_;
1253 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001254 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001255 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001256 // The dex program counter of the first instruction of this block.
1257 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001258 size_t lifetime_start_;
1259 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001260 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001261
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001262 friend class HGraph;
1263 friend class HInstruction;
1264
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001265 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1266};
1267
David Brazdilb2bd1c52015-03-25 11:17:37 +00001268// Iterates over the LoopInformation of all loops which contain 'block'
1269// from the innermost to the outermost.
1270class HLoopInformationOutwardIterator : public ValueObject {
1271 public:
1272 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1273 : current_(block.GetLoopInformation()) {}
1274
1275 bool Done() const { return current_ == nullptr; }
1276
1277 void Advance() {
1278 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001279 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001280 }
1281
1282 HLoopInformation* Current() const {
1283 DCHECK(!Done());
1284 return current_;
1285 }
1286
1287 private:
1288 HLoopInformation* current_;
1289
1290 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1291};
1292
Alexandre Ramesef20f712015-06-09 10:29:30 +01001293#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001294 M(Above, Condition) \
1295 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001296 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001297 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001298 M(ArrayGet, Instruction) \
1299 M(ArrayLength, Instruction) \
1300 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001301 M(Below, Condition) \
1302 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001303 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001304 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001305 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001306 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001307 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001308 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001309 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001310 M(Compare, BinaryOperation) \
Igor Murashkind01745e2017-04-05 16:40:31 -07001311 M(ConstructorFence, Instruction) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001312 M(CurrentMethod, Instruction) \
Mingyao Yang063fc772016-08-02 11:02:54 -07001313 M(ShouldDeoptimizeFlag, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001314 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001315 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001316 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001317 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001318 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001319 M(Exit, Instruction) \
1320 M(FloatConstant, Constant) \
1321 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001322 M(GreaterThan, Condition) \
1323 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001324 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001325 M(InstanceFieldGet, Instruction) \
1326 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001327 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001328 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001329 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001330 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001331 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001332 M(InvokeVirtual, Invoke) \
Orion Hodsonac141392017-01-13 11:53:47 +00001333 M(InvokePolymorphic, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001334 M(LessThan, Condition) \
1335 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001336 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001337 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001338 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001339 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001340 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001341 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001342 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001343 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001344 M(Neg, UnaryOperation) \
1345 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001346 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001347 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001348 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001349 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001350 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001351 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001352 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001353 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001354 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001355 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001356 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001357 M(Return, Instruction) \
1358 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001359 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001360 M(Shl, BinaryOperation) \
1361 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001362 M(StaticFieldGet, Instruction) \
1363 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001364 M(UnresolvedInstanceFieldGet, Instruction) \
1365 M(UnresolvedInstanceFieldSet, Instruction) \
1366 M(UnresolvedStaticFieldGet, Instruction) \
1367 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001368 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001369 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001370 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001371 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001372 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001373 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001374 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001375 M(Xor, BinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001376 M(VecReplicateScalar, VecUnaryOperation) \
Aart Bik0148de42017-09-05 09:25:01 -07001377 M(VecExtractScalar, VecUnaryOperation) \
1378 M(VecReduce, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001379 M(VecCnv, VecUnaryOperation) \
1380 M(VecNeg, VecUnaryOperation) \
Aart Bik6daebeb2017-04-03 14:35:41 -07001381 M(VecAbs, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001382 M(VecNot, VecUnaryOperation) \
1383 M(VecAdd, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001384 M(VecHalvingAdd, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001385 M(VecSub, VecBinaryOperation) \
1386 M(VecMul, VecBinaryOperation) \
1387 M(VecDiv, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001388 M(VecMin, VecBinaryOperation) \
1389 M(VecMax, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001390 M(VecAnd, VecBinaryOperation) \
1391 M(VecAndNot, VecBinaryOperation) \
1392 M(VecOr, VecBinaryOperation) \
1393 M(VecXor, VecBinaryOperation) \
1394 M(VecShl, VecBinaryOperation) \
1395 M(VecShr, VecBinaryOperation) \
1396 M(VecUShr, VecBinaryOperation) \
Aart Bik8de59162017-04-21 09:42:01 -07001397 M(VecSetScalars, VecOperation) \
Artem Serovf34dd202017-04-10 17:41:46 +01001398 M(VecMultiplyAccumulate, VecOperation) \
Aart Bikdbbac8f2017-09-01 13:06:08 -07001399 M(VecSADAccumulate, VecOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001400 M(VecLoad, VecMemoryOperation) \
1401 M(VecStore, VecMemoryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001402
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001403/*
1404 * Instructions, shared across several (not all) architectures.
1405 */
1406#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1407#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1408#else
1409#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001410 M(BitwiseNegatedRight, Instruction) \
Anton Kirilov74234da2017-01-13 14:42:47 +00001411 M(DataProcWithShifterOp, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001412 M(MultiplyAccumulate, Instruction) \
Artem Serove1811ed2017-04-27 16:50:47 +01001413 M(IntermediateAddress, Instruction) \
1414 M(IntermediateAddressIndex, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001415#endif
1416
Alexandre Ramesef20f712015-06-09 10:29:30 +01001417#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1418
1419#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1420
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001421#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001422#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001423#else
1424#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1425 M(MipsComputeBaseMethodAddress, Instruction) \
Lena Djokica2901602017-09-21 13:50:52 +02001426 M(MipsPackedSwitch, Instruction) \
1427 M(IntermediateArrayAddressIndex, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001428#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001429
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001430#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1431
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001432#ifndef ART_ENABLE_CODEGEN_x86
1433#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1434#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001435#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1436 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001437 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001438 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001439 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001440#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001441
1442#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1443
1444#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1445 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001446 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001447 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1448 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001449 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001450 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001451 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1452 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1453
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001454#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1455 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001456 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001457 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001458 M(BinaryOperation, Instruction) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001459 M(Invoke, Instruction) \
1460 M(VecOperation, Instruction) \
1461 M(VecUnaryOperation, VecOperation) \
1462 M(VecBinaryOperation, VecOperation) \
1463 M(VecMemoryOperation, VecOperation)
Dave Allison20dfc792014-06-16 20:44:29 -07001464
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001465#define FOR_EACH_INSTRUCTION(M) \
1466 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1467 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1468
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001469#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001470FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1471#undef FORWARD_DECLARATION
1472
Vladimir Marko372f10e2016-05-17 16:30:10 +01001473#define DECLARE_INSTRUCTION(type) \
1474 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
1475 const char* DebugName() const OVERRIDE { return #type; } \
1476 bool InstructionTypeEquals(const HInstruction* other) const OVERRIDE { \
1477 return other->Is##type(); \
1478 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001479 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001480
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001481#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1482 bool Is##type() const { return As##type() != nullptr; } \
1483 const H##type* As##type() const { return this; } \
1484 H##type* As##type() { return this; }
1485
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001486template <typename T>
Vladimir Marko82b07402017-03-01 19:02:04 +00001487class HUseListNode : public ArenaObject<kArenaAllocUseListNode>,
1488 public IntrusiveForwardListNode<HUseListNode<T>> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001489 public:
Igor Murashkind01745e2017-04-05 16:40:31 -07001490 // Get the instruction which has this use as one of the inputs.
David Brazdiled596192015-01-23 10:39:45 +00001491 T GetUser() const { return user_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001492 // Get the position of the input record that this use corresponds to.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001493 size_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001494 // Set the position of the input record that this use corresponds to.
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001495 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001496
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001497 private:
David Brazdiled596192015-01-23 10:39:45 +00001498 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001499 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001500
1501 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001502 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001503
Vladimir Marko46817b82016-03-29 12:21:58 +01001504 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001505
1506 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1507};
1508
David Brazdiled596192015-01-23 10:39:45 +00001509template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001510using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001511
David Brazdil1abb4192015-02-17 18:33:36 +00001512// This class is used by HEnvironment and HInstruction classes to record the
1513// instructions they use and pointers to the corresponding HUseListNodes kept
1514// by the used instructions.
1515template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001516class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001517 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001518 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1519 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001520
Vladimir Marko46817b82016-03-29 12:21:58 +01001521 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1522 : HUserRecord(old_record.instruction_, before_use_node) {}
1523 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1524 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001525 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001526 }
1527
1528 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001529 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1530 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001531
1532 private:
1533 // Instruction used by the user.
1534 HInstruction* instruction_;
1535
Vladimir Marko46817b82016-03-29 12:21:58 +01001536 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1537 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001538};
1539
Vladimir Markoe9004912016-06-16 16:50:52 +01001540// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1541// This is used for HInstruction::GetInputs() to return a container wrapper providing
1542// HInstruction* values even though the underlying container has HUserRecord<>s.
1543struct HInputExtractor {
1544 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1545 return record.GetInstruction();
1546 }
1547 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1548 return record.GetInstruction();
1549 }
1550};
1551
1552using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1553using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1554
Aart Bik854a02b2015-07-14 16:07:00 -07001555/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001556 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001557 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001558 * For write/read dependences on fields/arrays, the dependence analysis uses
1559 * type disambiguation (e.g. a float field write cannot modify the value of an
1560 * integer field read) and the access type (e.g. a reference array write cannot
1561 * modify the value of a reference field read [although it may modify the
1562 * reference fetch prior to reading the field, which is represented by its own
1563 * write/read dependence]). The analysis makes conservative points-to
1564 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1565 * the same, and any reference read depends on any reference read without
1566 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001567 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001568 * The internal representation uses 38-bit and is described in the table below.
1569 * The first line indicates the side effect, and for field/array accesses the
1570 * second line indicates the type of the access (in the order of the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001571 * DataType::Type enum).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001572 * The two numbered lines below indicate the bit position in the bitfield (read
1573 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001574 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001575 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1576 * +-------------+---------+---------+--------------+---------+---------+
1577 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1578 * | 3 |333333322|222222221| 1 |111111110|000000000|
1579 * | 7 |654321098|765432109| 8 |765432109|876543210|
1580 *
1581 * Note that, to ease the implementation, 'changes' bits are least significant
1582 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001583 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001584class SideEffects : public ValueObject {
1585 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001586 SideEffects() : flags_(0) {}
1587
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001588 static SideEffects None() {
1589 return SideEffects(0);
1590 }
1591
1592 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001593 return SideEffects(kAllChangeBits | kAllDependOnBits);
1594 }
1595
1596 static SideEffects AllChanges() {
1597 return SideEffects(kAllChangeBits);
1598 }
1599
1600 static SideEffects AllDependencies() {
1601 return SideEffects(kAllDependOnBits);
1602 }
1603
1604 static SideEffects AllExceptGCDependency() {
1605 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1606 }
1607
1608 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001609 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001610 }
1611
Aart Bik34c3ba92015-07-20 14:08:59 -07001612 static SideEffects AllWrites() {
1613 return SideEffects(kAllWrites);
1614 }
1615
1616 static SideEffects AllReads() {
1617 return SideEffects(kAllReads);
1618 }
1619
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001620 static SideEffects FieldWriteOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001621 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001622 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001623 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001624 }
1625
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001626 static SideEffects ArrayWriteOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001627 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001628 }
1629
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001630 static SideEffects FieldReadOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001631 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001632 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001633 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001634 }
1635
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001636 static SideEffects ArrayReadOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001637 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001638 }
1639
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001640 static SideEffects CanTriggerGC() {
1641 return SideEffects(1ULL << kCanTriggerGCBit);
1642 }
1643
1644 static SideEffects DependsOnGC() {
1645 return SideEffects(1ULL << kDependsOnGCBit);
1646 }
1647
Aart Bik854a02b2015-07-14 16:07:00 -07001648 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001649 SideEffects Union(SideEffects other) const {
1650 return SideEffects(flags_ | other.flags_);
1651 }
1652
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001653 SideEffects Exclusion(SideEffects other) const {
1654 return SideEffects(flags_ & ~other.flags_);
1655 }
1656
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001657 void Add(SideEffects other) {
1658 flags_ |= other.flags_;
1659 }
1660
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001661 bool Includes(SideEffects other) const {
1662 return (other.flags_ & flags_) == other.flags_;
1663 }
1664
1665 bool HasSideEffects() const {
1666 return (flags_ & kAllChangeBits);
1667 }
1668
1669 bool HasDependencies() const {
1670 return (flags_ & kAllDependOnBits);
1671 }
1672
1673 // Returns true if there are no side effects or dependencies.
1674 bool DoesNothing() const {
1675 return flags_ == 0;
1676 }
1677
Aart Bik854a02b2015-07-14 16:07:00 -07001678 // Returns true if something is written.
1679 bool DoesAnyWrite() const {
1680 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001681 }
1682
Aart Bik854a02b2015-07-14 16:07:00 -07001683 // Returns true if something is read.
1684 bool DoesAnyRead() const {
1685 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001686 }
1687
Aart Bik854a02b2015-07-14 16:07:00 -07001688 // Returns true if potentially everything is written and read
1689 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001690 bool DoesAllReadWrite() const {
1691 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1692 }
1693
Aart Bik854a02b2015-07-14 16:07:00 -07001694 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001695 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001696 }
1697
Roland Levillain0d5a2812015-11-13 10:07:31 +00001698 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001699 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001700 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1701 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001702 }
1703
1704 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001705 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001706 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001707 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001708 for (int s = kLastBit; s >= 0; s--) {
1709 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1710 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1711 // This is a bit for the GC side effect.
1712 if (current_bit_is_set) {
1713 flags += "GC";
1714 }
Aart Bik854a02b2015-07-14 16:07:00 -07001715 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001716 } else {
1717 // This is a bit for the array/field analysis.
1718 // The underscore character stands for the 'can trigger GC' bit.
1719 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1720 if (current_bit_is_set) {
1721 flags += kDebug[s];
1722 }
1723 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1724 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1725 flags += "|";
1726 }
1727 }
Aart Bik854a02b2015-07-14 16:07:00 -07001728 }
1729 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001730 }
1731
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001732 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001733
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001734 private:
1735 static constexpr int kFieldArrayAnalysisBits = 9;
1736
1737 static constexpr int kFieldWriteOffset = 0;
1738 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1739 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1740 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1741
1742 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1743
1744 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1745 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1746 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1747 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1748
1749 static constexpr int kLastBit = kDependsOnGCBit;
1750 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1751
1752 // Aliases.
1753
1754 static_assert(kChangeBits == kDependOnBits,
1755 "the 'change' bits should match the 'depend on' bits.");
1756
1757 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1758 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1759 static constexpr uint64_t kAllWrites =
1760 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1761 static constexpr uint64_t kAllReads =
1762 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001763
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001764 // Translates type to bit flag. The type must correspond to a Java type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001765 static uint64_t TypeFlag(DataType::Type type, int offset) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001766 int shift;
1767 switch (type) {
1768 case DataType::Type::kReference: shift = 0; break;
1769 case DataType::Type::kBool: shift = 1; break;
1770 case DataType::Type::kInt8: shift = 2; break;
1771 case DataType::Type::kUint16: shift = 3; break;
1772 case DataType::Type::kInt16: shift = 4; break;
1773 case DataType::Type::kInt32: shift = 5; break;
1774 case DataType::Type::kInt64: shift = 6; break;
1775 case DataType::Type::kFloat32: shift = 7; break;
1776 case DataType::Type::kFloat64: shift = 8; break;
1777 default:
1778 LOG(FATAL) << "Unexpected data type " << type;
1779 UNREACHABLE();
1780 }
Aart Bik854a02b2015-07-14 16:07:00 -07001781 DCHECK_LE(kFieldWriteOffset, shift);
1782 DCHECK_LT(shift, kArrayWriteOffset);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001783 return UINT64_C(1) << (shift + offset);
Aart Bik854a02b2015-07-14 16:07:00 -07001784 }
1785
1786 // Private constructor on direct flags value.
1787 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1788
1789 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001790};
1791
David Brazdiled596192015-01-23 10:39:45 +00001792// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001793class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001794 public:
Mingyao Yang01b47b02017-02-03 12:09:57 -08001795 ALWAYS_INLINE HEnvironment(ArenaAllocator* arena,
1796 size_t number_of_vregs,
1797 ArtMethod* method,
1798 uint32_t dex_pc,
1799 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001800 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
Vladimir Markoec32f642017-06-02 10:51:55 +01001801 locations_(arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001802 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001803 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001804 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001805 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001806 }
1807
Mingyao Yang01b47b02017-02-03 12:09:57 -08001808 ALWAYS_INLINE HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001809 : HEnvironment(arena,
1810 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001811 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001812 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001813 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001814
Vladimir Markoec32f642017-06-02 10:51:55 +01001815 void AllocateLocations() {
1816 DCHECK(locations_.empty());
1817 locations_.resize(vregs_.size());
1818 }
1819
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001820 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001821 if (parent_ != nullptr) {
1822 parent_->SetAndCopyParentChain(allocator, parent);
1823 } else {
1824 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1825 parent_->CopyFrom(parent);
1826 if (parent->GetParent() != nullptr) {
1827 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1828 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001829 }
David Brazdiled596192015-01-23 10:39:45 +00001830 }
1831
Vladimir Marko71bf8092015-09-15 15:33:14 +01001832 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001833 void CopyFrom(HEnvironment* environment);
1834
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001835 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1836 // input to the loop phi instead. This is for inserting instructions that
1837 // require an environment (like HDeoptimization) in the loop pre-header.
1838 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001839
1840 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001841 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001842 }
1843
1844 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001845 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001846 }
1847
David Brazdil1abb4192015-02-17 18:33:36 +00001848 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001849
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001850 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001851
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001852 HEnvironment* GetParent() const { return parent_; }
1853
1854 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001855 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001856 }
1857
1858 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001859 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001860 }
1861
1862 uint32_t GetDexPc() const {
1863 return dex_pc_;
1864 }
1865
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001866 ArtMethod* GetMethod() const {
1867 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001868 }
1869
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001870 HInstruction* GetHolder() const {
1871 return holder_;
1872 }
1873
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001874
1875 bool IsFromInlinedInvoke() const {
1876 return GetParent() != nullptr;
1877 }
1878
David Brazdiled596192015-01-23 10:39:45 +00001879 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001880 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1881 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001882 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001883 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001884 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00001885
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001886 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001887 HInstruction* const holder_;
1888
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001889 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001890
1891 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1892};
1893
Vladimir Markof9f64412015-09-02 14:05:49 +01001894class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001895 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001896 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001897 : previous_(nullptr),
1898 next_(nullptr),
1899 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001900 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001901 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001902 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001903 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001904 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001905 locations_(nullptr),
1906 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001907 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001908 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001909 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1910 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1911 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001912
Dave Allison20dfc792014-06-16 20:44:29 -07001913 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001914
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001915#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001916 enum InstructionKind {
1917 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1918 };
1919#undef DECLARE_KIND
1920
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001921 HInstruction* GetNext() const { return next_; }
1922 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001923
Calin Juravle77520bc2015-01-12 18:45:46 +00001924 HInstruction* GetNextDisregardingMoves() const;
1925 HInstruction* GetPreviousDisregardingMoves() const;
1926
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001927 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001928 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001929 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001930 bool IsInBlock() const { return block_ != nullptr; }
1931 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001932 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1933 bool IsIrreducibleLoopHeaderPhi() const {
1934 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1935 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001936
Vladimir Marko372f10e2016-05-17 16:30:10 +01001937 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
1938
1939 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
1940 // One virtual method is enough, just const_cast<> and then re-add the const.
1941 return ArrayRef<const HUserRecord<HInstruction*>>(
1942 const_cast<HInstruction*>(this)->GetInputRecords());
1943 }
1944
Vladimir Markoe9004912016-06-16 16:50:52 +01001945 HInputsRef GetInputs() {
1946 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001947 }
1948
Vladimir Markoe9004912016-06-16 16:50:52 +01001949 HConstInputsRef GetInputs() const {
1950 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001951 }
1952
1953 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001954 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001955
Aart Bik2767f4b2016-10-28 15:03:53 -07001956 bool HasInput(HInstruction* input) const {
1957 for (const HInstruction* i : GetInputs()) {
1958 if (i == input) {
1959 return true;
1960 }
1961 }
1962 return false;
1963 }
1964
Vladimir Marko372f10e2016-05-17 16:30:10 +01001965 void SetRawInputAt(size_t index, HInstruction* input) {
1966 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1967 }
1968
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001969 virtual void Accept(HGraphVisitor* visitor) = 0;
1970 virtual const char* DebugName() const = 0;
1971
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001972 virtual DataType::Type GetType() const { return DataType::Type::kVoid; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001973
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001974 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001975
1976 uint32_t GetDexPc() const { return dex_pc_; }
1977
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001978 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001979
Nicolas Geoffray331605a2017-03-01 11:01:41 +00001980 // Can the instruction throw?
1981 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
1982 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01001983 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001984 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001985
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001986 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001987 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001988
Calin Juravle10e244f2015-01-26 18:54:32 +00001989 // Does not apply for all instructions, but having this at top level greatly
1990 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001991 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001992 virtual bool CanBeNull() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001993 DCHECK_EQ(GetType(), DataType::Type::kReference) << "CanBeNull only applies to reference types";
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001994 return true;
1995 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001996
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001997 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001998 return false;
1999 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002000
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002001 virtual bool IsActualObject() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002002 return GetType() == DataType::Type::kReference;
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002003 }
2004
Calin Juravle2e768302015-07-28 14:41:11 +00002005 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00002006
Calin Juravle61d544b2015-02-23 16:46:57 +00002007 ReferenceTypeInfo GetReferenceTypeInfo() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002008 DCHECK_EQ(GetType(), DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002009 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00002010 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00002011 }
Calin Juravleacf735c2015-02-12 15:25:22 +00002012
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002013 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00002014 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002015 // Note: fixup_end remains valid across push_front().
2016 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
2017 HUseListNode<HInstruction*>* new_node =
2018 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HInstruction*>(user, index);
2019 uses_.push_front(*new_node);
2020 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002021 }
2022
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002023 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01002024 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002025 // Note: env_fixup_end remains valid across push_front().
2026 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
2027 HUseListNode<HEnvironment*>* new_node =
2028 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HEnvironment*>(user, index);
2029 env_uses_.push_front(*new_node);
2030 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002031 }
2032
David Brazdil1abb4192015-02-17 18:33:36 +00002033 void RemoveAsUserOfInput(size_t input) {
2034 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002035 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2036 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2037 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00002038 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002039
Vladimir Marko372f10e2016-05-17 16:30:10 +01002040 void RemoveAsUserOfAllInputs() {
2041 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2042 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2043 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2044 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
2045 }
2046 }
2047
David Brazdil1abb4192015-02-17 18:33:36 +00002048 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2049 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002050
Vladimir Marko46817b82016-03-29 12:21:58 +01002051 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2052 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2053 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002054 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002055 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002056 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002057
Aart Bikcc42be02016-10-20 16:14:16 -07002058 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002059 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002060 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002061 !CanThrow() &&
2062 !IsSuspendCheck() &&
2063 !IsControlFlow() &&
2064 !IsNativeDebugInfo() &&
2065 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002066 // If we added an explicit barrier then we should keep it.
Igor Murashkind01745e2017-04-05 16:40:31 -07002067 !IsMemoryBarrier() &&
2068 !IsConstructorFence();
Aart Bik482095d2016-10-10 15:39:10 -07002069 }
2070
Aart Bikcc42be02016-10-20 16:14:16 -07002071 bool IsDeadAndRemovable() const {
2072 return IsRemovable() && !HasUses();
2073 }
2074
Roland Levillain6c82d402014-10-13 16:10:27 +01002075 // Does this instruction strictly dominate `other_instruction`?
2076 // Returns false if this instruction and `other_instruction` are the same.
2077 // Aborts if this instruction and `other_instruction` are both phis.
2078 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002079
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002080 int GetId() const { return id_; }
2081 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002082
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002083 int GetSsaIndex() const { return ssa_index_; }
2084 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2085 bool HasSsaIndex() const { return ssa_index_ != -1; }
2086
2087 bool HasEnvironment() const { return environment_ != nullptr; }
2088 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002089 // Set the `environment_` field. Raw because this method does not
2090 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002091 void SetRawEnvironment(HEnvironment* environment) {
2092 DCHECK(environment_ == nullptr);
2093 DCHECK_EQ(environment->GetHolder(), this);
2094 environment_ = environment;
2095 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002096
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002097 void InsertRawEnvironment(HEnvironment* environment) {
2098 DCHECK(environment_ != nullptr);
2099 DCHECK_EQ(environment->GetHolder(), this);
2100 DCHECK(environment->GetParent() == nullptr);
2101 environment->parent_ = environment_;
2102 environment_ = environment;
2103 }
2104
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002105 void RemoveEnvironment();
2106
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002107 // Set the environment of this instruction, copying it from `environment`. While
2108 // copying, the uses lists are being updated.
2109 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002110 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002111 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002112 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002113 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002114 if (environment->GetParent() != nullptr) {
2115 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2116 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002117 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002118
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002119 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2120 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002121 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002122 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002123 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002124 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002125 if (environment->GetParent() != nullptr) {
2126 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2127 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002128 }
2129
Nicolas Geoffray39468442014-09-02 15:17:15 +01002130 // Returns the number of entries in the environment. Typically, that is the
2131 // number of dex registers in a method. It could be more in case of inlining.
2132 size_t EnvironmentSize() const;
2133
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002134 LocationSummary* GetLocations() const { return locations_; }
2135 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002136
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002137 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002138 void ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002139 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002140
Alexandre Rames188d4312015-04-09 18:30:21 +01002141 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2142 // uses of this instruction by `other` are *not* updated.
2143 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2144 ReplaceWith(other);
2145 other->ReplaceInput(this, use_index);
2146 }
2147
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002148 // Move `this` instruction before `cursor`
2149 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002150
Vladimir Markofb337ea2015-11-25 15:25:10 +00002151 // Move `this` before its first user and out of any loops. If there is no
2152 // out-of-loop user that dominates all other users, move the instruction
2153 // to the end of the out-of-loop common dominator of the user's blocks.
2154 //
2155 // This can be used only on non-throwing instructions with no side effects that
2156 // have at least one use but no environment uses.
2157 void MoveBeforeFirstUserAndOutOfLoops();
2158
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002159#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002160 bool Is##type() const; \
2161 const H##type* As##type() const; \
2162 H##type* As##type();
2163
2164 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2165#undef INSTRUCTION_TYPE_CHECK
2166
2167#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002168 bool Is##type() const { return (As##type() != nullptr); } \
2169 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002170 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002171 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002172#undef INSTRUCTION_TYPE_CHECK
2173
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002174 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002175 // TODO: this method is used by LICM and GVN with possibly different
2176 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002177 virtual bool CanBeMoved() const { return false; }
2178
2179 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002180 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002181 return false;
2182 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002183
2184 // Returns whether any data encoded in the two instructions is equal.
2185 // This method does not look at the inputs. Both instructions must be
2186 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002187 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002188 return false;
2189 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002190
2191 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002192 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002193 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002194 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002195
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002196 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2197 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2198 // the virtual function because the __attribute__((__pure__)) doesn't really
2199 // apply the strong requirement for virtual functions, preventing optimizations.
2200 InstructionKind GetKind() const PURE;
2201 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002202
2203 virtual size_t ComputeHashCode() const {
2204 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002205 for (const HInstruction* input : GetInputs()) {
2206 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002207 }
2208 return result;
2209 }
2210
2211 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002212 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002213 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002214
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002215 size_t GetLifetimePosition() const { return lifetime_position_; }
2216 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2217 LiveInterval* GetLiveInterval() const { return live_interval_; }
2218 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2219 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2220
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002221 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2222
2223 // Returns whether the code generation of the instruction will require to have access
2224 // to the current method. Such instructions are:
2225 // (1): Instructions that require an environment, as calling the runtime requires
2226 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002227 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2228 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002229 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002230 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002231 }
2232
Vladimir Markodc151b22015-10-15 18:02:30 +01002233 // Returns whether the code generation of the instruction will require to have access
2234 // to the dex cache of the current method's declaring class via the current method.
2235 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002236
Mark Mendellc4701932015-04-10 13:18:51 -04002237 // Does this instruction have any use in an environment before
2238 // control flow hits 'other'?
2239 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2240
2241 // Remove all references to environment uses of this instruction.
2242 // The caller must ensure that this is safe to do.
2243 void RemoveEnvironmentUsers();
2244
Vladimir Markoa1de9182016-02-25 11:37:38 +00002245 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2246 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002247
David Brazdil1abb4192015-02-17 18:33:36 +00002248 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002249 // If set, the machine code for this instruction is assumed to be generated by
2250 // its users. Used by liveness analysis to compute use positions accordingly.
2251 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2252 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2253 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2254 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2255
Vladimir Marko372f10e2016-05-17 16:30:10 +01002256 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2257 return GetInputRecords()[i];
2258 }
2259
2260 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2261 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2262 input_records[index] = input;
2263 }
David Brazdil1abb4192015-02-17 18:33:36 +00002264
Vladimir Markoa1de9182016-02-25 11:37:38 +00002265 uint32_t GetPackedFields() const {
2266 return packed_fields_;
2267 }
2268
2269 template <size_t flag>
2270 bool GetPackedFlag() const {
2271 return (packed_fields_ & (1u << flag)) != 0u;
2272 }
2273
2274 template <size_t flag>
2275 void SetPackedFlag(bool value = true) {
2276 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2277 }
2278
2279 template <typename BitFieldType>
2280 typename BitFieldType::value_type GetPackedField() const {
2281 return BitFieldType::Decode(packed_fields_);
2282 }
2283
2284 template <typename BitFieldType>
2285 void SetPackedField(typename BitFieldType::value_type value) {
2286 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2287 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2288 }
2289
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002290 private:
Vladimir Marko46817b82016-03-29 12:21:58 +01002291 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2292 auto before_use_node = uses_.before_begin();
2293 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2294 HInstruction* user = use_node->GetUser();
2295 size_t input_index = use_node->GetIndex();
2296 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2297 before_use_node = use_node;
2298 }
2299 }
2300
2301 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2302 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2303 if (next != uses_.end()) {
2304 HInstruction* next_user = next->GetUser();
2305 size_t next_index = next->GetIndex();
2306 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2307 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2308 }
2309 }
2310
2311 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2312 auto before_env_use_node = env_uses_.before_begin();
2313 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2314 HEnvironment* user = env_use_node->GetUser();
2315 size_t input_index = env_use_node->GetIndex();
2316 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2317 before_env_use_node = env_use_node;
2318 }
2319 }
2320
2321 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2322 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2323 if (next != env_uses_.end()) {
2324 HEnvironment* next_user = next->GetUser();
2325 size_t next_index = next->GetIndex();
2326 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2327 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2328 }
2329 }
David Brazdil1abb4192015-02-17 18:33:36 +00002330
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002331 HInstruction* previous_;
2332 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002333 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002334 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002335
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002336 // An instruction gets an id when it is added to the graph.
2337 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002338 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002339 int id_;
2340
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002341 // When doing liveness analysis, instructions that have uses get an SSA index.
2342 int ssa_index_;
2343
Vladimir Markoa1de9182016-02-25 11:37:38 +00002344 // Packed fields.
2345 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002346
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002347 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002348 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002349
2350 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002351 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002352
Nicolas Geoffray39468442014-09-02 15:17:15 +01002353 // The environment associated with this instruction. Not null if the instruction
2354 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002355 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002356
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002357 // Set by the code generator.
2358 LocationSummary* locations_;
2359
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002360 // Set by the liveness analysis.
2361 LiveInterval* live_interval_;
2362
2363 // Set by the liveness analysis, this is the position in a linear
2364 // order of blocks where this instruction's live interval start.
2365 size_t lifetime_position_;
2366
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002367 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002368
Vladimir Markoa1de9182016-02-25 11:37:38 +00002369 // The reference handle part of the reference type info.
2370 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002371 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002372 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002373
David Brazdil1abb4192015-02-17 18:33:36 +00002374 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002375 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002376 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002377 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002378 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002379
2380 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2381};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002382std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002383
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002384// Iterates over the instructions, while preserving the next instruction
2385// in case the current instruction gets removed from the list by the user
2386// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002387class HInstructionIterator : public ValueObject {
2388 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002389 explicit HInstructionIterator(const HInstructionList& instructions)
2390 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002391 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002392 }
2393
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002394 bool Done() const { return instruction_ == nullptr; }
2395 HInstruction* Current() const { return instruction_; }
2396 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002397 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002398 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002399 }
2400
2401 private:
2402 HInstruction* instruction_;
2403 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002404
2405 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002406};
2407
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002408// Iterates over the instructions without saving the next instruction,
2409// therefore handling changes in the graph potentially made by the user
2410// of this iterator.
2411class HInstructionIteratorHandleChanges : public ValueObject {
2412 public:
2413 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2414 : instruction_(instructions.first_instruction_) {
2415 }
2416
2417 bool Done() const { return instruction_ == nullptr; }
2418 HInstruction* Current() const { return instruction_; }
2419 void Advance() {
2420 instruction_ = instruction_->GetNext();
2421 }
2422
2423 private:
2424 HInstruction* instruction_;
2425
2426 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2427};
2428
2429
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002430class HBackwardInstructionIterator : public ValueObject {
2431 public:
2432 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2433 : instruction_(instructions.last_instruction_) {
2434 next_ = Done() ? nullptr : instruction_->GetPrevious();
2435 }
2436
2437 bool Done() const { return instruction_ == nullptr; }
2438 HInstruction* Current() const { return instruction_; }
2439 void Advance() {
2440 instruction_ = next_;
2441 next_ = Done() ? nullptr : instruction_->GetPrevious();
2442 }
2443
2444 private:
2445 HInstruction* instruction_;
2446 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002447
2448 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002449};
2450
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002451class HVariableInputSizeInstruction : public HInstruction {
2452 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002453 using HInstruction::GetInputRecords; // Keep the const version visible.
2454 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
2455 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2456 }
2457
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002458 void AddInput(HInstruction* input);
2459 void InsertInputAt(size_t index, HInstruction* input);
2460 void RemoveInputAt(size_t index);
2461
Igor Murashkind01745e2017-04-05 16:40:31 -07002462 // Removes all the inputs.
2463 // Also removes this instructions from each input's use list
2464 // (for non-environment uses only).
2465 void RemoveAllInputs();
2466
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002467 protected:
2468 HVariableInputSizeInstruction(SideEffects side_effects,
2469 uint32_t dex_pc,
2470 ArenaAllocator* arena,
2471 size_t number_of_inputs,
2472 ArenaAllocKind kind)
2473 : HInstruction(side_effects, dex_pc),
2474 inputs_(number_of_inputs, arena->Adapter(kind)) {}
2475
2476 ArenaVector<HUserRecord<HInstruction*>> inputs_;
2477
2478 private:
2479 DISALLOW_COPY_AND_ASSIGN(HVariableInputSizeInstruction);
2480};
2481
Vladimir Markof9f64412015-09-02 14:05:49 +01002482template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002483class HTemplateInstruction: public HInstruction {
2484 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002485 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002486 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002487 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002488
Vladimir Marko372f10e2016-05-17 16:30:10 +01002489 using HInstruction::GetInputRecords; // Keep the const version visible.
2490 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2491 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002492 }
2493
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002494 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002495 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002496
2497 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002498};
2499
Vladimir Markof9f64412015-09-02 14:05:49 +01002500// HTemplateInstruction specialization for N=0.
2501template<>
2502class HTemplateInstruction<0>: public HInstruction {
2503 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002504 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002505 : HInstruction(side_effects, dex_pc) {}
2506
Vladimir Markof9f64412015-09-02 14:05:49 +01002507 virtual ~HTemplateInstruction() {}
2508
Vladimir Marko372f10e2016-05-17 16:30:10 +01002509 using HInstruction::GetInputRecords; // Keep the const version visible.
2510 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2511 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002512 }
2513
2514 private:
2515 friend class SsaBuilder;
2516};
2517
Dave Allison20dfc792014-06-16 20:44:29 -07002518template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002519class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002520 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002521 HExpression<N>(DataType::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002522 : HTemplateInstruction<N>(side_effects, dex_pc) {
2523 this->template SetPackedField<TypeField>(type);
2524 }
Dave Allison20dfc792014-06-16 20:44:29 -07002525 virtual ~HExpression() {}
2526
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002527 DataType::Type GetType() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00002528 return TypeField::Decode(this->GetPackedFields());
2529 }
Dave Allison20dfc792014-06-16 20:44:29 -07002530
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002531 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002532 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2533 static constexpr size_t kFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002534 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00002535 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2536 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2537 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002538 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002539};
2540
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002541// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2542// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002543class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002544 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002545 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2546 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002547
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002548 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002549
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002550 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002551
2552 private:
2553 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2554};
2555
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002556// Represents dex's RETURN opcodes. A HReturn is a control flow
2557// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002558class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002559 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002560 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2561 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002562 SetRawInputAt(0, value);
2563 }
2564
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002565 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002566
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002567 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002568
2569 private:
2570 DISALLOW_COPY_AND_ASSIGN(HReturn);
2571};
2572
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002573class HPhi FINAL : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002574 public:
2575 HPhi(ArenaAllocator* arena,
2576 uint32_t reg_number,
2577 size_t number_of_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002578 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +00002579 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002580 : HVariableInputSizeInstruction(
2581 SideEffects::None(),
2582 dex_pc,
2583 arena,
2584 number_of_inputs,
2585 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002586 reg_number_(reg_number) {
2587 SetPackedField<TypeField>(ToPhiType(type));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002588 DCHECK_NE(GetType(), DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +00002589 // Phis are constructed live and marked dead if conflicting or unused.
2590 // Individual steps of SsaBuilder should assume that if a phi has been
2591 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2592 SetPackedFlag<kFlagIsLive>(true);
2593 SetPackedFlag<kFlagCanBeNull>(true);
2594 }
2595
2596 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002597 static DataType::Type ToPhiType(DataType::Type type) {
2598 return DataType::Kind(type);
David Brazdildee58d62016-04-07 09:54:26 +00002599 }
2600
2601 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2602
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002603 DataType::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2604 void SetType(DataType::Type new_type) {
David Brazdildee58d62016-04-07 09:54:26 +00002605 // Make sure that only valid type changes occur. The following are allowed:
2606 // (1) int -> float/ref (primitive type propagation),
2607 // (2) long -> double (primitive type propagation).
2608 DCHECK(GetType() == new_type ||
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002609 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kFloat32) ||
2610 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kReference) ||
2611 (GetType() == DataType::Type::kInt64 && new_type == DataType::Type::kFloat64));
David Brazdildee58d62016-04-07 09:54:26 +00002612 SetPackedField<TypeField>(new_type);
2613 }
2614
2615 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2616 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2617
2618 uint32_t GetRegNumber() const { return reg_number_; }
2619
2620 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2621 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2622 bool IsDead() const { return !IsLive(); }
2623 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2624
Vladimir Markoe9004912016-06-16 16:50:52 +01002625 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002626 return other != nullptr
2627 && other->IsPhi()
2628 && other->AsPhi()->GetBlock() == GetBlock()
2629 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2630 }
2631
Nicolas Geoffrayf57c1ae2017-06-28 17:40:18 +01002632 bool HasEquivalentPhi() const {
2633 if (GetPrevious() != nullptr && GetPrevious()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2634 return true;
2635 }
2636 if (GetNext() != nullptr && GetNext()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2637 return true;
2638 }
2639 return false;
2640 }
2641
David Brazdildee58d62016-04-07 09:54:26 +00002642 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2643 // An equivalent phi is a phi having the same dex register and type.
2644 // It assumes that phis with the same dex register are adjacent.
2645 HPhi* GetNextEquivalentPhiWithSameType() {
2646 HInstruction* next = GetNext();
2647 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2648 if (next->GetType() == GetType()) {
2649 return next->AsPhi();
2650 }
2651 next = next->GetNext();
2652 }
2653 return nullptr;
2654 }
2655
2656 DECLARE_INSTRUCTION(Phi);
2657
David Brazdildee58d62016-04-07 09:54:26 +00002658 private:
2659 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2660 static constexpr size_t kFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002661 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
David Brazdildee58d62016-04-07 09:54:26 +00002662 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2663 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2664 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2665 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002666 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
David Brazdildee58d62016-04-07 09:54:26 +00002667
David Brazdildee58d62016-04-07 09:54:26 +00002668 const uint32_t reg_number_;
2669
2670 DISALLOW_COPY_AND_ASSIGN(HPhi);
2671};
2672
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002673// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002674// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002675// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002676class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002677 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002678 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002679
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002680 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002681
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002682 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002683
2684 private:
2685 DISALLOW_COPY_AND_ASSIGN(HExit);
2686};
2687
2688// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002689class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002690 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002691 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002692
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002693 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002694
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002695 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002696 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002697 }
2698
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002699 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002700
2701 private:
2702 DISALLOW_COPY_AND_ASSIGN(HGoto);
2703};
2704
Roland Levillain9867bc72015-08-05 10:21:34 +01002705class HConstant : public HExpression<0> {
2706 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002707 explicit HConstant(DataType::Type type, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002708 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002709
2710 bool CanBeMoved() const OVERRIDE { return true; }
2711
Roland Levillain1a653882016-03-18 18:05:57 +00002712 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002713 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002714 // Is this constant 0 in the arithmetic sense?
2715 virtual bool IsArithmeticZero() const { return false; }
2716 // Is this constant a 0-bit pattern?
2717 virtual bool IsZeroBitPattern() const { return false; }
2718 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002719 virtual bool IsOne() const { return false; }
2720
David Brazdil77a48ae2015-09-15 12:34:04 +00002721 virtual uint64_t GetValueAsUint64() const = 0;
2722
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002723 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002724
2725 private:
2726 DISALLOW_COPY_AND_ASSIGN(HConstant);
2727};
2728
Vladimir Markofcb503c2016-05-18 12:48:17 +01002729class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002730 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002731 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002732 return true;
2733 }
2734
David Brazdil77a48ae2015-09-15 12:34:04 +00002735 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2736
Roland Levillain9867bc72015-08-05 10:21:34 +01002737 size_t ComputeHashCode() const OVERRIDE { return 0; }
2738
Roland Levillain1a653882016-03-18 18:05:57 +00002739 // The null constant representation is a 0-bit pattern.
2740 virtual bool IsZeroBitPattern() const { return true; }
2741
Roland Levillain9867bc72015-08-05 10:21:34 +01002742 DECLARE_INSTRUCTION(NullConstant);
2743
2744 private:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002745 explicit HNullConstant(uint32_t dex_pc = kNoDexPc)
2746 : HConstant(DataType::Type::kReference, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002747
2748 friend class HGraph;
2749 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2750};
2751
2752// Constants of the type int. Those can be from Dex instructions, or
2753// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002754class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002755 public:
2756 int32_t GetValue() const { return value_; }
2757
David Brazdil9f389d42015-10-01 14:32:56 +01002758 uint64_t GetValueAsUint64() const OVERRIDE {
2759 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2760 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002761
Vladimir Marko372f10e2016-05-17 16:30:10 +01002762 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002763 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002764 return other->AsIntConstant()->value_ == value_;
2765 }
2766
2767 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2768
2769 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002770 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2771 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002772 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2773
Roland Levillain1a653882016-03-18 18:05:57 +00002774 // Integer constants are used to encode Boolean values as well,
2775 // where 1 means true and 0 means false.
2776 bool IsTrue() const { return GetValue() == 1; }
2777 bool IsFalse() const { return GetValue() == 0; }
2778
Roland Levillain9867bc72015-08-05 10:21:34 +01002779 DECLARE_INSTRUCTION(IntConstant);
2780
2781 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002782 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002783 : HConstant(DataType::Type::kInt32, dex_pc), value_(value) {}
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002784 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002785 : HConstant(DataType::Type::kInt32, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002786
2787 const int32_t value_;
2788
2789 friend class HGraph;
2790 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2791 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2792 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2793};
2794
Vladimir Markofcb503c2016-05-18 12:48:17 +01002795class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002796 public:
2797 int64_t GetValue() const { return value_; }
2798
David Brazdil77a48ae2015-09-15 12:34:04 +00002799 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2800
Vladimir Marko372f10e2016-05-17 16:30:10 +01002801 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002802 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002803 return other->AsLongConstant()->value_ == value_;
2804 }
2805
2806 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2807
2808 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002809 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2810 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002811 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2812
2813 DECLARE_INSTRUCTION(LongConstant);
2814
2815 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002816 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002817 : HConstant(DataType::Type::kInt64, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002818
2819 const int64_t value_;
2820
2821 friend class HGraph;
2822 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2823};
Dave Allison20dfc792014-06-16 20:44:29 -07002824
Vladimir Markofcb503c2016-05-18 12:48:17 +01002825class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002826 public:
2827 float GetValue() const { return value_; }
2828
2829 uint64_t GetValueAsUint64() const OVERRIDE {
2830 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2831 }
2832
Vladimir Marko372f10e2016-05-17 16:30:10 +01002833 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002834 DCHECK(other->IsFloatConstant()) << other->DebugName();
2835 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2836 }
2837
2838 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2839
2840 bool IsMinusOne() const OVERRIDE {
2841 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2842 }
Roland Levillain1a653882016-03-18 18:05:57 +00002843 bool IsArithmeticZero() const OVERRIDE {
2844 return std::fpclassify(value_) == FP_ZERO;
2845 }
2846 bool IsArithmeticPositiveZero() const {
2847 return IsArithmeticZero() && !std::signbit(value_);
2848 }
2849 bool IsArithmeticNegativeZero() const {
2850 return IsArithmeticZero() && std::signbit(value_);
2851 }
2852 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002853 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002854 }
2855 bool IsOne() const OVERRIDE {
2856 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2857 }
2858 bool IsNaN() const {
2859 return std::isnan(value_);
2860 }
2861
2862 DECLARE_INSTRUCTION(FloatConstant);
2863
2864 private:
2865 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002866 : HConstant(DataType::Type::kFloat32, dex_pc), value_(value) {}
Roland Levillain31dd3d62016-02-16 12:21:02 +00002867 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002868 : HConstant(DataType::Type::kFloat32, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
Roland Levillain31dd3d62016-02-16 12:21:02 +00002869
2870 const float value_;
2871
2872 // Only the SsaBuilder and HGraph can create floating-point constants.
2873 friend class SsaBuilder;
2874 friend class HGraph;
2875 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2876};
2877
Vladimir Markofcb503c2016-05-18 12:48:17 +01002878class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002879 public:
2880 double GetValue() const { return value_; }
2881
2882 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2883
Vladimir Marko372f10e2016-05-17 16:30:10 +01002884 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002885 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2886 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2887 }
2888
2889 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2890
2891 bool IsMinusOne() const OVERRIDE {
2892 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2893 }
Roland Levillain1a653882016-03-18 18:05:57 +00002894 bool IsArithmeticZero() const OVERRIDE {
2895 return std::fpclassify(value_) == FP_ZERO;
2896 }
2897 bool IsArithmeticPositiveZero() const {
2898 return IsArithmeticZero() && !std::signbit(value_);
2899 }
2900 bool IsArithmeticNegativeZero() const {
2901 return IsArithmeticZero() && std::signbit(value_);
2902 }
2903 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002904 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002905 }
2906 bool IsOne() const OVERRIDE {
2907 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2908 }
2909 bool IsNaN() const {
2910 return std::isnan(value_);
2911 }
2912
2913 DECLARE_INSTRUCTION(DoubleConstant);
2914
2915 private:
2916 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002917 : HConstant(DataType::Type::kFloat64, dex_pc), value_(value) {}
Roland Levillain31dd3d62016-02-16 12:21:02 +00002918 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002919 : HConstant(DataType::Type::kFloat64, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
Roland Levillain31dd3d62016-02-16 12:21:02 +00002920
2921 const double value_;
2922
2923 // Only the SsaBuilder and HGraph can create floating-point constants.
2924 friend class SsaBuilder;
2925 friend class HGraph;
2926 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2927};
2928
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002929// Conditional branch. A block ending with an HIf instruction must have
2930// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002931class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002932 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002933 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2934 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002935 SetRawInputAt(0, input);
2936 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002937
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002938 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002939
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002940 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002941 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002942 }
2943
2944 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002945 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002946 }
2947
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002948 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002949
2950 private:
2951 DISALLOW_COPY_AND_ASSIGN(HIf);
2952};
2953
David Brazdilfc6a86a2015-06-26 10:33:45 +00002954
2955// Abstract instruction which marks the beginning and/or end of a try block and
2956// links it to the respective exception handlers. Behaves the same as a Goto in
2957// non-exceptional control flow.
2958// Normal-flow successor is stored at index zero, exception handlers under
2959// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002960class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002961 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002962 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002963 kEntry,
2964 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002965 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002966 };
2967
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002968 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002969 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2970 SetPackedField<BoundaryKindField>(kind);
2971 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002972
2973 bool IsControlFlow() const OVERRIDE { return true; }
2974
2975 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002976 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002977
David Brazdild26a4112015-11-10 11:07:31 +00002978 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2979 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2980 }
2981
David Brazdilfc6a86a2015-06-26 10:33:45 +00002982 // Returns whether `handler` is among its exception handlers (non-zero index
2983 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002984 bool HasExceptionHandler(const HBasicBlock& handler) const {
2985 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002986 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002987 }
2988
2989 // If not present already, adds `handler` to its block's list of exception
2990 // handlers.
2991 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002992 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002993 GetBlock()->AddSuccessor(handler);
2994 }
2995 }
2996
Vladimir Markoa1de9182016-02-25 11:37:38 +00002997 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2998 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002999
David Brazdilffee3d32015-07-06 11:48:53 +01003000 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
3001
David Brazdilfc6a86a2015-06-26 10:33:45 +00003002 DECLARE_INSTRUCTION(TryBoundary);
3003
3004 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003005 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
3006 static constexpr size_t kFieldBoundaryKindSize =
3007 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
3008 static constexpr size_t kNumberOfTryBoundaryPackedBits =
3009 kFieldBoundaryKind + kFieldBoundaryKindSize;
3010 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
3011 "Too many packed fields.");
3012 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00003013
3014 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
3015};
3016
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003017// Deoptimize to interpreter, upon checking a condition.
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003018class HDeoptimize FINAL : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003019 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003020 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
3021 // across.
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003022 HDeoptimize(ArenaAllocator* arena, HInstruction* cond, DeoptimizationKind kind, uint32_t dex_pc)
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003023 : HVariableInputSizeInstruction(
3024 SideEffects::All(),
3025 dex_pc,
3026 arena,
3027 /* number_of_inputs */ 1,
3028 kArenaAllocMisc) {
3029 SetPackedFlag<kFieldCanBeMoved>(false);
3030 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003031 SetRawInputAt(0, cond);
3032 }
3033
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003034 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
3035 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
3036 // instead of `guard`.
3037 // We set CanTriggerGC to prevent any intermediate address to be live
3038 // at the point of the `HDeoptimize`.
3039 HDeoptimize(ArenaAllocator* arena,
3040 HInstruction* cond,
3041 HInstruction* guard,
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003042 DeoptimizationKind kind,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003043 uint32_t dex_pc)
3044 : HVariableInputSizeInstruction(
3045 SideEffects::CanTriggerGC(),
3046 dex_pc,
3047 arena,
3048 /* number_of_inputs */ 2,
3049 kArenaAllocMisc) {
3050 SetPackedFlag<kFieldCanBeMoved>(true);
3051 SetPackedField<DeoptimizeKindField>(kind);
3052 SetRawInputAt(0, cond);
3053 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003054 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003055
3056 bool CanBeMoved() const OVERRIDE { return GetPackedFlag<kFieldCanBeMoved>(); }
3057
3058 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
3059 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
3060 }
3061
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003062 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003063
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003064 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003065
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003066 DeoptimizationKind GetDeoptimizationKind() const { return GetPackedField<DeoptimizeKindField>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003067
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003068 DataType::Type GetType() const OVERRIDE {
3069 return GuardsAnInput() ? GuardedInput()->GetType() : DataType::Type::kVoid;
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003070 }
3071
3072 bool GuardsAnInput() const {
3073 return InputCount() == 2;
3074 }
3075
3076 HInstruction* GuardedInput() const {
3077 DCHECK(GuardsAnInput());
3078 return InputAt(1);
3079 }
3080
3081 void RemoveGuard() {
3082 RemoveInputAt(1);
3083 }
3084
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003085 DECLARE_INSTRUCTION(Deoptimize);
3086
3087 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003088 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3089 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3090 static constexpr size_t kFieldDeoptimizeKindSize =
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003091 MinimumBitsToStore(static_cast<size_t>(DeoptimizationKind::kLast));
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003092 static constexpr size_t kNumberOfDeoptimizePackedBits =
3093 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3094 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3095 "Too many packed fields.");
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003096 using DeoptimizeKindField =
3097 BitField<DeoptimizationKind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003098
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003099 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
3100};
3101
Mingyao Yang063fc772016-08-02 11:02:54 -07003102// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3103// The compiled code checks this flag value in a guard before devirtualized call and
3104// if it's true, starts to do deoptimization.
3105// It has a 4-byte slot on stack.
3106// TODO: allocate a register for this flag.
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003107class HShouldDeoptimizeFlag FINAL : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003108 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003109 // CHA guards are only optimized in a separate pass and it has no side effects
3110 // with regard to other passes.
3111 HShouldDeoptimizeFlag(ArenaAllocator* arena, uint32_t dex_pc)
3112 : HVariableInputSizeInstruction(SideEffects::None(), dex_pc, arena, 0, kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003113 }
3114
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003115 DataType::Type GetType() const OVERRIDE { return DataType::Type::kInt32; }
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003116
3117 // We do all CHA guard elimination/motion in a single pass, after which there is no
3118 // further guard elimination/motion since a guard might have been used for justification
3119 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3120 // to avoid other optimizations trying to move it.
Mingyao Yang063fc772016-08-02 11:02:54 -07003121 bool CanBeMoved() const OVERRIDE { return false; }
3122
3123 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3124
3125 private:
3126 DISALLOW_COPY_AND_ASSIGN(HShouldDeoptimizeFlag);
3127};
3128
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003129// Represents the ArtMethod that was passed as a first argument to
3130// the method. It is used by instructions that depend on it, like
3131// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003132class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003133 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003134 explicit HCurrentMethod(DataType::Type type, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003135 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003136
3137 DECLARE_INSTRUCTION(CurrentMethod);
3138
3139 private:
3140 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
3141};
3142
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003143// Fetches an ArtMethod from the virtual table or the interface method table
3144// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003145class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003146 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003147 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003148 kVTable,
3149 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003150 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003151 };
3152 HClassTableGet(HInstruction* cls,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003153 DataType::Type type,
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003154 TableKind kind,
3155 size_t index,
3156 uint32_t dex_pc)
3157 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003158 index_(index) {
3159 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003160 SetRawInputAt(0, cls);
3161 }
3162
3163 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003164 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003165 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003166 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003167 }
3168
Vladimir Markoa1de9182016-02-25 11:37:38 +00003169 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003170 size_t GetIndex() const { return index_; }
3171
3172 DECLARE_INSTRUCTION(ClassTableGet);
3173
3174 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003175 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
3176 static constexpr size_t kFieldTableKindSize =
3177 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3178 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3179 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3180 "Too many packed fields.");
3181 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3182
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003183 // The index of the ArtMethod in the table.
3184 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003185
3186 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
3187};
3188
Mark Mendellfe57faa2015-09-18 09:26:15 -04003189// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3190// have one successor for each entry in the switch table, and the final successor
3191// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003192class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003193 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003194 HPackedSwitch(int32_t start_value,
3195 uint32_t num_entries,
3196 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003197 uint32_t dex_pc = kNoDexPc)
3198 : HTemplateInstruction(SideEffects::None(), dex_pc),
3199 start_value_(start_value),
3200 num_entries_(num_entries) {
3201 SetRawInputAt(0, input);
3202 }
3203
3204 bool IsControlFlow() const OVERRIDE { return true; }
3205
3206 int32_t GetStartValue() const { return start_value_; }
3207
Vladimir Marko211c2112015-09-24 16:52:33 +01003208 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003209
3210 HBasicBlock* GetDefaultBlock() const {
3211 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003212 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003213 }
3214 DECLARE_INSTRUCTION(PackedSwitch);
3215
3216 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003217 const int32_t start_value_;
3218 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003219
3220 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
3221};
3222
Roland Levillain88cb1752014-10-20 16:36:47 +01003223class HUnaryOperation : public HExpression<1> {
3224 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003225 HUnaryOperation(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003226 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003227 SetRawInputAt(0, input);
3228 }
3229
3230 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003231 DataType::Type GetResultType() const { return GetType(); }
Roland Levillain88cb1752014-10-20 16:36:47 +01003232
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003233 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003234 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003235 return true;
3236 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003237
Roland Levillain31dd3d62016-02-16 12:21:02 +00003238 // Try to statically evaluate `this` and return a HConstant
3239 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003240 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003241 HConstant* TryStaticEvaluation() const;
3242
3243 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003244 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3245 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003246 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3247 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003248
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003249 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003250
3251 private:
3252 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
3253};
3254
Dave Allison20dfc792014-06-16 20:44:29 -07003255class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003256 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003257 HBinaryOperation(DataType::Type result_type,
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003258 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003259 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003260 SideEffects side_effects = SideEffects::None(),
3261 uint32_t dex_pc = kNoDexPc)
3262 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003263 SetRawInputAt(0, left);
3264 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003265 }
3266
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003267 HInstruction* GetLeft() const { return InputAt(0); }
3268 HInstruction* GetRight() const { return InputAt(1); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003269 DataType::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003270
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003271 virtual bool IsCommutative() const { return false; }
3272
3273 // Put constant on the right.
3274 // Returns whether order is changed.
3275 bool OrderInputsWithConstantOnTheRight() {
3276 HInstruction* left = InputAt(0);
3277 HInstruction* right = InputAt(1);
3278 if (left->IsConstant() && !right->IsConstant()) {
3279 ReplaceInput(right, 0);
3280 ReplaceInput(left, 1);
3281 return true;
3282 }
3283 return false;
3284 }
3285
3286 // Order inputs by instruction id, but favor constant on the right side.
3287 // This helps GVN for commutative ops.
3288 void OrderInputs() {
3289 DCHECK(IsCommutative());
3290 HInstruction* left = InputAt(0);
3291 HInstruction* right = InputAt(1);
3292 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3293 return;
3294 }
3295 if (OrderInputsWithConstantOnTheRight()) {
3296 return;
3297 }
3298 // Order according to instruction id.
3299 if (left->GetId() > right->GetId()) {
3300 ReplaceInput(right, 0);
3301 ReplaceInput(left, 1);
3302 }
3303 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003304
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003305 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003306 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003307 return true;
3308 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003309
Roland Levillain31dd3d62016-02-16 12:21:02 +00003310 // Try to statically evaluate `this` and return a HConstant
3311 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003312 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003313 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003314
3315 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003316 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3317 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003318 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3319 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003320 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003321 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3322 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003323 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3324 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003325 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3326 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003327 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003328 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3329 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003330
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003331 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003332 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003333 HConstant* GetConstantRight() const;
3334
3335 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003336 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003337 HInstruction* GetLeastConstantLeft() const;
3338
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003339 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003340
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003341 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003342 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
3343};
3344
Mark Mendellc4701932015-04-10 13:18:51 -04003345// The comparison bias applies for floating point operations and indicates how NaN
3346// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003347enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003348 kNoBias, // bias is not applicable (i.e. for long operation)
3349 kGtBias, // return 1 for NaN comparisons
3350 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003351 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003352};
3353
Roland Levillain31dd3d62016-02-16 12:21:02 +00003354std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3355
Dave Allison20dfc792014-06-16 20:44:29 -07003356class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003357 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003358 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003359 : HBinaryOperation(DataType::Type::kBool, first, second, SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003360 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3361 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003362
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003363 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003364 // `instruction`, and disregard moves in between.
3365 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003366
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003367 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003368
3369 virtual IfCondition GetCondition() const = 0;
3370
Mark Mendellc4701932015-04-10 13:18:51 -04003371 virtual IfCondition GetOppositeCondition() const = 0;
3372
Vladimir Markoa1de9182016-02-25 11:37:38 +00003373 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003374 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3375
Vladimir Markoa1de9182016-02-25 11:37:38 +00003376 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3377 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003378
Vladimir Marko372f10e2016-05-17 16:30:10 +01003379 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003380 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003381 }
3382
Roland Levillain4fa13f62015-07-06 18:11:54 +01003383 bool IsFPConditionTrueIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003384 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003385 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003386 if (if_cond == kCondNE) {
3387 return true;
3388 } else if (if_cond == kCondEQ) {
3389 return false;
3390 }
3391 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003392 }
3393
3394 bool IsFPConditionFalseIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003395 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003396 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003397 if (if_cond == kCondEQ) {
3398 return true;
3399 } else if (if_cond == kCondNE) {
3400 return false;
3401 }
3402 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003403 }
3404
Roland Levillain31dd3d62016-02-16 12:21:02 +00003405 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003406 // Needed if we merge a HCompare into a HCondition.
3407 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3408 static constexpr size_t kFieldComparisonBiasSize =
3409 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3410 static constexpr size_t kNumberOfConditionPackedBits =
3411 kFieldComparisonBias + kFieldComparisonBiasSize;
3412 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3413 using ComparisonBiasField =
3414 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3415
Roland Levillain31dd3d62016-02-16 12:21:02 +00003416 template <typename T>
3417 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3418
3419 template <typename T>
3420 int32_t CompareFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003421 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003422 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3423 // Handle the bias.
3424 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3425 }
3426
3427 // Return an integer constant containing the result of a condition evaluated at compile time.
3428 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3429 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3430 }
3431
Dave Allison20dfc792014-06-16 20:44:29 -07003432 private:
3433 DISALLOW_COPY_AND_ASSIGN(HCondition);
3434};
3435
3436// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003437class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003438 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003439 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3440 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003441
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003442 bool IsCommutative() const OVERRIDE { return true; }
3443
Vladimir Marko9e23df52015-11-10 17:14:35 +00003444 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3445 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003446 return MakeConstantCondition(true, GetDexPc());
3447 }
3448 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3449 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3450 }
3451 // In the following Evaluate methods, a HCompare instruction has
3452 // been merged into this HEqual instruction; evaluate it as
3453 // `Compare(x, y) == 0`.
3454 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3455 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3456 GetDexPc());
3457 }
3458 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3459 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3460 }
3461 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3462 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003463 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003464
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003465 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003466
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003467 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003468 return kCondEQ;
3469 }
3470
Mark Mendellc4701932015-04-10 13:18:51 -04003471 IfCondition GetOppositeCondition() const OVERRIDE {
3472 return kCondNE;
3473 }
3474
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003475 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003476 template <typename T> static bool Compute(T x, T y) { return x == y; }
Aart Bike9f37602015-10-09 11:15:55 -07003477
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003478 DISALLOW_COPY_AND_ASSIGN(HEqual);
3479};
3480
Vladimir Markofcb503c2016-05-18 12:48:17 +01003481class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003482 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003483 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3484 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003485
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003486 bool IsCommutative() const OVERRIDE { return true; }
3487
Vladimir Marko9e23df52015-11-10 17:14:35 +00003488 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3489 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003490 return MakeConstantCondition(false, GetDexPc());
3491 }
3492 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3493 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3494 }
3495 // In the following Evaluate methods, a HCompare instruction has
3496 // been merged into this HNotEqual instruction; evaluate it as
3497 // `Compare(x, y) != 0`.
3498 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3499 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3500 }
3501 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3502 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3503 }
3504 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3505 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003506 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003507
Dave Allison20dfc792014-06-16 20:44:29 -07003508 DECLARE_INSTRUCTION(NotEqual);
3509
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003510 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003511 return kCondNE;
3512 }
3513
Mark Mendellc4701932015-04-10 13:18:51 -04003514 IfCondition GetOppositeCondition() const OVERRIDE {
3515 return kCondEQ;
3516 }
3517
Dave Allison20dfc792014-06-16 20:44:29 -07003518 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003519 template <typename T> static bool Compute(T x, T y) { return x != y; }
Aart Bike9f37602015-10-09 11:15:55 -07003520
Dave Allison20dfc792014-06-16 20:44:29 -07003521 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3522};
3523
Vladimir Markofcb503c2016-05-18 12:48:17 +01003524class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003525 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003526 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3527 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003528
Roland Levillain9867bc72015-08-05 10:21:34 +01003529 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003530 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003531 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003532 // In the following Evaluate methods, a HCompare instruction has
3533 // been merged into this HLessThan instruction; evaluate it as
3534 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003535 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003536 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3537 }
3538 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3539 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3540 }
3541 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3542 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003543 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003544
Dave Allison20dfc792014-06-16 20:44:29 -07003545 DECLARE_INSTRUCTION(LessThan);
3546
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003547 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003548 return kCondLT;
3549 }
3550
Mark Mendellc4701932015-04-10 13:18:51 -04003551 IfCondition GetOppositeCondition() const OVERRIDE {
3552 return kCondGE;
3553 }
3554
Dave Allison20dfc792014-06-16 20:44:29 -07003555 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003556 template <typename T> static bool Compute(T x, T y) { return x < y; }
Aart Bike9f37602015-10-09 11:15:55 -07003557
Dave Allison20dfc792014-06-16 20:44:29 -07003558 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3559};
3560
Vladimir Markofcb503c2016-05-18 12:48:17 +01003561class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003562 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003563 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3564 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003565
Roland Levillain9867bc72015-08-05 10:21:34 +01003566 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003567 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003568 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003569 // In the following Evaluate methods, a HCompare instruction has
3570 // been merged into this HLessThanOrEqual instruction; evaluate it as
3571 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003572 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003573 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3574 }
3575 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3576 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3577 }
3578 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3579 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003580 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003581
Dave Allison20dfc792014-06-16 20:44:29 -07003582 DECLARE_INSTRUCTION(LessThanOrEqual);
3583
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003584 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003585 return kCondLE;
3586 }
3587
Mark Mendellc4701932015-04-10 13:18:51 -04003588 IfCondition GetOppositeCondition() const OVERRIDE {
3589 return kCondGT;
3590 }
3591
Dave Allison20dfc792014-06-16 20:44:29 -07003592 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003593 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003594
Dave Allison20dfc792014-06-16 20:44:29 -07003595 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3596};
3597
Vladimir Markofcb503c2016-05-18 12:48:17 +01003598class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003599 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003600 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3601 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003602
Roland Levillain9867bc72015-08-05 10:21:34 +01003603 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003604 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003605 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003606 // In the following Evaluate methods, a HCompare instruction has
3607 // been merged into this HGreaterThan instruction; evaluate it as
3608 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003609 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003610 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3611 }
3612 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3613 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3614 }
3615 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3616 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003617 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003618
Dave Allison20dfc792014-06-16 20:44:29 -07003619 DECLARE_INSTRUCTION(GreaterThan);
3620
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003621 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003622 return kCondGT;
3623 }
3624
Mark Mendellc4701932015-04-10 13:18:51 -04003625 IfCondition GetOppositeCondition() const OVERRIDE {
3626 return kCondLE;
3627 }
3628
Dave Allison20dfc792014-06-16 20:44:29 -07003629 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003630 template <typename T> static bool Compute(T x, T y) { return x > y; }
Aart Bike9f37602015-10-09 11:15:55 -07003631
Dave Allison20dfc792014-06-16 20:44:29 -07003632 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3633};
3634
Vladimir Markofcb503c2016-05-18 12:48:17 +01003635class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003636 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003637 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3638 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003639
Roland Levillain9867bc72015-08-05 10:21:34 +01003640 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003641 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003642 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003643 // In the following Evaluate methods, a HCompare instruction has
3644 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3645 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003646 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003647 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3648 }
3649 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3650 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3651 }
3652 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3653 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003654 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003655
Dave Allison20dfc792014-06-16 20:44:29 -07003656 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3657
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003658 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003659 return kCondGE;
3660 }
3661
Mark Mendellc4701932015-04-10 13:18:51 -04003662 IfCondition GetOppositeCondition() const OVERRIDE {
3663 return kCondLT;
3664 }
3665
Dave Allison20dfc792014-06-16 20:44:29 -07003666 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003667 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003668
Dave Allison20dfc792014-06-16 20:44:29 -07003669 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3670};
3671
Vladimir Markofcb503c2016-05-18 12:48:17 +01003672class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003673 public:
3674 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3675 : HCondition(first, second, dex_pc) {}
3676
3677 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003678 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003679 }
3680 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003681 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3682 }
3683 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3684 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3685 LOG(FATAL) << DebugName() << " is not defined for float values";
3686 UNREACHABLE();
3687 }
3688 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3689 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3690 LOG(FATAL) << DebugName() << " is not defined for double values";
3691 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003692 }
3693
3694 DECLARE_INSTRUCTION(Below);
3695
3696 IfCondition GetCondition() const OVERRIDE {
3697 return kCondB;
3698 }
3699
3700 IfCondition GetOppositeCondition() const OVERRIDE {
3701 return kCondAE;
3702 }
3703
3704 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003705 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003706 return MakeUnsigned(x) < MakeUnsigned(y);
3707 }
Aart Bike9f37602015-10-09 11:15:55 -07003708
3709 DISALLOW_COPY_AND_ASSIGN(HBelow);
3710};
3711
Vladimir Markofcb503c2016-05-18 12:48:17 +01003712class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003713 public:
3714 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3715 : HCondition(first, second, dex_pc) {}
3716
3717 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003718 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003719 }
3720 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003721 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3722 }
3723 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3724 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3725 LOG(FATAL) << DebugName() << " is not defined for float values";
3726 UNREACHABLE();
3727 }
3728 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3729 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3730 LOG(FATAL) << DebugName() << " is not defined for double values";
3731 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003732 }
3733
3734 DECLARE_INSTRUCTION(BelowOrEqual);
3735
3736 IfCondition GetCondition() const OVERRIDE {
3737 return kCondBE;
3738 }
3739
3740 IfCondition GetOppositeCondition() const OVERRIDE {
3741 return kCondA;
3742 }
3743
3744 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003745 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003746 return MakeUnsigned(x) <= MakeUnsigned(y);
3747 }
Aart Bike9f37602015-10-09 11:15:55 -07003748
3749 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3750};
3751
Vladimir Markofcb503c2016-05-18 12:48:17 +01003752class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003753 public:
3754 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3755 : HCondition(first, second, dex_pc) {}
3756
3757 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003758 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003759 }
3760 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003761 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3762 }
3763 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3764 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3765 LOG(FATAL) << DebugName() << " is not defined for float values";
3766 UNREACHABLE();
3767 }
3768 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3769 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3770 LOG(FATAL) << DebugName() << " is not defined for double values";
3771 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003772 }
3773
3774 DECLARE_INSTRUCTION(Above);
3775
3776 IfCondition GetCondition() const OVERRIDE {
3777 return kCondA;
3778 }
3779
3780 IfCondition GetOppositeCondition() const OVERRIDE {
3781 return kCondBE;
3782 }
3783
3784 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003785 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003786 return MakeUnsigned(x) > MakeUnsigned(y);
3787 }
Aart Bike9f37602015-10-09 11:15:55 -07003788
3789 DISALLOW_COPY_AND_ASSIGN(HAbove);
3790};
3791
Vladimir Markofcb503c2016-05-18 12:48:17 +01003792class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003793 public:
3794 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3795 : HCondition(first, second, dex_pc) {}
3796
3797 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003798 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003799 }
3800 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003801 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3802 }
3803 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3804 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3805 LOG(FATAL) << DebugName() << " is not defined for float values";
3806 UNREACHABLE();
3807 }
3808 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3809 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3810 LOG(FATAL) << DebugName() << " is not defined for double values";
3811 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003812 }
3813
3814 DECLARE_INSTRUCTION(AboveOrEqual);
3815
3816 IfCondition GetCondition() const OVERRIDE {
3817 return kCondAE;
3818 }
3819
3820 IfCondition GetOppositeCondition() const OVERRIDE {
3821 return kCondB;
3822 }
3823
3824 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003825 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003826 return MakeUnsigned(x) >= MakeUnsigned(y);
3827 }
Aart Bike9f37602015-10-09 11:15:55 -07003828
3829 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3830};
Dave Allison20dfc792014-06-16 20:44:29 -07003831
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003832// Instruction to check how two inputs compare to each other.
3833// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003834class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003835 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003836 // Note that `comparison_type` is the type of comparison performed
3837 // between the comparison's inputs, not the type of the instantiated
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003838 // HCompare instruction (which is always DataType::Type::kInt).
3839 HCompare(DataType::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003840 HInstruction* first,
3841 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003842 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003843 uint32_t dex_pc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003844 : HBinaryOperation(DataType::Type::kInt32,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003845 first,
3846 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003847 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003848 dex_pc) {
3849 SetPackedField<ComparisonBiasField>(bias);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003850 DCHECK_EQ(comparison_type, DataType::Kind(first->GetType()));
3851 DCHECK_EQ(comparison_type, DataType::Kind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003852 }
3853
Roland Levillain9867bc72015-08-05 10:21:34 +01003854 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003855 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3856
3857 template <typename T>
3858 int32_t ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003859 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003860 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3861 // Handle the bias.
3862 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3863 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003864
Roland Levillain9867bc72015-08-05 10:21:34 +01003865 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003866 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3867 // reach this code path when processing a freshly built HIR
3868 // graph. However HCompare integer instructions can be synthesized
3869 // by the instruction simplifier to implement IntegerCompare and
3870 // IntegerSignum intrinsics, so we have to handle this case.
3871 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003872 }
3873 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003874 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3875 }
3876 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3877 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3878 }
3879 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3880 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003881 }
3882
Vladimir Marko372f10e2016-05-17 16:30:10 +01003883 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003884 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003885 }
3886
Vladimir Markoa1de9182016-02-25 11:37:38 +00003887 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003888
Roland Levillain31dd3d62016-02-16 12:21:02 +00003889 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003890 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003891 bool IsGtBias() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003892 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003893 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003894 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003895
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003896 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type type ATTRIBUTE_UNUSED) {
Roland Levillain1693a1f2016-03-15 14:57:31 +00003897 // Comparisons do not require a runtime call in any back end.
3898 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003899 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003900
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003901 DECLARE_INSTRUCTION(Compare);
3902
Roland Levillain31dd3d62016-02-16 12:21:02 +00003903 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003904 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3905 static constexpr size_t kFieldComparisonBiasSize =
3906 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3907 static constexpr size_t kNumberOfComparePackedBits =
3908 kFieldComparisonBias + kFieldComparisonBiasSize;
3909 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3910 using ComparisonBiasField =
3911 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3912
Roland Levillain31dd3d62016-02-16 12:21:02 +00003913 // Return an integer constant containing the result of a comparison evaluated at compile time.
3914 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3915 DCHECK(value == -1 || value == 0 || value == 1) << value;
3916 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3917 }
3918
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003919 private:
3920 DISALLOW_COPY_AND_ASSIGN(HCompare);
3921};
3922
Nicolas Geoffray2b615ba2017-01-06 14:40:07 +00003923class HNewInstance FINAL : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00003924 public:
3925 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00003926 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08003927 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00003928 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00003929 bool finalizable,
3930 QuickEntrypointEnum entrypoint)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003931 : HExpression(DataType::Type::kReference, SideEffects::CanTriggerGC(), dex_pc),
David Brazdil6de19382016-01-08 17:37:10 +00003932 type_index_(type_index),
3933 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003934 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003935 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003936 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00003937 }
3938
Andreas Gampea5b09a62016-11-17 15:21:22 -08003939 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00003940 const DexFile& GetDexFile() const { return dex_file_; }
3941
3942 // Calls runtime so needs an environment.
3943 bool NeedsEnvironment() const OVERRIDE { return true; }
3944
Mingyao Yang062157f2016-03-02 10:15:36 -08003945 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
3946 bool CanThrow() const OVERRIDE { return true; }
3947
Nicolas Geoffray5247c082017-01-13 14:17:29 +00003948 bool NeedsChecks() const {
3949 return entrypoint_ == kQuickAllocObjectWithChecks;
3950 }
David Brazdil6de19382016-01-08 17:37:10 +00003951
Vladimir Markoa1de9182016-02-25 11:37:38 +00003952 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003953
3954 bool CanBeNull() const OVERRIDE { return false; }
3955
3956 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3957
3958 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3959 entrypoint_ = entrypoint;
3960 }
3961
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00003962 HLoadClass* GetLoadClass() const {
3963 HInstruction* input = InputAt(0);
3964 if (input->IsClinitCheck()) {
3965 input = input->InputAt(0);
3966 }
3967 DCHECK(input->IsLoadClass());
3968 return input->AsLoadClass();
3969 }
3970
David Brazdil6de19382016-01-08 17:37:10 +00003971 bool IsStringAlloc() const;
3972
3973 DECLARE_INSTRUCTION(NewInstance);
3974
3975 private:
Nicolas Geoffray5247c082017-01-13 14:17:29 +00003976 static constexpr size_t kFlagFinalizable = kNumberOfExpressionPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003977 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3978 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3979 "Too many packed fields.");
3980
Andreas Gampea5b09a62016-11-17 15:21:22 -08003981 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00003982 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003983 QuickEntrypointEnum entrypoint_;
3984
3985 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3986};
3987
Agi Csaki05f20562015-08-19 14:58:14 -07003988enum IntrinsicNeedsEnvironmentOrCache {
3989 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3990 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003991};
3992
Aart Bik5d75afe2015-12-14 11:57:01 -08003993enum IntrinsicSideEffects {
3994 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3995 kReadSideEffects, // Intrinsic may read heap memory.
3996 kWriteSideEffects, // Intrinsic may write heap memory.
3997 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3998};
3999
4000enum IntrinsicExceptions {
4001 kNoThrow, // Intrinsic does not throw any exceptions.
4002 kCanThrow // Intrinsic may throw exceptions.
4003};
4004
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004005class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004006 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004007 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004008
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01004009 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004010 SetRawInputAt(index, argument);
4011 }
4012
Roland Levillain3e3d7332015-04-28 11:00:54 +01004013 // Return the number of arguments. This number can be lower than
4014 // the number of inputs returned by InputCount(), as some invoke
4015 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
4016 // inputs at the end of their list of inputs.
4017 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
4018
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004019 DataType::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004020
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004021 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4022
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004023 InvokeType GetInvokeType() const {
4024 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004025 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004026
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01004027 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004028 return intrinsic_;
4029 }
4030
Aart Bik5d75afe2015-12-14 11:57:01 -08004031 void SetIntrinsic(Intrinsics intrinsic,
4032 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
4033 IntrinsicSideEffects side_effects,
4034 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004035
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004036 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00004037 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004038 }
4039
Aart Bikff7d89c2016-11-07 08:49:28 -08004040 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4041
Vladimir Markoa1de9182016-02-25 11:37:38 +00004042 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004043
Aart Bik71bf7b42016-11-16 10:17:46 -08004044 bool CanBeMoved() const OVERRIDE { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004045
Vladimir Marko372f10e2016-05-17 16:30:10 +01004046 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08004047 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4048 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004049
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004050 uint32_t* GetIntrinsicOptimizations() {
4051 return &intrinsic_optimizations_;
4052 }
4053
4054 const uint32_t* GetIntrinsicOptimizations() const {
4055 return &intrinsic_optimizations_;
4056 }
4057
4058 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4059
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004060 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004061 void SetResolvedMethod(ArtMethod* method) { resolved_method_ = method; }
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004062
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004063 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004064
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004065 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004066 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4067 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004068 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4069 static constexpr size_t kFieldReturnType =
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004070 kFieldInvokeType + kFieldInvokeTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004071 static constexpr size_t kFieldReturnTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004072 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00004073 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
4074 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
4075 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004076 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004077 using ReturnTypeField = BitField<DataType::Type, kFieldReturnType, kFieldReturnTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004078
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004079 HInvoke(ArenaAllocator* arena,
4080 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004081 uint32_t number_of_other_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004082 DataType::Type return_type,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004083 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004084 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004085 ArtMethod* resolved_method,
4086 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004087 : HVariableInputSizeInstruction(
4088 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4089 dex_pc,
4090 arena,
4091 number_of_arguments + number_of_other_inputs,
4092 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004093 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004094 resolved_method_(resolved_method),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004095 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07004096 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004097 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004098 SetPackedField<ReturnTypeField>(return_type);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004099 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004100 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004101 }
4102
Roland Levillain3e3d7332015-04-28 11:00:54 +01004103 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004104 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004105 const uint32_t dex_method_index_;
4106 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004107
4108 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4109 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004110
4111 private:
4112 DISALLOW_COPY_AND_ASSIGN(HInvoke);
4113};
4114
Vladimir Markofcb503c2016-05-18 12:48:17 +01004115class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004116 public:
4117 HInvokeUnresolved(ArenaAllocator* arena,
4118 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004119 DataType::Type return_type,
Calin Juravle175dc732015-08-25 15:42:32 +01004120 uint32_t dex_pc,
4121 uint32_t dex_method_index,
4122 InvokeType invoke_type)
4123 : HInvoke(arena,
4124 number_of_arguments,
4125 0u /* number_of_other_inputs */,
4126 return_type,
4127 dex_pc,
4128 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004129 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01004130 invoke_type) {
4131 }
4132
4133 DECLARE_INSTRUCTION(InvokeUnresolved);
4134
4135 private:
4136 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
4137};
4138
Orion Hodsonac141392017-01-13 11:53:47 +00004139class HInvokePolymorphic FINAL : public HInvoke {
4140 public:
4141 HInvokePolymorphic(ArenaAllocator* arena,
4142 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004143 DataType::Type return_type,
Orion Hodsonac141392017-01-13 11:53:47 +00004144 uint32_t dex_pc,
4145 uint32_t dex_method_index)
4146 : HInvoke(arena,
4147 number_of_arguments,
4148 0u /* number_of_other_inputs */,
4149 return_type,
4150 dex_pc,
4151 dex_method_index,
4152 nullptr,
4153 kVirtual) {}
4154
4155 DECLARE_INSTRUCTION(InvokePolymorphic);
4156
4157 private:
4158 DISALLOW_COPY_AND_ASSIGN(HInvokePolymorphic);
4159};
4160
Vladimir Markofcb503c2016-05-18 12:48:17 +01004161class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004162 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004163 // Requirements of this method call regarding the class
4164 // initialization (clinit) check of its declaring class.
4165 enum class ClinitCheckRequirement {
4166 kNone, // Class already initialized.
4167 kExplicit, // Static call having explicit clinit check as last input.
4168 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004169 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004170 };
4171
Vladimir Marko58155012015-08-19 12:49:41 +00004172 // Determines how to load the target ArtMethod*.
4173 enum class MethodLoadKind {
4174 // Use a String init ArtMethod* loaded from Thread entrypoints.
4175 kStringInit,
4176
4177 // Use the method's own ArtMethod* loaded by the register allocator.
4178 kRecursive,
4179
Vladimir Marko65979462017-05-19 17:25:12 +01004180 // Use PC-relative boot image ArtMethod* address that will be known at link time.
4181 // Used for boot image methods referenced by boot image code.
4182 kBootImageLinkTimePcRelative,
4183
Vladimir Marko58155012015-08-19 12:49:41 +00004184 // Use ArtMethod* at a known address, embed the direct address in the code.
4185 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
4186 kDirectAddress,
4187
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004188 // Load from an entry in the .bss section using a PC-relative load.
4189 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
4190 kBssEntry,
Vladimir Marko58155012015-08-19 12:49:41 +00004191
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004192 // Make a runtime call to resolve and call the method. This is the last-resort-kind
4193 // used when other kinds are unimplemented on a particular architecture.
4194 kRuntimeCall,
Vladimir Marko58155012015-08-19 12:49:41 +00004195 };
4196
4197 // Determines the location of the code pointer.
4198 enum class CodePtrLocation {
4199 // Recursive call, use local PC-relative call instruction.
4200 kCallSelf,
4201
Vladimir Marko58155012015-08-19 12:49:41 +00004202 // Use code pointer from the ArtMethod*.
4203 // Used when we don't know the target code. This is also the last-resort-kind used when
4204 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4205 kCallArtMethod,
4206 };
4207
4208 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004209 MethodLoadKind method_load_kind;
4210 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004211 // The method load data holds
4212 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4213 // Note that there are multiple string init methods, each having its own offset.
4214 // - the method address for kDirectAddress
Vladimir Markodc151b22015-10-15 18:02:30 +01004215 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004216 };
4217
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004218 HInvokeStaticOrDirect(ArenaAllocator* arena,
4219 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004220 DataType::Type return_type,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004221 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004222 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004223 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004224 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004225 InvokeType invoke_type,
4226 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004227 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01004228 : HInvoke(arena,
4229 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004230 // There is potentially one extra argument for the HCurrentMethod node, and
4231 // potentially one other if the clinit check is explicit, and potentially
4232 // one other if the method is a string factory.
4233 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004234 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004235 return_type,
4236 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004237 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004238 resolved_method,
4239 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004240 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004241 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004242 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4243 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004244
Vladimir Markodc151b22015-10-15 18:02:30 +01004245 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004246 bool had_current_method_input = HasCurrentMethodInput();
4247 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4248
4249 // Using the current method is the default and once we find a better
4250 // method load kind, we should not go back to using the current method.
4251 DCHECK(had_current_method_input || !needs_current_method_input);
4252
4253 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004254 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4255 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004256 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004257 dispatch_info_ = dispatch_info;
4258 }
4259
Aart Bik6daebeb2017-04-03 14:35:41 -07004260 DispatchInfo GetDispatchInfo() const {
4261 return dispatch_info_;
4262 }
4263
Vladimir Markoc53c0792015-11-19 15:48:33 +00004264 void AddSpecialInput(HInstruction* input) {
4265 // We allow only one special input.
4266 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4267 DCHECK(InputCount() == GetSpecialInputIndex() ||
4268 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4269 InsertInputAt(GetSpecialInputIndex(), input);
4270 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004271
Vladimir Marko372f10e2016-05-17 16:30:10 +01004272 using HInstruction::GetInputRecords; // Keep the const version visible.
4273 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
4274 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4275 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4276 DCHECK(!input_records.empty());
4277 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4278 HInstruction* last_input = input_records.back().GetInstruction();
4279 // Note: `last_input` may be null during arguments setup.
4280 if (last_input != nullptr) {
4281 // `last_input` is the last input of a static invoke marked as having
4282 // an explicit clinit check. It must either be:
4283 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4284 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4285 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4286 }
4287 }
4288 return input_records;
4289 }
4290
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004291 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004292 // We access the method via the dex cache so we can't do an implicit null check.
4293 // TODO: for intrinsics we can generate implicit null checks.
4294 return false;
4295 }
4296
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004297 bool CanBeNull() const OVERRIDE {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004298 return GetPackedField<ReturnTypeField>() == DataType::Type::kReference && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004299 }
4300
Vladimir Markoc53c0792015-11-19 15:48:33 +00004301 // Get the index of the special input, if any.
4302 //
David Brazdil6de19382016-01-08 17:37:10 +00004303 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4304 // method pointer; otherwise there may be one platform-specific special input,
4305 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004306 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004307 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004308
Vladimir Marko58155012015-08-19 12:49:41 +00004309 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4310 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4311 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004312 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004313 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004314 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko65979462017-05-19 17:25:12 +01004315 bool HasPcRelativeMethodLoadKind() const {
4316 return GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004317 GetMethodLoadKind() == MethodLoadKind::kBssEntry;
Vladimir Marko65979462017-05-19 17:25:12 +01004318 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004319 bool HasCurrentMethodInput() const {
4320 // This function can be called only after the invoke has been fully initialized by the builder.
4321 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004322 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004323 return true;
4324 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004325 DCHECK(InputCount() == GetSpecialInputIndex() ||
4326 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004327 return false;
4328 }
4329 }
Vladimir Marko58155012015-08-19 12:49:41 +00004330
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004331 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004332 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004333 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004334 }
4335
4336 uint64_t GetMethodAddress() const {
4337 DCHECK(HasMethodAddress());
4338 return dispatch_info_.method_load_data;
4339 }
4340
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004341 const DexFile& GetDexFileForPcRelativeDexCache() const;
4342
Vladimir Markoa1de9182016-02-25 11:37:38 +00004343 ClinitCheckRequirement GetClinitCheckRequirement() const {
4344 return GetPackedField<ClinitCheckRequirementField>();
4345 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004346
Roland Levillain4c0eb422015-04-24 16:43:49 +01004347 // Is this instruction a call to a static method?
4348 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004349 return GetInvokeType() == kStatic;
4350 }
4351
4352 MethodReference GetTargetMethod() const {
4353 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004354 }
4355
Vladimir Markofbb184a2015-11-13 14:47:00 +00004356 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4357 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4358 // instruction; only relevant for static calls with explicit clinit check.
4359 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004360 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004361 size_t last_input_index = inputs_.size() - 1u;
4362 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004363 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004364 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004365 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004366 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004367 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004368 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004369 }
4370
4371 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004372 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004373 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004374 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004375 }
4376
4377 // Is this a call to a static method whose declaring class has an
4378 // implicit intialization check requirement?
4379 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004380 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004381 }
4382
Vladimir Markob554b5a2015-11-06 12:57:55 +00004383 // Does this method load kind need the current method as an input?
4384 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004385 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kRuntimeCall;
Vladimir Markob554b5a2015-11-06 12:57:55 +00004386 }
4387
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004388 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004389
4390 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004391 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004392 static constexpr size_t kFieldClinitCheckRequirementSize =
4393 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4394 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4395 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4396 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4397 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004398 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4399 kFieldClinitCheckRequirement,
4400 kFieldClinitCheckRequirementSize>;
4401
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004402 // Cached values of the resolved method, to avoid needing the mutator lock.
Vladimir Marko58155012015-08-19 12:49:41 +00004403 MethodReference target_method_;
4404 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004405
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004406 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004407};
Vladimir Markof64242a2015-12-01 14:58:23 +00004408std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004409std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004410
Vladimir Markofcb503c2016-05-18 12:48:17 +01004411class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004412 public:
4413 HInvokeVirtual(ArenaAllocator* arena,
4414 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004415 DataType::Type return_type,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004416 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004417 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004418 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004419 uint32_t vtable_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004420 : HInvoke(arena,
4421 number_of_arguments,
4422 0u,
4423 return_type,
4424 dex_pc,
4425 dex_method_index,
4426 resolved_method,
4427 kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004428 vtable_index_(vtable_index) {}
4429
Aart Bik71bf7b42016-11-16 10:17:46 -08004430 bool CanBeNull() const OVERRIDE {
4431 switch (GetIntrinsic()) {
4432 case Intrinsics::kThreadCurrentThread:
4433 case Intrinsics::kStringBufferAppend:
4434 case Intrinsics::kStringBufferToString:
4435 case Intrinsics::kStringBuilderAppend:
4436 case Intrinsics::kStringBuilderToString:
4437 return false;
4438 default:
4439 return HInvoke::CanBeNull();
4440 }
4441 }
4442
Calin Juravle641547a2015-04-21 22:08:51 +01004443 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004444 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004445 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004446 }
4447
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004448 uint32_t GetVTableIndex() const { return vtable_index_; }
4449
4450 DECLARE_INSTRUCTION(InvokeVirtual);
4451
4452 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004453 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004454 const uint32_t vtable_index_;
4455
4456 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4457};
4458
Vladimir Markofcb503c2016-05-18 12:48:17 +01004459class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004460 public:
4461 HInvokeInterface(ArenaAllocator* arena,
4462 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004463 DataType::Type return_type,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004464 uint32_t dex_pc,
4465 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004466 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004467 uint32_t imt_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004468 : HInvoke(arena,
4469 number_of_arguments,
4470 0u,
4471 return_type,
4472 dex_pc,
4473 dex_method_index,
4474 resolved_method,
4475 kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004476 imt_index_(imt_index) {}
4477
Calin Juravle641547a2015-04-21 22:08:51 +01004478 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004479 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004480 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004481 }
4482
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004483 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
4484 // The assembly stub currently needs it.
4485 return true;
4486 }
4487
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004488 uint32_t GetImtIndex() const { return imt_index_; }
4489 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4490
4491 DECLARE_INSTRUCTION(InvokeInterface);
4492
4493 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004494 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004495 const uint32_t imt_index_;
4496
4497 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4498};
4499
Vladimir Markofcb503c2016-05-18 12:48:17 +01004500class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004501 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004502 HNeg(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004503 : HUnaryOperation(result_type, input, dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004504 DCHECK_EQ(result_type, DataType::Kind(input->GetType()));
Roland Levillain937e6cd2016-03-22 11:54:37 +00004505 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004506
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004507 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004508
4509 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004510 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004511 }
4512 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004513 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004514 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004515 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4516 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4517 }
4518 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4519 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4520 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004521
Roland Levillain88cb1752014-10-20 16:36:47 +01004522 DECLARE_INSTRUCTION(Neg);
4523
4524 private:
4525 DISALLOW_COPY_AND_ASSIGN(HNeg);
4526};
4527
Vladimir Markofcb503c2016-05-18 12:48:17 +01004528class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004529 public:
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004530 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004531 : HExpression(DataType::Type::kReference, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004532 SetRawInputAt(0, cls);
4533 SetRawInputAt(1, length);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004534 }
4535
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004536 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004537 bool NeedsEnvironment() const OVERRIDE { return true; }
4538
Mingyao Yang0c365e62015-03-31 15:09:29 -07004539 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4540 bool CanThrow() const OVERRIDE { return true; }
4541
Calin Juravle10e244f2015-01-26 18:54:32 +00004542 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004543
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004544 HLoadClass* GetLoadClass() const {
4545 DCHECK(InputAt(0)->IsLoadClass());
4546 return InputAt(0)->AsLoadClass();
4547 }
4548
4549 HInstruction* GetLength() const {
4550 return InputAt(1);
4551 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004552
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004553 DECLARE_INSTRUCTION(NewArray);
4554
4555 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004556 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4557};
4558
Vladimir Markofcb503c2016-05-18 12:48:17 +01004559class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004560 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004561 HAdd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004562 HInstruction* left,
4563 HInstruction* right,
4564 uint32_t dex_pc = kNoDexPc)
4565 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004566
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004567 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004568
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004569 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004570
4571 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004572 return GetBlock()->GetGraph()->GetIntConstant(
4573 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004574 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004575 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004576 return GetBlock()->GetGraph()->GetLongConstant(
4577 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004578 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004579 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4580 return GetBlock()->GetGraph()->GetFloatConstant(
4581 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4582 }
4583 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4584 return GetBlock()->GetGraph()->GetDoubleConstant(
4585 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4586 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004587
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004588 DECLARE_INSTRUCTION(Add);
4589
4590 private:
4591 DISALLOW_COPY_AND_ASSIGN(HAdd);
4592};
4593
Vladimir Markofcb503c2016-05-18 12:48:17 +01004594class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004595 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004596 HSub(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004597 HInstruction* left,
4598 HInstruction* right,
4599 uint32_t dex_pc = kNoDexPc)
4600 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004601
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004602 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004603
4604 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004605 return GetBlock()->GetGraph()->GetIntConstant(
4606 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004607 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004608 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004609 return GetBlock()->GetGraph()->GetLongConstant(
4610 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004611 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004612 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4613 return GetBlock()->GetGraph()->GetFloatConstant(
4614 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4615 }
4616 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4617 return GetBlock()->GetGraph()->GetDoubleConstant(
4618 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4619 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004620
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004621 DECLARE_INSTRUCTION(Sub);
4622
4623 private:
4624 DISALLOW_COPY_AND_ASSIGN(HSub);
4625};
4626
Vladimir Markofcb503c2016-05-18 12:48:17 +01004627class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004628 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004629 HMul(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004630 HInstruction* left,
4631 HInstruction* right,
4632 uint32_t dex_pc = kNoDexPc)
4633 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004634
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004635 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004636
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004637 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004638
4639 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004640 return GetBlock()->GetGraph()->GetIntConstant(
4641 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004642 }
4643 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004644 return GetBlock()->GetGraph()->GetLongConstant(
4645 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004646 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004647 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4648 return GetBlock()->GetGraph()->GetFloatConstant(
4649 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4650 }
4651 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4652 return GetBlock()->GetGraph()->GetDoubleConstant(
4653 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4654 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004655
4656 DECLARE_INSTRUCTION(Mul);
4657
4658 private:
4659 DISALLOW_COPY_AND_ASSIGN(HMul);
4660};
4661
Vladimir Markofcb503c2016-05-18 12:48:17 +01004662class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004663 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004664 HDiv(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004665 HInstruction* left,
4666 HInstruction* right,
4667 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004668 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004669
Roland Levillain9867bc72015-08-05 10:21:34 +01004670 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004671 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004672 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004673 // Our graph structure ensures we never have 0 for `y` during
4674 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004675 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004676 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004677 return (y == -1) ? -x : x / y;
4678 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004679
Roland Levillain31dd3d62016-02-16 12:21:02 +00004680 template <typename T>
4681 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004682 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004683 return x / y;
4684 }
4685
Roland Levillain9867bc72015-08-05 10:21:34 +01004686 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004687 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004688 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004689 }
4690 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004691 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004692 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4693 }
4694 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4695 return GetBlock()->GetGraph()->GetFloatConstant(
4696 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4697 }
4698 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4699 return GetBlock()->GetGraph()->GetDoubleConstant(
4700 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004701 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004702
4703 DECLARE_INSTRUCTION(Div);
4704
4705 private:
4706 DISALLOW_COPY_AND_ASSIGN(HDiv);
4707};
4708
Vladimir Markofcb503c2016-05-18 12:48:17 +01004709class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004710 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004711 HRem(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004712 HInstruction* left,
4713 HInstruction* right,
4714 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004715 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004716
Roland Levillain9867bc72015-08-05 10:21:34 +01004717 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004718 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004719 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004720 // Our graph structure ensures we never have 0 for `y` during
4721 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004722 DCHECK_NE(y, 0);
4723 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4724 return (y == -1) ? 0 : x % y;
4725 }
4726
Roland Levillain31dd3d62016-02-16 12:21:02 +00004727 template <typename T>
4728 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004729 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004730 return std::fmod(x, y);
4731 }
4732
Roland Levillain9867bc72015-08-05 10:21:34 +01004733 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004734 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004735 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004736 }
4737 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004738 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004739 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004740 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004741 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4742 return GetBlock()->GetGraph()->GetFloatConstant(
4743 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4744 }
4745 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4746 return GetBlock()->GetGraph()->GetDoubleConstant(
4747 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4748 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004749
4750 DECLARE_INSTRUCTION(Rem);
4751
4752 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004753 DISALLOW_COPY_AND_ASSIGN(HRem);
4754};
4755
Vladimir Markofcb503c2016-05-18 12:48:17 +01004756class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00004757 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004758 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4759 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004760 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004761 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004762 SetRawInputAt(0, value);
4763 }
4764
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004765 DataType::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004766
Calin Juravled0d48522014-11-04 16:40:20 +00004767 bool CanBeMoved() const OVERRIDE { return true; }
4768
Vladimir Marko372f10e2016-05-17 16:30:10 +01004769 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004770 return true;
4771 }
4772
4773 bool NeedsEnvironment() const OVERRIDE { return true; }
4774 bool CanThrow() const OVERRIDE { return true; }
4775
Calin Juravled0d48522014-11-04 16:40:20 +00004776 DECLARE_INSTRUCTION(DivZeroCheck);
4777
4778 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004779 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4780};
4781
Vladimir Markofcb503c2016-05-18 12:48:17 +01004782class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004783 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004784 HShl(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004785 HInstruction* value,
4786 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004787 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004788 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004789 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
4790 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004791 }
4792
Roland Levillain5b5b9312016-03-22 14:57:31 +00004793 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004794 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004795 return value << (distance & max_shift_distance);
4796 }
4797
4798 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004799 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004800 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004801 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004802 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004803 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004804 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004805 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004806 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4807 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4808 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4809 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004810 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004811 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4812 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004813 LOG(FATAL) << DebugName() << " is not defined for float values";
4814 UNREACHABLE();
4815 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004816 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4817 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004818 LOG(FATAL) << DebugName() << " is not defined for double values";
4819 UNREACHABLE();
4820 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004821
4822 DECLARE_INSTRUCTION(Shl);
4823
4824 private:
4825 DISALLOW_COPY_AND_ASSIGN(HShl);
4826};
4827
Vladimir Markofcb503c2016-05-18 12:48:17 +01004828class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004829 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004830 HShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004831 HInstruction* value,
4832 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004833 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004834 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004835 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
4836 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004837 }
4838
Roland Levillain5b5b9312016-03-22 14:57:31 +00004839 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004840 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004841 return value >> (distance & max_shift_distance);
4842 }
4843
4844 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004845 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004846 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004847 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004848 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004849 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004850 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004851 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004852 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4853 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4854 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4855 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004856 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004857 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4858 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004859 LOG(FATAL) << DebugName() << " is not defined for float values";
4860 UNREACHABLE();
4861 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004862 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4863 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004864 LOG(FATAL) << DebugName() << " is not defined for double values";
4865 UNREACHABLE();
4866 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004867
4868 DECLARE_INSTRUCTION(Shr);
4869
4870 private:
4871 DISALLOW_COPY_AND_ASSIGN(HShr);
4872};
4873
Vladimir Markofcb503c2016-05-18 12:48:17 +01004874class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004875 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004876 HUShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004877 HInstruction* value,
4878 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004879 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004880 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004881 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
4882 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004883 }
4884
Roland Levillain5b5b9312016-03-22 14:57:31 +00004885 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004886 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004887 typedef typename std::make_unsigned<T>::type V;
4888 V ux = static_cast<V>(value);
4889 return static_cast<T>(ux >> (distance & max_shift_distance));
4890 }
4891
4892 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004893 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004894 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004895 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004896 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004897 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004898 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004899 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004900 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4901 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4902 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4903 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004904 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004905 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4906 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004907 LOG(FATAL) << DebugName() << " is not defined for float values";
4908 UNREACHABLE();
4909 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004910 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4911 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004912 LOG(FATAL) << DebugName() << " is not defined for double values";
4913 UNREACHABLE();
4914 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004915
4916 DECLARE_INSTRUCTION(UShr);
4917
4918 private:
4919 DISALLOW_COPY_AND_ASSIGN(HUShr);
4920};
4921
Vladimir Markofcb503c2016-05-18 12:48:17 +01004922class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004923 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004924 HAnd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004925 HInstruction* left,
4926 HInstruction* right,
4927 uint32_t dex_pc = kNoDexPc)
4928 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004929
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004930 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004931
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004932 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004933
4934 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004935 return GetBlock()->GetGraph()->GetIntConstant(
4936 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004937 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004938 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004939 return GetBlock()->GetGraph()->GetLongConstant(
4940 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004941 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004942 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4943 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4944 LOG(FATAL) << DebugName() << " is not defined for float values";
4945 UNREACHABLE();
4946 }
4947 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4948 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4949 LOG(FATAL) << DebugName() << " is not defined for double values";
4950 UNREACHABLE();
4951 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004952
4953 DECLARE_INSTRUCTION(And);
4954
4955 private:
4956 DISALLOW_COPY_AND_ASSIGN(HAnd);
4957};
4958
Vladimir Markofcb503c2016-05-18 12:48:17 +01004959class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004960 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004961 HOr(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004962 HInstruction* left,
4963 HInstruction* right,
4964 uint32_t dex_pc = kNoDexPc)
4965 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004966
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004967 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004968
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004969 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004970
4971 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004972 return GetBlock()->GetGraph()->GetIntConstant(
4973 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004974 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004975 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004976 return GetBlock()->GetGraph()->GetLongConstant(
4977 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004978 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004979 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4980 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4981 LOG(FATAL) << DebugName() << " is not defined for float values";
4982 UNREACHABLE();
4983 }
4984 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4985 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4986 LOG(FATAL) << DebugName() << " is not defined for double values";
4987 UNREACHABLE();
4988 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004989
4990 DECLARE_INSTRUCTION(Or);
4991
4992 private:
4993 DISALLOW_COPY_AND_ASSIGN(HOr);
4994};
4995
Vladimir Markofcb503c2016-05-18 12:48:17 +01004996class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004997 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004998 HXor(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004999 HInstruction* left,
5000 HInstruction* right,
5001 uint32_t dex_pc = kNoDexPc)
5002 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005003
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005004 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005005
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005006 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005007
5008 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005009 return GetBlock()->GetGraph()->GetIntConstant(
5010 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005011 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005012 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005013 return GetBlock()->GetGraph()->GetLongConstant(
5014 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005015 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005016 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5017 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5018 LOG(FATAL) << DebugName() << " is not defined for float values";
5019 UNREACHABLE();
5020 }
5021 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5022 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5023 LOG(FATAL) << DebugName() << " is not defined for double values";
5024 UNREACHABLE();
5025 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005026
5027 DECLARE_INSTRUCTION(Xor);
5028
5029 private:
5030 DISALLOW_COPY_AND_ASSIGN(HXor);
5031};
5032
Vladimir Markofcb503c2016-05-18 12:48:17 +01005033class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005034 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005035 HRor(DataType::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00005036 : HBinaryOperation(result_type, value, distance) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005037 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5038 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00005039 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005040
Roland Levillain5b5b9312016-03-22 14:57:31 +00005041 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005042 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005043 typedef typename std::make_unsigned<T>::type V;
5044 V ux = static_cast<V>(value);
5045 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005046 return static_cast<T>(ux);
5047 } else {
5048 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005049 return static_cast<T>(ux >> (distance & max_shift_value)) |
5050 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005051 }
5052 }
5053
Roland Levillain5b5b9312016-03-22 14:57:31 +00005054 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005055 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005056 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005057 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005058 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005059 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005060 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005061 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005062 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5063 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5064 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5065 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005066 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005067 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5068 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005069 LOG(FATAL) << DebugName() << " is not defined for float values";
5070 UNREACHABLE();
5071 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005072 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5073 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005074 LOG(FATAL) << DebugName() << " is not defined for double values";
5075 UNREACHABLE();
5076 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005077
5078 DECLARE_INSTRUCTION(Ror);
5079
5080 private:
5081 DISALLOW_COPY_AND_ASSIGN(HRor);
5082};
5083
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005084// The value of a parameter in this method. Its location depends on
5085// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01005086class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005087 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005088 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005089 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005090 uint8_t index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005091 DataType::Type parameter_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005092 bool is_this = false)
5093 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005094 dex_file_(dex_file),
5095 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005096 index_(index) {
5097 SetPackedFlag<kFlagIsThis>(is_this);
5098 SetPackedFlag<kFlagCanBeNull>(!is_this);
5099 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005100
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005101 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005102 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005103 uint8_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07005104 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005105
Vladimir Markoa1de9182016-02-25 11:37:38 +00005106 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
5107 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005108
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005109 DECLARE_INSTRUCTION(ParameterValue);
5110
5111 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005112 // Whether or not the parameter value corresponds to 'this' argument.
5113 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
5114 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5115 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5116 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5117 "Too many packed fields.");
5118
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005119 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005120 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005121 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005122 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005123 const uint8_t index_;
5124
5125 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
5126};
5127
Vladimir Markofcb503c2016-05-18 12:48:17 +01005128class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005129 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005130 HNot(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005131 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005132
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005133 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005134 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005135 return true;
5136 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005137
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005138 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005139
5140 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005141 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005142 }
5143 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005144 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005145 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005146 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5147 LOG(FATAL) << DebugName() << " is not defined for float values";
5148 UNREACHABLE();
5149 }
5150 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5151 LOG(FATAL) << DebugName() << " is not defined for double values";
5152 UNREACHABLE();
5153 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005154
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005155 DECLARE_INSTRUCTION(Not);
5156
5157 private:
5158 DISALLOW_COPY_AND_ASSIGN(HNot);
5159};
5160
Vladimir Markofcb503c2016-05-18 12:48:17 +01005161class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005162 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005163 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005164 : HUnaryOperation(DataType::Type::kBool, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01005165
5166 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005167 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01005168 return true;
5169 }
5170
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005171 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005172 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005173 return !x;
5174 }
5175
Roland Levillain9867bc72015-08-05 10:21:34 +01005176 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005177 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005178 }
5179 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5180 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005181 UNREACHABLE();
5182 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005183 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5184 LOG(FATAL) << DebugName() << " is not defined for float values";
5185 UNREACHABLE();
5186 }
5187 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5188 LOG(FATAL) << DebugName() << " is not defined for double values";
5189 UNREACHABLE();
5190 }
David Brazdil66d126e2015-04-03 16:02:44 +01005191
5192 DECLARE_INSTRUCTION(BooleanNot);
5193
5194 private:
5195 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
5196};
5197
Vladimir Markofcb503c2016-05-18 12:48:17 +01005198class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005199 public:
5200 // Instantiate a type conversion of `input` to `result_type`.
Vladimir Markoc8fb2112017-10-03 11:37:52 +01005201 HTypeConversion(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005202 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005203 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005204 // Invariant: We should never generate a conversion to a Boolean value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005205 DCHECK_NE(DataType::Type::kBool, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005206 }
5207
5208 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005209 DataType::Type GetInputType() const { return GetInput()->GetType(); }
5210 DataType::Type GetResultType() const { return GetType(); }
Roland Levillaindff1f282014-11-05 14:15:05 +00005211
5212 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005213 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5214 return true;
5215 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005216
Mark Mendelle82549b2015-05-06 10:55:34 -04005217 // Try to statically evaluate the conversion and return a HConstant
5218 // containing the result. If the input cannot be converted, return nullptr.
5219 HConstant* TryStaticEvaluation() const;
5220
Roland Levillaindff1f282014-11-05 14:15:05 +00005221 DECLARE_INSTRUCTION(TypeConversion);
5222
5223 private:
5224 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
5225};
5226
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005227static constexpr uint32_t kNoRegNumber = -1;
5228
Vladimir Markofcb503c2016-05-18 12:48:17 +01005229class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005230 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005231 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
5232 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005233 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005234 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005235 SetRawInputAt(0, value);
5236 }
5237
Calin Juravle10e244f2015-01-26 18:54:32 +00005238 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005239 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005240 return true;
5241 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005242
Calin Juravle10e244f2015-01-26 18:54:32 +00005243 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005244
Calin Juravle10e244f2015-01-26 18:54:32 +00005245 bool CanThrow() const OVERRIDE { return true; }
5246
5247 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005248
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005249
5250 DECLARE_INSTRUCTION(NullCheck);
5251
5252 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005253 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
5254};
5255
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005256// Embeds an ArtField and all the information required by the compiler. We cache
5257// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005258class FieldInfo : public ValueObject {
5259 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005260 FieldInfo(ArtField* field,
5261 MemberOffset field_offset,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005262 DataType::Type field_type,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005263 bool is_volatile,
5264 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005265 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005266 const DexFile& dex_file)
5267 : field_(field),
5268 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005269 field_type_(field_type),
5270 is_volatile_(is_volatile),
5271 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005272 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005273 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005274
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005275 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005276 MemberOffset GetFieldOffset() const { return field_offset_; }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005277 DataType::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005278 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005279 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005280 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005281 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005282
5283 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005284 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005285 const MemberOffset field_offset_;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005286 const DataType::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005287 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005288 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005289 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005290 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005291};
5292
Vladimir Markofcb503c2016-05-18 12:48:17 +01005293class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005294 public:
5295 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005296 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005297 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005298 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005299 bool is_volatile,
5300 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005301 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005302 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005303 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005304 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005305 field_info_(field,
5306 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005307 field_type,
5308 is_volatile,
5309 field_idx,
5310 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005311 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005312 SetRawInputAt(0, value);
5313 }
5314
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005315 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005316
Vladimir Marko372f10e2016-05-17 16:30:10 +01005317 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5318 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005319 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005320 }
5321
Calin Juravle641547a2015-04-21 22:08:51 +01005322 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005323 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005324 }
5325
5326 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005327 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5328 }
5329
Calin Juravle52c48962014-12-16 17:02:57 +00005330 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005331 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005332 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005333 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005334
5335 DECLARE_INSTRUCTION(InstanceFieldGet);
5336
5337 private:
5338 const FieldInfo field_info_;
5339
5340 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5341};
5342
Vladimir Markofcb503c2016-05-18 12:48:17 +01005343class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005344 public:
5345 HInstanceFieldSet(HInstruction* object,
5346 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005347 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005348 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005349 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005350 bool is_volatile,
5351 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005352 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005353 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005354 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005355 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005356 field_info_(field,
5357 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005358 field_type,
5359 is_volatile,
5360 field_idx,
5361 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005362 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005363 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005364 SetRawInputAt(0, object);
5365 SetRawInputAt(1, value);
5366 }
5367
Calin Juravle641547a2015-04-21 22:08:51 +01005368 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005369 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005370 }
5371
Calin Juravle52c48962014-12-16 17:02:57 +00005372 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005373 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005374 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005375 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005376 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005377 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5378 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005379
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005380 DECLARE_INSTRUCTION(InstanceFieldSet);
5381
5382 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005383 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5384 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5385 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5386 "Too many packed fields.");
5387
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005388 const FieldInfo field_info_;
5389
5390 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5391};
5392
Vladimir Markofcb503c2016-05-18 12:48:17 +01005393class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005394 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005395 HArrayGet(HInstruction* array,
5396 HInstruction* index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005397 DataType::Type type,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005398 uint32_t dex_pc)
5399 : HArrayGet(array,
5400 index,
5401 type,
5402 SideEffects::ArrayReadOfType(type),
5403 dex_pc,
5404 /* is_string_char_at */ false) {}
5405
5406 HArrayGet(HInstruction* array,
5407 HInstruction* index,
5408 DataType::Type type,
5409 SideEffects side_effects,
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005410 uint32_t dex_pc,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005411 bool is_string_char_at)
5412 : HExpression(type, side_effects, dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005413 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005414 SetRawInputAt(0, array);
5415 SetRawInputAt(1, index);
5416 }
5417
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005418 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005419 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005420 return true;
5421 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005422 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005423 // TODO: We can be smarter here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01005424 // Currently, unless the array is the result of NewArray, the array access is always
5425 // preceded by some form of null NullCheck necessary for the bounds check, usually
5426 // implicit null check on the ArrayLength input to BoundsCheck or Deoptimize for
5427 // dynamic BCE. There are cases when these could be removed to produce better code.
5428 // If we ever add optimizations to do so we should allow an implicit check here
5429 // (as long as the address falls in the first page).
5430 //
5431 // As an example of such fancy optimization, we could eliminate BoundsCheck for
5432 // a = cond ? new int[1] : null;
5433 // a[0]; // The Phi does not need bounds check for either input.
Calin Juravle77520bc2015-01-12 18:45:46 +00005434 return false;
5435 }
5436
David Brazdil4833f5a2015-12-16 10:37:39 +00005437 bool IsEquivalentOf(HArrayGet* other) const {
5438 bool result = (GetDexPc() == other->GetDexPc());
5439 if (kIsDebugBuild && result) {
5440 DCHECK_EQ(GetBlock(), other->GetBlock());
5441 DCHECK_EQ(GetArray(), other->GetArray());
5442 DCHECK_EQ(GetIndex(), other->GetIndex());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005443 if (DataType::IsIntOrLongType(GetType())) {
5444 DCHECK(DataType::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005445 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005446 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
5447 DCHECK(DataType::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005448 }
5449 }
5450 return result;
5451 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005452
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005453 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5454
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005455 HInstruction* GetArray() const { return InputAt(0); }
5456 HInstruction* GetIndex() const { return InputAt(1); }
5457
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005458 DECLARE_INSTRUCTION(ArrayGet);
5459
5460 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005461 // We treat a String as an array, creating the HArrayGet from String.charAt()
5462 // intrinsic in the instruction simplifier. We can always determine whether
5463 // a particular HArrayGet is actually a String.charAt() by looking at the type
5464 // of the input but that requires holding the mutator lock, so we prefer to use
5465 // a flag, so that code generators don't need to do the locking.
5466 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
5467 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5468 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5469 "Too many packed fields.");
5470
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005471 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5472};
5473
Vladimir Markofcb503c2016-05-18 12:48:17 +01005474class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005475 public:
5476 HArraySet(HInstruction* array,
5477 HInstruction* index,
5478 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005479 DataType::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005480 uint32_t dex_pc)
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005481 : HArraySet(array,
5482 index,
5483 value,
5484 expected_component_type,
5485 // Make a best guess for side effects now, may be refined during SSA building.
5486 ComputeSideEffects(GetComponentType(value->GetType(), expected_component_type)),
5487 dex_pc) {}
5488
5489 HArraySet(HInstruction* array,
5490 HInstruction* index,
5491 HInstruction* value,
5492 DataType::Type expected_component_type,
5493 SideEffects side_effects,
5494 uint32_t dex_pc)
5495 : HTemplateInstruction(side_effects, dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005496 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005497 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005498 SetPackedFlag<kFlagValueCanBeNull>(true);
5499 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005500 SetRawInputAt(0, array);
5501 SetRawInputAt(1, index);
5502 SetRawInputAt(2, value);
5503 }
5504
Calin Juravle77520bc2015-01-12 18:45:46 +00005505 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005506 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005507 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005508 }
5509
Mingyao Yang81014cb2015-06-02 03:16:27 -07005510 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005511 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005512
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005513 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005514 // TODO: Same as for ArrayGet.
5515 return false;
5516 }
5517
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005518 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005519 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005520 }
5521
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005522 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005523 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005524 }
5525
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005526 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005527 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005528 }
5529
Vladimir Markoa1de9182016-02-25 11:37:38 +00005530 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5531 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5532 bool StaticTypeOfArrayIsObjectArray() const {
5533 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5534 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005535
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005536 HInstruction* GetArray() const { return InputAt(0); }
5537 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005538 HInstruction* GetValue() const { return InputAt(2); }
5539
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005540 DataType::Type GetComponentType() const {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005541 return GetComponentType(GetValue()->GetType(), GetRawExpectedComponentType());
5542 }
5543
5544 static DataType::Type GetComponentType(DataType::Type value_type,
5545 DataType::Type expected_component_type) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005546 // The Dex format does not type floating point index operations. Since the
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005547 // `expected_component_type` comes from SSA building and can therefore not
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005548 // be correct, we also check what is the value type. If it is a floating
5549 // point type, we must use that type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005550 return ((value_type == DataType::Type::kFloat32) || (value_type == DataType::Type::kFloat64))
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005551 ? value_type
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005552 : expected_component_type;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005553 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005554
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005555 DataType::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005556 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005557 }
5558
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005559 static SideEffects ComputeSideEffects(DataType::Type type) {
5560 return SideEffects::ArrayWriteOfType(type).Union(SideEffectsForArchRuntimeCalls(type));
Aart Bik18b36ab2016-04-13 16:41:35 -07005561 }
5562
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005563 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type value_type) {
5564 return (value_type == DataType::Type::kReference) ? SideEffects::CanTriggerGC()
5565 : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005566 }
5567
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005568 DECLARE_INSTRUCTION(ArraySet);
5569
5570 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005571 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5572 static constexpr size_t kFieldExpectedComponentTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005573 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00005574 static constexpr size_t kFlagNeedsTypeCheck =
5575 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5576 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005577 // Cached information for the reference_type_info_ so that codegen
5578 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005579 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5580 static constexpr size_t kNumberOfArraySetPackedBits =
5581 kFlagStaticTypeOfArrayIsObjectArray + 1;
5582 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5583 using ExpectedComponentTypeField =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005584 BitField<DataType::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005585
5586 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5587};
5588
Vladimir Markofcb503c2016-05-18 12:48:17 +01005589class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005590 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005591 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005592 : HExpression(DataType::Type::kInt32, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005593 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005594 // Note that arrays do not change length, so the instruction does not
5595 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005596 SetRawInputAt(0, array);
5597 }
5598
Calin Juravle77520bc2015-01-12 18:45:46 +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 }
Calin Juravle641547a2015-04-21 22:08:51 +01005603 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5604 return obj == InputAt(0);
5605 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005606
Vladimir Markodce016e2016-04-28 13:10:02 +01005607 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5608
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005609 DECLARE_INSTRUCTION(ArrayLength);
5610
5611 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005612 // We treat a String as an array, creating the HArrayLength from String.length()
5613 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5614 // determine whether a particular HArrayLength is actually a String.length() by
5615 // looking at the type of the input but that requires holding the mutator lock, so
5616 // we prefer to use a flag, so that code generators don't need to do the locking.
5617 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5618 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5619 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5620 "Too many packed fields.");
5621
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005622 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5623};
5624
Vladimir Markofcb503c2016-05-18 12:48:17 +01005625class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005626 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005627 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5628 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005629 HBoundsCheck(HInstruction* index,
5630 HInstruction* length,
5631 uint32_t dex_pc,
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005632 bool string_char_at = false)
5633 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005634 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(index->GetType()));
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005635 SetPackedFlag<kFlagIsStringCharAt>(string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005636 SetRawInputAt(0, index);
5637 SetRawInputAt(1, length);
5638 }
5639
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005640 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005641 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005642 return true;
5643 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005644
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005645 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005646
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005647 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005648
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005649 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005650
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005651 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005652
5653 DECLARE_INSTRUCTION(BoundsCheck);
5654
5655 private:
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005656 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005657
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005658 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5659};
5660
Vladimir Markofcb503c2016-05-18 12:48:17 +01005661class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005662 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005663 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005664 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005665
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005666 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005667 return true;
5668 }
5669
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005670 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5671 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005672
5673 DECLARE_INSTRUCTION(SuspendCheck);
5674
5675 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005676 // Only used for code generation, in order to share the same slow path between back edges
5677 // of a same loop.
5678 SlowPathCode* slow_path_;
5679
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005680 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5681};
5682
David Srbecky0cf44932015-12-09 14:09:59 +00005683// Pseudo-instruction which provides the native debugger with mapping information.
5684// It ensures that we can generate line number and local variables at this point.
5685class HNativeDebugInfo : public HTemplateInstruction<0> {
5686 public:
5687 explicit HNativeDebugInfo(uint32_t dex_pc)
5688 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5689
5690 bool NeedsEnvironment() const OVERRIDE {
5691 return true;
5692 }
5693
5694 DECLARE_INSTRUCTION(NativeDebugInfo);
5695
5696 private:
5697 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5698};
5699
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005700/**
5701 * Instruction to load a Class object.
5702 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005703class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005704 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005705 // Determines how to load the Class.
5706 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005707 // We cannot load this class. See HSharpening::SharpenLoadClass.
5708 kInvalid = -1,
5709
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005710 // Use the Class* from the method's own ArtMethod*.
5711 kReferrersClass,
5712
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005713 // Use PC-relative boot image Class* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01005714 // Used for boot image classes referenced by boot image code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005715 kBootImageLinkTimePcRelative,
5716
5717 // Use a known boot image Class* address, embedded in the code by the codegen.
5718 // Used for boot image classes referenced by apps in AOT- and JIT-compiled code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005719 kBootImageAddress,
5720
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005721 // Use a PC-relative load from a boot image ClassTable mmapped into the .bss
5722 // of the oat file.
5723 kBootImageClassTable,
5724
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005725 // Load from an entry in the .bss section using a PC-relative load.
5726 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
5727 kBssEntry,
5728
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005729 // Load from the root table associated with the JIT compiled method.
5730 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005731
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005732 // Load using a simple runtime call. This is the fall-back load kind when
5733 // the codegen is unable to use another appropriate kind.
5734 kRuntimeCall,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005735
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005736 kLast = kRuntimeCall
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005737 };
5738
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005739 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005740 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005741 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005742 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005743 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005744 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00005745 bool needs_access_check)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005746 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5747 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005748 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005749 dex_file_(dex_file),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005750 klass_(klass),
Calin Juravle2e768302015-07-28 14:41:11 +00005751 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005752 // Referrers class should not need access check. We never inline unverified
5753 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005754 DCHECK(!is_referrers_class || !needs_access_check);
5755
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005756 SetPackedField<LoadKindField>(
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005757 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kRuntimeCall);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005758 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00005759 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005760 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005761 }
5762
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005763 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005764
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005765 LoadKind GetLoadKind() const {
5766 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005767 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005768
5769 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005770
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005771 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005772
Andreas Gampea5b09a62016-11-17 15:21:22 -08005773 size_t ComputeHashCode() const OVERRIDE { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005774
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005775 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005776
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005777 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005778 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005779 }
5780
Calin Juravle0ba218d2015-05-19 18:46:01 +01005781 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005782 // The entrypoint the code generator is going to call does not do
5783 // clinit of the class.
5784 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005785 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005786 }
5787
5788 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00005789 return NeedsAccessCheck() ||
5790 MustGenerateClinitCheck() ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005791 GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005792 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01005793 }
5794
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005795 bool CanThrow() const OVERRIDE {
Vladimir Marko41559982017-01-06 14:04:23 +00005796 return NeedsAccessCheck() ||
5797 MustGenerateClinitCheck() ||
5798 // If the class is in the boot image, the lookup in the runtime call cannot throw.
5799 // This keeps CanThrow() consistent between non-PIC (using kBootImageAddress) and
5800 // PIC and subsequently avoids a DCE behavior dependency on the PIC option.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005801 ((GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005802 GetLoadKind() == LoadKind::kBssEntry) &&
5803 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005804 }
5805
Calin Juravleacf735c2015-02-12 15:25:22 +00005806 ReferenceTypeInfo GetLoadedClassRTI() {
5807 return loaded_class_rti_;
5808 }
5809
5810 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5811 // Make sure we only set exact types (the loaded class should never be merged).
5812 DCHECK(rti.IsExact());
5813 loaded_class_rti_ = rti;
5814 }
5815
Andreas Gampea5b09a62016-11-17 15:21:22 -08005816 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005817 const DexFile& GetDexFile() const { return dex_file_; }
5818
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005819 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005820 return GetLoadKind() == LoadKind::kRuntimeCall;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005821 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005822
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005823 static SideEffects SideEffectsForArchRuntimeCalls() {
5824 return SideEffects::CanTriggerGC();
5825 }
5826
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005827 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005828 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005829 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005830 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005831
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005832 void MarkInBootImage() {
5833 SetPackedFlag<kFlagIsInBootImage>(true);
5834 }
5835
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005836 void AddSpecialInput(HInstruction* special_input);
5837
5838 using HInstruction::GetInputRecords; // Keep the const version visible.
5839 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5840 return ArrayRef<HUserRecord<HInstruction*>>(
5841 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
5842 }
5843
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005844 DataType::Type GetType() const OVERRIDE {
5845 return DataType::Type::kReference;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005846 }
5847
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005848 Handle<mirror::Class> GetClass() const {
5849 return klass_;
5850 }
5851
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005852 DECLARE_INSTRUCTION(LoadClass);
5853
5854 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005855 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00005856 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005857 // Whether this instruction must generate the initialization check.
5858 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005859 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005860 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
5861 static constexpr size_t kFieldLoadKindSize =
5862 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5863 static constexpr size_t kNumberOfLoadClassPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005864 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005865 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
5866
5867 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005868 return load_kind == LoadKind::kReferrersClass ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005869 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005870 load_kind == LoadKind::kBootImageClassTable ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005871 load_kind == LoadKind::kBssEntry ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005872 load_kind == LoadKind::kRuntimeCall;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005873 }
5874
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005875 void SetLoadKindInternal(LoadKind load_kind);
5876
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005877 // The special input is the HCurrentMethod for kRuntimeCall or kReferrersClass.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005878 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005879 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005880 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005881
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005882 // A type index and dex file where the class can be accessed. The dex file can be:
5883 // - The compiling method's dex file if the class is defined there too.
5884 // - The compiling method's dex file if the class is referenced there.
5885 // - The dex file where the class is defined. When the load kind can only be
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005886 // kBssEntry or kRuntimeCall, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08005887 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005888 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005889
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005890 Handle<mirror::Class> klass_;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005891
Calin Juravleacf735c2015-02-12 15:25:22 +00005892 ReferenceTypeInfo loaded_class_rti_;
5893
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005894 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5895};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005896std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
5897
5898// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005899inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005900 // The special input is used for PC-relative loads on some architectures,
5901 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005902 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005903 GetLoadKind() == LoadKind::kBootImageAddress ||
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005904 GetLoadKind() == LoadKind::kBootImageClassTable ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005905 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005906 DCHECK(special_input_.GetInstruction() == nullptr);
5907 special_input_ = HUserRecord<HInstruction*>(special_input);
5908 special_input->AddUseAt(this, 0);
5909}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005910
Vladimir Marko372f10e2016-05-17 16:30:10 +01005911class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005912 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005913 // Determines how to load the String.
5914 enum class LoadKind {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005915 // Use PC-relative boot image String* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01005916 // Used for boot image strings referenced by boot image code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005917 kBootImageLinkTimePcRelative,
5918
5919 // Use a known boot image String* address, embedded in the code by the codegen.
5920 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005921 kBootImageAddress,
5922
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005923 // Use a PC-relative load from a boot image InternTable mmapped into the .bss
5924 // of the oat file.
5925 kBootImageInternTable,
5926
Vladimir Markoaad75c62016-10-03 08:46:48 +00005927 // Load from an entry in the .bss section using a PC-relative load.
5928 // Used for strings outside boot image when .bss is accessible with a PC-relative load.
5929 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005930
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005931 // Load from the root table associated with the JIT compiled method.
5932 kJitTableAddress,
5933
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005934 // Load using a simple runtime call. This is the fall-back load kind when
5935 // the codegen is unable to use another appropriate kind.
5936 kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005937
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005938 kLast = kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005939 };
5940
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005941 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005942 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005943 const DexFile& dex_file,
5944 uint32_t dex_pc)
Vladimir Marko372f10e2016-05-17 16:30:10 +01005945 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5946 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005947 string_index_(string_index),
5948 dex_file_(dex_file) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005949 SetPackedField<LoadKindField>(LoadKind::kRuntimeCall);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005950 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005951
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005952 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005953
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005954 LoadKind GetLoadKind() const {
5955 return GetPackedField<LoadKindField>();
5956 }
5957
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005958 const DexFile& GetDexFile() const {
5959 return dex_file_;
5960 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005961
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005962 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005963 return string_index_;
5964 }
5965
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005966 Handle<mirror::String> GetString() const {
5967 return string_;
5968 }
5969
5970 void SetString(Handle<mirror::String> str) {
5971 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005972 }
5973
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005974 bool CanBeMoved() const OVERRIDE { return true; }
5975
Vladimir Marko372f10e2016-05-17 16:30:10 +01005976 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005977
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005978 size_t ComputeHashCode() const OVERRIDE { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005979
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005980 // Will call the runtime if we need to load the string through
5981 // the dex cache and the string is not guaranteed to be there yet.
5982 bool NeedsEnvironment() const OVERRIDE {
5983 LoadKind load_kind = GetLoadKind();
Vladimir Marko764d4542017-05-16 10:31:41 +01005984 if (load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005985 load_kind == LoadKind::kBootImageAddress ||
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005986 load_kind == LoadKind::kBootImageInternTable ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005987 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005988 return false;
5989 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00005990 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005991 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005992
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005993 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005994 return GetLoadKind() == LoadKind::kRuntimeCall;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005995 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005996
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005997 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005998 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005999
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006000 static SideEffects SideEffectsForArchRuntimeCalls() {
6001 return SideEffects::CanTriggerGC();
6002 }
6003
Vladimir Marko372f10e2016-05-17 16:30:10 +01006004 void AddSpecialInput(HInstruction* special_input);
6005
6006 using HInstruction::GetInputRecords; // Keep the const version visible.
6007 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
6008 return ArrayRef<HUserRecord<HInstruction*>>(
6009 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006010 }
6011
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006012 DataType::Type GetType() const OVERRIDE {
6013 return DataType::Type::kReference;
Vladimir Marko372f10e2016-05-17 16:30:10 +01006014 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006015
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006016 DECLARE_INSTRUCTION(LoadString);
6017
6018 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006019 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006020 static constexpr size_t kFieldLoadKindSize =
6021 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
6022 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006023 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006024 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006025
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006026 void SetLoadKindInternal(LoadKind load_kind);
6027
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006028 // The special input is the HCurrentMethod for kRuntimeCall.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006029 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00006030 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01006031 HUserRecord<HInstruction*> special_input_;
6032
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006033 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006034 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006035
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006036 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006037
6038 DISALLOW_COPY_AND_ASSIGN(HLoadString);
6039};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006040std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
6041
6042// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006043inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006044 // The special input is used for PC-relative loads on some architectures,
6045 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006046 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006047 GetLoadKind() == LoadKind::kBootImageAddress ||
6048 GetLoadKind() == LoadKind::kBootImageInternTable ||
6049 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01006050 // HLoadString::GetInputRecords() returns an empty array at this point,
6051 // so use the GetInputRecords() from the base class to set the input record.
6052 DCHECK(special_input_.GetInstruction() == nullptr);
6053 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006054 special_input->AddUseAt(this, 0);
6055}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006056
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006057/**
6058 * Performs an initialization check on its Class object input.
6059 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01006060class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006061 public:
Roland Levillain3887c462015-08-12 18:15:42 +01006062 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07006063 : HExpression(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006064 DataType::Type::kReference,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006065 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
6066 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006067 SetRawInputAt(0, constant);
6068 }
6069
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006070 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006071 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006072 return true;
6073 }
6074
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006075 bool NeedsEnvironment() const OVERRIDE {
6076 // May call runtime to initialize the class.
6077 return true;
6078 }
6079
Nicolas Geoffray729645a2015-11-19 13:29:02 +00006080 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006081
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006082 HLoadClass* GetLoadClass() const {
6083 DCHECK(InputAt(0)->IsLoadClass());
6084 return InputAt(0)->AsLoadClass();
6085 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006086
6087 DECLARE_INSTRUCTION(ClinitCheck);
6088
6089 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006090 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
6091};
6092
Vladimir Markofcb503c2016-05-18 12:48:17 +01006093class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006094 public:
6095 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006096 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006097 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006098 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006099 bool is_volatile,
6100 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006101 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006102 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006103 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01006104 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006105 field_info_(field,
6106 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006107 field_type,
6108 is_volatile,
6109 field_idx,
6110 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006111 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006112 SetRawInputAt(0, cls);
6113 }
6114
Calin Juravle52c48962014-12-16 17:02:57 +00006115
Calin Juravle10c9cbe2014-12-19 10:50:19 +00006116 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006117
Vladimir Marko372f10e2016-05-17 16:30:10 +01006118 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
6119 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006120 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006121 }
6122
6123 size_t ComputeHashCode() const OVERRIDE {
6124 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6125 }
6126
Calin Juravle52c48962014-12-16 17:02:57 +00006127 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006128 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006129 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006130 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006131
6132 DECLARE_INSTRUCTION(StaticFieldGet);
6133
6134 private:
6135 const FieldInfo field_info_;
6136
6137 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
6138};
6139
Vladimir Markofcb503c2016-05-18 12:48:17 +01006140class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006141 public:
6142 HStaticFieldSet(HInstruction* cls,
6143 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006144 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006145 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006146 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006147 bool is_volatile,
6148 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006149 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006150 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006151 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01006152 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006153 field_info_(field,
6154 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006155 field_type,
6156 is_volatile,
6157 field_idx,
6158 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006159 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006160 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006161 SetRawInputAt(0, cls);
6162 SetRawInputAt(1, value);
6163 }
6164
Calin Juravle52c48962014-12-16 17:02:57 +00006165 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006166 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006167 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006168 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006169
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006170 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006171 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6172 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006173
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006174 DECLARE_INSTRUCTION(StaticFieldSet);
6175
6176 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006177 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6178 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6179 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6180 "Too many packed fields.");
6181
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006182 const FieldInfo field_info_;
6183
6184 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
6185};
6186
Vladimir Markofcb503c2016-05-18 12:48:17 +01006187class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006188 public:
6189 HUnresolvedInstanceFieldGet(HInstruction* obj,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006190 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006191 uint32_t field_index,
6192 uint32_t dex_pc)
6193 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6194 field_index_(field_index) {
6195 SetRawInputAt(0, obj);
6196 }
6197
6198 bool NeedsEnvironment() const OVERRIDE { return true; }
6199 bool CanThrow() const OVERRIDE { return true; }
6200
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006201 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006202 uint32_t GetFieldIndex() const { return field_index_; }
6203
6204 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6205
6206 private:
6207 const uint32_t field_index_;
6208
6209 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
6210};
6211
Vladimir Markofcb503c2016-05-18 12:48:17 +01006212class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006213 public:
6214 HUnresolvedInstanceFieldSet(HInstruction* obj,
6215 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006216 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006217 uint32_t field_index,
6218 uint32_t dex_pc)
6219 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006220 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006221 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006222 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006223 SetRawInputAt(0, obj);
6224 SetRawInputAt(1, value);
6225 }
6226
6227 bool NeedsEnvironment() const OVERRIDE { return true; }
6228 bool CanThrow() const OVERRIDE { return true; }
6229
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006230 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006231 uint32_t GetFieldIndex() const { return field_index_; }
6232
6233 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6234
6235 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006236 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6237 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006238 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006239 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6240 kFieldFieldType + kFieldFieldTypeSize;
6241 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6242 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006243 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006244
Calin Juravlee460d1d2015-09-29 04:52:17 +01006245 const uint32_t field_index_;
6246
6247 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
6248};
6249
Vladimir Markofcb503c2016-05-18 12:48:17 +01006250class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006251 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006252 HUnresolvedStaticFieldGet(DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006253 uint32_t field_index,
6254 uint32_t dex_pc)
6255 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6256 field_index_(field_index) {
6257 }
6258
6259 bool NeedsEnvironment() const OVERRIDE { return true; }
6260 bool CanThrow() const OVERRIDE { return true; }
6261
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006262 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006263 uint32_t GetFieldIndex() const { return field_index_; }
6264
6265 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6266
6267 private:
6268 const uint32_t field_index_;
6269
6270 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
6271};
6272
Vladimir Markofcb503c2016-05-18 12:48:17 +01006273class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006274 public:
6275 HUnresolvedStaticFieldSet(HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006276 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006277 uint32_t field_index,
6278 uint32_t dex_pc)
6279 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006280 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006281 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006282 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006283 SetRawInputAt(0, value);
6284 }
6285
6286 bool NeedsEnvironment() const OVERRIDE { return true; }
6287 bool CanThrow() const OVERRIDE { return true; }
6288
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006289 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006290 uint32_t GetFieldIndex() const { return field_index_; }
6291
6292 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6293
6294 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006295 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6296 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006297 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006298 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6299 kFieldFieldType + kFieldFieldTypeSize;
6300 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6301 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006302 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006303
Calin Juravlee460d1d2015-09-29 04:52:17 +01006304 const uint32_t field_index_;
6305
6306 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
6307};
6308
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006309// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006310class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006311 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006312 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006313 : HExpression(DataType::Type::kReference, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006314
David Brazdilbbd733e2015-08-18 17:48:17 +01006315 bool CanBeNull() const OVERRIDE { return false; }
6316
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006317 DECLARE_INSTRUCTION(LoadException);
6318
6319 private:
6320 DISALLOW_COPY_AND_ASSIGN(HLoadException);
6321};
6322
David Brazdilcb1c0552015-08-04 16:22:25 +01006323// Implicit part of move-exception which clears thread-local exception storage.
6324// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006325class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006326 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006327 explicit HClearException(uint32_t dex_pc = kNoDexPc)
6328 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01006329
6330 DECLARE_INSTRUCTION(ClearException);
6331
6332 private:
6333 DISALLOW_COPY_AND_ASSIGN(HClearException);
6334};
6335
Vladimir Markofcb503c2016-05-18 12:48:17 +01006336class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006337 public:
6338 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006339 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006340 SetRawInputAt(0, exception);
6341 }
6342
6343 bool IsControlFlow() const OVERRIDE { return true; }
6344
6345 bool NeedsEnvironment() const OVERRIDE { return true; }
6346
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006347 bool CanThrow() const OVERRIDE { return true; }
6348
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006349
6350 DECLARE_INSTRUCTION(Throw);
6351
6352 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006353 DISALLOW_COPY_AND_ASSIGN(HThrow);
6354};
6355
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006356/**
6357 * Implementation strategies for the code generator of a HInstanceOf
6358 * or `HCheckCast`.
6359 */
6360enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006361 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006362 kExactCheck, // Can do a single class compare.
6363 kClassHierarchyCheck, // Can just walk the super class chain.
6364 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6365 kInterfaceCheck, // No optimization yet when checking against an interface.
6366 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006367 kArrayCheck, // No optimization yet when checking against a generic array.
6368 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006369};
6370
Roland Levillain86503782016-02-11 19:07:30 +00006371std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6372
Vladimir Markofcb503c2016-05-18 12:48:17 +01006373class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006374 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006375 HInstanceOf(HInstruction* object,
6376 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006377 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006378 uint32_t dex_pc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006379 : HExpression(DataType::Type::kBool,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006380 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006381 dex_pc) {
6382 SetPackedField<TypeCheckKindField>(check_kind);
6383 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006384 SetRawInputAt(0, object);
6385 SetRawInputAt(1, constant);
6386 }
6387
6388 bool CanBeMoved() const OVERRIDE { return true; }
6389
Vladimir Marko372f10e2016-05-17 16:30:10 +01006390 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006391 return true;
6392 }
6393
6394 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006395 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006396 }
6397
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006398 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006399 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6400 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6401 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6402 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006403
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006404 static bool CanCallRuntime(TypeCheckKind check_kind) {
6405 // Mips currently does runtime calls for any other checks.
6406 return check_kind != TypeCheckKind::kExactCheck;
6407 }
6408
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006409 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006410 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006411 }
6412
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006413 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006414
6415 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006416 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6417 static constexpr size_t kFieldTypeCheckKindSize =
6418 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6419 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6420 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6421 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6422 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006423
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006424 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6425};
6426
Vladimir Markofcb503c2016-05-18 12:48:17 +01006427class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006428 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07006429 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006430 : HExpression(DataType::Type::kReference, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006431 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6432 SetPackedFlag<kFlagUpperCanBeNull>(true);
6433 SetPackedFlag<kFlagCanBeNull>(true);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006434 DCHECK_EQ(input->GetType(), DataType::Type::kReference);
Calin Juravleb1498f62015-02-16 13:13:29 +00006435 SetRawInputAt(0, input);
6436 }
6437
David Brazdilf5552582015-12-27 13:36:12 +00006438 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006439 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006440 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006441 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006442
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006443 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006444 DCHECK(GetUpperCanBeNull() || !can_be_null);
6445 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006446 }
6447
Vladimir Markoa1de9182016-02-25 11:37:38 +00006448 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006449
Calin Juravleb1498f62015-02-16 13:13:29 +00006450 DECLARE_INSTRUCTION(BoundType);
6451
6452 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006453 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6454 // is false then CanBeNull() cannot be true).
6455 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6456 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6457 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6458 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6459
Calin Juravleb1498f62015-02-16 13:13:29 +00006460 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006461 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6462 // It is used to bound the type in cases like:
6463 // if (x instanceof ClassX) {
6464 // // uper_bound_ will be ClassX
6465 // }
David Brazdilf5552582015-12-27 13:36:12 +00006466 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006467
6468 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6469};
6470
Vladimir Markofcb503c2016-05-18 12:48:17 +01006471class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006472 public:
6473 HCheckCast(HInstruction* object,
6474 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006475 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006476 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006477 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6478 SetPackedField<TypeCheckKindField>(check_kind);
6479 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006480 SetRawInputAt(0, object);
6481 SetRawInputAt(1, constant);
6482 }
6483
6484 bool CanBeMoved() const OVERRIDE { return true; }
6485
Vladimir Marko372f10e2016-05-17 16:30:10 +01006486 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006487 return true;
6488 }
6489
6490 bool NeedsEnvironment() const OVERRIDE {
6491 // Instruction may throw a CheckCastError.
6492 return true;
6493 }
6494
6495 bool CanThrow() const OVERRIDE { return true; }
6496
Vladimir Markoa1de9182016-02-25 11:37:38 +00006497 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6498 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6499 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6500 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006501
6502 DECLARE_INSTRUCTION(CheckCast);
6503
6504 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006505 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6506 static constexpr size_t kFieldTypeCheckKindSize =
6507 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6508 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6509 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6510 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6511 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006512
6513 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006514};
6515
Andreas Gampe26de38b2016-07-27 17:53:11 -07006516/**
6517 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
6518 * @details We define the combined barrier types that are actually required
6519 * by the Java Memory Model, rather than using exactly the terminology from
6520 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
6521 * primitives. Note that the JSR-133 cookbook generally does not deal with
6522 * store atomicity issues, and the recipes there are not always entirely sufficient.
6523 * The current recipe is as follows:
6524 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
6525 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
6526 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
6527 * -# Use StoreStore barrier after all stores but before return from any constructor whose
6528 * class has final fields.
6529 * -# Use NTStoreStore to order non-temporal stores with respect to all later
6530 * store-to-memory instructions. Only generated together with non-temporal stores.
6531 */
6532enum MemBarrierKind {
6533 kAnyStore,
6534 kLoadAny,
6535 kStoreStore,
6536 kAnyAny,
6537 kNTStoreStore,
6538 kLastBarrierKind = kNTStoreStore
6539};
6540std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
6541
Vladimir Markofcb503c2016-05-18 12:48:17 +01006542class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006543 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006544 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006545 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006546 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6547 SetPackedField<BarrierKindField>(barrier_kind);
6548 }
Calin Juravle27df7582015-04-17 19:12:31 +01006549
Vladimir Markoa1de9182016-02-25 11:37:38 +00006550 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006551
6552 DECLARE_INSTRUCTION(MemoryBarrier);
6553
6554 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006555 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6556 static constexpr size_t kFieldBarrierKindSize =
6557 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6558 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6559 kFieldBarrierKind + kFieldBarrierKindSize;
6560 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6561 "Too many packed fields.");
6562 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006563
6564 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6565};
6566
Igor Murashkind01745e2017-04-05 16:40:31 -07006567// A constructor fence orders all prior stores to fields that could be accessed via a final field of
6568// the specified object(s), with respect to any subsequent store that might "publish"
6569// (i.e. make visible) the specified object to another thread.
6570//
6571// JLS 17.5.1 "Semantics of final fields" states that a freeze action happens
6572// for all final fields (that were set) at the end of the invoked constructor.
6573//
6574// The constructor fence models the freeze actions for the final fields of an object
6575// being constructed (semantically at the end of the constructor). Constructor fences
6576// have a per-object affinity; two separate objects being constructed get two separate
6577// constructor fences.
6578//
6579// (Note: that if calling a super-constructor or forwarding to another constructor,
6580// the freezes would happen at the end of *that* constructor being invoked).
6581//
6582// The memory model guarantees that when the object being constructed is "published" after
6583// constructor completion (i.e. escapes the current thread via a store), then any final field
6584// writes must be observable on other threads (once they observe that publication).
6585//
6586// Further, anything written before the freeze, and read by dereferencing through the final field,
6587// must also be visible (so final object field could itself have an object with non-final fields;
6588// yet the freeze must also extend to them).
6589//
6590// Constructor example:
6591//
6592// class HasFinal {
6593// final int field; Optimizing IR for <init>()V:
6594// HasFinal() {
6595// field = 123; HInstanceFieldSet(this, HasFinal.field, 123)
6596// // freeze(this.field); HConstructorFence(this)
6597// } HReturn
6598// }
6599//
6600// HConstructorFence can serve double duty as a fence for new-instance/new-array allocations of
6601// already-initialized classes; in that case the allocation must act as a "default-initializer"
6602// of the object which effectively writes the class pointer "final field".
6603//
6604// For example, we can model default-initialiation as roughly the equivalent of the following:
6605//
6606// class Object {
6607// private final Class header;
6608// }
6609//
6610// Java code: Optimizing IR:
6611//
6612// T new_instance<T>() {
6613// Object obj = allocate_memory(T.class.size); obj = HInvoke(art_quick_alloc_object, T)
6614// obj.header = T.class; // header write is done by above call.
6615// // freeze(obj.header) HConstructorFence(obj)
6616// return (T)obj;
6617// }
6618//
6619// See also:
6620// * CompilerDriver::RequiresConstructorBarrier
6621// * QuasiAtomic::ThreadFenceForConstructor
6622//
6623class HConstructorFence FINAL : public HVariableInputSizeInstruction {
6624 // A fence has variable inputs because the inputs can be removed
6625 // after prepare_for_register_allocation phase.
6626 // (TODO: In the future a fence could freeze multiple objects
6627 // after merging two fences together.)
6628 public:
6629 // `fence_object` is the reference that needs to be protected for correct publication.
6630 //
6631 // It makes sense in the following situations:
6632 // * <init> constructors, it's the "this" parameter (i.e. HParameterValue, s.t. IsThis() == true).
6633 // * new-instance-like instructions, it's the return value (i.e. HNewInstance).
6634 //
6635 // After construction the `fence_object` becomes the 0th input.
6636 // This is not an input in a real sense, but just a convenient place to stash the information
6637 // about the associated object.
6638 HConstructorFence(HInstruction* fence_object,
6639 uint32_t dex_pc,
6640 ArenaAllocator* arena)
6641 // We strongly suspect there is not a more accurate way to describe the fine-grained reordering
6642 // constraints described in the class header. We claim that these SideEffects constraints
6643 // enforce a superset of the real constraints.
6644 //
6645 // The ordering described above is conservatively modeled with SideEffects as follows:
6646 //
6647 // * To prevent reordering of the publication stores:
6648 // ----> "Reads of objects" is the initial SideEffect.
6649 // * For every primitive final field store in the constructor:
6650 // ----> Union that field's type as a read (e.g. "Read of T") into the SideEffect.
6651 // * If there are any stores to reference final fields in the constructor:
6652 // ----> Use a more conservative "AllReads" SideEffect because any stores to any references
6653 // that are reachable from `fence_object` also need to be prevented for reordering
6654 // (and we do not want to do alias analysis to figure out what those stores are).
6655 //
6656 // In the implementation, this initially starts out as an "all reads" side effect; this is an
6657 // even more conservative approach than the one described above, and prevents all of the
6658 // above reordering without analyzing any of the instructions in the constructor.
6659 //
6660 // If in a later phase we discover that there are no writes to reference final fields,
6661 // we can refine the side effect to a smaller set of type reads (see above constraints).
6662 : HVariableInputSizeInstruction(SideEffects::AllReads(),
6663 dex_pc,
6664 arena,
6665 /* number_of_inputs */ 1,
6666 kArenaAllocConstructorFenceInputs) {
6667 DCHECK(fence_object != nullptr);
6668 SetRawInputAt(0, fence_object);
6669 }
6670
6671 // The object associated with this constructor fence.
6672 //
6673 // (Note: This will be null after the prepare_for_register_allocation phase,
6674 // as all constructor fence inputs are removed there).
6675 HInstruction* GetFenceObject() const {
6676 return InputAt(0);
6677 }
6678
6679 // Find all the HConstructorFence uses (`fence_use`) for `this` and:
6680 // - Delete `fence_use` from `this`'s use list.
6681 // - Delete `this` from `fence_use`'s inputs list.
6682 // - If the `fence_use` is dead, remove it from the graph.
6683 //
6684 // A fence is considered dead once it no longer has any uses
6685 // and all of the inputs are dead.
6686 //
6687 // This must *not* be called during/after prepare_for_register_allocation,
6688 // because that removes all the inputs to the fences but the fence is actually
6689 // still considered live.
Igor Murashkin6ef45672017-08-08 13:59:55 -07006690 //
6691 // Returns how many HConstructorFence instructions were removed from graph.
6692 static size_t RemoveConstructorFences(HInstruction* instruction);
Igor Murashkind01745e2017-04-05 16:40:31 -07006693
Igor Murashkindd018df2017-08-09 10:38:31 -07006694 // Combine all inputs of `this` and `other` instruction and remove
6695 // `other` from the graph.
6696 //
6697 // Inputs are unique after the merge.
6698 //
6699 // Requirement: `this` must not be the same as `other.
6700 void Merge(HConstructorFence* other);
6701
Igor Murashkin79d8fa72017-04-18 09:37:23 -07006702 // Check if this constructor fence is protecting
6703 // an HNewInstance or HNewArray that is also the immediate
6704 // predecessor of `this`.
6705 //
Igor Murashkindd018df2017-08-09 10:38:31 -07006706 // If `ignore_inputs` is true, then the immediate predecessor doesn't need
6707 // to be one of the inputs of `this`.
6708 //
Igor Murashkin79d8fa72017-04-18 09:37:23 -07006709 // Returns the associated HNewArray or HNewInstance,
6710 // or null otherwise.
Igor Murashkindd018df2017-08-09 10:38:31 -07006711 HInstruction* GetAssociatedAllocation(bool ignore_inputs = false);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07006712
Igor Murashkind01745e2017-04-05 16:40:31 -07006713 DECLARE_INSTRUCTION(ConstructorFence);
6714
6715 private:
6716 DISALLOW_COPY_AND_ASSIGN(HConstructorFence);
6717};
6718
Vladimir Markofcb503c2016-05-18 12:48:17 +01006719class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006720 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006721 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006722 kEnter,
6723 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006724 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006725 };
6726
6727 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006728 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006729 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6730 dex_pc) {
6731 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006732 SetRawInputAt(0, object);
6733 }
6734
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006735 // Instruction may go into runtime, so we need an environment.
6736 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006737
6738 bool CanThrow() const OVERRIDE {
6739 // Verifier guarantees that monitor-exit cannot throw.
6740 // This is important because it allows the HGraphBuilder to remove
6741 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6742 return IsEnter();
6743 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006744
Vladimir Markoa1de9182016-02-25 11:37:38 +00006745 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6746 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006747
6748 DECLARE_INSTRUCTION(MonitorOperation);
6749
Calin Juravle52c48962014-12-16 17:02:57 +00006750 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006751 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6752 static constexpr size_t kFieldOperationKindSize =
6753 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6754 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6755 kFieldOperationKind + kFieldOperationKindSize;
6756 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6757 "Too many packed fields.");
6758 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006759
6760 private:
6761 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6762};
6763
Vladimir Markofcb503c2016-05-18 12:48:17 +01006764class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00006765 public:
6766 HSelect(HInstruction* condition,
6767 HInstruction* true_value,
6768 HInstruction* false_value,
6769 uint32_t dex_pc)
6770 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6771 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6772
6773 // First input must be `true_value` or `false_value` to allow codegens to
6774 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6775 // that architectures which implement HSelect as a conditional move also
6776 // will not need to invert the condition.
6777 SetRawInputAt(0, false_value);
6778 SetRawInputAt(1, true_value);
6779 SetRawInputAt(2, condition);
6780 }
6781
6782 HInstruction* GetFalseValue() const { return InputAt(0); }
6783 HInstruction* GetTrueValue() const { return InputAt(1); }
6784 HInstruction* GetCondition() const { return InputAt(2); }
6785
6786 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006787 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6788 return true;
6789 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006790
6791 bool CanBeNull() const OVERRIDE {
6792 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6793 }
6794
6795 DECLARE_INSTRUCTION(Select);
6796
6797 private:
6798 DISALLOW_COPY_AND_ASSIGN(HSelect);
6799};
6800
Vladimir Markof9f64412015-09-02 14:05:49 +01006801class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006802 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006803 MoveOperands(Location source,
6804 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006805 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01006806 HInstruction* instruction)
6807 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006808
6809 Location GetSource() const { return source_; }
6810 Location GetDestination() const { return destination_; }
6811
6812 void SetSource(Location value) { source_ = value; }
6813 void SetDestination(Location value) { destination_ = value; }
6814
6815 // The parallel move resolver marks moves as "in-progress" by clearing the
6816 // destination (but not the source).
6817 Location MarkPending() {
6818 DCHECK(!IsPending());
6819 Location dest = destination_;
6820 destination_ = Location::NoLocation();
6821 return dest;
6822 }
6823
6824 void ClearPending(Location dest) {
6825 DCHECK(IsPending());
6826 destination_ = dest;
6827 }
6828
6829 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006830 DCHECK(source_.IsValid() || destination_.IsInvalid());
6831 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006832 }
6833
6834 // True if this blocks a move from the given location.
6835 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006836 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006837 }
6838
6839 // A move is redundant if it's been eliminated, if its source and
6840 // destination are the same, or if its destination is unneeded.
6841 bool IsRedundant() const {
6842 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6843 }
6844
6845 // We clear both operands to indicate move that's been eliminated.
6846 void Eliminate() {
6847 source_ = destination_ = Location::NoLocation();
6848 }
6849
6850 bool IsEliminated() const {
6851 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6852 return source_.IsInvalid();
6853 }
6854
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006855 DataType::Type GetType() const { return type_; }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006856
Nicolas Geoffray90218252015-04-15 11:56:51 +01006857 bool Is64BitMove() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006858 return DataType::Is64BitType(type_);
Nicolas Geoffray90218252015-04-15 11:56:51 +01006859 }
6860
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006861 HInstruction* GetInstruction() const { return instruction_; }
6862
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006863 private:
6864 Location source_;
6865 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006866 // The type this move is for.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006867 DataType::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006868 // The instruction this move is assocatied with. Null when this move is
6869 // for moving an input in the expected locations of user (including a phi user).
6870 // This is only used in debug mode, to ensure we do not connect interval siblings
6871 // in the same parallel move.
6872 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006873};
6874
Roland Levillainc9285912015-12-18 10:38:42 +00006875std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6876
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006877static constexpr size_t kDefaultNumberOfMoves = 4;
6878
Vladimir Markofcb503c2016-05-18 12:48:17 +01006879class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006880 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006881 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006882 : HTemplateInstruction(SideEffects::None(), dex_pc),
6883 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6884 moves_.reserve(kDefaultNumberOfMoves);
6885 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006886
Nicolas Geoffray90218252015-04-15 11:56:51 +01006887 void AddMove(Location source,
6888 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006889 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01006890 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006891 DCHECK(source.IsValid());
6892 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006893 if (kIsDebugBuild) {
6894 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006895 for (const MoveOperands& move : moves_) {
6896 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006897 // Special case the situation where the move is for the spill slot
6898 // of the instruction.
6899 if ((GetPrevious() == instruction)
6900 || ((GetPrevious() == nullptr)
6901 && instruction->IsPhi()
6902 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006903 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006904 << "Doing parallel moves for the same instruction.";
6905 } else {
6906 DCHECK(false) << "Doing parallel moves for the same instruction.";
6907 }
6908 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006909 }
6910 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006911 for (const MoveOperands& move : moves_) {
6912 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006913 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006914 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006915 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006916 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006917 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006918 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006919 }
6920
Vladimir Marko225b6462015-09-28 12:17:40 +01006921 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006922 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006923 }
6924
Vladimir Marko225b6462015-09-28 12:17:40 +01006925 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006926
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006927 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006928
6929 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006930 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006931
6932 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6933};
6934
Mark Mendell0616ae02015-04-17 12:49:27 -04006935} // namespace art
6936
Aart Bikf8f5a162017-02-06 15:35:29 -08006937#include "nodes_vector.h"
6938
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006939#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6940#include "nodes_shared.h"
6941#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006942#ifdef ART_ENABLE_CODEGEN_mips
6943#include "nodes_mips.h"
6944#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006945#ifdef ART_ENABLE_CODEGEN_x86
6946#include "nodes_x86.h"
6947#endif
6948
6949namespace art {
6950
Igor Murashkin6ef45672017-08-08 13:59:55 -07006951class OptimizingCompilerStats;
6952
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006953class HGraphVisitor : public ValueObject {
6954 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07006955 explicit HGraphVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
6956 : stats_(stats),
6957 graph_(graph) {}
Dave Allison20dfc792014-06-16 20:44:29 -07006958 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006959
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006960 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006961 virtual void VisitBasicBlock(HBasicBlock* block);
6962
Roland Levillain633021e2014-10-01 14:12:25 +01006963 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006964 void VisitInsertionOrder();
6965
Roland Levillain633021e2014-10-01 14:12:25 +01006966 // Visit the graph following dominator tree reverse post-order.
6967 void VisitReversePostOrder();
6968
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006969 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006970
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006971 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006972#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006973 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6974
6975 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6976
6977#undef DECLARE_VISIT_INSTRUCTION
6978
Igor Murashkin6ef45672017-08-08 13:59:55 -07006979 protected:
6980 OptimizingCompilerStats* stats_;
6981
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006982 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006983 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006984
6985 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6986};
6987
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006988class HGraphDelegateVisitor : public HGraphVisitor {
6989 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07006990 explicit HGraphDelegateVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
6991 : HGraphVisitor(graph, stats) {}
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006992 virtual ~HGraphDelegateVisitor() {}
6993
6994 // Visit functions that delegate to to super class.
6995#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006996 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006997
6998 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6999
7000#undef DECLARE_VISIT_INSTRUCTION
7001
7002 private:
7003 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
7004};
7005
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007006// Iterator over the blocks that art part of the loop. Includes blocks part
7007// of an inner loop. The order in which the blocks are iterated is on their
7008// block id.
7009class HBlocksInLoopIterator : public ValueObject {
7010 public:
7011 explicit HBlocksInLoopIterator(const HLoopInformation& info)
7012 : blocks_in_loop_(info.GetBlocks()),
7013 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
7014 index_(0) {
7015 if (!blocks_in_loop_.IsBitSet(index_)) {
7016 Advance();
7017 }
7018 }
7019
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007020 bool Done() const { return index_ == blocks_.size(); }
7021 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007022 void Advance() {
7023 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007024 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007025 if (blocks_in_loop_.IsBitSet(index_)) {
7026 break;
7027 }
7028 }
7029 }
7030
7031 private:
7032 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007033 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007034 size_t index_;
7035
7036 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
7037};
7038
Mingyao Yang3584bce2015-05-19 16:01:59 -07007039// Iterator over the blocks that art part of the loop. Includes blocks part
7040// of an inner loop. The order in which the blocks are iterated is reverse
7041// post order.
7042class HBlocksInLoopReversePostOrderIterator : public ValueObject {
7043 public:
7044 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
7045 : blocks_in_loop_(info.GetBlocks()),
7046 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
7047 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007048 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007049 Advance();
7050 }
7051 }
7052
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007053 bool Done() const { return index_ == blocks_.size(); }
7054 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07007055 void Advance() {
7056 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007057 for (size_t e = blocks_.size(); index_ < e; ++index_) {
7058 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007059 break;
7060 }
7061 }
7062 }
7063
7064 private:
7065 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007066 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07007067 size_t index_;
7068
7069 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
7070};
7071
Aart Bikf3e61ee2017-04-12 17:09:20 -07007072// Returns int64_t value of a properly typed constant.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007073inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00007074 if (constant->IsIntConstant()) {
7075 return constant->AsIntConstant()->GetValue();
7076 } else if (constant->IsLongConstant()) {
7077 return constant->AsLongConstant()->GetValue();
7078 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00007079 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00007080 return 0;
7081 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007082}
7083
Aart Bikf3e61ee2017-04-12 17:09:20 -07007084// Returns true iff instruction is an integral constant (and sets value on success).
7085inline bool IsInt64AndGet(HInstruction* instruction, /*out*/ int64_t* value) {
7086 if (instruction->IsIntConstant()) {
7087 *value = instruction->AsIntConstant()->GetValue();
7088 return true;
7089 } else if (instruction->IsLongConstant()) {
7090 *value = instruction->AsLongConstant()->GetValue();
7091 return true;
7092 } else if (instruction->IsNullConstant()) {
7093 *value = 0;
7094 return true;
7095 }
7096 return false;
7097}
7098
Aart Bik0148de42017-09-05 09:25:01 -07007099// Returns true iff instruction is the given integral constant.
7100inline bool IsInt64Value(HInstruction* instruction, int64_t value) {
7101 int64_t val = 0;
7102 return IsInt64AndGet(instruction, &val) && val == value;
7103}
7104
7105// Returns true iff instruction is a zero bit pattern.
7106inline bool IsZeroBitPattern(HInstruction* instruction) {
7107 return instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern();
7108}
7109
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007110#define INSTRUCTION_TYPE_CHECK(type, super) \
7111 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
7112 inline const H##type* HInstruction::As##type() const { \
7113 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
7114 } \
7115 inline H##type* HInstruction::As##type() { \
7116 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
7117 }
7118
7119 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
7120#undef INSTRUCTION_TYPE_CHECK
7121
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00007122// Create space in `blocks` for adding `number_of_new_blocks` entries
7123// starting at location `at`. Blocks after `at` are moved accordingly.
7124inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
7125 size_t number_of_new_blocks,
7126 size_t after) {
7127 DCHECK_LT(after, blocks->size());
7128 size_t old_size = blocks->size();
7129 size_t new_size = old_size + number_of_new_blocks;
7130 blocks->resize(new_size);
7131 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
7132}
7133
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00007134/*
7135 * Hunt "under the hood" of array lengths (leading to array references),
7136 * null checks (also leading to array references), and new arrays
7137 * (leading to the actual length). This makes it more likely related
7138 * instructions become actually comparable.
7139 */
7140inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
7141 while (instruction->IsArrayLength() ||
7142 instruction->IsNullCheck() ||
7143 instruction->IsNewArray()) {
7144 instruction = instruction->IsNewArray()
7145 ? instruction->AsNewArray()->GetLength()
7146 : instruction->InputAt(0);
7147 }
7148 return instruction;
7149}
7150
Artem Serov21c7e6f2017-07-27 16:04:42 +01007151void RemoveEnvironmentUses(HInstruction* instruction);
7152bool HasEnvironmentUsedByOthers(HInstruction* instruction);
7153void ResetEnvironmentInputRecords(HInstruction* instruction);
7154
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007155} // namespace art
7156
7157#endif // ART_COMPILER_OPTIMIZING_NODES_H_