blob: 07ab325a0e26a3765001dbc8a02ef8b3e8919952 [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
Vladimir Markoca6fff82017-10-03 14:49:14 +010048class ArenaStack;
David Brazdil1abb4192015-02-17 18:33:36 +000049class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000050class HBasicBlock;
Igor Murashkind01745e2017-04-05 16:40:31 -070051class HConstructorFence;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010052class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000053class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010054class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000055class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000056class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000057class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000058class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000059class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000060class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000061class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000062class HNullConstant;
Igor Murashkind01745e2017-04-05 16:40:31 -070063class HParameterValue;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010064class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010065class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010066class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010067class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000068class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010069class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000070class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000071
Mathieu Chartier736b5602015-09-02 14:54:11 -070072namespace mirror {
73class DexCache;
74} // namespace mirror
75
Nicolas Geoffray818f2102014-02-18 16:43:35 +000076static const int kDefaultNumberOfBlocks = 8;
77static const int kDefaultNumberOfSuccessors = 2;
78static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010079static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010080static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000081static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000082
Roland Levillain5b5b9312016-03-22 14:57:31 +000083// The maximum (meaningful) distance (31) that can be used in an integer shift/rotate operation.
84static constexpr int32_t kMaxIntShiftDistance = 0x1f;
85// The maximum (meaningful) distance (63) that can be used in a long shift/rotate operation.
86static constexpr int32_t kMaxLongShiftDistance = 0x3f;
Calin Juravle9aec02f2014-11-18 23:06:35 +000087
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010088static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070089static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010090
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010091static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
92
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060093static constexpr uint32_t kNoDexPc = -1;
94
Vladimir Markodbb7f5b2016-03-30 13:23:58 +010095inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
96 // For the purposes of the compiler, the dex files must actually be the same object
97 // if we want to safely treat them as the same. This is especially important for JIT
98 // as custom class loaders can open the same underlying file (or memory) multiple
99 // times and provide different class resolution but no two class loaders should ever
100 // use the same DexFile object - doing so is an unsupported hack that can lead to
101 // all sorts of weird failures.
102 return &lhs == &rhs;
103}
104
Dave Allison20dfc792014-06-16 20:44:29 -0700105enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -0700106 // All types.
107 kCondEQ, // ==
108 kCondNE, // !=
109 // Signed integers and floating-point numbers.
110 kCondLT, // <
111 kCondLE, // <=
112 kCondGT, // >
113 kCondGE, // >=
114 // Unsigned integers.
115 kCondB, // <
116 kCondBE, // <=
117 kCondA, // >
118 kCondAE, // >=
Scott Wakeling2c76e062016-08-31 09:48:54 +0100119 // First and last aliases.
120 kCondFirst = kCondEQ,
121 kCondLast = kCondAE,
Dave Allison20dfc792014-06-16 20:44:29 -0700122};
123
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000124enum GraphAnalysisResult {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000125 kAnalysisSkipped,
David Brazdilbadd8262016-02-02 16:28:56 +0000126 kAnalysisInvalidBytecode,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000127 kAnalysisFailThrowCatchLoop,
128 kAnalysisFailAmbiguousArrayOp,
129 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000130};
131
Andreas Gampe9186ced2016-12-12 14:28:21 -0800132template <typename T>
133static inline typename std::make_unsigned<T>::type MakeUnsigned(T x) {
134 return static_cast<typename std::make_unsigned<T>::type>(x);
135}
136
Vladimir Markof9f64412015-09-02 14:05:49 +0100137class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100138 public:
139 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
140
141 void AddInstruction(HInstruction* instruction);
142 void RemoveInstruction(HInstruction* instruction);
143
David Brazdilc3d743f2015-04-22 13:40:50 +0100144 // Insert `instruction` before/after an existing instruction `cursor`.
145 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
146 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
147
Roland Levillain6b469232014-09-25 10:10:38 +0100148 // Return true if this list contains `instruction`.
149 bool Contains(HInstruction* instruction) const;
150
Roland Levillainccc07a92014-09-16 14:48:16 +0100151 // Return true if `instruction1` is found before `instruction2` in
152 // this instruction list and false otherwise. Abort if none
153 // of these instructions is found.
154 bool FoundBefore(const HInstruction* instruction1,
155 const HInstruction* instruction2) const;
156
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000157 bool IsEmpty() const { return first_instruction_ == nullptr; }
158 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
159
160 // Update the block of all instructions to be `block`.
161 void SetBlockOfInstructions(HBasicBlock* block) const;
162
163 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000164 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000165 void Add(const HInstructionList& instruction_list);
166
David Brazdil2d7352b2015-04-20 14:52:42 +0100167 // Return the number of instructions in the list. This is an expensive operation.
168 size_t CountSize() const;
169
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100170 private:
171 HInstruction* first_instruction_;
172 HInstruction* last_instruction_;
173
174 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000175 friend class HGraph;
176 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100177 friend class HInstructionIterator;
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +0000178 friend class HInstructionIteratorHandleChanges;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100179 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100180
181 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
182};
183
David Brazdil4833f5a2015-12-16 10:37:39 +0000184class ReferenceTypeInfo : ValueObject {
185 public:
186 typedef Handle<mirror::Class> TypeHandle;
187
Vladimir Markoa1de9182016-02-25 11:37:38 +0000188 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
189
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700190 static ReferenceTypeInfo Create(TypeHandle type_handle) REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil94ab38f2016-06-21 17:48:19 +0100191 return Create(type_handle, type_handle->CannotBeAssignedFromOtherTypes());
192 }
193
Vladimir Markoa1de9182016-02-25 11:37:38 +0000194 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000195 return ReferenceTypeInfo(type_handle, is_exact);
196 }
197
198 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
199
Vladimir Markof39745e2016-01-26 12:16:55 +0000200 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000201 return handle.GetReference() != nullptr;
202 }
203
Vladimir Marko456307a2016-04-19 14:12:13 +0000204 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000205 return IsValidHandle(type_handle_);
206 }
207
208 bool IsExact() const { return is_exact_; }
209
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700210 bool IsObjectClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000211 DCHECK(IsValid());
212 return GetTypeHandle()->IsObjectClass();
213 }
214
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700215 bool IsStringClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000216 DCHECK(IsValid());
217 return GetTypeHandle()->IsStringClass();
218 }
219
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700220 bool IsObjectArray() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000221 DCHECK(IsValid());
222 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
223 }
224
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700225 bool IsInterface() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000226 DCHECK(IsValid());
227 return GetTypeHandle()->IsInterface();
228 }
229
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700230 bool IsArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000231 DCHECK(IsValid());
232 return GetTypeHandle()->IsArrayClass();
233 }
234
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700235 bool IsPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000236 DCHECK(IsValid());
237 return GetTypeHandle()->IsPrimitiveArray();
238 }
239
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700240 bool IsNonPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000241 DCHECK(IsValid());
242 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
243 }
244
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700245 bool CanArrayHold(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000246 DCHECK(IsValid());
247 if (!IsExact()) return false;
248 if (!IsArrayClass()) return false;
249 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
250 }
251
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700252 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000253 DCHECK(IsValid());
254 if (!IsExact()) return false;
255 if (!IsArrayClass()) return false;
256 if (!rti.IsArrayClass()) return false;
257 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
258 rti.GetTypeHandle()->GetComponentType());
259 }
260
261 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
262
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700263 bool IsSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000264 DCHECK(IsValid());
265 DCHECK(rti.IsValid());
266 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
267 }
268
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700269 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000270 DCHECK(IsValid());
271 DCHECK(rti.IsValid());
272 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
273 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
274 }
275
276 // Returns true if the type information provide the same amount of details.
277 // Note that it does not mean that the instructions have the same actual type
278 // (because the type can be the result of a merge).
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700279 bool IsEqual(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000280 if (!IsValid() && !rti.IsValid()) {
281 // Invalid types are equal.
282 return true;
283 }
284 if (!IsValid() || !rti.IsValid()) {
285 // One is valid, the other not.
286 return false;
287 }
288 return IsExact() == rti.IsExact()
289 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
290 }
291
292 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000293 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
294 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
295 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000296
297 // The class of the object.
298 TypeHandle type_handle_;
299 // Whether or not the type is exact or a superclass of the actual type.
300 // Whether or not we have any information about this type.
301 bool is_exact_;
302};
303
304std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
305
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000306// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100307class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000308 public:
Vladimir Markoca6fff82017-10-03 14:49:14 +0100309 HGraph(ArenaAllocator* allocator,
310 ArenaStack* arena_stack,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100311 const DexFile& dex_file,
312 uint32_t method_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700313 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100314 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100315 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000316 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100317 int start_instruction_id = 0)
Vladimir Markoca6fff82017-10-03 14:49:14 +0100318 : allocator_(allocator),
319 arena_stack_(arena_stack),
320 blocks_(allocator->Adapter(kArenaAllocBlockList)),
321 reverse_post_order_(allocator->Adapter(kArenaAllocReversePostOrder)),
322 linear_order_(allocator->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700323 entry_block_(nullptr),
324 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100325 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100326 number_of_vregs_(0),
327 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000328 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400329 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000330 has_try_catch_(false),
Aart Bikb13c65b2017-03-21 20:14:07 -0700331 has_simd_(false),
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800332 has_loops_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000333 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000334 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000335 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100336 dex_file_(dex_file),
337 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100338 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100339 in_ssa_form_(false),
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800340 number_of_cha_guards_(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700341 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000342 cached_null_constant_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100343 cached_int_constants_(std::less<int32_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
344 cached_float_constants_(std::less<int32_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
345 cached_long_constants_(std::less<int64_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
346 cached_double_constants_(std::less<int64_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000347 cached_current_method_(nullptr),
Nicolas Geoffray53fec082017-03-27 12:56:16 +0100348 art_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000349 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
Mingyao Yang063fc772016-08-02 11:02:54 -0700350 osr_(osr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100351 cha_single_implementation_list_(allocator->Adapter(kArenaAllocCHA)) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100352 blocks_.reserve(kDefaultNumberOfBlocks);
353 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000354
David Brazdilbadd8262016-02-02 16:28:56 +0000355 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700356 void InitializeInexactObjectRTI(VariableSizedHandleScope* handles);
David Brazdilbadd8262016-02-02 16:28:56 +0000357
Vladimir Markoca6fff82017-10-03 14:49:14 +0100358 ArenaAllocator* GetAllocator() const { return allocator_; }
359 ArenaStack* GetArenaStack() const { return arena_stack_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100360 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
361
David Brazdil69ba7b72015-06-23 18:27:30 +0100362 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000363 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100364
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000365 HBasicBlock* GetEntryBlock() const { return entry_block_; }
366 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100367 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000368
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000369 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
370 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000371
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000372 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100373
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100374 void ComputeDominanceInformation();
375 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000376 void ClearLoopInformation();
377 void FindBackEdges(ArenaBitVector* visited);
378 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100379 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100380 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000381
David Brazdil4833f5a2015-12-16 10:37:39 +0000382 // Analyze all natural loops in this graph. Returns a code specifying that it
383 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000384 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000385 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100386
David Brazdilffee3d32015-07-06 11:48:53 +0100387 // Iterate over blocks to compute try block membership. Needs reverse post
388 // order and loop information.
389 void ComputeTryBlockInformation();
390
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000391 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000392 // Returns the instruction to replace the invoke expression or null if the
393 // invoke is for a void method. Note that the caller is responsible for replacing
394 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000395 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000396
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000397 // Update the loop and try membership of `block`, which was spawned from `reference`.
398 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
399 // should be the new back edge.
400 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
401 HBasicBlock* reference,
402 bool replace_if_back_edge);
403
Mingyao Yang3584bce2015-05-19 16:01:59 -0700404 // Need to add a couple of blocks to test if the loop body is entered and
405 // put deoptimization instructions, etc.
406 void TransformLoopHeaderForBCE(HBasicBlock* header);
407
Aart Bikf8f5a162017-02-06 15:35:29 -0800408 // Adds a new loop directly after the loop with the given header and exit.
409 // Returns the new preheader.
410 HBasicBlock* TransformLoopForVectorization(HBasicBlock* header,
411 HBasicBlock* body,
412 HBasicBlock* exit);
413
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000414 // Removes `block` from the graph. Assumes `block` has been disconnected from
415 // other blocks and has no instructions or phis.
416 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000417
David Brazdilfc6a86a2015-06-26 10:33:45 +0000418 // Splits the edge between `block` and `successor` while preserving the
419 // indices in the predecessor/successor lists. If there are multiple edges
420 // between the blocks, the lowest indices are used.
421 // Returns the new block which is empty and has the same dex pc as `successor`.
422 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
423
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100424 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
Artem Serovc73ee372017-07-31 15:08:40 +0100425 void OrderLoopHeaderPredecessors(HBasicBlock* header);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100426 void SimplifyLoop(HBasicBlock* header);
427
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000428 int32_t GetNextInstructionId() {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100429 CHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000430 return current_instruction_id_++;
431 }
432
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000433 int32_t GetCurrentInstructionId() const {
434 return current_instruction_id_;
435 }
436
437 void SetCurrentInstructionId(int32_t id) {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100438 CHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000439 current_instruction_id_ = id;
440 }
441
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100442 uint16_t GetMaximumNumberOfOutVRegs() const {
443 return maximum_number_of_out_vregs_;
444 }
445
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000446 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
447 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100448 }
449
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100450 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
451 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
452 }
453
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000454 void UpdateTemporariesVRegSlots(size_t slots) {
455 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100456 }
457
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000458 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100459 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000460 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100461 }
462
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100463 void SetNumberOfVRegs(uint16_t number_of_vregs) {
464 number_of_vregs_ = number_of_vregs;
465 }
466
467 uint16_t GetNumberOfVRegs() const {
468 return number_of_vregs_;
469 }
470
471 void SetNumberOfInVRegs(uint16_t value) {
472 number_of_in_vregs_ = value;
473 }
474
David Brazdildee58d62016-04-07 09:54:26 +0000475 uint16_t GetNumberOfInVRegs() const {
476 return number_of_in_vregs_;
477 }
478
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100479 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100480 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100481 return number_of_vregs_ - number_of_in_vregs_;
482 }
483
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100484 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100485 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100486 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100487
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100488 ArrayRef<HBasicBlock* const> GetReversePostOrderSkipEntryBlock() {
489 DCHECK(GetReversePostOrder()[0] == entry_block_);
490 return ArrayRef<HBasicBlock* const>(GetReversePostOrder()).SubArray(1);
491 }
492
493 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetPostOrder() const {
494 return ReverseRange(GetReversePostOrder());
495 }
496
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100497 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100498 return linear_order_;
499 }
500
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100501 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetLinearPostOrder() const {
502 return ReverseRange(GetLinearOrder());
503 }
504
Mark Mendell1152c922015-04-24 17:06:35 -0400505 bool HasBoundsChecks() const {
506 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800507 }
508
Mark Mendell1152c922015-04-24 17:06:35 -0400509 void SetHasBoundsChecks(bool value) {
510 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800511 }
512
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000513 bool IsDebuggable() const { return debuggable_; }
514
David Brazdil8d5b8b22015-03-24 10:51:52 +0000515 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000516 // already, it is created and inserted into the graph. This method is only for
517 // integral types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100518 HConstant* GetConstant(DataType::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000519
520 // TODO: This is problematic for the consistency of reference type propagation
521 // because it can be created anytime after the pass and thus it will be left
522 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600523 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000524
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600525 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
526 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000527 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600528 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
529 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000530 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600531 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
532 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000533 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600534 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
535 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000536 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000537
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100538 HCurrentMethod* GetCurrentMethod();
539
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100540 const DexFile& GetDexFile() const {
541 return dex_file_;
542 }
543
544 uint32_t GetMethodIdx() const {
545 return method_idx_;
546 }
547
Igor Murashkind01745e2017-04-05 16:40:31 -0700548 // Get the method name (without the signature), e.g. "<init>"
549 const char* GetMethodName() const;
550
551 // Get the pretty method name (class + name + optionally signature).
552 std::string PrettyMethod(bool with_signature = true) const;
553
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100554 InvokeType GetInvokeType() const {
555 return invoke_type_;
556 }
557
Mark Mendellc4701932015-04-10 13:18:51 -0400558 InstructionSet GetInstructionSet() const {
559 return instruction_set_;
560 }
561
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000562 bool IsCompilingOsr() const { return osr_; }
563
Mingyao Yang063fc772016-08-02 11:02:54 -0700564 ArenaSet<ArtMethod*>& GetCHASingleImplementationList() {
565 return cha_single_implementation_list_;
566 }
567
568 void AddCHASingleImplementationDependency(ArtMethod* method) {
569 cha_single_implementation_list_.insert(method);
570 }
571
572 bool HasShouldDeoptimizeFlag() const {
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800573 return number_of_cha_guards_ != 0;
Mingyao Yang063fc772016-08-02 11:02:54 -0700574 }
575
David Brazdil77a48ae2015-09-15 12:34:04 +0000576 bool HasTryCatch() const { return has_try_catch_; }
577 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100578
Aart Bikb13c65b2017-03-21 20:14:07 -0700579 bool HasSIMD() const { return has_simd_; }
580 void SetHasSIMD(bool value) { has_simd_ = value; }
581
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800582 bool HasLoops() const { return has_loops_; }
583 void SetHasLoops(bool value) { has_loops_ = value; }
584
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000585 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
586 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
587
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100588 ArtMethod* GetArtMethod() const { return art_method_; }
589 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
590
Roland Levillain5e8d5f02016-10-18 18:03:43 +0100591 // Returns an instruction with the opposite Boolean value from 'cond'.
Mark Mendellf6529172015-11-17 11:16:56 -0500592 // The instruction has been inserted into the graph, either as a constant, or
593 // before cursor.
594 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
595
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000596 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
597
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800598 uint32_t GetNumberOfCHAGuards() { return number_of_cha_guards_; }
599 void SetNumberOfCHAGuards(uint32_t num) { number_of_cha_guards_ = num; }
600 void IncrementNumberOfCHAGuards() { number_of_cha_guards_++; }
601
David Brazdil2d7352b2015-04-20 14:52:42 +0100602 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000603 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100604 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000605
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000606 template <class InstructionType, typename ValueType>
607 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600608 ArenaSafeMap<ValueType, InstructionType*>* cache,
609 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000610 // Try to find an existing constant of the given value.
611 InstructionType* constant = nullptr;
612 auto cached_constant = cache->find(value);
613 if (cached_constant != cache->end()) {
614 constant = cached_constant->second;
615 }
616
617 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100618 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000619 if (constant == nullptr || constant->GetBlock() == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100620 constant = new (allocator_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000621 cache->Overwrite(value, constant);
622 InsertConstant(constant);
623 }
624 return constant;
625 }
626
David Brazdil8d5b8b22015-03-24 10:51:52 +0000627 void InsertConstant(HConstant* instruction);
628
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000629 // Cache a float constant into the graph. This method should only be
630 // called by the SsaBuilder when creating "equivalent" instructions.
631 void CacheFloatConstant(HFloatConstant* constant);
632
633 // See CacheFloatConstant comment.
634 void CacheDoubleConstant(HDoubleConstant* constant);
635
Vladimir Markoca6fff82017-10-03 14:49:14 +0100636 ArenaAllocator* const allocator_;
637 ArenaStack* const arena_stack_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000638
639 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100640 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000641
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100642 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100643 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000644
Aart Bik281c6812016-08-26 11:31:48 -0700645 // List of blocks to perform a linear order tree traversal. Unlike the reverse
646 // post order, this order is not incrementally kept up-to-date.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100647 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100648
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000649 HBasicBlock* entry_block_;
650 HBasicBlock* exit_block_;
651
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100652 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100653 uint16_t maximum_number_of_out_vregs_;
654
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100655 // The number of virtual registers in this method. Contains the parameters.
656 uint16_t number_of_vregs_;
657
658 // The number of virtual registers used by parameters of this method.
659 uint16_t number_of_in_vregs_;
660
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000661 // Number of vreg size slots that the temporaries use (used in baseline compiler).
662 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100663
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800664 // Flag whether there are bounds checks in the graph. We can skip
665 // BCE if it's false. It's only best effort to keep it up to date in
666 // the presence of code elimination so there might be false positives.
Mark Mendell1152c922015-04-24 17:06:35 -0400667 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800668
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800669 // Flag whether there are try/catch blocks in the graph. We will skip
670 // try/catch-related passes if it's false. It's only best effort to keep
671 // it up to date in the presence of code elimination so there might be
672 // false positives.
David Brazdil77a48ae2015-09-15 12:34:04 +0000673 bool has_try_catch_;
674
Aart Bikb13c65b2017-03-21 20:14:07 -0700675 // Flag whether SIMD instructions appear in the graph. If true, the
676 // code generators may have to be more careful spilling the wider
677 // contents of SIMD registers.
678 bool has_simd_;
679
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800680 // Flag whether there are any loops in the graph. We can skip loop
681 // optimization if it's false. It's only best effort to keep it up
682 // to date in the presence of code elimination so there might be false
683 // positives.
684 bool has_loops_;
685
686 // Flag whether there are any irreducible loops in the graph. It's only
687 // best effort to keep it up to date in the presence of code elimination
688 // so there might be false positives.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000689 bool has_irreducible_loops_;
690
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000691 // Indicates whether the graph should be compiled in a way that
692 // ensures full debuggability. If false, we can apply more
693 // aggressive optimizations that may limit the level of debugging.
694 const bool debuggable_;
695
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000696 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000697 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000698
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100699 // The dex file from which the method is from.
700 const DexFile& dex_file_;
701
702 // The method index in the dex file.
703 const uint32_t method_idx_;
704
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100705 // If inlined, this encodes how the callee is being invoked.
706 const InvokeType invoke_type_;
707
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100708 // Whether the graph has been transformed to SSA form. Only used
709 // in debug mode to ensure we are not using properties only valid
710 // for non-SSA form (like the number of temporaries).
711 bool in_ssa_form_;
712
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800713 // Number of CHA guards in the graph. Used to short-circuit the
714 // CHA guard optimization pass when there is no CHA guard left.
715 uint32_t number_of_cha_guards_;
716
Mathieu Chartiere401d142015-04-22 13:56:20 -0700717 const InstructionSet instruction_set_;
718
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000719 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000720 HNullConstant* cached_null_constant_;
721 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000722 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000723 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000724 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000725
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100726 HCurrentMethod* cached_current_method_;
727
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100728 // The ArtMethod this graph is for. Note that for AOT, it may be null,
729 // for example for methods whose declaring class could not be resolved
730 // (such as when the superclass could not be found).
731 ArtMethod* art_method_;
732
David Brazdil4833f5a2015-12-16 10:37:39 +0000733 // Keep the RTI of inexact Object to avoid having to pass stack handle
734 // collection pointer to passes which may create NullConstant.
735 ReferenceTypeInfo inexact_object_rti_;
736
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000737 // Whether we are compiling this graph for on stack replacement: this will
738 // make all loops seen as irreducible and emit special stack maps to mark
739 // compiled code entries which the interpreter can directly jump to.
740 const bool osr_;
741
Mingyao Yang063fc772016-08-02 11:02:54 -0700742 // List of methods that are assumed to have single implementation.
743 ArenaSet<ArtMethod*> cha_single_implementation_list_;
744
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000745 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100746 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000747 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000748 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000749 DISALLOW_COPY_AND_ASSIGN(HGraph);
750};
751
Vladimir Markof9f64412015-09-02 14:05:49 +0100752class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000753 public:
754 HLoopInformation(HBasicBlock* header, HGraph* graph)
755 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100756 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000757 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100758 contains_irreducible_loop_(false),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100759 back_edges_(graph->GetAllocator()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100760 // Make bit vector growable, as the number of blocks may change.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100761 blocks_(graph->GetAllocator(),
762 graph->GetBlocks().size(),
763 true,
764 kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100765 back_edges_.reserve(kDefaultNumberOfBackEdges);
766 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100767
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000768 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100769 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000770
771 void Dump(std::ostream& os);
772
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100773 HBasicBlock* GetHeader() const {
774 return header_;
775 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000776
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000777 void SetHeader(HBasicBlock* block) {
778 header_ = block;
779 }
780
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100781 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
782 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
783 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
784
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000785 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100786 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000787 }
788
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100789 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100790 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100791 }
792
David Brazdil46e2a392015-03-16 17:31:52 +0000793 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100794 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100795 }
796
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000797 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100798 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000799 }
800
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100801 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100802
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100803 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100804 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100805 }
806
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100807 // Returns the lifetime position of the back edge that has the
808 // greatest lifetime position.
809 size_t GetLifetimeEnd() const;
810
811 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100812 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100813 }
814
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000815 // Finds blocks that are part of this loop.
816 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100817
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100818 // Returns whether this loop information contains `block`.
819 // Note that this loop information *must* be populated before entering this function.
820 bool Contains(const HBasicBlock& block) const;
821
822 // Returns whether this loop information is an inner loop of `other`.
823 // Note that `other` *must* be populated before entering this function.
824 bool IsIn(const HLoopInformation& other) const;
825
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800826 // Returns true if instruction is not defined within this loop.
827 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700828
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100829 const ArenaBitVector& GetBlocks() const { return blocks_; }
830
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000831 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000832 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000833
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000834 void ClearAllBlocks() {
835 blocks_.ClearAllBits();
836 }
837
David Brazdil3f4a5222016-05-06 12:46:21 +0100838 bool HasBackEdgeNotDominatedByHeader() const;
839
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100840 bool IsPopulated() const {
841 return blocks_.GetHighestBitSet() != -1;
842 }
843
Anton Shaminf89381f2016-05-16 16:44:13 +0600844 bool DominatesAllBackEdges(HBasicBlock* block);
845
David Sehrc757dec2016-11-04 15:48:34 -0700846 bool HasExitEdge() const;
847
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000848 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100849 // Internal recursive implementation of `Populate`.
850 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100851 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100852
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000853 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100854 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000855 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100856 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100857 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100858 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000859
860 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
861};
862
David Brazdilec16f792015-08-19 15:04:01 +0100863// Stores try/catch information for basic blocks.
864// Note that HGraph is constructed so that catch blocks cannot simultaneously
865// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100866class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100867 public:
868 // Try block information constructor.
869 explicit TryCatchInformation(const HTryBoundary& try_entry)
870 : try_entry_(&try_entry),
871 catch_dex_file_(nullptr),
872 catch_type_index_(DexFile::kDexNoIndex16) {
873 DCHECK(try_entry_ != nullptr);
874 }
875
876 // Catch block information constructor.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800877 TryCatchInformation(dex::TypeIndex catch_type_index, const DexFile& dex_file)
David Brazdilec16f792015-08-19 15:04:01 +0100878 : try_entry_(nullptr),
879 catch_dex_file_(&dex_file),
880 catch_type_index_(catch_type_index) {}
881
882 bool IsTryBlock() const { return try_entry_ != nullptr; }
883
884 const HTryBoundary& GetTryEntry() const {
885 DCHECK(IsTryBlock());
886 return *try_entry_;
887 }
888
889 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
890
891 bool IsCatchAllTypeIndex() const {
892 DCHECK(IsCatchBlock());
Andreas Gampea5b09a62016-11-17 15:21:22 -0800893 return !catch_type_index_.IsValid();
David Brazdilec16f792015-08-19 15:04:01 +0100894 }
895
Andreas Gampea5b09a62016-11-17 15:21:22 -0800896 dex::TypeIndex GetCatchTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +0100897 DCHECK(IsCatchBlock());
898 return catch_type_index_;
899 }
900
901 const DexFile& GetCatchDexFile() const {
902 DCHECK(IsCatchBlock());
903 return *catch_dex_file_;
904 }
905
906 private:
907 // One of possibly several TryBoundary instructions entering the block's try.
908 // Only set for try blocks.
909 const HTryBoundary* try_entry_;
910
911 // Exception type information. Only set for catch blocks.
912 const DexFile* catch_dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800913 const dex::TypeIndex catch_type_index_;
David Brazdilec16f792015-08-19 15:04:01 +0100914};
915
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100916static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100917static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100918
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000919// A block in a method. Contains the list of instructions represented
920// as a double linked list. Each block knows its predecessors and
921// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100922
Vladimir Markof9f64412015-09-02 14:05:49 +0100923class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000924 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700925 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000926 : graph_(graph),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100927 predecessors_(graph->GetAllocator()->Adapter(kArenaAllocPredecessors)),
928 successors_(graph->GetAllocator()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000929 loop_information_(nullptr),
930 dominator_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100931 dominated_blocks_(graph->GetAllocator()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100932 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100933 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100934 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000935 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000936 try_catch_information_(nullptr) {
937 predecessors_.reserve(kDefaultNumberOfPredecessors);
938 successors_.reserve(kDefaultNumberOfSuccessors);
939 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
940 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000941
Vladimir Marko60584552015-09-03 13:35:12 +0000942 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100943 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000944 }
945
Vladimir Marko60584552015-09-03 13:35:12 +0000946 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100947 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000948 }
949
David Brazdild26a4112015-11-10 11:07:31 +0000950 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
951 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
952
Vladimir Marko60584552015-09-03 13:35:12 +0000953 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
954 return ContainsElement(successors_, block, start_from);
955 }
956
957 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100958 return dominated_blocks_;
959 }
960
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100961 bool IsEntryBlock() const {
962 return graph_->GetEntryBlock() == this;
963 }
964
965 bool IsExitBlock() const {
966 return graph_->GetExitBlock() == this;
967 }
968
David Brazdil46e2a392015-03-16 17:31:52 +0000969 bool IsSingleGoto() const;
Mads Ager16e52892017-07-14 13:11:37 +0200970 bool IsSingleReturn() const;
Mingyao Yang46721ef2017-10-05 14:45:17 -0700971 bool IsSingleReturnOrReturnVoidAllowingPhis() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000972 bool IsSingleTryBoundary() const;
973
974 // Returns true if this block emits nothing but a jump.
975 bool IsSingleJump() const {
976 HLoopInformation* loop_info = GetLoopInformation();
977 return (IsSingleGoto() || IsSingleTryBoundary())
978 // Back edges generate a suspend check.
979 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
980 }
David Brazdil46e2a392015-03-16 17:31:52 +0000981
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000982 void AddBackEdge(HBasicBlock* back_edge) {
983 if (loop_information_ == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100984 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000985 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100986 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000987 loop_information_->AddBackEdge(back_edge);
988 }
989
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000990 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000991 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000992
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100993 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000994 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600995 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000996
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000997 HBasicBlock* GetDominator() const { return dominator_; }
998 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000999 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
1000
1001 void RemoveDominatedBlock(HBasicBlock* block) {
1002 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +01001003 }
Vladimir Marko60584552015-09-03 13:35:12 +00001004
1005 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
1006 ReplaceElement(dominated_blocks_, existing, new_block);
1007 }
1008
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001009 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001010
1011 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001012 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001013 }
1014
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001015 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
1016 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +01001017 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001018 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +01001019 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
1020 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001021
Nicolas Geoffray09aa1472016-01-19 10:52:54 +00001022 HInstruction* GetFirstInstructionDisregardMoves() const;
1023
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001024 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001025 successors_.push_back(block);
1026 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001027 }
1028
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001029 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
1030 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001031 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001032 new_block->predecessors_.push_back(this);
1033 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001034 }
1035
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001036 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
1037 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001038 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001039 new_block->successors_.push_back(this);
1040 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001041 }
1042
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001043 // Insert `this` between `predecessor` and `successor. This method
1044 // preserves the indicies, and will update the first edge found between
1045 // `predecessor` and `successor`.
1046 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
1047 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001048 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +00001049 successor->predecessors_[predecessor_index] = this;
1050 predecessor->successors_[successor_index] = this;
1051 successors_.push_back(successor);
1052 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001053 }
1054
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001055 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001056 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001057 }
1058
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001059 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001060 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001061 }
1062
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001063 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001064 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001065 }
1066
1067 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001068 predecessors_.push_back(block);
1069 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001070 }
1071
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001072 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001073 DCHECK_EQ(predecessors_.size(), 2u);
1074 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001075 }
1076
David Brazdil769c9e52015-04-27 13:54:09 +01001077 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001078 DCHECK_EQ(successors_.size(), 2u);
1079 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +01001080 }
1081
David Brazdilfc6a86a2015-06-26 10:33:45 +00001082 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001083 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001084 }
1085
David Brazdilfc6a86a2015-06-26 10:33:45 +00001086 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001087 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001088 }
1089
David Brazdilfc6a86a2015-06-26 10:33:45 +00001090 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001091 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001092 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001093 }
1094
1095 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001096 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001097 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001098 }
1099
1100 // Returns whether the first occurrence of `predecessor` in the list of
1101 // predecessors is at index `idx`.
1102 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001103 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001104 return GetPredecessorIndexOf(predecessor) == idx;
1105 }
1106
David Brazdild7558da2015-09-22 13:04:14 +01001107 // Create a new block between this block and its predecessors. The new block
1108 // is added to the graph, all predecessor edges are relinked to it and an edge
1109 // is created to `this`. Returns the new empty block. Reverse post order or
1110 // loop and try/catch information are not updated.
1111 HBasicBlock* CreateImmediateDominator();
1112
David Brazdilfc6a86a2015-06-26 10:33:45 +00001113 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001114 // created, latter block. Note that this method will add the block to the
1115 // graph, create a Goto at the end of the former block and will create an edge
1116 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001117 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001118 HBasicBlock* SplitBefore(HInstruction* cursor);
1119
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001120 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001121 // created block. Note that this method just updates raw block information,
1122 // like predecessors, successors, dominators, and instruction list. It does not
1123 // update the graph, reverse post order, loop information, nor make sure the
1124 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001125 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1126
1127 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1128 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001129
1130 // Merge `other` at the end of `this`. Successors and dominated blocks of
1131 // `other` are changed to be successors and dominated blocks of `this`. Note
1132 // that this method does not update the graph, reverse post order, loop
1133 // information, nor make sure the blocks are consistent (for example ending
1134 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001135 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001136
1137 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1138 // of `this` are moved to `other`.
1139 // Note that this method does not update the graph, reverse post order, loop
1140 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001141 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001142 void ReplaceWith(HBasicBlock* other);
1143
Aart Bik6b69e0a2017-01-11 10:20:43 -08001144 // Merges the instructions of `other` at the end of `this`.
1145 void MergeInstructionsWith(HBasicBlock* other);
1146
David Brazdil2d7352b2015-04-20 14:52:42 +01001147 // Merge `other` at the end of `this`. This method updates loops, reverse post
1148 // order, links to predecessors, successors, dominators and deletes the block
1149 // from the graph. The two blocks must be successive, i.e. `this` the only
1150 // predecessor of `other` and vice versa.
1151 void MergeWith(HBasicBlock* other);
1152
1153 // Disconnects `this` from all its predecessors, successors and dominator,
1154 // removes it from all loops it is included in and eventually from the graph.
1155 // The block must not dominate any other block. Predecessors and successors
1156 // are safely updated.
1157 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001158
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001159 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001160 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001161 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001162 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Artem Serovcced8ba2017-07-19 18:18:09 +01001163 // Replace phi `initial` with `replacement` within this block.
1164 void ReplaceAndRemovePhiWith(HPhi* initial, HPhi* replacement);
Roland Levillainccc07a92014-09-16 14:48:16 +01001165 // Replace instruction `initial` with `replacement` within this block.
1166 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1167 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001168 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001169 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001170 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1171 // instruction list. With 'ensure_safety' set to true, it verifies that the
1172 // instruction is not in use and removes it from the use lists of its inputs.
1173 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1174 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001175 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001176
1177 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001178 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001179 }
1180
Roland Levillain6b879dd2014-09-22 17:13:44 +01001181 bool IsLoopPreHeaderFirstPredecessor() const {
1182 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001183 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001184 }
1185
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001186 bool IsFirstPredecessorBackEdge() const {
1187 DCHECK(IsLoopHeader());
1188 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1189 }
1190
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001191 HLoopInformation* GetLoopInformation() const {
1192 return loop_information_;
1193 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001194
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001195 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001196 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001197 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001198 void SetInLoop(HLoopInformation* info) {
1199 if (IsLoopHeader()) {
1200 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001201 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001202 loop_information_ = info;
1203 } else if (loop_information_->Contains(*info->GetHeader())) {
1204 // Block is currently part of an outer loop. Make it part of this inner loop.
1205 // Note that a non loop header having a loop information means this loop information
1206 // has already been populated
1207 loop_information_ = info;
1208 } else {
1209 // Block is part of an inner loop. Do not update the loop information.
1210 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1211 // at this point, because this method is being called while populating `info`.
1212 }
1213 }
1214
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001215 // Raw update of the loop information.
1216 void SetLoopInformation(HLoopInformation* info) {
1217 loop_information_ = info;
1218 }
1219
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001220 bool IsInLoop() const { return loop_information_ != nullptr; }
1221
David Brazdilec16f792015-08-19 15:04:01 +01001222 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1223
1224 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1225 try_catch_information_ = try_catch_information;
1226 }
1227
1228 bool IsTryBlock() const {
1229 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1230 }
1231
1232 bool IsCatchBlock() const {
1233 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1234 }
David Brazdilffee3d32015-07-06 11:48:53 +01001235
1236 // Returns the try entry that this block's successors should have. They will
1237 // be in the same try, unless the block ends in a try boundary. In that case,
1238 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001239 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001240
David Brazdild7558da2015-09-22 13:04:14 +01001241 bool HasThrowingInstructions() const;
1242
David Brazdila4b8c212015-05-07 09:59:30 +01001243 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001244 bool Dominates(HBasicBlock* block) const;
1245
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001246 size_t GetLifetimeStart() const { return lifetime_start_; }
1247 size_t GetLifetimeEnd() const { return lifetime_end_; }
1248
1249 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1250 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1251
David Brazdil8d5b8b22015-03-24 10:51:52 +00001252 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001253 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001254 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001255 bool HasSinglePhi() const;
1256
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001257 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001258 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001259 ArenaVector<HBasicBlock*> predecessors_;
1260 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001261 HInstructionList instructions_;
1262 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001263 HLoopInformation* loop_information_;
1264 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001265 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001266 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001267 // The dex program counter of the first instruction of this block.
1268 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001269 size_t lifetime_start_;
1270 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001271 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001272
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001273 friend class HGraph;
1274 friend class HInstruction;
1275
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001276 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1277};
1278
David Brazdilb2bd1c52015-03-25 11:17:37 +00001279// Iterates over the LoopInformation of all loops which contain 'block'
1280// from the innermost to the outermost.
1281class HLoopInformationOutwardIterator : public ValueObject {
1282 public:
1283 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1284 : current_(block.GetLoopInformation()) {}
1285
1286 bool Done() const { return current_ == nullptr; }
1287
1288 void Advance() {
1289 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001290 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001291 }
1292
1293 HLoopInformation* Current() const {
1294 DCHECK(!Done());
1295 return current_;
1296 }
1297
1298 private:
1299 HLoopInformation* current_;
1300
1301 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1302};
1303
Alexandre Ramesef20f712015-06-09 10:29:30 +01001304#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001305 M(Above, Condition) \
1306 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001307 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001308 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001309 M(ArrayGet, Instruction) \
1310 M(ArrayLength, Instruction) \
1311 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001312 M(Below, Condition) \
1313 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001314 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001315 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001316 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001317 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001318 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001319 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001320 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001321 M(Compare, BinaryOperation) \
Igor Murashkind01745e2017-04-05 16:40:31 -07001322 M(ConstructorFence, Instruction) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001323 M(CurrentMethod, Instruction) \
Mingyao Yang063fc772016-08-02 11:02:54 -07001324 M(ShouldDeoptimizeFlag, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001325 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001326 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001327 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001328 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001329 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001330 M(Exit, Instruction) \
1331 M(FloatConstant, Constant) \
1332 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001333 M(GreaterThan, Condition) \
1334 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001335 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001336 M(InstanceFieldGet, Instruction) \
1337 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001338 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001339 M(IntConstant, Constant) \
xueliang.zhonge0eb4832017-10-30 13:43:14 +00001340 M(IntermediateAddress, Instruction) \
Calin Juravle175dc732015-08-25 15:42:32 +01001341 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001342 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001343 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001344 M(InvokeVirtual, Invoke) \
Orion Hodsonac141392017-01-13 11:53:47 +00001345 M(InvokePolymorphic, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001346 M(LessThan, Condition) \
1347 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001348 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001349 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001350 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001351 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001352 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001353 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001354 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001355 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001356 M(Neg, UnaryOperation) \
1357 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001358 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001359 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001360 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001361 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001362 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001363 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001364 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001365 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001366 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001367 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001368 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001369 M(Return, Instruction) \
1370 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001371 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001372 M(Shl, BinaryOperation) \
1373 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001374 M(StaticFieldGet, Instruction) \
1375 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001376 M(UnresolvedInstanceFieldGet, Instruction) \
1377 M(UnresolvedInstanceFieldSet, Instruction) \
1378 M(UnresolvedStaticFieldGet, Instruction) \
1379 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001380 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001381 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001382 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001383 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001384 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001385 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001386 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001387 M(Xor, BinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001388 M(VecReplicateScalar, VecUnaryOperation) \
Aart Bik0148de42017-09-05 09:25:01 -07001389 M(VecExtractScalar, VecUnaryOperation) \
1390 M(VecReduce, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001391 M(VecCnv, VecUnaryOperation) \
1392 M(VecNeg, VecUnaryOperation) \
Aart Bik6daebeb2017-04-03 14:35:41 -07001393 M(VecAbs, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001394 M(VecNot, VecUnaryOperation) \
1395 M(VecAdd, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001396 M(VecHalvingAdd, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001397 M(VecSub, VecBinaryOperation) \
1398 M(VecMul, VecBinaryOperation) \
1399 M(VecDiv, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001400 M(VecMin, VecBinaryOperation) \
1401 M(VecMax, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001402 M(VecAnd, VecBinaryOperation) \
1403 M(VecAndNot, VecBinaryOperation) \
1404 M(VecOr, VecBinaryOperation) \
1405 M(VecXor, VecBinaryOperation) \
1406 M(VecShl, VecBinaryOperation) \
1407 M(VecShr, VecBinaryOperation) \
1408 M(VecUShr, VecBinaryOperation) \
Aart Bik8de59162017-04-21 09:42:01 -07001409 M(VecSetScalars, VecOperation) \
Artem Serovf34dd202017-04-10 17:41:46 +01001410 M(VecMultiplyAccumulate, VecOperation) \
Aart Bikdbbac8f2017-09-01 13:06:08 -07001411 M(VecSADAccumulate, VecOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001412 M(VecLoad, VecMemoryOperation) \
1413 M(VecStore, VecMemoryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001414
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001415/*
1416 * Instructions, shared across several (not all) architectures.
1417 */
1418#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1419#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1420#else
1421#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001422 M(BitwiseNegatedRight, Instruction) \
Anton Kirilov74234da2017-01-13 14:42:47 +00001423 M(DataProcWithShifterOp, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001424 M(MultiplyAccumulate, Instruction) \
Artem Serove1811ed2017-04-27 16:50:47 +01001425 M(IntermediateAddressIndex, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001426#endif
1427
Alexandre Ramesef20f712015-06-09 10:29:30 +01001428#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1429
1430#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1431
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001432#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001433#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001434#else
1435#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1436 M(MipsComputeBaseMethodAddress, Instruction) \
Lena Djokica2901602017-09-21 13:50:52 +02001437 M(MipsPackedSwitch, Instruction) \
1438 M(IntermediateArrayAddressIndex, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001439#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001440
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001441#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1442
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001443#ifndef ART_ENABLE_CODEGEN_x86
1444#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1445#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001446#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1447 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001448 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001449 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001450 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001451#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001452
1453#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1454
1455#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1456 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001457 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001458 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1459 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001460 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001461 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001462 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1463 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1464
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001465#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1466 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001467 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001468 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001469 M(BinaryOperation, Instruction) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001470 M(Invoke, Instruction) \
1471 M(VecOperation, Instruction) \
1472 M(VecUnaryOperation, VecOperation) \
1473 M(VecBinaryOperation, VecOperation) \
1474 M(VecMemoryOperation, VecOperation)
Dave Allison20dfc792014-06-16 20:44:29 -07001475
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001476#define FOR_EACH_INSTRUCTION(M) \
1477 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1478 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1479
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001480#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001481FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1482#undef FORWARD_DECLARATION
1483
Vladimir Marko372f10e2016-05-17 16:30:10 +01001484#define DECLARE_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001485 private: \
1486 H##type& operator=(const H##type&) = delete; \
1487 public: \
Vladimir Marko372f10e2016-05-17 16:30:10 +01001488 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
1489 const char* DebugName() const OVERRIDE { return #type; } \
1490 bool InstructionTypeEquals(const HInstruction* other) const OVERRIDE { \
1491 return other->Is##type(); \
1492 } \
Artem Serovcced8ba2017-07-19 18:18:09 +01001493 HInstruction* Clone(ArenaAllocator* arena) const OVERRIDE { \
1494 DCHECK(IsClonable()); \
1495 return new (arena) H##type(*this->As##type()); \
1496 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001497 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001498
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001499#define DECLARE_ABSTRACT_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001500 private: \
1501 H##type& operator=(const H##type&) = delete; \
1502 public: \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001503 bool Is##type() const { return As##type() != nullptr; } \
1504 const H##type* As##type() const { return this; } \
1505 H##type* As##type() { return this; }
1506
Artem Serovcced8ba2017-07-19 18:18:09 +01001507#define DEFAULT_COPY_CONSTRUCTOR(type) \
1508 explicit H##type(const H##type& other) = default;
1509
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001510template <typename T>
Vladimir Marko82b07402017-03-01 19:02:04 +00001511class HUseListNode : public ArenaObject<kArenaAllocUseListNode>,
1512 public IntrusiveForwardListNode<HUseListNode<T>> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001513 public:
Igor Murashkind01745e2017-04-05 16:40:31 -07001514 // Get the instruction which has this use as one of the inputs.
David Brazdiled596192015-01-23 10:39:45 +00001515 T GetUser() const { return user_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001516 // Get the position of the input record that this use corresponds to.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001517 size_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001518 // Set the position of the input record that this use corresponds to.
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001519 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001520
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001521 private:
David Brazdiled596192015-01-23 10:39:45 +00001522 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001523 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001524
1525 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001526 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001527
Vladimir Marko46817b82016-03-29 12:21:58 +01001528 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001529
1530 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1531};
1532
David Brazdiled596192015-01-23 10:39:45 +00001533template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001534using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001535
David Brazdil1abb4192015-02-17 18:33:36 +00001536// This class is used by HEnvironment and HInstruction classes to record the
1537// instructions they use and pointers to the corresponding HUseListNodes kept
1538// by the used instructions.
1539template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001540class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001541 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001542 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1543 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001544
Vladimir Marko46817b82016-03-29 12:21:58 +01001545 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1546 : HUserRecord(old_record.instruction_, before_use_node) {}
1547 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1548 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001549 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001550 }
1551
1552 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001553 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1554 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001555
1556 private:
1557 // Instruction used by the user.
1558 HInstruction* instruction_;
1559
Vladimir Marko46817b82016-03-29 12:21:58 +01001560 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1561 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001562};
1563
Vladimir Markoe9004912016-06-16 16:50:52 +01001564// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1565// This is used for HInstruction::GetInputs() to return a container wrapper providing
1566// HInstruction* values even though the underlying container has HUserRecord<>s.
1567struct HInputExtractor {
1568 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1569 return record.GetInstruction();
1570 }
1571 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1572 return record.GetInstruction();
1573 }
1574};
1575
1576using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1577using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1578
Aart Bik854a02b2015-07-14 16:07:00 -07001579/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001580 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001581 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001582 * For write/read dependences on fields/arrays, the dependence analysis uses
1583 * type disambiguation (e.g. a float field write cannot modify the value of an
1584 * integer field read) and the access type (e.g. a reference array write cannot
1585 * modify the value of a reference field read [although it may modify the
1586 * reference fetch prior to reading the field, which is represented by its own
1587 * write/read dependence]). The analysis makes conservative points-to
1588 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1589 * the same, and any reference read depends on any reference read without
1590 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001591 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001592 * The internal representation uses 38-bit and is described in the table below.
1593 * The first line indicates the side effect, and for field/array accesses the
1594 * second line indicates the type of the access (in the order of the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001595 * DataType::Type enum).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001596 * The two numbered lines below indicate the bit position in the bitfield (read
1597 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001598 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001599 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1600 * +-------------+---------+---------+--------------+---------+---------+
1601 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1602 * | 3 |333333322|222222221| 1 |111111110|000000000|
1603 * | 7 |654321098|765432109| 8 |765432109|876543210|
1604 *
1605 * Note that, to ease the implementation, 'changes' bits are least significant
1606 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001607 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001608class SideEffects : public ValueObject {
1609 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001610 SideEffects() : flags_(0) {}
1611
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001612 static SideEffects None() {
1613 return SideEffects(0);
1614 }
1615
1616 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001617 return SideEffects(kAllChangeBits | kAllDependOnBits);
1618 }
1619
1620 static SideEffects AllChanges() {
1621 return SideEffects(kAllChangeBits);
1622 }
1623
1624 static SideEffects AllDependencies() {
1625 return SideEffects(kAllDependOnBits);
1626 }
1627
1628 static SideEffects AllExceptGCDependency() {
1629 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1630 }
1631
1632 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001633 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001634 }
1635
Aart Bik34c3ba92015-07-20 14:08:59 -07001636 static SideEffects AllWrites() {
1637 return SideEffects(kAllWrites);
1638 }
1639
1640 static SideEffects AllReads() {
1641 return SideEffects(kAllReads);
1642 }
1643
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001644 static SideEffects FieldWriteOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001645 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001646 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001647 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001648 }
1649
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001650 static SideEffects ArrayWriteOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001651 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001652 }
1653
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001654 static SideEffects FieldReadOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001655 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001656 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001657 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001658 }
1659
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001660 static SideEffects ArrayReadOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001661 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001662 }
1663
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001664 static SideEffects CanTriggerGC() {
1665 return SideEffects(1ULL << kCanTriggerGCBit);
1666 }
1667
1668 static SideEffects DependsOnGC() {
1669 return SideEffects(1ULL << kDependsOnGCBit);
1670 }
1671
Aart Bik854a02b2015-07-14 16:07:00 -07001672 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001673 SideEffects Union(SideEffects other) const {
1674 return SideEffects(flags_ | other.flags_);
1675 }
1676
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001677 SideEffects Exclusion(SideEffects other) const {
1678 return SideEffects(flags_ & ~other.flags_);
1679 }
1680
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001681 void Add(SideEffects other) {
1682 flags_ |= other.flags_;
1683 }
1684
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001685 bool Includes(SideEffects other) const {
1686 return (other.flags_ & flags_) == other.flags_;
1687 }
1688
1689 bool HasSideEffects() const {
1690 return (flags_ & kAllChangeBits);
1691 }
1692
1693 bool HasDependencies() const {
1694 return (flags_ & kAllDependOnBits);
1695 }
1696
1697 // Returns true if there are no side effects or dependencies.
1698 bool DoesNothing() const {
1699 return flags_ == 0;
1700 }
1701
Aart Bik854a02b2015-07-14 16:07:00 -07001702 // Returns true if something is written.
1703 bool DoesAnyWrite() const {
1704 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001705 }
1706
Aart Bik854a02b2015-07-14 16:07:00 -07001707 // Returns true if something is read.
1708 bool DoesAnyRead() const {
1709 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001710 }
1711
Aart Bik854a02b2015-07-14 16:07:00 -07001712 // Returns true if potentially everything is written and read
1713 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001714 bool DoesAllReadWrite() const {
1715 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1716 }
1717
Aart Bik854a02b2015-07-14 16:07:00 -07001718 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001719 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001720 }
1721
Roland Levillain0d5a2812015-11-13 10:07:31 +00001722 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001723 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001724 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1725 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001726 }
1727
1728 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001729 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001730 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001731 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001732 for (int s = kLastBit; s >= 0; s--) {
1733 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1734 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1735 // This is a bit for the GC side effect.
1736 if (current_bit_is_set) {
1737 flags += "GC";
1738 }
Aart Bik854a02b2015-07-14 16:07:00 -07001739 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001740 } else {
1741 // This is a bit for the array/field analysis.
1742 // The underscore character stands for the 'can trigger GC' bit.
1743 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1744 if (current_bit_is_set) {
1745 flags += kDebug[s];
1746 }
1747 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1748 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1749 flags += "|";
1750 }
1751 }
Aart Bik854a02b2015-07-14 16:07:00 -07001752 }
1753 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001754 }
1755
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001756 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001757
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001758 private:
1759 static constexpr int kFieldArrayAnalysisBits = 9;
1760
1761 static constexpr int kFieldWriteOffset = 0;
1762 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1763 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1764 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1765
1766 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1767
1768 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1769 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1770 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1771 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1772
1773 static constexpr int kLastBit = kDependsOnGCBit;
1774 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1775
1776 // Aliases.
1777
1778 static_assert(kChangeBits == kDependOnBits,
1779 "the 'change' bits should match the 'depend on' bits.");
1780
1781 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1782 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1783 static constexpr uint64_t kAllWrites =
1784 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1785 static constexpr uint64_t kAllReads =
1786 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001787
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001788 // Translates type to bit flag. The type must correspond to a Java type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001789 static uint64_t TypeFlag(DataType::Type type, int offset) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001790 int shift;
1791 switch (type) {
1792 case DataType::Type::kReference: shift = 0; break;
1793 case DataType::Type::kBool: shift = 1; break;
1794 case DataType::Type::kInt8: shift = 2; break;
1795 case DataType::Type::kUint16: shift = 3; break;
1796 case DataType::Type::kInt16: shift = 4; break;
1797 case DataType::Type::kInt32: shift = 5; break;
1798 case DataType::Type::kInt64: shift = 6; break;
1799 case DataType::Type::kFloat32: shift = 7; break;
1800 case DataType::Type::kFloat64: shift = 8; break;
1801 default:
1802 LOG(FATAL) << "Unexpected data type " << type;
1803 UNREACHABLE();
1804 }
Aart Bik854a02b2015-07-14 16:07:00 -07001805 DCHECK_LE(kFieldWriteOffset, shift);
1806 DCHECK_LT(shift, kArrayWriteOffset);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001807 return UINT64_C(1) << (shift + offset);
Aart Bik854a02b2015-07-14 16:07:00 -07001808 }
1809
1810 // Private constructor on direct flags value.
1811 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1812
1813 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001814};
1815
David Brazdiled596192015-01-23 10:39:45 +00001816// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001817class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001818 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001819 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
Mingyao Yang01b47b02017-02-03 12:09:57 -08001820 size_t number_of_vregs,
1821 ArtMethod* method,
1822 uint32_t dex_pc,
1823 HInstruction* holder)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001824 : vregs_(number_of_vregs, allocator->Adapter(kArenaAllocEnvironmentVRegs)),
1825 locations_(allocator->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001826 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001827 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001828 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001829 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001830 }
1831
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001832 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
1833 const HEnvironment& to_copy,
1834 HInstruction* holder)
1835 : HEnvironment(allocator,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001836 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001837 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001838 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001839 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001840
Vladimir Markoec32f642017-06-02 10:51:55 +01001841 void AllocateLocations() {
1842 DCHECK(locations_.empty());
1843 locations_.resize(vregs_.size());
1844 }
1845
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001846 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001847 if (parent_ != nullptr) {
1848 parent_->SetAndCopyParentChain(allocator, parent);
1849 } else {
1850 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1851 parent_->CopyFrom(parent);
1852 if (parent->GetParent() != nullptr) {
1853 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1854 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001855 }
David Brazdiled596192015-01-23 10:39:45 +00001856 }
1857
Vladimir Marko69d310e2017-10-09 14:12:23 +01001858 void CopyFrom(ArrayRef<HInstruction* const> locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001859 void CopyFrom(HEnvironment* environment);
1860
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001861 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1862 // input to the loop phi instead. This is for inserting instructions that
1863 // require an environment (like HDeoptimization) in the loop pre-header.
1864 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001865
1866 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001867 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001868 }
1869
1870 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001871 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001872 }
1873
David Brazdil1abb4192015-02-17 18:33:36 +00001874 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001875
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001876 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001877
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001878 HEnvironment* GetParent() const { return parent_; }
1879
1880 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001881 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001882 }
1883
1884 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001885 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001886 }
1887
1888 uint32_t GetDexPc() const {
1889 return dex_pc_;
1890 }
1891
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001892 ArtMethod* GetMethod() const {
1893 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001894 }
1895
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001896 HInstruction* GetHolder() const {
1897 return holder_;
1898 }
1899
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001900
1901 bool IsFromInlinedInvoke() const {
1902 return GetParent() != nullptr;
1903 }
1904
David Brazdiled596192015-01-23 10:39:45 +00001905 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001906 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1907 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001908 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001909 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001910 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00001911
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001912 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001913 HInstruction* const holder_;
1914
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001915 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001916
1917 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1918};
1919
Vladimir Markof9f64412015-09-02 14:05:49 +01001920class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001921 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001922 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001923 : previous_(nullptr),
1924 next_(nullptr),
1925 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001926 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001927 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001928 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001929 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001930 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001931 locations_(nullptr),
1932 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001933 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001934 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001935 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1936 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1937 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001938
Dave Allison20dfc792014-06-16 20:44:29 -07001939 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001940
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001941#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001942 enum InstructionKind {
1943 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1944 };
1945#undef DECLARE_KIND
1946
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001947 HInstruction* GetNext() const { return next_; }
1948 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001949
Calin Juravle77520bc2015-01-12 18:45:46 +00001950 HInstruction* GetNextDisregardingMoves() const;
1951 HInstruction* GetPreviousDisregardingMoves() const;
1952
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001953 HBasicBlock* GetBlock() const { return block_; }
Vladimir Markoca6fff82017-10-03 14:49:14 +01001954 ArenaAllocator* GetAllocator() const { return block_->GetGraph()->GetAllocator(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001955 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001956 bool IsInBlock() const { return block_ != nullptr; }
1957 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001958 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1959 bool IsIrreducibleLoopHeaderPhi() const {
1960 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1961 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001962
Vladimir Marko372f10e2016-05-17 16:30:10 +01001963 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
1964
1965 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
1966 // One virtual method is enough, just const_cast<> and then re-add the const.
1967 return ArrayRef<const HUserRecord<HInstruction*>>(
1968 const_cast<HInstruction*>(this)->GetInputRecords());
1969 }
1970
Vladimir Markoe9004912016-06-16 16:50:52 +01001971 HInputsRef GetInputs() {
1972 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001973 }
1974
Vladimir Markoe9004912016-06-16 16:50:52 +01001975 HConstInputsRef GetInputs() const {
1976 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001977 }
1978
1979 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001980 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001981
Aart Bik2767f4b2016-10-28 15:03:53 -07001982 bool HasInput(HInstruction* input) const {
1983 for (const HInstruction* i : GetInputs()) {
1984 if (i == input) {
1985 return true;
1986 }
1987 }
1988 return false;
1989 }
1990
Vladimir Marko372f10e2016-05-17 16:30:10 +01001991 void SetRawInputAt(size_t index, HInstruction* input) {
1992 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1993 }
1994
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001995 virtual void Accept(HGraphVisitor* visitor) = 0;
1996 virtual const char* DebugName() const = 0;
1997
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001998 virtual DataType::Type GetType() const { return DataType::Type::kVoid; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001999
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002000 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002001
2002 uint32_t GetDexPc() const { return dex_pc_; }
2003
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002004 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01002005
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002006 // Can the instruction throw?
2007 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
2008 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01002009 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01002010 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002011
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002012 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002013 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002014
Calin Juravle10e244f2015-01-26 18:54:32 +00002015 // Does not apply for all instructions, but having this at top level greatly
2016 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00002017 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002018 virtual bool CanBeNull() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002019 DCHECK_EQ(GetType(), DataType::Type::kReference) << "CanBeNull only applies to reference types";
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002020 return true;
2021 }
Calin Juravle10e244f2015-01-26 18:54:32 +00002022
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002023 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01002024 return false;
2025 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002026
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002027 virtual bool IsActualObject() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002028 return GetType() == DataType::Type::kReference;
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002029 }
2030
Calin Juravle2e768302015-07-28 14:41:11 +00002031 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00002032
Calin Juravle61d544b2015-02-23 16:46:57 +00002033 ReferenceTypeInfo GetReferenceTypeInfo() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002034 DCHECK_EQ(GetType(), DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002035 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00002036 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00002037 }
Calin Juravleacf735c2015-02-12 15:25:22 +00002038
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002039 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00002040 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002041 // Note: fixup_end remains valid across push_front().
2042 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
2043 HUseListNode<HInstruction*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002044 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HInstruction*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002045 uses_.push_front(*new_node);
2046 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002047 }
2048
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002049 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01002050 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002051 // Note: env_fixup_end remains valid across push_front().
2052 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
2053 HUseListNode<HEnvironment*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002054 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HEnvironment*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002055 env_uses_.push_front(*new_node);
2056 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002057 }
2058
David Brazdil1abb4192015-02-17 18:33:36 +00002059 void RemoveAsUserOfInput(size_t input) {
2060 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002061 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2062 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2063 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00002064 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002065
Vladimir Marko372f10e2016-05-17 16:30:10 +01002066 void RemoveAsUserOfAllInputs() {
2067 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2068 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2069 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2070 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
2071 }
2072 }
2073
David Brazdil1abb4192015-02-17 18:33:36 +00002074 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2075 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002076
Vladimir Marko46817b82016-03-29 12:21:58 +01002077 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2078 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2079 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002080 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002081 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002082 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002083
Aart Bikcc42be02016-10-20 16:14:16 -07002084 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002085 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002086 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002087 !CanThrow() &&
2088 !IsSuspendCheck() &&
2089 !IsControlFlow() &&
2090 !IsNativeDebugInfo() &&
2091 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002092 // If we added an explicit barrier then we should keep it.
Igor Murashkind01745e2017-04-05 16:40:31 -07002093 !IsMemoryBarrier() &&
2094 !IsConstructorFence();
Aart Bik482095d2016-10-10 15:39:10 -07002095 }
2096
Aart Bikcc42be02016-10-20 16:14:16 -07002097 bool IsDeadAndRemovable() const {
2098 return IsRemovable() && !HasUses();
2099 }
2100
Artem Serov1de1e112017-07-20 16:33:59 +01002101 // Does this instruction dominate (strictly or in regular sense depending on 'strictly')
2102 // `other_instruction`?
2103 // Returns '!strictly' if this instruction and `other_instruction` are the same.
Roland Levillain6c82d402014-10-13 16:10:27 +01002104 // Aborts if this instruction and `other_instruction` are both phis.
Artem Serov1de1e112017-07-20 16:33:59 +01002105 bool Dominates(HInstruction* other_instruction, bool strictly) const;
2106
2107 // Return 'Dominates(other_instruction, /*strictly*/ true)'.
Roland Levillain6c82d402014-10-13 16:10:27 +01002108 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002109
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002110 int GetId() const { return id_; }
2111 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002112
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002113 int GetSsaIndex() const { return ssa_index_; }
2114 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2115 bool HasSsaIndex() const { return ssa_index_ != -1; }
2116
2117 bool HasEnvironment() const { return environment_ != nullptr; }
2118 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002119 // Set the `environment_` field. Raw because this method does not
2120 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002121 void SetRawEnvironment(HEnvironment* environment) {
2122 DCHECK(environment_ == nullptr);
2123 DCHECK_EQ(environment->GetHolder(), this);
2124 environment_ = environment;
2125 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002126
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002127 void InsertRawEnvironment(HEnvironment* environment) {
2128 DCHECK(environment_ != nullptr);
2129 DCHECK_EQ(environment->GetHolder(), this);
2130 DCHECK(environment->GetParent() == nullptr);
2131 environment->parent_ = environment_;
2132 environment_ = environment;
2133 }
2134
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002135 void RemoveEnvironment();
2136
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002137 // Set the environment of this instruction, copying it from `environment`. While
2138 // copying, the uses lists are being updated.
2139 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002140 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002141 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002142 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002143 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002144 if (environment->GetParent() != nullptr) {
2145 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2146 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002147 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002148
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002149 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2150 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002151 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002152 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002153 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002154 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002155 if (environment->GetParent() != nullptr) {
2156 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2157 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002158 }
2159
Nicolas Geoffray39468442014-09-02 15:17:15 +01002160 // Returns the number of entries in the environment. Typically, that is the
2161 // number of dex registers in a method. It could be more in case of inlining.
2162 size_t EnvironmentSize() const;
2163
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002164 LocationSummary* GetLocations() const { return locations_; }
2165 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002166
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002167 void ReplaceWith(HInstruction* instruction);
Artem Serov1de1e112017-07-20 16:33:59 +01002168
2169 // Replace all uses of the instruction which are dominated by 'dominator' with 'replacement'.
2170 // 'strictly' determines whether strict or regular domination relation should be checked.
2171 void ReplaceUsesDominatedBy(HInstruction* dominator,
2172 HInstruction* replacement,
2173 bool strictly = true);
2174
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002175 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002176
Alexandre Rames188d4312015-04-09 18:30:21 +01002177 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2178 // uses of this instruction by `other` are *not* updated.
2179 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2180 ReplaceWith(other);
2181 other->ReplaceInput(this, use_index);
2182 }
2183
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002184 // Move `this` instruction before `cursor`
2185 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002186
Vladimir Markofb337ea2015-11-25 15:25:10 +00002187 // Move `this` before its first user and out of any loops. If there is no
2188 // out-of-loop user that dominates all other users, move the instruction
2189 // to the end of the out-of-loop common dominator of the user's blocks.
2190 //
2191 // This can be used only on non-throwing instructions with no side effects that
2192 // have at least one use but no environment uses.
2193 void MoveBeforeFirstUserAndOutOfLoops();
2194
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002195#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002196 bool Is##type() const; \
2197 const H##type* As##type() const; \
2198 H##type* As##type();
2199
2200 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2201#undef INSTRUCTION_TYPE_CHECK
2202
2203#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002204 bool Is##type() const { return (As##type() != nullptr); } \
2205 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002206 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002207 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002208#undef INSTRUCTION_TYPE_CHECK
2209
Artem Serovcced8ba2017-07-19 18:18:09 +01002210 // Return a clone of the instruction if it is clonable (shallow copy by default, custom copy
2211 // if a custom copy-constructor is provided for a particular type). If IsClonable() is false for
2212 // the instruction then the behaviour of this function is undefined.
2213 //
2214 // Note: It is semantically valid to create a clone of the instruction only until
2215 // prepare_for_register_allocator phase as lifetime, intervals and codegen info are not
2216 // copied.
2217 //
2218 // Note: HEnvironment and some other fields are not copied and are set to default values, see
2219 // 'explicit HInstruction(const HInstruction& other)' for details.
2220 virtual HInstruction* Clone(ArenaAllocator* arena ATTRIBUTE_UNUSED) const {
2221 LOG(FATAL) << "Cloning is not implemented for the instruction " <<
2222 DebugName() << " " << GetId();
2223 UNREACHABLE();
2224 }
2225
2226 // Return whether instruction can be cloned (copied).
2227 virtual bool IsClonable() const { return false; }
2228
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002229 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002230 // TODO: this method is used by LICM and GVN with possibly different
2231 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002232 virtual bool CanBeMoved() const { return false; }
2233
2234 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002235 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002236 return false;
2237 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002238
2239 // Returns whether any data encoded in the two instructions is equal.
2240 // This method does not look at the inputs. Both instructions must be
2241 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002242 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002243 return false;
2244 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002245
2246 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002247 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002248 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002249 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002250
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002251 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2252 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2253 // the virtual function because the __attribute__((__pure__)) doesn't really
2254 // apply the strong requirement for virtual functions, preventing optimizations.
2255 InstructionKind GetKind() const PURE;
2256 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002257
2258 virtual size_t ComputeHashCode() const {
2259 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002260 for (const HInstruction* input : GetInputs()) {
2261 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002262 }
2263 return result;
2264 }
2265
2266 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002267 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002268 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002269
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002270 size_t GetLifetimePosition() const { return lifetime_position_; }
2271 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2272 LiveInterval* GetLiveInterval() const { return live_interval_; }
2273 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2274 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2275
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002276 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2277
2278 // Returns whether the code generation of the instruction will require to have access
2279 // to the current method. Such instructions are:
2280 // (1): Instructions that require an environment, as calling the runtime requires
2281 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002282 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2283 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002284 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002285 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002286 }
2287
Vladimir Markodc151b22015-10-15 18:02:30 +01002288 // Returns whether the code generation of the instruction will require to have access
2289 // to the dex cache of the current method's declaring class via the current method.
2290 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002291
Mark Mendellc4701932015-04-10 13:18:51 -04002292 // Does this instruction have any use in an environment before
2293 // control flow hits 'other'?
2294 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2295
2296 // Remove all references to environment uses of this instruction.
2297 // The caller must ensure that this is safe to do.
2298 void RemoveEnvironmentUsers();
2299
Vladimir Markoa1de9182016-02-25 11:37:38 +00002300 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2301 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002302
David Brazdil1abb4192015-02-17 18:33:36 +00002303 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002304 // If set, the machine code for this instruction is assumed to be generated by
2305 // its users. Used by liveness analysis to compute use positions accordingly.
2306 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2307 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2308 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2309 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2310
Vladimir Marko372f10e2016-05-17 16:30:10 +01002311 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2312 return GetInputRecords()[i];
2313 }
2314
2315 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2316 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2317 input_records[index] = input;
2318 }
David Brazdil1abb4192015-02-17 18:33:36 +00002319
Vladimir Markoa1de9182016-02-25 11:37:38 +00002320 uint32_t GetPackedFields() const {
2321 return packed_fields_;
2322 }
2323
2324 template <size_t flag>
2325 bool GetPackedFlag() const {
2326 return (packed_fields_ & (1u << flag)) != 0u;
2327 }
2328
2329 template <size_t flag>
2330 void SetPackedFlag(bool value = true) {
2331 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2332 }
2333
2334 template <typename BitFieldType>
2335 typename BitFieldType::value_type GetPackedField() const {
2336 return BitFieldType::Decode(packed_fields_);
2337 }
2338
2339 template <typename BitFieldType>
2340 void SetPackedField(typename BitFieldType::value_type value) {
2341 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2342 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2343 }
2344
Artem Serovcced8ba2017-07-19 18:18:09 +01002345 // Copy construction for the instruction (used for Clone function).
2346 //
2347 // Fields (e.g. lifetime, intervals and codegen info) associated with phases starting from
2348 // prepare_for_register_allocator are not copied (set to default values).
2349 //
2350 // Copy constructors must be provided for every HInstruction type; default copy constructor is
2351 // fine for most of them. However for some of the instructions a custom copy constructor must be
2352 // specified (when instruction has non-trivially copyable fields and must have a special behaviour
2353 // for copying them).
2354 explicit HInstruction(const HInstruction& other)
2355 : previous_(nullptr),
2356 next_(nullptr),
2357 block_(nullptr),
2358 dex_pc_(other.dex_pc_),
2359 id_(-1),
2360 ssa_index_(-1),
2361 packed_fields_(other.packed_fields_),
2362 environment_(nullptr),
2363 locations_(nullptr),
2364 live_interval_(nullptr),
2365 lifetime_position_(kNoLifetime),
2366 side_effects_(other.side_effects_),
2367 reference_type_handle_(other.reference_type_handle_) {}
2368
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002369 private:
Vladimir Marko46817b82016-03-29 12:21:58 +01002370 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2371 auto before_use_node = uses_.before_begin();
2372 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2373 HInstruction* user = use_node->GetUser();
2374 size_t input_index = use_node->GetIndex();
2375 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2376 before_use_node = use_node;
2377 }
2378 }
2379
2380 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2381 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2382 if (next != uses_.end()) {
2383 HInstruction* next_user = next->GetUser();
2384 size_t next_index = next->GetIndex();
2385 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2386 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2387 }
2388 }
2389
2390 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2391 auto before_env_use_node = env_uses_.before_begin();
2392 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2393 HEnvironment* user = env_use_node->GetUser();
2394 size_t input_index = env_use_node->GetIndex();
2395 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2396 before_env_use_node = env_use_node;
2397 }
2398 }
2399
2400 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2401 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2402 if (next != env_uses_.end()) {
2403 HEnvironment* next_user = next->GetUser();
2404 size_t next_index = next->GetIndex();
2405 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2406 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2407 }
2408 }
David Brazdil1abb4192015-02-17 18:33:36 +00002409
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002410 HInstruction* previous_;
2411 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002412 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002413 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002414
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002415 // An instruction gets an id when it is added to the graph.
2416 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002417 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002418 int id_;
2419
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002420 // When doing liveness analysis, instructions that have uses get an SSA index.
2421 int ssa_index_;
2422
Vladimir Markoa1de9182016-02-25 11:37:38 +00002423 // Packed fields.
2424 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002425
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002426 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002427 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002428
2429 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002430 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002431
Nicolas Geoffray39468442014-09-02 15:17:15 +01002432 // The environment associated with this instruction. Not null if the instruction
2433 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002434 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002435
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002436 // Set by the code generator.
2437 LocationSummary* locations_;
2438
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002439 // Set by the liveness analysis.
2440 LiveInterval* live_interval_;
2441
2442 // Set by the liveness analysis, this is the position in a linear
2443 // order of blocks where this instruction's live interval start.
2444 size_t lifetime_position_;
2445
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002446 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002447
Vladimir Markoa1de9182016-02-25 11:37:38 +00002448 // The reference handle part of the reference type info.
2449 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002450 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002451 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002452
David Brazdil1abb4192015-02-17 18:33:36 +00002453 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002454 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002455 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002456 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002457 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002458};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002459std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002460
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002461// Iterates over the instructions, while preserving the next instruction
2462// in case the current instruction gets removed from the list by the user
2463// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002464class HInstructionIterator : public ValueObject {
2465 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002466 explicit HInstructionIterator(const HInstructionList& instructions)
2467 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002468 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002469 }
2470
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002471 bool Done() const { return instruction_ == nullptr; }
2472 HInstruction* Current() const { return instruction_; }
2473 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002474 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002475 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002476 }
2477
2478 private:
2479 HInstruction* instruction_;
2480 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002481
2482 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002483};
2484
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002485// Iterates over the instructions without saving the next instruction,
2486// therefore handling changes in the graph potentially made by the user
2487// of this iterator.
2488class HInstructionIteratorHandleChanges : public ValueObject {
2489 public:
2490 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2491 : instruction_(instructions.first_instruction_) {
2492 }
2493
2494 bool Done() const { return instruction_ == nullptr; }
2495 HInstruction* Current() const { return instruction_; }
2496 void Advance() {
2497 instruction_ = instruction_->GetNext();
2498 }
2499
2500 private:
2501 HInstruction* instruction_;
2502
2503 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2504};
2505
2506
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002507class HBackwardInstructionIterator : public ValueObject {
2508 public:
2509 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2510 : instruction_(instructions.last_instruction_) {
2511 next_ = Done() ? nullptr : instruction_->GetPrevious();
2512 }
2513
2514 bool Done() const { return instruction_ == nullptr; }
2515 HInstruction* Current() const { return instruction_; }
2516 void Advance() {
2517 instruction_ = next_;
2518 next_ = Done() ? nullptr : instruction_->GetPrevious();
2519 }
2520
2521 private:
2522 HInstruction* instruction_;
2523 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002524
2525 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002526};
2527
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002528class HVariableInputSizeInstruction : public HInstruction {
2529 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002530 using HInstruction::GetInputRecords; // Keep the const version visible.
2531 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
2532 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2533 }
2534
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002535 void AddInput(HInstruction* input);
2536 void InsertInputAt(size_t index, HInstruction* input);
2537 void RemoveInputAt(size_t index);
2538
Igor Murashkind01745e2017-04-05 16:40:31 -07002539 // Removes all the inputs.
2540 // Also removes this instructions from each input's use list
2541 // (for non-environment uses only).
2542 void RemoveAllInputs();
2543
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002544 protected:
2545 HVariableInputSizeInstruction(SideEffects side_effects,
2546 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002547 ArenaAllocator* allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002548 size_t number_of_inputs,
2549 ArenaAllocKind kind)
2550 : HInstruction(side_effects, dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002551 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002552
Artem Serovcced8ba2017-07-19 18:18:09 +01002553 DEFAULT_COPY_CONSTRUCTOR(VariableInputSizeInstruction);
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002554
Artem Serovcced8ba2017-07-19 18:18:09 +01002555 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002556};
2557
Vladimir Markof9f64412015-09-02 14:05:49 +01002558template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002559class HTemplateInstruction: public HInstruction {
2560 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002561 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002562 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002563 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002564
Vladimir Marko372f10e2016-05-17 16:30:10 +01002565 using HInstruction::GetInputRecords; // Keep the const version visible.
2566 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2567 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002568 }
2569
Artem Serovcced8ba2017-07-19 18:18:09 +01002570 protected:
2571 DEFAULT_COPY_CONSTRUCTOR(TemplateInstruction<N>);
2572
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002573 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002574 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002575
2576 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002577};
2578
Vladimir Markof9f64412015-09-02 14:05:49 +01002579// HTemplateInstruction specialization for N=0.
2580template<>
2581class HTemplateInstruction<0>: public HInstruction {
2582 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002583 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002584 : HInstruction(side_effects, dex_pc) {}
2585
Vladimir Markof9f64412015-09-02 14:05:49 +01002586 virtual ~HTemplateInstruction() {}
2587
Vladimir Marko372f10e2016-05-17 16:30:10 +01002588 using HInstruction::GetInputRecords; // Keep the const version visible.
2589 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2590 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002591 }
2592
Artem Serovcced8ba2017-07-19 18:18:09 +01002593 protected:
2594 DEFAULT_COPY_CONSTRUCTOR(TemplateInstruction<0>);
2595
Vladimir Markof9f64412015-09-02 14:05:49 +01002596 private:
2597 friend class SsaBuilder;
2598};
2599
Dave Allison20dfc792014-06-16 20:44:29 -07002600template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002601class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002602 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002603 HExpression<N>(DataType::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002604 : HTemplateInstruction<N>(side_effects, dex_pc) {
2605 this->template SetPackedField<TypeField>(type);
2606 }
Dave Allison20dfc792014-06-16 20:44:29 -07002607 virtual ~HExpression() {}
2608
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002609 DataType::Type GetType() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00002610 return TypeField::Decode(this->GetPackedFields());
2611 }
Dave Allison20dfc792014-06-16 20:44:29 -07002612
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002613 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002614 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2615 static constexpr size_t kFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002616 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00002617 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2618 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2619 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002620 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
Artem Serovcced8ba2017-07-19 18:18:09 +01002621 DEFAULT_COPY_CONSTRUCTOR(Expression<N>);
Dave Allison20dfc792014-06-16 20:44:29 -07002622};
2623
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002624// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2625// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002626class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002627 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002628 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2629 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002630
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002631 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002632
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002633 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002634
Artem Serovcced8ba2017-07-19 18:18:09 +01002635 protected:
2636 DEFAULT_COPY_CONSTRUCTOR(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002637};
2638
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002639// Represents dex's RETURN opcodes. A HReturn is a control flow
2640// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002641class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002642 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002643 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2644 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002645 SetRawInputAt(0, value);
2646 }
2647
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002648 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002649
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002650 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002651
Artem Serovcced8ba2017-07-19 18:18:09 +01002652 protected:
2653 DEFAULT_COPY_CONSTRUCTOR(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002654};
2655
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002656class HPhi FINAL : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002657 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002658 HPhi(ArenaAllocator* allocator,
David Brazdildee58d62016-04-07 09:54:26 +00002659 uint32_t reg_number,
2660 size_t number_of_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002661 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +00002662 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002663 : HVariableInputSizeInstruction(
2664 SideEffects::None(),
2665 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002666 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002667 number_of_inputs,
2668 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002669 reg_number_(reg_number) {
2670 SetPackedField<TypeField>(ToPhiType(type));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002671 DCHECK_NE(GetType(), DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +00002672 // Phis are constructed live and marked dead if conflicting or unused.
2673 // Individual steps of SsaBuilder should assume that if a phi has been
2674 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2675 SetPackedFlag<kFlagIsLive>(true);
2676 SetPackedFlag<kFlagCanBeNull>(true);
2677 }
2678
Artem Serovcced8ba2017-07-19 18:18:09 +01002679 bool IsClonable() const OVERRIDE { return true; }
2680
David Brazdildee58d62016-04-07 09:54:26 +00002681 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002682 static DataType::Type ToPhiType(DataType::Type type) {
2683 return DataType::Kind(type);
David Brazdildee58d62016-04-07 09:54:26 +00002684 }
2685
2686 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2687
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002688 DataType::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2689 void SetType(DataType::Type new_type) {
David Brazdildee58d62016-04-07 09:54:26 +00002690 // Make sure that only valid type changes occur. The following are allowed:
2691 // (1) int -> float/ref (primitive type propagation),
2692 // (2) long -> double (primitive type propagation).
2693 DCHECK(GetType() == new_type ||
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002694 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kFloat32) ||
2695 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kReference) ||
2696 (GetType() == DataType::Type::kInt64 && new_type == DataType::Type::kFloat64));
David Brazdildee58d62016-04-07 09:54:26 +00002697 SetPackedField<TypeField>(new_type);
2698 }
2699
2700 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2701 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2702
2703 uint32_t GetRegNumber() const { return reg_number_; }
2704
2705 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2706 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2707 bool IsDead() const { return !IsLive(); }
2708 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2709
Vladimir Markoe9004912016-06-16 16:50:52 +01002710 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002711 return other != nullptr
2712 && other->IsPhi()
2713 && other->AsPhi()->GetBlock() == GetBlock()
2714 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2715 }
2716
Nicolas Geoffrayf57c1ae2017-06-28 17:40:18 +01002717 bool HasEquivalentPhi() const {
2718 if (GetPrevious() != nullptr && GetPrevious()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2719 return true;
2720 }
2721 if (GetNext() != nullptr && GetNext()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2722 return true;
2723 }
2724 return false;
2725 }
2726
David Brazdildee58d62016-04-07 09:54:26 +00002727 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2728 // An equivalent phi is a phi having the same dex register and type.
2729 // It assumes that phis with the same dex register are adjacent.
2730 HPhi* GetNextEquivalentPhiWithSameType() {
2731 HInstruction* next = GetNext();
2732 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2733 if (next->GetType() == GetType()) {
2734 return next->AsPhi();
2735 }
2736 next = next->GetNext();
2737 }
2738 return nullptr;
2739 }
2740
2741 DECLARE_INSTRUCTION(Phi);
2742
Artem Serovcced8ba2017-07-19 18:18:09 +01002743 protected:
2744 DEFAULT_COPY_CONSTRUCTOR(Phi);
2745
David Brazdildee58d62016-04-07 09:54:26 +00002746 private:
2747 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2748 static constexpr size_t kFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002749 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
David Brazdildee58d62016-04-07 09:54:26 +00002750 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2751 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2752 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2753 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002754 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
David Brazdildee58d62016-04-07 09:54:26 +00002755
David Brazdildee58d62016-04-07 09:54:26 +00002756 const uint32_t reg_number_;
David Brazdildee58d62016-04-07 09:54:26 +00002757};
2758
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002759// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002760// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002761// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002762class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002763 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002764 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002765
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002766 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002767
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002768 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002769
Artem Serovcced8ba2017-07-19 18:18:09 +01002770 protected:
2771 DEFAULT_COPY_CONSTRUCTOR(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002772};
2773
2774// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002775class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002776 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002777 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002778
Artem Serovcced8ba2017-07-19 18:18:09 +01002779 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002780 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002781
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002782 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002783 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002784 }
2785
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002786 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002787
Artem Serovcced8ba2017-07-19 18:18:09 +01002788 protected:
2789 DEFAULT_COPY_CONSTRUCTOR(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002790};
2791
Roland Levillain9867bc72015-08-05 10:21:34 +01002792class HConstant : public HExpression<0> {
2793 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002794 explicit HConstant(DataType::Type type, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002795 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002796
2797 bool CanBeMoved() const OVERRIDE { return true; }
2798
Roland Levillain1a653882016-03-18 18:05:57 +00002799 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002800 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002801 // Is this constant 0 in the arithmetic sense?
2802 virtual bool IsArithmeticZero() const { return false; }
2803 // Is this constant a 0-bit pattern?
2804 virtual bool IsZeroBitPattern() const { return false; }
2805 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002806 virtual bool IsOne() const { return false; }
2807
David Brazdil77a48ae2015-09-15 12:34:04 +00002808 virtual uint64_t GetValueAsUint64() const = 0;
2809
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002810 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002811
Artem Serovcced8ba2017-07-19 18:18:09 +01002812 protected:
2813 DEFAULT_COPY_CONSTRUCTOR(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002814};
2815
Vladimir Markofcb503c2016-05-18 12:48:17 +01002816class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002817 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002818 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002819 return true;
2820 }
2821
David Brazdil77a48ae2015-09-15 12:34:04 +00002822 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2823
Roland Levillain9867bc72015-08-05 10:21:34 +01002824 size_t ComputeHashCode() const OVERRIDE { return 0; }
2825
Roland Levillain1a653882016-03-18 18:05:57 +00002826 // The null constant representation is a 0-bit pattern.
2827 virtual bool IsZeroBitPattern() const { return true; }
2828
Roland Levillain9867bc72015-08-05 10:21:34 +01002829 DECLARE_INSTRUCTION(NullConstant);
2830
Artem Serovcced8ba2017-07-19 18:18:09 +01002831 protected:
2832 DEFAULT_COPY_CONSTRUCTOR(NullConstant);
2833
Roland Levillain9867bc72015-08-05 10:21:34 +01002834 private:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002835 explicit HNullConstant(uint32_t dex_pc = kNoDexPc)
2836 : HConstant(DataType::Type::kReference, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002837
2838 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01002839};
2840
2841// Constants of the type int. Those can be from Dex instructions, or
2842// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002843class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002844 public:
2845 int32_t GetValue() const { return value_; }
2846
David Brazdil9f389d42015-10-01 14:32:56 +01002847 uint64_t GetValueAsUint64() const OVERRIDE {
2848 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2849 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002850
Vladimir Marko372f10e2016-05-17 16:30:10 +01002851 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002852 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002853 return other->AsIntConstant()->value_ == value_;
2854 }
2855
2856 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2857
2858 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002859 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2860 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002861 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2862
Roland Levillain1a653882016-03-18 18:05:57 +00002863 // Integer constants are used to encode Boolean values as well,
2864 // where 1 means true and 0 means false.
2865 bool IsTrue() const { return GetValue() == 1; }
2866 bool IsFalse() const { return GetValue() == 0; }
2867
Roland Levillain9867bc72015-08-05 10:21:34 +01002868 DECLARE_INSTRUCTION(IntConstant);
2869
Artem Serovcced8ba2017-07-19 18:18:09 +01002870 protected:
2871 DEFAULT_COPY_CONSTRUCTOR(IntConstant);
2872
Roland Levillain9867bc72015-08-05 10:21:34 +01002873 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002874 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002875 : HConstant(DataType::Type::kInt32, dex_pc), value_(value) {}
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002876 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002877 : HConstant(DataType::Type::kInt32, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002878
2879 const int32_t value_;
2880
2881 friend class HGraph;
2882 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2883 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Roland Levillain9867bc72015-08-05 10:21:34 +01002884};
2885
Vladimir Markofcb503c2016-05-18 12:48:17 +01002886class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002887 public:
2888 int64_t GetValue() const { return value_; }
2889
David Brazdil77a48ae2015-09-15 12:34:04 +00002890 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2891
Vladimir Marko372f10e2016-05-17 16:30:10 +01002892 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002893 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002894 return other->AsLongConstant()->value_ == value_;
2895 }
2896
2897 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2898
2899 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002900 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2901 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002902 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2903
2904 DECLARE_INSTRUCTION(LongConstant);
2905
Artem Serovcced8ba2017-07-19 18:18:09 +01002906 protected:
2907 DEFAULT_COPY_CONSTRUCTOR(LongConstant);
2908
Roland Levillain9867bc72015-08-05 10:21:34 +01002909 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002910 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002911 : HConstant(DataType::Type::kInt64, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002912
2913 const int64_t value_;
2914
2915 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01002916};
Dave Allison20dfc792014-06-16 20:44:29 -07002917
Vladimir Markofcb503c2016-05-18 12:48:17 +01002918class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002919 public:
2920 float GetValue() const { return value_; }
2921
2922 uint64_t GetValueAsUint64() const OVERRIDE {
2923 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2924 }
2925
Vladimir Marko372f10e2016-05-17 16:30:10 +01002926 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002927 DCHECK(other->IsFloatConstant()) << other->DebugName();
2928 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2929 }
2930
2931 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2932
2933 bool IsMinusOne() const OVERRIDE {
2934 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2935 }
Roland Levillain1a653882016-03-18 18:05:57 +00002936 bool IsArithmeticZero() const OVERRIDE {
2937 return std::fpclassify(value_) == FP_ZERO;
2938 }
2939 bool IsArithmeticPositiveZero() const {
2940 return IsArithmeticZero() && !std::signbit(value_);
2941 }
2942 bool IsArithmeticNegativeZero() const {
2943 return IsArithmeticZero() && std::signbit(value_);
2944 }
2945 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002946 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002947 }
2948 bool IsOne() const OVERRIDE {
2949 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2950 }
2951 bool IsNaN() const {
2952 return std::isnan(value_);
2953 }
2954
2955 DECLARE_INSTRUCTION(FloatConstant);
2956
Artem Serovcced8ba2017-07-19 18:18:09 +01002957 protected:
2958 DEFAULT_COPY_CONSTRUCTOR(FloatConstant);
2959
Roland Levillain31dd3d62016-02-16 12:21:02 +00002960 private:
2961 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002962 : HConstant(DataType::Type::kFloat32, dex_pc), value_(value) {}
Roland Levillain31dd3d62016-02-16 12:21:02 +00002963 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002964 : HConstant(DataType::Type::kFloat32, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
Roland Levillain31dd3d62016-02-16 12:21:02 +00002965
2966 const float value_;
2967
2968 // Only the SsaBuilder and HGraph can create floating-point constants.
2969 friend class SsaBuilder;
2970 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00002971};
2972
Vladimir Markofcb503c2016-05-18 12:48:17 +01002973class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002974 public:
2975 double GetValue() const { return value_; }
2976
2977 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2978
Vladimir Marko372f10e2016-05-17 16:30:10 +01002979 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002980 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2981 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2982 }
2983
2984 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2985
2986 bool IsMinusOne() const OVERRIDE {
2987 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2988 }
Roland Levillain1a653882016-03-18 18:05:57 +00002989 bool IsArithmeticZero() const OVERRIDE {
2990 return std::fpclassify(value_) == FP_ZERO;
2991 }
2992 bool IsArithmeticPositiveZero() const {
2993 return IsArithmeticZero() && !std::signbit(value_);
2994 }
2995 bool IsArithmeticNegativeZero() const {
2996 return IsArithmeticZero() && std::signbit(value_);
2997 }
2998 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002999 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00003000 }
3001 bool IsOne() const OVERRIDE {
3002 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3003 }
3004 bool IsNaN() const {
3005 return std::isnan(value_);
3006 }
3007
3008 DECLARE_INSTRUCTION(DoubleConstant);
3009
Artem Serovcced8ba2017-07-19 18:18:09 +01003010 protected:
3011 DEFAULT_COPY_CONSTRUCTOR(DoubleConstant);
3012
Roland Levillain31dd3d62016-02-16 12:21:02 +00003013 private:
3014 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003015 : HConstant(DataType::Type::kFloat64, dex_pc), value_(value) {}
Roland Levillain31dd3d62016-02-16 12:21:02 +00003016 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003017 : HConstant(DataType::Type::kFloat64, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
Roland Levillain31dd3d62016-02-16 12:21:02 +00003018
3019 const double value_;
3020
3021 // Only the SsaBuilder and HGraph can create floating-point constants.
3022 friend class SsaBuilder;
3023 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003024};
3025
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003026// Conditional branch. A block ending with an HIf instruction must have
3027// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003028class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003029 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003030 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
3031 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003032 SetRawInputAt(0, input);
3033 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003034
Artem Serovcced8ba2017-07-19 18:18:09 +01003035 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003036 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003037
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003038 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003039 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003040 }
3041
3042 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003043 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003044 }
3045
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003046 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003047
Artem Serovcced8ba2017-07-19 18:18:09 +01003048 protected:
3049 DEFAULT_COPY_CONSTRUCTOR(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003050};
3051
David Brazdilfc6a86a2015-06-26 10:33:45 +00003052
3053// Abstract instruction which marks the beginning and/or end of a try block and
3054// links it to the respective exception handlers. Behaves the same as a Goto in
3055// non-exceptional control flow.
3056// Normal-flow successor is stored at index zero, exception handlers under
3057// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003058class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003059 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003060 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01003061 kEntry,
3062 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003063 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01003064 };
3065
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003066 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003067 : HTemplateInstruction(SideEffects::None(), dex_pc) {
3068 SetPackedField<BoundaryKindField>(kind);
3069 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003070
3071 bool IsControlFlow() const OVERRIDE { return true; }
3072
3073 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01003074 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003075
David Brazdild26a4112015-11-10 11:07:31 +00003076 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
3077 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
3078 }
3079
David Brazdilfc6a86a2015-06-26 10:33:45 +00003080 // Returns whether `handler` is among its exception handlers (non-zero index
3081 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01003082 bool HasExceptionHandler(const HBasicBlock& handler) const {
3083 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00003084 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00003085 }
3086
3087 // If not present already, adds `handler` to its block's list of exception
3088 // handlers.
3089 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01003090 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003091 GetBlock()->AddSuccessor(handler);
3092 }
3093 }
3094
Vladimir Markoa1de9182016-02-25 11:37:38 +00003095 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
3096 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003097
David Brazdilffee3d32015-07-06 11:48:53 +01003098 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
3099
David Brazdilfc6a86a2015-06-26 10:33:45 +00003100 DECLARE_INSTRUCTION(TryBoundary);
3101
Artem Serovcced8ba2017-07-19 18:18:09 +01003102 protected:
3103 DEFAULT_COPY_CONSTRUCTOR(TryBoundary);
3104
David Brazdilfc6a86a2015-06-26 10:33:45 +00003105 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003106 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
3107 static constexpr size_t kFieldBoundaryKindSize =
3108 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
3109 static constexpr size_t kNumberOfTryBoundaryPackedBits =
3110 kFieldBoundaryKind + kFieldBoundaryKindSize;
3111 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
3112 "Too many packed fields.");
3113 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00003114};
3115
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003116// Deoptimize to interpreter, upon checking a condition.
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003117class HDeoptimize FINAL : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003118 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003119 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
3120 // across.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003121 HDeoptimize(ArenaAllocator* allocator,
3122 HInstruction* cond,
3123 DeoptimizationKind kind,
3124 uint32_t dex_pc)
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003125 : HVariableInputSizeInstruction(
3126 SideEffects::All(),
3127 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003128 allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003129 /* number_of_inputs */ 1,
3130 kArenaAllocMisc) {
3131 SetPackedFlag<kFieldCanBeMoved>(false);
3132 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003133 SetRawInputAt(0, cond);
3134 }
3135
Artem Serovcced8ba2017-07-19 18:18:09 +01003136 bool IsClonable() const OVERRIDE { return true; }
3137
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003138 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
3139 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
3140 // instead of `guard`.
3141 // We set CanTriggerGC to prevent any intermediate address to be live
3142 // at the point of the `HDeoptimize`.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003143 HDeoptimize(ArenaAllocator* allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003144 HInstruction* cond,
3145 HInstruction* guard,
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003146 DeoptimizationKind kind,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003147 uint32_t dex_pc)
3148 : HVariableInputSizeInstruction(
3149 SideEffects::CanTriggerGC(),
3150 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003151 allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003152 /* number_of_inputs */ 2,
3153 kArenaAllocMisc) {
3154 SetPackedFlag<kFieldCanBeMoved>(true);
3155 SetPackedField<DeoptimizeKindField>(kind);
3156 SetRawInputAt(0, cond);
3157 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003158 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003159
3160 bool CanBeMoved() const OVERRIDE { return GetPackedFlag<kFieldCanBeMoved>(); }
3161
3162 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
3163 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
3164 }
3165
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003166 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003167
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003168 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003169
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003170 DeoptimizationKind GetDeoptimizationKind() const { return GetPackedField<DeoptimizeKindField>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003171
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003172 DataType::Type GetType() const OVERRIDE {
3173 return GuardsAnInput() ? GuardedInput()->GetType() : DataType::Type::kVoid;
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003174 }
3175
3176 bool GuardsAnInput() const {
3177 return InputCount() == 2;
3178 }
3179
3180 HInstruction* GuardedInput() const {
3181 DCHECK(GuardsAnInput());
3182 return InputAt(1);
3183 }
3184
3185 void RemoveGuard() {
3186 RemoveInputAt(1);
3187 }
3188
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003189 DECLARE_INSTRUCTION(Deoptimize);
3190
Artem Serovcced8ba2017-07-19 18:18:09 +01003191 protected:
3192 DEFAULT_COPY_CONSTRUCTOR(Deoptimize);
3193
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003194 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003195 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3196 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3197 static constexpr size_t kFieldDeoptimizeKindSize =
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003198 MinimumBitsToStore(static_cast<size_t>(DeoptimizationKind::kLast));
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003199 static constexpr size_t kNumberOfDeoptimizePackedBits =
3200 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3201 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3202 "Too many packed fields.");
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003203 using DeoptimizeKindField =
3204 BitField<DeoptimizationKind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003205};
3206
Mingyao Yang063fc772016-08-02 11:02:54 -07003207// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3208// The compiled code checks this flag value in a guard before devirtualized call and
3209// if it's true, starts to do deoptimization.
3210// It has a 4-byte slot on stack.
3211// TODO: allocate a register for this flag.
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003212class HShouldDeoptimizeFlag FINAL : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003213 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003214 // CHA guards are only optimized in a separate pass and it has no side effects
3215 // with regard to other passes.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003216 HShouldDeoptimizeFlag(ArenaAllocator* allocator, uint32_t dex_pc)
3217 : HVariableInputSizeInstruction(SideEffects::None(), dex_pc, allocator, 0, kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003218 }
3219
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003220 DataType::Type GetType() const OVERRIDE { return DataType::Type::kInt32; }
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003221
3222 // We do all CHA guard elimination/motion in a single pass, after which there is no
3223 // further guard elimination/motion since a guard might have been used for justification
3224 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3225 // to avoid other optimizations trying to move it.
Mingyao Yang063fc772016-08-02 11:02:54 -07003226 bool CanBeMoved() const OVERRIDE { return false; }
3227
3228 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3229
Artem Serovcced8ba2017-07-19 18:18:09 +01003230 protected:
3231 DEFAULT_COPY_CONSTRUCTOR(ShouldDeoptimizeFlag);
Mingyao Yang063fc772016-08-02 11:02:54 -07003232};
3233
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003234// Represents the ArtMethod that was passed as a first argument to
3235// the method. It is used by instructions that depend on it, like
3236// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003237class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003238 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003239 explicit HCurrentMethod(DataType::Type type, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003240 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003241
3242 DECLARE_INSTRUCTION(CurrentMethod);
3243
Artem Serovcced8ba2017-07-19 18:18:09 +01003244 protected:
3245 DEFAULT_COPY_CONSTRUCTOR(CurrentMethod);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003246};
3247
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003248// Fetches an ArtMethod from the virtual table or the interface method table
3249// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003250class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003251 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003252 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003253 kVTable,
3254 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003255 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003256 };
3257 HClassTableGet(HInstruction* cls,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003258 DataType::Type type,
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003259 TableKind kind,
3260 size_t index,
3261 uint32_t dex_pc)
3262 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003263 index_(index) {
3264 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003265 SetRawInputAt(0, cls);
3266 }
3267
Artem Serovcced8ba2017-07-19 18:18:09 +01003268 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003269 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003270 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003271 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003272 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003273 }
3274
Vladimir Markoa1de9182016-02-25 11:37:38 +00003275 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003276 size_t GetIndex() const { return index_; }
3277
3278 DECLARE_INSTRUCTION(ClassTableGet);
3279
Artem Serovcced8ba2017-07-19 18:18:09 +01003280 protected:
3281 DEFAULT_COPY_CONSTRUCTOR(ClassTableGet);
3282
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003283 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003284 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
3285 static constexpr size_t kFieldTableKindSize =
3286 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3287 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3288 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3289 "Too many packed fields.");
3290 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3291
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003292 // The index of the ArtMethod in the table.
3293 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003294};
3295
Mark Mendellfe57faa2015-09-18 09:26:15 -04003296// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3297// have one successor for each entry in the switch table, and the final successor
3298// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003299class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003300 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003301 HPackedSwitch(int32_t start_value,
3302 uint32_t num_entries,
3303 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003304 uint32_t dex_pc = kNoDexPc)
3305 : HTemplateInstruction(SideEffects::None(), dex_pc),
3306 start_value_(start_value),
3307 num_entries_(num_entries) {
3308 SetRawInputAt(0, input);
3309 }
3310
Artem Serovcced8ba2017-07-19 18:18:09 +01003311 bool IsClonable() const OVERRIDE { return true; }
3312
Mark Mendellfe57faa2015-09-18 09:26:15 -04003313 bool IsControlFlow() const OVERRIDE { return true; }
3314
3315 int32_t GetStartValue() const { return start_value_; }
3316
Vladimir Marko211c2112015-09-24 16:52:33 +01003317 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003318
3319 HBasicBlock* GetDefaultBlock() const {
3320 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003321 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003322 }
3323 DECLARE_INSTRUCTION(PackedSwitch);
3324
Artem Serovcced8ba2017-07-19 18:18:09 +01003325 protected:
3326 DEFAULT_COPY_CONSTRUCTOR(PackedSwitch);
3327
Mark Mendellfe57faa2015-09-18 09:26:15 -04003328 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003329 const int32_t start_value_;
3330 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003331};
3332
Roland Levillain88cb1752014-10-20 16:36:47 +01003333class HUnaryOperation : public HExpression<1> {
3334 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003335 HUnaryOperation(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003336 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003337 SetRawInputAt(0, input);
3338 }
3339
Artem Serovcced8ba2017-07-19 18:18:09 +01003340 // All of the UnaryOperation instructions are clonable.
3341 bool IsClonable() const OVERRIDE { return true; }
3342
Roland Levillain88cb1752014-10-20 16:36:47 +01003343 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003344 DataType::Type GetResultType() const { return GetType(); }
Roland Levillain88cb1752014-10-20 16:36:47 +01003345
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003346 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003347 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003348 return true;
3349 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003350
Roland Levillain31dd3d62016-02-16 12:21:02 +00003351 // Try to statically evaluate `this` and return a HConstant
3352 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003353 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003354 HConstant* TryStaticEvaluation() const;
3355
3356 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003357 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3358 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003359 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3360 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003361
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003362 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003363
Artem Serovcced8ba2017-07-19 18:18:09 +01003364 protected:
3365 DEFAULT_COPY_CONSTRUCTOR(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003366};
3367
Dave Allison20dfc792014-06-16 20:44:29 -07003368class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003369 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003370 HBinaryOperation(DataType::Type result_type,
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003371 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003372 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003373 SideEffects side_effects = SideEffects::None(),
3374 uint32_t dex_pc = kNoDexPc)
3375 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003376 SetRawInputAt(0, left);
3377 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003378 }
3379
Artem Serovcced8ba2017-07-19 18:18:09 +01003380 // All of the BinaryOperation instructions are clonable.
3381 bool IsClonable() const OVERRIDE { return true; }
3382
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003383 HInstruction* GetLeft() const { return InputAt(0); }
3384 HInstruction* GetRight() const { return InputAt(1); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003385 DataType::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003386
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003387 virtual bool IsCommutative() const { return false; }
3388
3389 // Put constant on the right.
3390 // Returns whether order is changed.
3391 bool OrderInputsWithConstantOnTheRight() {
3392 HInstruction* left = InputAt(0);
3393 HInstruction* right = InputAt(1);
3394 if (left->IsConstant() && !right->IsConstant()) {
3395 ReplaceInput(right, 0);
3396 ReplaceInput(left, 1);
3397 return true;
3398 }
3399 return false;
3400 }
3401
3402 // Order inputs by instruction id, but favor constant on the right side.
3403 // This helps GVN for commutative ops.
3404 void OrderInputs() {
3405 DCHECK(IsCommutative());
3406 HInstruction* left = InputAt(0);
3407 HInstruction* right = InputAt(1);
3408 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3409 return;
3410 }
3411 if (OrderInputsWithConstantOnTheRight()) {
3412 return;
3413 }
3414 // Order according to instruction id.
3415 if (left->GetId() > right->GetId()) {
3416 ReplaceInput(right, 0);
3417 ReplaceInput(left, 1);
3418 }
3419 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003420
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003421 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003422 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003423 return true;
3424 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003425
Roland Levillain31dd3d62016-02-16 12:21:02 +00003426 // Try to statically evaluate `this` and return a HConstant
3427 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003428 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003429 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003430
3431 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003432 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3433 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003434 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3435 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003436 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003437 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3438 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003439 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3440 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003441 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3442 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003443 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003444 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3445 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003446
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003447 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003448 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003449 HConstant* GetConstantRight() const;
3450
3451 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003452 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003453 HInstruction* GetLeastConstantLeft() const;
3454
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003455 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003456
Artem Serovcced8ba2017-07-19 18:18:09 +01003457 protected:
3458 DEFAULT_COPY_CONSTRUCTOR(BinaryOperation);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003459};
3460
Mark Mendellc4701932015-04-10 13:18:51 -04003461// The comparison bias applies for floating point operations and indicates how NaN
3462// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003463enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003464 kNoBias, // bias is not applicable (i.e. for long operation)
3465 kGtBias, // return 1 for NaN comparisons
3466 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003467 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003468};
3469
Roland Levillain31dd3d62016-02-16 12:21:02 +00003470std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3471
Dave Allison20dfc792014-06-16 20:44:29 -07003472class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003473 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003474 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003475 : HBinaryOperation(DataType::Type::kBool, first, second, SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003476 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3477 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003478
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003479 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003480 // `instruction`, and disregard moves in between.
3481 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003482
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003483 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003484
3485 virtual IfCondition GetCondition() const = 0;
3486
Mark Mendellc4701932015-04-10 13:18:51 -04003487 virtual IfCondition GetOppositeCondition() const = 0;
3488
Vladimir Markoa1de9182016-02-25 11:37:38 +00003489 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003490 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3491
Vladimir Markoa1de9182016-02-25 11:37:38 +00003492 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3493 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003494
Vladimir Marko372f10e2016-05-17 16:30:10 +01003495 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003496 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003497 }
3498
Roland Levillain4fa13f62015-07-06 18:11:54 +01003499 bool IsFPConditionTrueIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003500 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003501 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003502 if (if_cond == kCondNE) {
3503 return true;
3504 } else if (if_cond == kCondEQ) {
3505 return false;
3506 }
3507 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003508 }
3509
3510 bool IsFPConditionFalseIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003511 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003512 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003513 if (if_cond == kCondEQ) {
3514 return true;
3515 } else if (if_cond == kCondNE) {
3516 return false;
3517 }
3518 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003519 }
3520
Roland Levillain31dd3d62016-02-16 12:21:02 +00003521 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003522 // Needed if we merge a HCompare into a HCondition.
3523 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3524 static constexpr size_t kFieldComparisonBiasSize =
3525 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3526 static constexpr size_t kNumberOfConditionPackedBits =
3527 kFieldComparisonBias + kFieldComparisonBiasSize;
3528 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3529 using ComparisonBiasField =
3530 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3531
Roland Levillain31dd3d62016-02-16 12:21:02 +00003532 template <typename T>
3533 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3534
3535 template <typename T>
3536 int32_t CompareFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003537 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003538 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3539 // Handle the bias.
3540 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3541 }
3542
3543 // Return an integer constant containing the result of a condition evaluated at compile time.
3544 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3545 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3546 }
3547
Artem Serovcced8ba2017-07-19 18:18:09 +01003548 DEFAULT_COPY_CONSTRUCTOR(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003549};
3550
3551// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003552class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003553 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003554 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3555 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003556
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003557 bool IsCommutative() const OVERRIDE { return true; }
3558
Vladimir Marko9e23df52015-11-10 17:14:35 +00003559 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3560 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003561 return MakeConstantCondition(true, GetDexPc());
3562 }
3563 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3564 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3565 }
3566 // In the following Evaluate methods, a HCompare instruction has
3567 // been merged into this HEqual instruction; evaluate it as
3568 // `Compare(x, y) == 0`.
3569 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3570 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3571 GetDexPc());
3572 }
3573 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3574 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3575 }
3576 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3577 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003578 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003579
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003580 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003581
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003582 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003583 return kCondEQ;
3584 }
3585
Mark Mendellc4701932015-04-10 13:18:51 -04003586 IfCondition GetOppositeCondition() const OVERRIDE {
3587 return kCondNE;
3588 }
3589
Artem Serovcced8ba2017-07-19 18:18:09 +01003590 protected:
3591 DEFAULT_COPY_CONSTRUCTOR(Equal);
3592
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003593 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003594 template <typename T> static bool Compute(T x, T y) { return x == y; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003595};
3596
Vladimir Markofcb503c2016-05-18 12:48:17 +01003597class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003598 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003599 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3600 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003601
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003602 bool IsCommutative() const OVERRIDE { return true; }
3603
Vladimir Marko9e23df52015-11-10 17:14:35 +00003604 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3605 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003606 return MakeConstantCondition(false, GetDexPc());
3607 }
3608 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3609 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3610 }
3611 // In the following Evaluate methods, a HCompare instruction has
3612 // been merged into this HNotEqual instruction; evaluate it as
3613 // `Compare(x, y) != 0`.
3614 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3615 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3616 }
3617 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3618 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3619 }
3620 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3621 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003622 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003623
Dave Allison20dfc792014-06-16 20:44:29 -07003624 DECLARE_INSTRUCTION(NotEqual);
3625
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003626 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003627 return kCondNE;
3628 }
3629
Mark Mendellc4701932015-04-10 13:18:51 -04003630 IfCondition GetOppositeCondition() const OVERRIDE {
3631 return kCondEQ;
3632 }
3633
Artem Serovcced8ba2017-07-19 18:18:09 +01003634 protected:
3635 DEFAULT_COPY_CONSTRUCTOR(NotEqual);
3636
Dave Allison20dfc792014-06-16 20:44:29 -07003637 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003638 template <typename T> static bool Compute(T x, T y) { return x != y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003639};
3640
Vladimir Markofcb503c2016-05-18 12:48:17 +01003641class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003642 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003643 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3644 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003645
Roland Levillain9867bc72015-08-05 10:21:34 +01003646 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003647 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003648 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003649 // In the following Evaluate methods, a HCompare instruction has
3650 // been merged into this HLessThan instruction; evaluate it as
3651 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003652 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003653 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3654 }
3655 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3656 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3657 }
3658 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3659 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003660 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003661
Dave Allison20dfc792014-06-16 20:44:29 -07003662 DECLARE_INSTRUCTION(LessThan);
3663
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003664 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003665 return kCondLT;
3666 }
3667
Mark Mendellc4701932015-04-10 13:18:51 -04003668 IfCondition GetOppositeCondition() const OVERRIDE {
3669 return kCondGE;
3670 }
3671
Artem Serovcced8ba2017-07-19 18:18:09 +01003672 protected:
3673 DEFAULT_COPY_CONSTRUCTOR(LessThan);
3674
Dave Allison20dfc792014-06-16 20:44:29 -07003675 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003676 template <typename T> static bool Compute(T x, T y) { return x < y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003677};
3678
Vladimir Markofcb503c2016-05-18 12:48:17 +01003679class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003680 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003681 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3682 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003683
Roland Levillain9867bc72015-08-05 10:21:34 +01003684 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003685 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003686 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003687 // In the following Evaluate methods, a HCompare instruction has
3688 // been merged into this HLessThanOrEqual instruction; evaluate it as
3689 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003690 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003691 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3692 }
3693 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3694 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3695 }
3696 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3697 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003698 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003699
Dave Allison20dfc792014-06-16 20:44:29 -07003700 DECLARE_INSTRUCTION(LessThanOrEqual);
3701
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003702 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003703 return kCondLE;
3704 }
3705
Mark Mendellc4701932015-04-10 13:18:51 -04003706 IfCondition GetOppositeCondition() const OVERRIDE {
3707 return kCondGT;
3708 }
3709
Artem Serovcced8ba2017-07-19 18:18:09 +01003710 protected:
3711 DEFAULT_COPY_CONSTRUCTOR(LessThanOrEqual);
3712
Dave Allison20dfc792014-06-16 20:44:29 -07003713 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003714 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003715};
3716
Vladimir Markofcb503c2016-05-18 12:48:17 +01003717class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003718 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003719 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3720 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003721
Roland Levillain9867bc72015-08-05 10:21:34 +01003722 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003723 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003724 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003725 // In the following Evaluate methods, a HCompare instruction has
3726 // been merged into this HGreaterThan instruction; evaluate it as
3727 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003728 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003729 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3730 }
3731 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3732 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3733 }
3734 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3735 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003736 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003737
Dave Allison20dfc792014-06-16 20:44:29 -07003738 DECLARE_INSTRUCTION(GreaterThan);
3739
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003740 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003741 return kCondGT;
3742 }
3743
Mark Mendellc4701932015-04-10 13:18:51 -04003744 IfCondition GetOppositeCondition() const OVERRIDE {
3745 return kCondLE;
3746 }
3747
Artem Serovcced8ba2017-07-19 18:18:09 +01003748 protected:
3749 DEFAULT_COPY_CONSTRUCTOR(GreaterThan);
3750
Dave Allison20dfc792014-06-16 20:44:29 -07003751 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003752 template <typename T> static bool Compute(T x, T y) { return x > y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003753};
3754
Vladimir Markofcb503c2016-05-18 12:48:17 +01003755class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003756 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003757 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3758 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003759
Roland Levillain9867bc72015-08-05 10:21:34 +01003760 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003761 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003762 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003763 // In the following Evaluate methods, a HCompare instruction has
3764 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3765 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003766 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003767 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3768 }
3769 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3770 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3771 }
3772 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3773 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003774 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003775
Dave Allison20dfc792014-06-16 20:44:29 -07003776 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3777
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003778 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003779 return kCondGE;
3780 }
3781
Mark Mendellc4701932015-04-10 13:18:51 -04003782 IfCondition GetOppositeCondition() const OVERRIDE {
3783 return kCondLT;
3784 }
3785
Artem Serovcced8ba2017-07-19 18:18:09 +01003786 protected:
3787 DEFAULT_COPY_CONSTRUCTOR(GreaterThanOrEqual);
3788
Dave Allison20dfc792014-06-16 20:44:29 -07003789 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003790 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003791};
3792
Vladimir Markofcb503c2016-05-18 12:48:17 +01003793class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003794 public:
3795 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3796 : HCondition(first, second, dex_pc) {}
3797
3798 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003799 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003800 }
3801 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003802 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3803 }
3804 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3805 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3806 LOG(FATAL) << DebugName() << " is not defined for float values";
3807 UNREACHABLE();
3808 }
3809 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3810 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3811 LOG(FATAL) << DebugName() << " is not defined for double values";
3812 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003813 }
3814
3815 DECLARE_INSTRUCTION(Below);
3816
3817 IfCondition GetCondition() const OVERRIDE {
3818 return kCondB;
3819 }
3820
3821 IfCondition GetOppositeCondition() const OVERRIDE {
3822 return kCondAE;
3823 }
3824
Artem Serovcced8ba2017-07-19 18:18:09 +01003825 protected:
3826 DEFAULT_COPY_CONSTRUCTOR(Below);
3827
Aart Bike9f37602015-10-09 11:15:55 -07003828 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003829 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003830 return MakeUnsigned(x) < MakeUnsigned(y);
3831 }
Aart Bike9f37602015-10-09 11:15:55 -07003832};
3833
Vladimir Markofcb503c2016-05-18 12:48:17 +01003834class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003835 public:
3836 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3837 : HCondition(first, second, dex_pc) {}
3838
3839 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003840 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003841 }
3842 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003843 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3844 }
3845 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3846 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3847 LOG(FATAL) << DebugName() << " is not defined for float values";
3848 UNREACHABLE();
3849 }
3850 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3851 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3852 LOG(FATAL) << DebugName() << " is not defined for double values";
3853 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003854 }
3855
3856 DECLARE_INSTRUCTION(BelowOrEqual);
3857
3858 IfCondition GetCondition() const OVERRIDE {
3859 return kCondBE;
3860 }
3861
3862 IfCondition GetOppositeCondition() const OVERRIDE {
3863 return kCondA;
3864 }
3865
Artem Serovcced8ba2017-07-19 18:18:09 +01003866 protected:
3867 DEFAULT_COPY_CONSTRUCTOR(BelowOrEqual);
3868
Aart Bike9f37602015-10-09 11:15:55 -07003869 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003870 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003871 return MakeUnsigned(x) <= MakeUnsigned(y);
3872 }
Aart Bike9f37602015-10-09 11:15:55 -07003873};
3874
Vladimir Markofcb503c2016-05-18 12:48:17 +01003875class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003876 public:
3877 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3878 : HCondition(first, second, dex_pc) {}
3879
3880 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003881 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003882 }
3883 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003884 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3885 }
3886 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3887 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3888 LOG(FATAL) << DebugName() << " is not defined for float values";
3889 UNREACHABLE();
3890 }
3891 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3892 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3893 LOG(FATAL) << DebugName() << " is not defined for double values";
3894 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003895 }
3896
3897 DECLARE_INSTRUCTION(Above);
3898
3899 IfCondition GetCondition() const OVERRIDE {
3900 return kCondA;
3901 }
3902
3903 IfCondition GetOppositeCondition() const OVERRIDE {
3904 return kCondBE;
3905 }
3906
Artem Serovcced8ba2017-07-19 18:18:09 +01003907 protected:
3908 DEFAULT_COPY_CONSTRUCTOR(Above);
3909
Aart Bike9f37602015-10-09 11:15:55 -07003910 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003911 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003912 return MakeUnsigned(x) > MakeUnsigned(y);
3913 }
Aart Bike9f37602015-10-09 11:15:55 -07003914};
3915
Vladimir Markofcb503c2016-05-18 12:48:17 +01003916class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003917 public:
3918 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3919 : HCondition(first, second, dex_pc) {}
3920
3921 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003922 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003923 }
3924 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003925 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3926 }
3927 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3928 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3929 LOG(FATAL) << DebugName() << " is not defined for float values";
3930 UNREACHABLE();
3931 }
3932 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3933 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3934 LOG(FATAL) << DebugName() << " is not defined for double values";
3935 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003936 }
3937
3938 DECLARE_INSTRUCTION(AboveOrEqual);
3939
3940 IfCondition GetCondition() const OVERRIDE {
3941 return kCondAE;
3942 }
3943
3944 IfCondition GetOppositeCondition() const OVERRIDE {
3945 return kCondB;
3946 }
3947
Artem Serovcced8ba2017-07-19 18:18:09 +01003948 protected:
3949 DEFAULT_COPY_CONSTRUCTOR(AboveOrEqual);
3950
Aart Bike9f37602015-10-09 11:15:55 -07003951 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003952 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003953 return MakeUnsigned(x) >= MakeUnsigned(y);
3954 }
Aart Bike9f37602015-10-09 11:15:55 -07003955};
Dave Allison20dfc792014-06-16 20:44:29 -07003956
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003957// Instruction to check how two inputs compare to each other.
3958// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003959class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003960 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003961 // Note that `comparison_type` is the type of comparison performed
3962 // between the comparison's inputs, not the type of the instantiated
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003963 // HCompare instruction (which is always DataType::Type::kInt).
3964 HCompare(DataType::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003965 HInstruction* first,
3966 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003967 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003968 uint32_t dex_pc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003969 : HBinaryOperation(DataType::Type::kInt32,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003970 first,
3971 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003972 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003973 dex_pc) {
3974 SetPackedField<ComparisonBiasField>(bias);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003975 DCHECK_EQ(comparison_type, DataType::Kind(first->GetType()));
3976 DCHECK_EQ(comparison_type, DataType::Kind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003977 }
3978
Roland Levillain9867bc72015-08-05 10:21:34 +01003979 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003980 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3981
3982 template <typename T>
3983 int32_t ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003984 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003985 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3986 // Handle the bias.
3987 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3988 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003989
Roland Levillain9867bc72015-08-05 10:21:34 +01003990 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003991 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3992 // reach this code path when processing a freshly built HIR
3993 // graph. However HCompare integer instructions can be synthesized
3994 // by the instruction simplifier to implement IntegerCompare and
3995 // IntegerSignum intrinsics, so we have to handle this case.
3996 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003997 }
3998 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003999 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4000 }
4001 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4002 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4003 }
4004 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4005 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01004006 }
4007
Vladimir Marko372f10e2016-05-17 16:30:10 +01004008 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004009 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00004010 }
4011
Vladimir Markoa1de9182016-02-25 11:37:38 +00004012 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04004013
Roland Levillain31dd3d62016-02-16 12:21:02 +00004014 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00004015 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00004016 bool IsGtBias() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004017 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004018 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00004019 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004020
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004021 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type type ATTRIBUTE_UNUSED) {
Roland Levillain1693a1f2016-03-15 14:57:31 +00004022 // Comparisons do not require a runtime call in any back end.
4023 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004024 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004025
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004026 DECLARE_INSTRUCTION(Compare);
4027
Roland Levillain31dd3d62016-02-16 12:21:02 +00004028 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004029 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
4030 static constexpr size_t kFieldComparisonBiasSize =
4031 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
4032 static constexpr size_t kNumberOfComparePackedBits =
4033 kFieldComparisonBias + kFieldComparisonBiasSize;
4034 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4035 using ComparisonBiasField =
4036 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
4037
Roland Levillain31dd3d62016-02-16 12:21:02 +00004038 // Return an integer constant containing the result of a comparison evaluated at compile time.
4039 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
4040 DCHECK(value == -1 || value == 0 || value == 1) << value;
4041 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
4042 }
4043
Artem Serovcced8ba2017-07-19 18:18:09 +01004044 DEFAULT_COPY_CONSTRUCTOR(Compare);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004045};
4046
Nicolas Geoffray2b615ba2017-01-06 14:40:07 +00004047class HNewInstance FINAL : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00004048 public:
4049 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00004050 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004051 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00004052 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00004053 bool finalizable,
4054 QuickEntrypointEnum entrypoint)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004055 : HExpression(DataType::Type::kReference, SideEffects::CanTriggerGC(), dex_pc),
David Brazdil6de19382016-01-08 17:37:10 +00004056 type_index_(type_index),
4057 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00004058 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004059 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00004060 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00004061 }
4062
Artem Serovcced8ba2017-07-19 18:18:09 +01004063 bool IsClonable() const OVERRIDE { return true; }
4064
Andreas Gampea5b09a62016-11-17 15:21:22 -08004065 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00004066 const DexFile& GetDexFile() const { return dex_file_; }
4067
4068 // Calls runtime so needs an environment.
4069 bool NeedsEnvironment() const OVERRIDE { return true; }
4070
Mingyao Yang062157f2016-03-02 10:15:36 -08004071 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
4072 bool CanThrow() const OVERRIDE { return true; }
4073
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004074 bool NeedsChecks() const {
4075 return entrypoint_ == kQuickAllocObjectWithChecks;
4076 }
David Brazdil6de19382016-01-08 17:37:10 +00004077
Vladimir Markoa1de9182016-02-25 11:37:38 +00004078 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00004079
4080 bool CanBeNull() const OVERRIDE { return false; }
4081
4082 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4083
4084 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
4085 entrypoint_ = entrypoint;
4086 }
4087
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004088 HLoadClass* GetLoadClass() const {
4089 HInstruction* input = InputAt(0);
4090 if (input->IsClinitCheck()) {
4091 input = input->InputAt(0);
4092 }
4093 DCHECK(input->IsLoadClass());
4094 return input->AsLoadClass();
4095 }
4096
David Brazdil6de19382016-01-08 17:37:10 +00004097 bool IsStringAlloc() const;
4098
4099 DECLARE_INSTRUCTION(NewInstance);
4100
Artem Serovcced8ba2017-07-19 18:18:09 +01004101 protected:
4102 DEFAULT_COPY_CONSTRUCTOR(NewInstance);
4103
David Brazdil6de19382016-01-08 17:37:10 +00004104 private:
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004105 static constexpr size_t kFlagFinalizable = kNumberOfExpressionPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004106 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
4107 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
4108 "Too many packed fields.");
4109
Andreas Gampea5b09a62016-11-17 15:21:22 -08004110 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00004111 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00004112 QuickEntrypointEnum entrypoint_;
David Brazdil6de19382016-01-08 17:37:10 +00004113};
4114
Agi Csaki05f20562015-08-19 14:58:14 -07004115enum IntrinsicNeedsEnvironmentOrCache {
4116 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
4117 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07004118};
4119
Aart Bik5d75afe2015-12-14 11:57:01 -08004120enum IntrinsicSideEffects {
4121 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
4122 kReadSideEffects, // Intrinsic may read heap memory.
4123 kWriteSideEffects, // Intrinsic may write heap memory.
4124 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
4125};
4126
4127enum IntrinsicExceptions {
4128 kNoThrow, // Intrinsic does not throw any exceptions.
4129 kCanThrow // Intrinsic may throw exceptions.
4130};
4131
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004132class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004133 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004134 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004135
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01004136 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004137 SetRawInputAt(index, argument);
4138 }
4139
Roland Levillain3e3d7332015-04-28 11:00:54 +01004140 // Return the number of arguments. This number can be lower than
4141 // the number of inputs returned by InputCount(), as some invoke
4142 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
4143 // inputs at the end of their list of inputs.
4144 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
4145
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004146 DataType::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004147
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004148 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4149
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004150 InvokeType GetInvokeType() const {
4151 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004152 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004153
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01004154 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004155 return intrinsic_;
4156 }
4157
Aart Bik5d75afe2015-12-14 11:57:01 -08004158 void SetIntrinsic(Intrinsics intrinsic,
4159 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
4160 IntrinsicSideEffects side_effects,
4161 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004162
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004163 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00004164 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004165 }
4166
Aart Bikff7d89c2016-11-07 08:49:28 -08004167 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4168
Vladimir Markoa1de9182016-02-25 11:37:38 +00004169 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004170
Aart Bik71bf7b42016-11-16 10:17:46 -08004171 bool CanBeMoved() const OVERRIDE { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004172
Vladimir Marko372f10e2016-05-17 16:30:10 +01004173 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08004174 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4175 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004176
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004177 uint32_t* GetIntrinsicOptimizations() {
4178 return &intrinsic_optimizations_;
4179 }
4180
4181 const uint32_t* GetIntrinsicOptimizations() const {
4182 return &intrinsic_optimizations_;
4183 }
4184
4185 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4186
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004187 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004188 void SetResolvedMethod(ArtMethod* method) { resolved_method_ = method; }
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004189
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004190 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004191
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004192 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004193 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4194 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004195 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4196 static constexpr size_t kFieldReturnType =
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004197 kFieldInvokeType + kFieldInvokeTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004198 static constexpr size_t kFieldReturnTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004199 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00004200 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
4201 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
4202 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004203 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004204 using ReturnTypeField = BitField<DataType::Type, kFieldReturnType, kFieldReturnTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004205
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004206 HInvoke(ArenaAllocator* allocator,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004207 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004208 uint32_t number_of_other_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004209 DataType::Type return_type,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004210 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004211 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004212 ArtMethod* resolved_method,
4213 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004214 : HVariableInputSizeInstruction(
4215 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4216 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004217 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004218 number_of_arguments + number_of_other_inputs,
4219 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004220 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004221 resolved_method_(resolved_method),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004222 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07004223 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004224 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004225 SetPackedField<ReturnTypeField>(return_type);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004226 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004227 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004228 }
4229
Artem Serovcced8ba2017-07-19 18:18:09 +01004230 DEFAULT_COPY_CONSTRUCTOR(Invoke);
4231
Roland Levillain3e3d7332015-04-28 11:00:54 +01004232 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004233 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004234 const uint32_t dex_method_index_;
4235 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004236
4237 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4238 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004239};
4240
Vladimir Markofcb503c2016-05-18 12:48:17 +01004241class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004242 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004243 HInvokeUnresolved(ArenaAllocator* allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004244 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004245 DataType::Type return_type,
Calin Juravle175dc732015-08-25 15:42:32 +01004246 uint32_t dex_pc,
4247 uint32_t dex_method_index,
4248 InvokeType invoke_type)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004249 : HInvoke(allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004250 number_of_arguments,
4251 0u /* number_of_other_inputs */,
4252 return_type,
4253 dex_pc,
4254 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004255 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01004256 invoke_type) {
4257 }
4258
Artem Serovcced8ba2017-07-19 18:18:09 +01004259 bool IsClonable() const OVERRIDE { return true; }
4260
Calin Juravle175dc732015-08-25 15:42:32 +01004261 DECLARE_INSTRUCTION(InvokeUnresolved);
4262
Artem Serovcced8ba2017-07-19 18:18:09 +01004263 protected:
4264 DEFAULT_COPY_CONSTRUCTOR(InvokeUnresolved);
Calin Juravle175dc732015-08-25 15:42:32 +01004265};
4266
Orion Hodsonac141392017-01-13 11:53:47 +00004267class HInvokePolymorphic FINAL : public HInvoke {
4268 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004269 HInvokePolymorphic(ArenaAllocator* allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004270 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004271 DataType::Type return_type,
Orion Hodsonac141392017-01-13 11:53:47 +00004272 uint32_t dex_pc,
4273 uint32_t dex_method_index)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004274 : HInvoke(allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004275 number_of_arguments,
4276 0u /* number_of_other_inputs */,
4277 return_type,
4278 dex_pc,
4279 dex_method_index,
4280 nullptr,
4281 kVirtual) {}
4282
Artem Serovcced8ba2017-07-19 18:18:09 +01004283 bool IsClonable() const OVERRIDE { return true; }
4284
Orion Hodsonac141392017-01-13 11:53:47 +00004285 DECLARE_INSTRUCTION(InvokePolymorphic);
4286
Artem Serovcced8ba2017-07-19 18:18:09 +01004287 protected:
4288 DEFAULT_COPY_CONSTRUCTOR(InvokePolymorphic);
Orion Hodsonac141392017-01-13 11:53:47 +00004289};
4290
Vladimir Markofcb503c2016-05-18 12:48:17 +01004291class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004292 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004293 // Requirements of this method call regarding the class
4294 // initialization (clinit) check of its declaring class.
4295 enum class ClinitCheckRequirement {
4296 kNone, // Class already initialized.
4297 kExplicit, // Static call having explicit clinit check as last input.
4298 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004299 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004300 };
4301
Vladimir Marko58155012015-08-19 12:49:41 +00004302 // Determines how to load the target ArtMethod*.
4303 enum class MethodLoadKind {
4304 // Use a String init ArtMethod* loaded from Thread entrypoints.
4305 kStringInit,
4306
4307 // Use the method's own ArtMethod* loaded by the register allocator.
4308 kRecursive,
4309
Vladimir Marko65979462017-05-19 17:25:12 +01004310 // Use PC-relative boot image ArtMethod* address that will be known at link time.
4311 // Used for boot image methods referenced by boot image code.
4312 kBootImageLinkTimePcRelative,
4313
Vladimir Marko58155012015-08-19 12:49:41 +00004314 // Use ArtMethod* at a known address, embed the direct address in the code.
4315 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
4316 kDirectAddress,
4317
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004318 // Load from an entry in the .bss section using a PC-relative load.
4319 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
4320 kBssEntry,
Vladimir Marko58155012015-08-19 12:49:41 +00004321
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004322 // Make a runtime call to resolve and call the method. This is the last-resort-kind
4323 // used when other kinds are unimplemented on a particular architecture.
4324 kRuntimeCall,
Vladimir Marko58155012015-08-19 12:49:41 +00004325 };
4326
4327 // Determines the location of the code pointer.
4328 enum class CodePtrLocation {
4329 // Recursive call, use local PC-relative call instruction.
4330 kCallSelf,
4331
Vladimir Marko58155012015-08-19 12:49:41 +00004332 // Use code pointer from the ArtMethod*.
4333 // Used when we don't know the target code. This is also the last-resort-kind used when
4334 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4335 kCallArtMethod,
4336 };
4337
4338 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004339 MethodLoadKind method_load_kind;
4340 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004341 // The method load data holds
4342 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4343 // Note that there are multiple string init methods, each having its own offset.
4344 // - the method address for kDirectAddress
Vladimir Markodc151b22015-10-15 18:02:30 +01004345 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004346 };
4347
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004348 HInvokeStaticOrDirect(ArenaAllocator* allocator,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004349 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004350 DataType::Type return_type,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004351 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004352 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004353 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004354 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004355 InvokeType invoke_type,
4356 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004357 ClinitCheckRequirement clinit_check_requirement)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004358 : HInvoke(allocator,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004359 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004360 // There is potentially one extra argument for the HCurrentMethod node, and
4361 // potentially one other if the clinit check is explicit, and potentially
4362 // one other if the method is a string factory.
4363 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004364 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004365 return_type,
4366 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004367 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004368 resolved_method,
4369 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004370 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004371 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004372 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4373 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004374
Artem Serovcced8ba2017-07-19 18:18:09 +01004375 bool IsClonable() const OVERRIDE { return true; }
4376
Vladimir Markodc151b22015-10-15 18:02:30 +01004377 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004378 bool had_current_method_input = HasCurrentMethodInput();
4379 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4380
4381 // Using the current method is the default and once we find a better
4382 // method load kind, we should not go back to using the current method.
4383 DCHECK(had_current_method_input || !needs_current_method_input);
4384
4385 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004386 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4387 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004388 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004389 dispatch_info_ = dispatch_info;
4390 }
4391
Aart Bik6daebeb2017-04-03 14:35:41 -07004392 DispatchInfo GetDispatchInfo() const {
4393 return dispatch_info_;
4394 }
4395
Vladimir Markoc53c0792015-11-19 15:48:33 +00004396 void AddSpecialInput(HInstruction* input) {
4397 // We allow only one special input.
4398 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4399 DCHECK(InputCount() == GetSpecialInputIndex() ||
4400 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4401 InsertInputAt(GetSpecialInputIndex(), input);
4402 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004403
Vladimir Marko372f10e2016-05-17 16:30:10 +01004404 using HInstruction::GetInputRecords; // Keep the const version visible.
4405 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
4406 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4407 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4408 DCHECK(!input_records.empty());
4409 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4410 HInstruction* last_input = input_records.back().GetInstruction();
4411 // Note: `last_input` may be null during arguments setup.
4412 if (last_input != nullptr) {
4413 // `last_input` is the last input of a static invoke marked as having
4414 // an explicit clinit check. It must either be:
4415 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4416 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4417 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4418 }
4419 }
4420 return input_records;
4421 }
4422
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004423 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004424 // We access the method via the dex cache so we can't do an implicit null check.
4425 // TODO: for intrinsics we can generate implicit null checks.
4426 return false;
4427 }
4428
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004429 bool CanBeNull() const OVERRIDE {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004430 return GetPackedField<ReturnTypeField>() == DataType::Type::kReference && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004431 }
4432
Vladimir Markoc53c0792015-11-19 15:48:33 +00004433 // Get the index of the special input, if any.
4434 //
David Brazdil6de19382016-01-08 17:37:10 +00004435 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4436 // method pointer; otherwise there may be one platform-specific special input,
4437 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004438 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004439 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004440
Vladimir Marko58155012015-08-19 12:49:41 +00004441 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4442 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4443 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004444 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004445 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004446 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko65979462017-05-19 17:25:12 +01004447 bool HasPcRelativeMethodLoadKind() const {
4448 return GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004449 GetMethodLoadKind() == MethodLoadKind::kBssEntry;
Vladimir Marko65979462017-05-19 17:25:12 +01004450 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004451 bool HasCurrentMethodInput() const {
4452 // This function can be called only after the invoke has been fully initialized by the builder.
4453 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004454 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004455 return true;
4456 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004457 DCHECK(InputCount() == GetSpecialInputIndex() ||
4458 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004459 return false;
4460 }
4461 }
Vladimir Marko58155012015-08-19 12:49:41 +00004462
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004463 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004464 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004465 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004466 }
4467
4468 uint64_t GetMethodAddress() const {
4469 DCHECK(HasMethodAddress());
4470 return dispatch_info_.method_load_data;
4471 }
4472
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004473 const DexFile& GetDexFileForPcRelativeDexCache() const;
4474
Vladimir Markoa1de9182016-02-25 11:37:38 +00004475 ClinitCheckRequirement GetClinitCheckRequirement() const {
4476 return GetPackedField<ClinitCheckRequirementField>();
4477 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004478
Roland Levillain4c0eb422015-04-24 16:43:49 +01004479 // Is this instruction a call to a static method?
4480 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004481 return GetInvokeType() == kStatic;
4482 }
4483
4484 MethodReference GetTargetMethod() const {
4485 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004486 }
4487
Vladimir Markofbb184a2015-11-13 14:47:00 +00004488 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4489 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4490 // instruction; only relevant for static calls with explicit clinit check.
4491 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004492 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004493 size_t last_input_index = inputs_.size() - 1u;
4494 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004495 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004496 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004497 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004498 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004499 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004500 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004501 }
4502
4503 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004504 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004505 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004506 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004507 }
4508
4509 // Is this a call to a static method whose declaring class has an
4510 // implicit intialization check requirement?
4511 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004512 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004513 }
4514
Vladimir Markob554b5a2015-11-06 12:57:55 +00004515 // Does this method load kind need the current method as an input?
4516 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004517 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kRuntimeCall;
Vladimir Markob554b5a2015-11-06 12:57:55 +00004518 }
4519
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004520 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004521
Artem Serovcced8ba2017-07-19 18:18:09 +01004522 protected:
4523 DEFAULT_COPY_CONSTRUCTOR(InvokeStaticOrDirect);
4524
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004525 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004526 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004527 static constexpr size_t kFieldClinitCheckRequirementSize =
4528 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4529 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4530 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4531 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4532 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004533 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4534 kFieldClinitCheckRequirement,
4535 kFieldClinitCheckRequirementSize>;
4536
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004537 // Cached values of the resolved method, to avoid needing the mutator lock.
Vladimir Marko58155012015-08-19 12:49:41 +00004538 MethodReference target_method_;
4539 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004540};
Vladimir Markof64242a2015-12-01 14:58:23 +00004541std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004542std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004543
Vladimir Markofcb503c2016-05-18 12:48:17 +01004544class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004545 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004546 HInvokeVirtual(ArenaAllocator* allocator,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004547 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004548 DataType::Type return_type,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004549 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004550 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004551 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004552 uint32_t vtable_index)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004553 : HInvoke(allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004554 number_of_arguments,
4555 0u,
4556 return_type,
4557 dex_pc,
4558 dex_method_index,
4559 resolved_method,
4560 kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004561 vtable_index_(vtable_index) {}
4562
Artem Serovcced8ba2017-07-19 18:18:09 +01004563 bool IsClonable() const OVERRIDE { return true; }
4564
Aart Bik71bf7b42016-11-16 10:17:46 -08004565 bool CanBeNull() const OVERRIDE {
4566 switch (GetIntrinsic()) {
4567 case Intrinsics::kThreadCurrentThread:
4568 case Intrinsics::kStringBufferAppend:
4569 case Intrinsics::kStringBufferToString:
4570 case Intrinsics::kStringBuilderAppend:
4571 case Intrinsics::kStringBuilderToString:
4572 return false;
4573 default:
4574 return HInvoke::CanBeNull();
4575 }
4576 }
4577
Calin Juravle641547a2015-04-21 22:08:51 +01004578 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004579 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004580 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004581 }
4582
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004583 uint32_t GetVTableIndex() const { return vtable_index_; }
4584
4585 DECLARE_INSTRUCTION(InvokeVirtual);
4586
Artem Serovcced8ba2017-07-19 18:18:09 +01004587 protected:
4588 DEFAULT_COPY_CONSTRUCTOR(InvokeVirtual);
4589
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004590 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004591 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004592 const uint32_t vtable_index_;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004593};
4594
Vladimir Markofcb503c2016-05-18 12:48:17 +01004595class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004596 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004597 HInvokeInterface(ArenaAllocator* allocator,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004598 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004599 DataType::Type return_type,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004600 uint32_t dex_pc,
4601 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004602 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004603 uint32_t imt_index)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004604 : HInvoke(allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004605 number_of_arguments,
4606 0u,
4607 return_type,
4608 dex_pc,
4609 dex_method_index,
4610 resolved_method,
4611 kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004612 imt_index_(imt_index) {}
4613
Artem Serovcced8ba2017-07-19 18:18:09 +01004614 bool IsClonable() const OVERRIDE { return true; }
4615
Calin Juravle641547a2015-04-21 22:08:51 +01004616 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004617 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004618 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004619 }
4620
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004621 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
4622 // The assembly stub currently needs it.
4623 return true;
4624 }
4625
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004626 uint32_t GetImtIndex() const { return imt_index_; }
4627 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4628
4629 DECLARE_INSTRUCTION(InvokeInterface);
4630
Artem Serovcced8ba2017-07-19 18:18:09 +01004631 protected:
4632 DEFAULT_COPY_CONSTRUCTOR(InvokeInterface);
4633
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004634 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004635 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004636 const uint32_t imt_index_;
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004637};
4638
Vladimir Markofcb503c2016-05-18 12:48:17 +01004639class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004640 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004641 HNeg(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004642 : HUnaryOperation(result_type, input, dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004643 DCHECK_EQ(result_type, DataType::Kind(input->GetType()));
Roland Levillain937e6cd2016-03-22 11:54:37 +00004644 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004645
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004646 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004647
4648 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004649 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004650 }
4651 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004652 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004653 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004654 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4655 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4656 }
4657 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4658 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4659 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004660
Roland Levillain88cb1752014-10-20 16:36:47 +01004661 DECLARE_INSTRUCTION(Neg);
4662
Artem Serovcced8ba2017-07-19 18:18:09 +01004663 protected:
4664 DEFAULT_COPY_CONSTRUCTOR(Neg);
Roland Levillain88cb1752014-10-20 16:36:47 +01004665};
4666
Vladimir Markofcb503c2016-05-18 12:48:17 +01004667class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004668 public:
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004669 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004670 : HExpression(DataType::Type::kReference, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004671 SetRawInputAt(0, cls);
4672 SetRawInputAt(1, length);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004673 }
4674
Artem Serovcced8ba2017-07-19 18:18:09 +01004675 bool IsClonable() const OVERRIDE { return true; }
4676
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004677 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004678 bool NeedsEnvironment() const OVERRIDE { return true; }
4679
Mingyao Yang0c365e62015-03-31 15:09:29 -07004680 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4681 bool CanThrow() const OVERRIDE { return true; }
4682
Calin Juravle10e244f2015-01-26 18:54:32 +00004683 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004684
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004685 HLoadClass* GetLoadClass() const {
4686 DCHECK(InputAt(0)->IsLoadClass());
4687 return InputAt(0)->AsLoadClass();
4688 }
4689
4690 HInstruction* GetLength() const {
4691 return InputAt(1);
4692 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004693
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004694 DECLARE_INSTRUCTION(NewArray);
4695
Artem Serovcced8ba2017-07-19 18:18:09 +01004696 protected:
4697 DEFAULT_COPY_CONSTRUCTOR(NewArray);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004698};
4699
Vladimir Markofcb503c2016-05-18 12:48:17 +01004700class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004701 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004702 HAdd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004703 HInstruction* left,
4704 HInstruction* right,
4705 uint32_t dex_pc = kNoDexPc)
4706 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004707
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004708 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004709
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004710 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004711
4712 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004713 return GetBlock()->GetGraph()->GetIntConstant(
4714 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004715 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004716 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004717 return GetBlock()->GetGraph()->GetLongConstant(
4718 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004719 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004720 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4721 return GetBlock()->GetGraph()->GetFloatConstant(
4722 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4723 }
4724 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4725 return GetBlock()->GetGraph()->GetDoubleConstant(
4726 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4727 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004728
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004729 DECLARE_INSTRUCTION(Add);
4730
Artem Serovcced8ba2017-07-19 18:18:09 +01004731 protected:
4732 DEFAULT_COPY_CONSTRUCTOR(Add);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004733};
4734
Vladimir Markofcb503c2016-05-18 12:48:17 +01004735class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004736 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004737 HSub(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004738 HInstruction* left,
4739 HInstruction* right,
4740 uint32_t dex_pc = kNoDexPc)
4741 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004742
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004743 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004744
4745 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004746 return GetBlock()->GetGraph()->GetIntConstant(
4747 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004748 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004749 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004750 return GetBlock()->GetGraph()->GetLongConstant(
4751 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004752 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004753 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4754 return GetBlock()->GetGraph()->GetFloatConstant(
4755 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4756 }
4757 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4758 return GetBlock()->GetGraph()->GetDoubleConstant(
4759 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4760 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004761
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004762 DECLARE_INSTRUCTION(Sub);
4763
Artem Serovcced8ba2017-07-19 18:18:09 +01004764 protected:
4765 DEFAULT_COPY_CONSTRUCTOR(Sub);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004766};
4767
Vladimir Markofcb503c2016-05-18 12:48:17 +01004768class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004769 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004770 HMul(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004771 HInstruction* left,
4772 HInstruction* right,
4773 uint32_t dex_pc = kNoDexPc)
4774 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004775
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004776 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004777
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004778 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004779
4780 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004781 return GetBlock()->GetGraph()->GetIntConstant(
4782 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004783 }
4784 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004785 return GetBlock()->GetGraph()->GetLongConstant(
4786 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004787 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004788 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4789 return GetBlock()->GetGraph()->GetFloatConstant(
4790 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4791 }
4792 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4793 return GetBlock()->GetGraph()->GetDoubleConstant(
4794 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4795 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004796
4797 DECLARE_INSTRUCTION(Mul);
4798
Artem Serovcced8ba2017-07-19 18:18:09 +01004799 protected:
4800 DEFAULT_COPY_CONSTRUCTOR(Mul);
Calin Juravle34bacdf2014-10-07 20:23:36 +01004801};
4802
Vladimir Markofcb503c2016-05-18 12:48:17 +01004803class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004804 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004805 HDiv(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004806 HInstruction* left,
4807 HInstruction* right,
4808 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004809 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004810
Roland Levillain9867bc72015-08-05 10:21:34 +01004811 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004812 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004813 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004814 // Our graph structure ensures we never have 0 for `y` during
4815 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004816 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004817 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004818 return (y == -1) ? -x : x / y;
4819 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004820
Roland Levillain31dd3d62016-02-16 12:21:02 +00004821 template <typename T>
4822 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004823 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004824 return x / y;
4825 }
4826
Roland Levillain9867bc72015-08-05 10:21:34 +01004827 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004828 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004829 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004830 }
4831 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004832 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004833 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4834 }
4835 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4836 return GetBlock()->GetGraph()->GetFloatConstant(
4837 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4838 }
4839 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4840 return GetBlock()->GetGraph()->GetDoubleConstant(
4841 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004842 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004843
4844 DECLARE_INSTRUCTION(Div);
4845
Artem Serovcced8ba2017-07-19 18:18:09 +01004846 protected:
4847 DEFAULT_COPY_CONSTRUCTOR(Div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00004848};
4849
Vladimir Markofcb503c2016-05-18 12:48:17 +01004850class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004851 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004852 HRem(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004853 HInstruction* left,
4854 HInstruction* right,
4855 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004856 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004857
Roland Levillain9867bc72015-08-05 10:21:34 +01004858 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004859 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004860 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004861 // Our graph structure ensures we never have 0 for `y` during
4862 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004863 DCHECK_NE(y, 0);
4864 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4865 return (y == -1) ? 0 : x % y;
4866 }
4867
Roland Levillain31dd3d62016-02-16 12:21:02 +00004868 template <typename T>
4869 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004870 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004871 return std::fmod(x, y);
4872 }
4873
Roland Levillain9867bc72015-08-05 10:21:34 +01004874 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004875 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004876 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004877 }
4878 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004879 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004880 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004881 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004882 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4883 return GetBlock()->GetGraph()->GetFloatConstant(
4884 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4885 }
4886 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4887 return GetBlock()->GetGraph()->GetDoubleConstant(
4888 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4889 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004890
4891 DECLARE_INSTRUCTION(Rem);
4892
Artem Serovcced8ba2017-07-19 18:18:09 +01004893 protected:
4894 DEFAULT_COPY_CONSTRUCTOR(Rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00004895};
4896
Vladimir Markofcb503c2016-05-18 12:48:17 +01004897class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00004898 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004899 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4900 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004901 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004902 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004903 SetRawInputAt(0, value);
4904 }
4905
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004906 DataType::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004907
Calin Juravled0d48522014-11-04 16:40:20 +00004908 bool CanBeMoved() const OVERRIDE { return true; }
4909
Vladimir Marko372f10e2016-05-17 16:30:10 +01004910 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004911 return true;
4912 }
4913
4914 bool NeedsEnvironment() const OVERRIDE { return true; }
4915 bool CanThrow() const OVERRIDE { return true; }
4916
Calin Juravled0d48522014-11-04 16:40:20 +00004917 DECLARE_INSTRUCTION(DivZeroCheck);
4918
Artem Serovcced8ba2017-07-19 18:18:09 +01004919 protected:
4920 DEFAULT_COPY_CONSTRUCTOR(DivZeroCheck);
Calin Juravled0d48522014-11-04 16:40:20 +00004921};
4922
Vladimir Markofcb503c2016-05-18 12:48:17 +01004923class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004924 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004925 HShl(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004926 HInstruction* value,
4927 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004928 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004929 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004930 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
4931 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004932 }
4933
Roland Levillain5b5b9312016-03-22 14:57:31 +00004934 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004935 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004936 return value << (distance & max_shift_distance);
4937 }
4938
4939 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004940 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004941 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004942 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004943 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004944 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004945 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004946 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004947 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4948 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4949 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4950 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004951 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004952 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4953 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004954 LOG(FATAL) << DebugName() << " is not defined for float values";
4955 UNREACHABLE();
4956 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004957 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4958 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004959 LOG(FATAL) << DebugName() << " is not defined for double values";
4960 UNREACHABLE();
4961 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004962
4963 DECLARE_INSTRUCTION(Shl);
4964
Artem Serovcced8ba2017-07-19 18:18:09 +01004965 protected:
4966 DEFAULT_COPY_CONSTRUCTOR(Shl);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004967};
4968
Vladimir Markofcb503c2016-05-18 12:48:17 +01004969class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004970 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004971 HShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004972 HInstruction* value,
4973 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004974 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004975 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004976 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
4977 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004978 }
4979
Roland Levillain5b5b9312016-03-22 14:57:31 +00004980 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004981 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004982 return value >> (distance & max_shift_distance);
4983 }
4984
4985 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004986 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004987 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004988 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004989 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004990 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004991 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004992 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004993 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4994 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4995 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4996 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004997 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004998 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4999 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005000 LOG(FATAL) << DebugName() << " is not defined for float values";
5001 UNREACHABLE();
5002 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005003 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5004 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005005 LOG(FATAL) << DebugName() << " is not defined for double values";
5006 UNREACHABLE();
5007 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005008
5009 DECLARE_INSTRUCTION(Shr);
5010
Artem Serovcced8ba2017-07-19 18:18:09 +01005011 protected:
5012 DEFAULT_COPY_CONSTRUCTOR(Shr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005013};
5014
Vladimir Markofcb503c2016-05-18 12:48:17 +01005015class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005016 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005017 HUShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005018 HInstruction* value,
5019 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005020 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00005021 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005022 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5023 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00005024 }
5025
Roland Levillain5b5b9312016-03-22 14:57:31 +00005026 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005027 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005028 typedef typename std::make_unsigned<T>::type V;
5029 V ux = static_cast<V>(value);
5030 return static_cast<T>(ux >> (distance & max_shift_distance));
5031 }
5032
5033 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005034 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005035 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005036 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005037 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005038 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005039 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005040 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005041 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5042 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5043 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5044 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00005045 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005046 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5047 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005048 LOG(FATAL) << DebugName() << " is not defined for float values";
5049 UNREACHABLE();
5050 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005051 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5052 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005053 LOG(FATAL) << DebugName() << " is not defined for double values";
5054 UNREACHABLE();
5055 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005056
5057 DECLARE_INSTRUCTION(UShr);
5058
Artem Serovcced8ba2017-07-19 18:18:09 +01005059 protected:
5060 DEFAULT_COPY_CONSTRUCTOR(UShr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005061};
5062
Vladimir Markofcb503c2016-05-18 12:48:17 +01005063class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005064 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005065 HAnd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005066 HInstruction* left,
5067 HInstruction* right,
5068 uint32_t dex_pc = kNoDexPc)
5069 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005070
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005071 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005072
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005073 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005074
5075 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005076 return GetBlock()->GetGraph()->GetIntConstant(
5077 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005078 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005079 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005080 return GetBlock()->GetGraph()->GetLongConstant(
5081 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005082 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005083 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5084 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5085 LOG(FATAL) << DebugName() << " is not defined for float values";
5086 UNREACHABLE();
5087 }
5088 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5089 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5090 LOG(FATAL) << DebugName() << " is not defined for double values";
5091 UNREACHABLE();
5092 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005093
5094 DECLARE_INSTRUCTION(And);
5095
Artem Serovcced8ba2017-07-19 18:18:09 +01005096 protected:
5097 DEFAULT_COPY_CONSTRUCTOR(And);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005098};
5099
Vladimir Markofcb503c2016-05-18 12:48:17 +01005100class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005101 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005102 HOr(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005103 HInstruction* left,
5104 HInstruction* right,
5105 uint32_t dex_pc = kNoDexPc)
5106 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005107
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005108 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005109
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005110 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005111
5112 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005113 return GetBlock()->GetGraph()->GetIntConstant(
5114 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005115 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005116 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005117 return GetBlock()->GetGraph()->GetLongConstant(
5118 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005119 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005120 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5121 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5122 LOG(FATAL) << DebugName() << " is not defined for float values";
5123 UNREACHABLE();
5124 }
5125 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5126 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5127 LOG(FATAL) << DebugName() << " is not defined for double values";
5128 UNREACHABLE();
5129 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005130
5131 DECLARE_INSTRUCTION(Or);
5132
Artem Serovcced8ba2017-07-19 18:18:09 +01005133 protected:
5134 DEFAULT_COPY_CONSTRUCTOR(Or);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005135};
5136
Vladimir Markofcb503c2016-05-18 12:48:17 +01005137class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005138 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005139 HXor(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005140 HInstruction* left,
5141 HInstruction* right,
5142 uint32_t dex_pc = kNoDexPc)
5143 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005144
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005145 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005146
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005147 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005148
5149 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005150 return GetBlock()->GetGraph()->GetIntConstant(
5151 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005152 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005153 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005154 return GetBlock()->GetGraph()->GetLongConstant(
5155 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005156 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005157 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5158 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5159 LOG(FATAL) << DebugName() << " is not defined for float values";
5160 UNREACHABLE();
5161 }
5162 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5163 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5164 LOG(FATAL) << DebugName() << " is not defined for double values";
5165 UNREACHABLE();
5166 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005167
5168 DECLARE_INSTRUCTION(Xor);
5169
Artem Serovcced8ba2017-07-19 18:18:09 +01005170 protected:
5171 DEFAULT_COPY_CONSTRUCTOR(Xor);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005172};
5173
Vladimir Markofcb503c2016-05-18 12:48:17 +01005174class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005175 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005176 HRor(DataType::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00005177 : HBinaryOperation(result_type, value, distance) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005178 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5179 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00005180 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005181
Roland Levillain5b5b9312016-03-22 14:57:31 +00005182 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005183 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005184 typedef typename std::make_unsigned<T>::type V;
5185 V ux = static_cast<V>(value);
5186 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005187 return static_cast<T>(ux);
5188 } else {
5189 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005190 return static_cast<T>(ux >> (distance & max_shift_value)) |
5191 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005192 }
5193 }
5194
Roland Levillain5b5b9312016-03-22 14:57:31 +00005195 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005196 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005197 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005198 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005199 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005200 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005201 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005202 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005203 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5204 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5205 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5206 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005207 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005208 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5209 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005210 LOG(FATAL) << DebugName() << " is not defined for float values";
5211 UNREACHABLE();
5212 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005213 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5214 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005215 LOG(FATAL) << DebugName() << " is not defined for double values";
5216 UNREACHABLE();
5217 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005218
5219 DECLARE_INSTRUCTION(Ror);
5220
Artem Serovcced8ba2017-07-19 18:18:09 +01005221 protected:
5222 DEFAULT_COPY_CONSTRUCTOR(Ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005223};
5224
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005225// The value of a parameter in this method. Its location depends on
5226// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01005227class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005228 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005229 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005230 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005231 uint8_t index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005232 DataType::Type parameter_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005233 bool is_this = false)
5234 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005235 dex_file_(dex_file),
5236 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005237 index_(index) {
5238 SetPackedFlag<kFlagIsThis>(is_this);
5239 SetPackedFlag<kFlagCanBeNull>(!is_this);
5240 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005241
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005242 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005243 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005244 uint8_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07005245 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005246
Vladimir Markoa1de9182016-02-25 11:37:38 +00005247 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
5248 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005249
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005250 DECLARE_INSTRUCTION(ParameterValue);
5251
Artem Serovcced8ba2017-07-19 18:18:09 +01005252 protected:
5253 DEFAULT_COPY_CONSTRUCTOR(ParameterValue);
5254
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005255 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005256 // Whether or not the parameter value corresponds to 'this' argument.
5257 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
5258 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5259 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5260 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5261 "Too many packed fields.");
5262
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005263 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005264 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005265 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005266 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005267 const uint8_t index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005268};
5269
Vladimir Markofcb503c2016-05-18 12:48:17 +01005270class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005271 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005272 HNot(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005273 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005274
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005275 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005276 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005277 return true;
5278 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005279
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005280 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005281
5282 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005283 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005284 }
5285 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005286 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005287 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005288 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5289 LOG(FATAL) << DebugName() << " is not defined for float values";
5290 UNREACHABLE();
5291 }
5292 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5293 LOG(FATAL) << DebugName() << " is not defined for double values";
5294 UNREACHABLE();
5295 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005296
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005297 DECLARE_INSTRUCTION(Not);
5298
Artem Serovcced8ba2017-07-19 18:18:09 +01005299 protected:
5300 DEFAULT_COPY_CONSTRUCTOR(Not);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005301};
5302
Vladimir Markofcb503c2016-05-18 12:48:17 +01005303class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005304 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005305 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005306 : HUnaryOperation(DataType::Type::kBool, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01005307
5308 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005309 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01005310 return true;
5311 }
5312
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005313 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005314 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005315 return !x;
5316 }
5317
Roland Levillain9867bc72015-08-05 10:21:34 +01005318 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005319 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005320 }
5321 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5322 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005323 UNREACHABLE();
5324 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005325 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5326 LOG(FATAL) << DebugName() << " is not defined for float values";
5327 UNREACHABLE();
5328 }
5329 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5330 LOG(FATAL) << DebugName() << " is not defined for double values";
5331 UNREACHABLE();
5332 }
David Brazdil66d126e2015-04-03 16:02:44 +01005333
5334 DECLARE_INSTRUCTION(BooleanNot);
5335
Artem Serovcced8ba2017-07-19 18:18:09 +01005336 protected:
5337 DEFAULT_COPY_CONSTRUCTOR(BooleanNot);
David Brazdil66d126e2015-04-03 16:02:44 +01005338};
5339
Vladimir Markofcb503c2016-05-18 12:48:17 +01005340class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005341 public:
5342 // Instantiate a type conversion of `input` to `result_type`.
Vladimir Markoc8fb2112017-10-03 11:37:52 +01005343 HTypeConversion(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005344 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005345 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005346 // Invariant: We should never generate a conversion to a Boolean value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005347 DCHECK_NE(DataType::Type::kBool, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005348 }
5349
5350 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005351 DataType::Type GetInputType() const { return GetInput()->GetType(); }
5352 DataType::Type GetResultType() const { return GetType(); }
Roland Levillaindff1f282014-11-05 14:15:05 +00005353
5354 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005355 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5356 return true;
5357 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005358
Mark Mendelle82549b2015-05-06 10:55:34 -04005359 // Try to statically evaluate the conversion and return a HConstant
5360 // containing the result. If the input cannot be converted, return nullptr.
5361 HConstant* TryStaticEvaluation() const;
5362
Roland Levillaindff1f282014-11-05 14:15:05 +00005363 DECLARE_INSTRUCTION(TypeConversion);
5364
Artem Serovcced8ba2017-07-19 18:18:09 +01005365 protected:
5366 DEFAULT_COPY_CONSTRUCTOR(TypeConversion);
Roland Levillaindff1f282014-11-05 14:15:05 +00005367};
5368
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005369static constexpr uint32_t kNoRegNumber = -1;
5370
Vladimir Markofcb503c2016-05-18 12:48:17 +01005371class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005372 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005373 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
5374 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005375 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005376 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005377 SetRawInputAt(0, value);
5378 }
5379
Artem Serovcced8ba2017-07-19 18:18:09 +01005380 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00005381 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005382 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005383 return true;
5384 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005385
Calin Juravle10e244f2015-01-26 18:54:32 +00005386 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005387
Calin Juravle10e244f2015-01-26 18:54:32 +00005388 bool CanThrow() const OVERRIDE { return true; }
5389
5390 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005391
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005392 DECLARE_INSTRUCTION(NullCheck);
5393
Artem Serovcced8ba2017-07-19 18:18:09 +01005394 protected:
5395 DEFAULT_COPY_CONSTRUCTOR(NullCheck);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005396};
5397
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005398// Embeds an ArtField and all the information required by the compiler. We cache
5399// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005400class FieldInfo : public ValueObject {
5401 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005402 FieldInfo(ArtField* field,
5403 MemberOffset field_offset,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005404 DataType::Type field_type,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005405 bool is_volatile,
5406 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005407 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005408 const DexFile& dex_file)
5409 : field_(field),
5410 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005411 field_type_(field_type),
5412 is_volatile_(is_volatile),
5413 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005414 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005415 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005416
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005417 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005418 MemberOffset GetFieldOffset() const { return field_offset_; }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005419 DataType::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005420 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005421 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005422 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005423 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005424
5425 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005426 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005427 const MemberOffset field_offset_;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005428 const DataType::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005429 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005430 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005431 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005432 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005433};
5434
Vladimir Markofcb503c2016-05-18 12:48:17 +01005435class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005436 public:
5437 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005438 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005439 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005440 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005441 bool is_volatile,
5442 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005443 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005444 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005445 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005446 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005447 field_info_(field,
5448 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005449 field_type,
5450 is_volatile,
5451 field_idx,
5452 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005453 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005454 SetRawInputAt(0, value);
5455 }
5456
Artem Serovcced8ba2017-07-19 18:18:09 +01005457 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005458 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005459
Vladimir Marko372f10e2016-05-17 16:30:10 +01005460 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5461 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005462 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005463 }
5464
Calin Juravle641547a2015-04-21 22:08:51 +01005465 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005466 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005467 }
5468
5469 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005470 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5471 }
5472
Calin Juravle52c48962014-12-16 17:02:57 +00005473 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005474 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005475 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005476 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005477
Vladimir Marko61b92282017-10-11 13:23:17 +01005478 void SetType(DataType::Type new_type) {
5479 DCHECK(DataType::IsIntegralType(GetType()));
5480 DCHECK(DataType::IsIntegralType(new_type));
5481 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5482 SetPackedField<TypeField>(new_type);
5483 }
5484
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005485 DECLARE_INSTRUCTION(InstanceFieldGet);
5486
Artem Serovcced8ba2017-07-19 18:18:09 +01005487 protected:
5488 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldGet);
5489
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005490 private:
5491 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005492};
5493
Vladimir Markofcb503c2016-05-18 12:48:17 +01005494class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005495 public:
5496 HInstanceFieldSet(HInstruction* object,
5497 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005498 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005499 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005500 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005501 bool is_volatile,
5502 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005503 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005504 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005505 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005506 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005507 field_info_(field,
5508 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005509 field_type,
5510 is_volatile,
5511 field_idx,
5512 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005513 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005514 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005515 SetRawInputAt(0, object);
5516 SetRawInputAt(1, value);
5517 }
5518
Artem Serovcced8ba2017-07-19 18:18:09 +01005519 bool IsClonable() const OVERRIDE { return true; }
5520
Calin Juravle641547a2015-04-21 22:08:51 +01005521 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005522 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005523 }
5524
Calin Juravle52c48962014-12-16 17:02:57 +00005525 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005526 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005527 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005528 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005529 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005530 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5531 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005532
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005533 DECLARE_INSTRUCTION(InstanceFieldSet);
5534
Artem Serovcced8ba2017-07-19 18:18:09 +01005535 protected:
5536 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldSet);
5537
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005538 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005539 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5540 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5541 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5542 "Too many packed fields.");
5543
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005544 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005545};
5546
Vladimir Markofcb503c2016-05-18 12:48:17 +01005547class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005548 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005549 HArrayGet(HInstruction* array,
5550 HInstruction* index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005551 DataType::Type type,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005552 uint32_t dex_pc)
5553 : HArrayGet(array,
5554 index,
5555 type,
5556 SideEffects::ArrayReadOfType(type),
5557 dex_pc,
5558 /* is_string_char_at */ false) {}
5559
5560 HArrayGet(HInstruction* array,
5561 HInstruction* index,
5562 DataType::Type type,
5563 SideEffects side_effects,
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005564 uint32_t dex_pc,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005565 bool is_string_char_at)
5566 : HExpression(type, side_effects, dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005567 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005568 SetRawInputAt(0, array);
5569 SetRawInputAt(1, index);
5570 }
5571
Artem Serovcced8ba2017-07-19 18:18:09 +01005572 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005573 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005574 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005575 return true;
5576 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005577 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005578 // TODO: We can be smarter here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01005579 // Currently, unless the array is the result of NewArray, the array access is always
5580 // preceded by some form of null NullCheck necessary for the bounds check, usually
5581 // implicit null check on the ArrayLength input to BoundsCheck or Deoptimize for
5582 // dynamic BCE. There are cases when these could be removed to produce better code.
5583 // If we ever add optimizations to do so we should allow an implicit check here
5584 // (as long as the address falls in the first page).
5585 //
5586 // As an example of such fancy optimization, we could eliminate BoundsCheck for
5587 // a = cond ? new int[1] : null;
5588 // a[0]; // The Phi does not need bounds check for either input.
Calin Juravle77520bc2015-01-12 18:45:46 +00005589 return false;
5590 }
5591
David Brazdil4833f5a2015-12-16 10:37:39 +00005592 bool IsEquivalentOf(HArrayGet* other) const {
5593 bool result = (GetDexPc() == other->GetDexPc());
5594 if (kIsDebugBuild && result) {
5595 DCHECK_EQ(GetBlock(), other->GetBlock());
5596 DCHECK_EQ(GetArray(), other->GetArray());
5597 DCHECK_EQ(GetIndex(), other->GetIndex());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005598 if (DataType::IsIntOrLongType(GetType())) {
5599 DCHECK(DataType::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005600 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005601 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
5602 DCHECK(DataType::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005603 }
5604 }
5605 return result;
5606 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005607
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005608 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5609
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005610 HInstruction* GetArray() const { return InputAt(0); }
5611 HInstruction* GetIndex() const { return InputAt(1); }
5612
Vladimir Marko61b92282017-10-11 13:23:17 +01005613 void SetType(DataType::Type new_type) {
5614 DCHECK(DataType::IsIntegralType(GetType()));
5615 DCHECK(DataType::IsIntegralType(new_type));
5616 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5617 SetPackedField<TypeField>(new_type);
5618 }
5619
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005620 DECLARE_INSTRUCTION(ArrayGet);
5621
Artem Serovcced8ba2017-07-19 18:18:09 +01005622 protected:
5623 DEFAULT_COPY_CONSTRUCTOR(ArrayGet);
5624
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005625 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005626 // We treat a String as an array, creating the HArrayGet from String.charAt()
5627 // intrinsic in the instruction simplifier. We can always determine whether
5628 // a particular HArrayGet is actually a String.charAt() by looking at the type
5629 // of the input but that requires holding the mutator lock, so we prefer to use
5630 // a flag, so that code generators don't need to do the locking.
5631 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
5632 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5633 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5634 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005635};
5636
Vladimir Markofcb503c2016-05-18 12:48:17 +01005637class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005638 public:
5639 HArraySet(HInstruction* array,
5640 HInstruction* index,
5641 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005642 DataType::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005643 uint32_t dex_pc)
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005644 : HArraySet(array,
5645 index,
5646 value,
5647 expected_component_type,
5648 // Make a best guess for side effects now, may be refined during SSA building.
5649 ComputeSideEffects(GetComponentType(value->GetType(), expected_component_type)),
5650 dex_pc) {}
5651
5652 HArraySet(HInstruction* array,
5653 HInstruction* index,
5654 HInstruction* value,
5655 DataType::Type expected_component_type,
5656 SideEffects side_effects,
5657 uint32_t dex_pc)
5658 : HTemplateInstruction(side_effects, dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005659 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005660 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005661 SetPackedFlag<kFlagValueCanBeNull>(true);
5662 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005663 SetRawInputAt(0, array);
5664 SetRawInputAt(1, index);
5665 SetRawInputAt(2, value);
5666 }
5667
Artem Serovcced8ba2017-07-19 18:18:09 +01005668 bool IsClonable() const OVERRIDE { return true; }
5669
Calin Juravle77520bc2015-01-12 18:45:46 +00005670 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005671 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005672 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005673 }
5674
Mingyao Yang81014cb2015-06-02 03:16:27 -07005675 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005676 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005677
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005678 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005679 // TODO: Same as for ArrayGet.
5680 return false;
5681 }
5682
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005683 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005684 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005685 }
5686
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005687 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005688 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005689 }
5690
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005691 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005692 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005693 }
5694
Vladimir Markoa1de9182016-02-25 11:37:38 +00005695 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5696 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5697 bool StaticTypeOfArrayIsObjectArray() const {
5698 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5699 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005700
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005701 HInstruction* GetArray() const { return InputAt(0); }
5702 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005703 HInstruction* GetValue() const { return InputAt(2); }
5704
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005705 DataType::Type GetComponentType() const {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005706 return GetComponentType(GetValue()->GetType(), GetRawExpectedComponentType());
5707 }
5708
5709 static DataType::Type GetComponentType(DataType::Type value_type,
5710 DataType::Type expected_component_type) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005711 // The Dex format does not type floating point index operations. Since the
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005712 // `expected_component_type` comes from SSA building and can therefore not
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005713 // be correct, we also check what is the value type. If it is a floating
5714 // point type, we must use that type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005715 return ((value_type == DataType::Type::kFloat32) || (value_type == DataType::Type::kFloat64))
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005716 ? value_type
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005717 : expected_component_type;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005718 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005719
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005720 DataType::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005721 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005722 }
5723
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005724 static SideEffects ComputeSideEffects(DataType::Type type) {
5725 return SideEffects::ArrayWriteOfType(type).Union(SideEffectsForArchRuntimeCalls(type));
Aart Bik18b36ab2016-04-13 16:41:35 -07005726 }
5727
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005728 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type value_type) {
5729 return (value_type == DataType::Type::kReference) ? SideEffects::CanTriggerGC()
5730 : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005731 }
5732
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005733 DECLARE_INSTRUCTION(ArraySet);
5734
Artem Serovcced8ba2017-07-19 18:18:09 +01005735 protected:
5736 DEFAULT_COPY_CONSTRUCTOR(ArraySet);
5737
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005738 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005739 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5740 static constexpr size_t kFieldExpectedComponentTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005741 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00005742 static constexpr size_t kFlagNeedsTypeCheck =
5743 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5744 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005745 // Cached information for the reference_type_info_ so that codegen
5746 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005747 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5748 static constexpr size_t kNumberOfArraySetPackedBits =
5749 kFlagStaticTypeOfArrayIsObjectArray + 1;
5750 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5751 using ExpectedComponentTypeField =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005752 BitField<DataType::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005753};
5754
Vladimir Markofcb503c2016-05-18 12:48:17 +01005755class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005756 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005757 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005758 : HExpression(DataType::Type::kInt32, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005759 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005760 // Note that arrays do not change length, so the instruction does not
5761 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005762 SetRawInputAt(0, array);
5763 }
5764
Artem Serovcced8ba2017-07-19 18:18:09 +01005765 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle77520bc2015-01-12 18:45:46 +00005766 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005767 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005768 return true;
5769 }
Calin Juravle641547a2015-04-21 22:08:51 +01005770 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5771 return obj == InputAt(0);
5772 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005773
Vladimir Markodce016e2016-04-28 13:10:02 +01005774 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5775
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005776 DECLARE_INSTRUCTION(ArrayLength);
5777
Artem Serovcced8ba2017-07-19 18:18:09 +01005778 protected:
5779 DEFAULT_COPY_CONSTRUCTOR(ArrayLength);
5780
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005781 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005782 // We treat a String as an array, creating the HArrayLength from String.length()
5783 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5784 // determine whether a particular HArrayLength is actually a String.length() by
5785 // looking at the type of the input but that requires holding the mutator lock, so
5786 // we prefer to use a flag, so that code generators don't need to do the locking.
5787 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5788 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5789 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5790 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005791};
5792
Vladimir Markofcb503c2016-05-18 12:48:17 +01005793class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005794 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005795 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5796 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005797 HBoundsCheck(HInstruction* index,
5798 HInstruction* length,
5799 uint32_t dex_pc,
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005800 bool string_char_at = false)
5801 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005802 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(index->GetType()));
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005803 SetPackedFlag<kFlagIsStringCharAt>(string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005804 SetRawInputAt(0, index);
5805 SetRawInputAt(1, length);
5806 }
5807
Artem Serovcced8ba2017-07-19 18:18:09 +01005808 bool IsClonable() const OVERRIDE { return true; }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005809 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005810 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005811 return true;
5812 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005813
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005814 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005815
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005816 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005817
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005818 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005819
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005820 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005821
5822 DECLARE_INSTRUCTION(BoundsCheck);
5823
Artem Serovcced8ba2017-07-19 18:18:09 +01005824 protected:
5825 DEFAULT_COPY_CONSTRUCTOR(BoundsCheck);
5826
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005827 private:
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005828 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005829};
5830
Vladimir Markofcb503c2016-05-18 12:48:17 +01005831class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005832 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005833 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005834 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005835
Artem Serovcced8ba2017-07-19 18:18:09 +01005836 bool IsClonable() const OVERRIDE { return true; }
5837
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005838 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005839 return true;
5840 }
5841
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005842 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5843 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005844
5845 DECLARE_INSTRUCTION(SuspendCheck);
5846
Artem Serovcced8ba2017-07-19 18:18:09 +01005847 protected:
5848 DEFAULT_COPY_CONSTRUCTOR(SuspendCheck);
5849
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005850 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005851 // Only used for code generation, in order to share the same slow path between back edges
5852 // of a same loop.
5853 SlowPathCode* slow_path_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005854};
5855
David Srbecky0cf44932015-12-09 14:09:59 +00005856// Pseudo-instruction which provides the native debugger with mapping information.
5857// It ensures that we can generate line number and local variables at this point.
5858class HNativeDebugInfo : public HTemplateInstruction<0> {
5859 public:
5860 explicit HNativeDebugInfo(uint32_t dex_pc)
5861 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5862
5863 bool NeedsEnvironment() const OVERRIDE {
5864 return true;
5865 }
5866
5867 DECLARE_INSTRUCTION(NativeDebugInfo);
5868
Artem Serovcced8ba2017-07-19 18:18:09 +01005869 protected:
5870 DEFAULT_COPY_CONSTRUCTOR(NativeDebugInfo);
David Srbecky0cf44932015-12-09 14:09:59 +00005871};
5872
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005873/**
5874 * Instruction to load a Class object.
5875 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005876class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005877 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005878 // Determines how to load the Class.
5879 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005880 // We cannot load this class. See HSharpening::SharpenLoadClass.
5881 kInvalid = -1,
5882
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005883 // Use the Class* from the method's own ArtMethod*.
5884 kReferrersClass,
5885
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005886 // Use PC-relative boot image Class* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01005887 // Used for boot image classes referenced by boot image code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005888 kBootImageLinkTimePcRelative,
5889
5890 // Use a known boot image Class* address, embedded in the code by the codegen.
5891 // Used for boot image classes referenced by apps in AOT- and JIT-compiled code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005892 kBootImageAddress,
5893
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005894 // Use a PC-relative load from a boot image ClassTable mmapped into the .bss
5895 // of the oat file.
5896 kBootImageClassTable,
5897
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005898 // Load from an entry in the .bss section using a PC-relative load.
5899 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
5900 kBssEntry,
5901
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005902 // Load from the root table associated with the JIT compiled method.
5903 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005904
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005905 // Load using a simple runtime call. This is the fall-back load kind when
5906 // the codegen is unable to use another appropriate kind.
5907 kRuntimeCall,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005908
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005909 kLast = kRuntimeCall
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005910 };
5911
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005912 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005913 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005914 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005915 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005916 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005917 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00005918 bool needs_access_check)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005919 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5920 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005921 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005922 dex_file_(dex_file),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005923 klass_(klass),
Calin Juravle2e768302015-07-28 14:41:11 +00005924 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005925 // Referrers class should not need access check. We never inline unverified
5926 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005927 DCHECK(!is_referrers_class || !needs_access_check);
5928
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005929 SetPackedField<LoadKindField>(
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005930 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kRuntimeCall);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005931 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00005932 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005933 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005934 }
5935
Artem Serovcced8ba2017-07-19 18:18:09 +01005936 bool IsClonable() const OVERRIDE { return true; }
5937
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005938 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005939
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005940 LoadKind GetLoadKind() const {
5941 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005942 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005943
5944 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005945
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005946 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005947
Andreas Gampea5b09a62016-11-17 15:21:22 -08005948 size_t ComputeHashCode() const OVERRIDE { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005949
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005950 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005951
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005952 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005953 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005954 }
5955
Calin Juravle0ba218d2015-05-19 18:46:01 +01005956 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005957 // The entrypoint the code generator is going to call does not do
5958 // clinit of the class.
5959 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005960 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005961 }
5962
5963 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00005964 return NeedsAccessCheck() ||
5965 MustGenerateClinitCheck() ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005966 GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005967 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01005968 }
5969
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005970 bool CanThrow() const OVERRIDE {
Vladimir Marko41559982017-01-06 14:04:23 +00005971 return NeedsAccessCheck() ||
5972 MustGenerateClinitCheck() ||
5973 // If the class is in the boot image, the lookup in the runtime call cannot throw.
5974 // This keeps CanThrow() consistent between non-PIC (using kBootImageAddress) and
5975 // PIC and subsequently avoids a DCE behavior dependency on the PIC option.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005976 ((GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005977 GetLoadKind() == LoadKind::kBssEntry) &&
5978 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005979 }
5980
Calin Juravleacf735c2015-02-12 15:25:22 +00005981 ReferenceTypeInfo GetLoadedClassRTI() {
5982 return loaded_class_rti_;
5983 }
5984
5985 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5986 // Make sure we only set exact types (the loaded class should never be merged).
5987 DCHECK(rti.IsExact());
5988 loaded_class_rti_ = rti;
5989 }
5990
Andreas Gampea5b09a62016-11-17 15:21:22 -08005991 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005992 const DexFile& GetDexFile() const { return dex_file_; }
5993
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005994 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005995 return GetLoadKind() == LoadKind::kRuntimeCall;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005996 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005997
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005998 static SideEffects SideEffectsForArchRuntimeCalls() {
5999 return SideEffects::CanTriggerGC();
6000 }
6001
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006002 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006003 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006004 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006005 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006006
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006007 void MarkInBootImage() {
6008 SetPackedFlag<kFlagIsInBootImage>(true);
6009 }
6010
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006011 void AddSpecialInput(HInstruction* special_input);
6012
6013 using HInstruction::GetInputRecords; // Keep the const version visible.
6014 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
6015 return ArrayRef<HUserRecord<HInstruction*>>(
6016 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6017 }
6018
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006019 DataType::Type GetType() const OVERRIDE {
6020 return DataType::Type::kReference;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006021 }
6022
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006023 Handle<mirror::Class> GetClass() const {
6024 return klass_;
6025 }
6026
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006027 DECLARE_INSTRUCTION(LoadClass);
6028
Artem Serovcced8ba2017-07-19 18:18:09 +01006029 protected:
6030 DEFAULT_COPY_CONSTRUCTOR(LoadClass);
6031
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006032 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006033 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00006034 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006035 // Whether this instruction must generate the initialization check.
6036 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006037 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006038 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
6039 static constexpr size_t kFieldLoadKindSize =
6040 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
6041 static constexpr size_t kNumberOfLoadClassPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006042 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006043 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
6044
6045 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006046 return load_kind == LoadKind::kReferrersClass ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006047 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006048 load_kind == LoadKind::kBootImageClassTable ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006049 load_kind == LoadKind::kBssEntry ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006050 load_kind == LoadKind::kRuntimeCall;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006051 }
6052
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006053 void SetLoadKindInternal(LoadKind load_kind);
6054
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006055 // The special input is the HCurrentMethod for kRuntimeCall or kReferrersClass.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006056 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006057 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006058 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006059
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006060 // A type index and dex file where the class can be accessed. The dex file can be:
6061 // - The compiling method's dex file if the class is defined there too.
6062 // - The compiling method's dex file if the class is referenced there.
6063 // - The dex file where the class is defined. When the load kind can only be
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006064 // kBssEntry or kRuntimeCall, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08006065 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006066 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006067
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006068 Handle<mirror::Class> klass_;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006069
Calin Juravleacf735c2015-02-12 15:25:22 +00006070 ReferenceTypeInfo loaded_class_rti_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006071};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006072std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
6073
6074// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006075inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006076 // The special input is used for PC-relative loads on some architectures,
6077 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006078 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006079 GetLoadKind() == LoadKind::kBootImageAddress ||
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006080 GetLoadKind() == LoadKind::kBootImageClassTable ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006081 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006082 DCHECK(special_input_.GetInstruction() == nullptr);
6083 special_input_ = HUserRecord<HInstruction*>(special_input);
6084 special_input->AddUseAt(this, 0);
6085}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006086
Vladimir Marko372f10e2016-05-17 16:30:10 +01006087class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006088 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006089 // Determines how to load the String.
6090 enum class LoadKind {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006091 // Use PC-relative boot image String* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006092 // Used for boot image strings referenced by boot image code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006093 kBootImageLinkTimePcRelative,
6094
6095 // Use a known boot image String* address, embedded in the code by the codegen.
6096 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006097 kBootImageAddress,
6098
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006099 // Use a PC-relative load from a boot image InternTable mmapped into the .bss
6100 // of the oat file.
6101 kBootImageInternTable,
6102
Vladimir Markoaad75c62016-10-03 08:46:48 +00006103 // Load from an entry in the .bss section using a PC-relative load.
6104 // Used for strings outside boot image when .bss is accessible with a PC-relative load.
6105 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006106
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006107 // Load from the root table associated with the JIT compiled method.
6108 kJitTableAddress,
6109
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006110 // Load using a simple runtime call. This is the fall-back load kind when
6111 // the codegen is unable to use another appropriate kind.
6112 kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006113
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006114 kLast = kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006115 };
6116
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006117 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006118 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006119 const DexFile& dex_file,
6120 uint32_t dex_pc)
Vladimir Marko372f10e2016-05-17 16:30:10 +01006121 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
6122 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006123 string_index_(string_index),
6124 dex_file_(dex_file) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006125 SetPackedField<LoadKindField>(LoadKind::kRuntimeCall);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006126 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006127
Artem Serovcced8ba2017-07-19 18:18:09 +01006128 bool IsClonable() const OVERRIDE { return true; }
6129
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006130 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006131
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006132 LoadKind GetLoadKind() const {
6133 return GetPackedField<LoadKindField>();
6134 }
6135
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006136 const DexFile& GetDexFile() const {
6137 return dex_file_;
6138 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006139
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006140 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006141 return string_index_;
6142 }
6143
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006144 Handle<mirror::String> GetString() const {
6145 return string_;
6146 }
6147
6148 void SetString(Handle<mirror::String> str) {
6149 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006150 }
6151
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006152 bool CanBeMoved() const OVERRIDE { return true; }
6153
Vladimir Marko372f10e2016-05-17 16:30:10 +01006154 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006155
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006156 size_t ComputeHashCode() const OVERRIDE { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006157
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006158 // Will call the runtime if we need to load the string through
6159 // the dex cache and the string is not guaranteed to be there yet.
6160 bool NeedsEnvironment() const OVERRIDE {
6161 LoadKind load_kind = GetLoadKind();
Vladimir Marko764d4542017-05-16 10:31:41 +01006162 if (load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006163 load_kind == LoadKind::kBootImageAddress ||
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006164 load_kind == LoadKind::kBootImageInternTable ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006165 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006166 return false;
6167 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006168 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006169 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006170
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006171 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006172 return GetLoadKind() == LoadKind::kRuntimeCall;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006173 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006174
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07006175 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006176 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006177
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006178 static SideEffects SideEffectsForArchRuntimeCalls() {
6179 return SideEffects::CanTriggerGC();
6180 }
6181
Vladimir Marko372f10e2016-05-17 16:30:10 +01006182 void AddSpecialInput(HInstruction* special_input);
6183
6184 using HInstruction::GetInputRecords; // Keep the const version visible.
6185 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
6186 return ArrayRef<HUserRecord<HInstruction*>>(
6187 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006188 }
6189
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006190 DataType::Type GetType() const OVERRIDE {
6191 return DataType::Type::kReference;
Vladimir Marko372f10e2016-05-17 16:30:10 +01006192 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006193
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006194 DECLARE_INSTRUCTION(LoadString);
6195
Artem Serovcced8ba2017-07-19 18:18:09 +01006196 protected:
6197 DEFAULT_COPY_CONSTRUCTOR(LoadString);
6198
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006199 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006200 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006201 static constexpr size_t kFieldLoadKindSize =
6202 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
6203 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006204 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006205 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006206
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006207 void SetLoadKindInternal(LoadKind load_kind);
6208
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006209 // The special input is the HCurrentMethod for kRuntimeCall.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006210 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00006211 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01006212 HUserRecord<HInstruction*> special_input_;
6213
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006214 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006215 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006216
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006217 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006218};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006219std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
6220
6221// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006222inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006223 // The special input is used for PC-relative loads on some architectures,
6224 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006225 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006226 GetLoadKind() == LoadKind::kBootImageAddress ||
6227 GetLoadKind() == LoadKind::kBootImageInternTable ||
6228 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01006229 // HLoadString::GetInputRecords() returns an empty array at this point,
6230 // so use the GetInputRecords() from the base class to set the input record.
6231 DCHECK(special_input_.GetInstruction() == nullptr);
6232 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006233 special_input->AddUseAt(this, 0);
6234}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006235
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006236/**
6237 * Performs an initialization check on its Class object input.
6238 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01006239class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006240 public:
Roland Levillain3887c462015-08-12 18:15:42 +01006241 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07006242 : HExpression(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006243 DataType::Type::kReference,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006244 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
6245 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006246 SetRawInputAt(0, constant);
6247 }
6248
Artem Serovcced8ba2017-07-19 18:18:09 +01006249 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006250 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006251 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006252 return true;
6253 }
6254
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006255 bool NeedsEnvironment() const OVERRIDE {
6256 // May call runtime to initialize the class.
6257 return true;
6258 }
6259
Nicolas Geoffray729645a2015-11-19 13:29:02 +00006260 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006261
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006262 HLoadClass* GetLoadClass() const {
6263 DCHECK(InputAt(0)->IsLoadClass());
6264 return InputAt(0)->AsLoadClass();
6265 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006266
6267 DECLARE_INSTRUCTION(ClinitCheck);
6268
Artem Serovcced8ba2017-07-19 18:18:09 +01006269
6270 protected:
6271 DEFAULT_COPY_CONSTRUCTOR(ClinitCheck);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006272};
6273
Vladimir Markofcb503c2016-05-18 12:48:17 +01006274class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006275 public:
6276 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006277 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006278 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006279 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006280 bool is_volatile,
6281 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006282 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006283 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006284 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01006285 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006286 field_info_(field,
6287 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006288 field_type,
6289 is_volatile,
6290 field_idx,
6291 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006292 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006293 SetRawInputAt(0, cls);
6294 }
6295
Calin Juravle52c48962014-12-16 17:02:57 +00006296
Artem Serovcced8ba2017-07-19 18:18:09 +01006297 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle10c9cbe2014-12-19 10:50:19 +00006298 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006299
Vladimir Marko372f10e2016-05-17 16:30:10 +01006300 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
6301 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006302 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006303 }
6304
6305 size_t ComputeHashCode() const OVERRIDE {
6306 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6307 }
6308
Calin Juravle52c48962014-12-16 17:02:57 +00006309 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006310 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006311 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006312 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006313
Vladimir Marko61b92282017-10-11 13:23:17 +01006314 void SetType(DataType::Type new_type) {
6315 DCHECK(DataType::IsIntegralType(GetType()));
6316 DCHECK(DataType::IsIntegralType(new_type));
6317 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6318 SetPackedField<TypeField>(new_type);
6319 }
6320
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006321 DECLARE_INSTRUCTION(StaticFieldGet);
6322
Artem Serovcced8ba2017-07-19 18:18:09 +01006323 protected:
6324 DEFAULT_COPY_CONSTRUCTOR(StaticFieldGet);
6325
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006326 private:
6327 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006328};
6329
Vladimir Markofcb503c2016-05-18 12:48:17 +01006330class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006331 public:
6332 HStaticFieldSet(HInstruction* cls,
6333 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006334 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006335 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006336 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006337 bool is_volatile,
6338 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006339 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006340 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006341 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01006342 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006343 field_info_(field,
6344 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006345 field_type,
6346 is_volatile,
6347 field_idx,
6348 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006349 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006350 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006351 SetRawInputAt(0, cls);
6352 SetRawInputAt(1, value);
6353 }
6354
Artem Serovcced8ba2017-07-19 18:18:09 +01006355 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle52c48962014-12-16 17:02:57 +00006356 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006357 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006358 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006359 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006360
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006361 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006362 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6363 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006364
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006365 DECLARE_INSTRUCTION(StaticFieldSet);
6366
Artem Serovcced8ba2017-07-19 18:18:09 +01006367 protected:
6368 DEFAULT_COPY_CONSTRUCTOR(StaticFieldSet);
6369
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006370 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006371 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6372 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6373 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6374 "Too many packed fields.");
6375
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006376 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006377};
6378
Vladimir Markofcb503c2016-05-18 12:48:17 +01006379class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006380 public:
6381 HUnresolvedInstanceFieldGet(HInstruction* obj,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006382 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006383 uint32_t field_index,
6384 uint32_t dex_pc)
6385 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6386 field_index_(field_index) {
6387 SetRawInputAt(0, obj);
6388 }
6389
Artem Serovcced8ba2017-07-19 18:18:09 +01006390 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006391 bool NeedsEnvironment() const OVERRIDE { return true; }
6392 bool CanThrow() const OVERRIDE { return true; }
6393
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006394 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006395 uint32_t GetFieldIndex() const { return field_index_; }
6396
6397 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6398
Artem Serovcced8ba2017-07-19 18:18:09 +01006399 protected:
6400 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldGet);
6401
Calin Juravlee460d1d2015-09-29 04:52:17 +01006402 private:
6403 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006404};
6405
Vladimir Markofcb503c2016-05-18 12:48:17 +01006406class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006407 public:
6408 HUnresolvedInstanceFieldSet(HInstruction* obj,
6409 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006410 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006411 uint32_t field_index,
6412 uint32_t dex_pc)
6413 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006414 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006415 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006416 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006417 SetRawInputAt(0, obj);
6418 SetRawInputAt(1, value);
6419 }
6420
Artem Serovcced8ba2017-07-19 18:18:09 +01006421 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006422 bool NeedsEnvironment() const OVERRIDE { return true; }
6423 bool CanThrow() const OVERRIDE { return true; }
6424
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006425 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006426 uint32_t GetFieldIndex() const { return field_index_; }
6427
6428 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6429
Artem Serovcced8ba2017-07-19 18:18:09 +01006430 protected:
6431 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldSet);
6432
Calin Juravlee460d1d2015-09-29 04:52:17 +01006433 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006434 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6435 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006436 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006437 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6438 kFieldFieldType + kFieldFieldTypeSize;
6439 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6440 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006441 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006442
Calin Juravlee460d1d2015-09-29 04:52:17 +01006443 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006444};
6445
Vladimir Markofcb503c2016-05-18 12:48:17 +01006446class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006447 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006448 HUnresolvedStaticFieldGet(DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006449 uint32_t field_index,
6450 uint32_t dex_pc)
6451 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6452 field_index_(field_index) {
6453 }
6454
Artem Serovcced8ba2017-07-19 18:18:09 +01006455 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006456 bool NeedsEnvironment() const OVERRIDE { return true; }
6457 bool CanThrow() const OVERRIDE { return true; }
6458
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006459 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006460 uint32_t GetFieldIndex() const { return field_index_; }
6461
6462 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6463
Artem Serovcced8ba2017-07-19 18:18:09 +01006464 protected:
6465 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldGet);
6466
Calin Juravlee460d1d2015-09-29 04:52:17 +01006467 private:
6468 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006469};
6470
Vladimir Markofcb503c2016-05-18 12:48:17 +01006471class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006472 public:
6473 HUnresolvedStaticFieldSet(HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006474 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006475 uint32_t field_index,
6476 uint32_t dex_pc)
6477 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006478 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006479 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006480 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006481 SetRawInputAt(0, value);
6482 }
6483
Artem Serovcced8ba2017-07-19 18:18:09 +01006484 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006485 bool NeedsEnvironment() const OVERRIDE { return true; }
6486 bool CanThrow() const OVERRIDE { return true; }
6487
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006488 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006489 uint32_t GetFieldIndex() const { return field_index_; }
6490
6491 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6492
Artem Serovcced8ba2017-07-19 18:18:09 +01006493 protected:
6494 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldSet);
6495
Calin Juravlee460d1d2015-09-29 04:52:17 +01006496 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006497 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6498 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006499 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006500 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6501 kFieldFieldType + kFieldFieldTypeSize;
6502 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6503 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006504 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006505
Calin Juravlee460d1d2015-09-29 04:52:17 +01006506 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006507};
6508
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006509// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006510class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006511 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006512 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006513 : HExpression(DataType::Type::kReference, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006514
David Brazdilbbd733e2015-08-18 17:48:17 +01006515 bool CanBeNull() const OVERRIDE { return false; }
6516
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006517 DECLARE_INSTRUCTION(LoadException);
6518
Artem Serovcced8ba2017-07-19 18:18:09 +01006519 protected:
6520 DEFAULT_COPY_CONSTRUCTOR(LoadException);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006521};
6522
David Brazdilcb1c0552015-08-04 16:22:25 +01006523// Implicit part of move-exception which clears thread-local exception storage.
6524// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006525class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006526 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006527 explicit HClearException(uint32_t dex_pc = kNoDexPc)
6528 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01006529
6530 DECLARE_INSTRUCTION(ClearException);
6531
Artem Serovcced8ba2017-07-19 18:18:09 +01006532 protected:
6533 DEFAULT_COPY_CONSTRUCTOR(ClearException);
David Brazdilcb1c0552015-08-04 16:22:25 +01006534};
6535
Vladimir Markofcb503c2016-05-18 12:48:17 +01006536class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006537 public:
6538 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006539 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006540 SetRawInputAt(0, exception);
6541 }
6542
6543 bool IsControlFlow() const OVERRIDE { return true; }
6544
6545 bool NeedsEnvironment() const OVERRIDE { return true; }
6546
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006547 bool CanThrow() const OVERRIDE { return true; }
6548
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006549 DECLARE_INSTRUCTION(Throw);
6550
Artem Serovcced8ba2017-07-19 18:18:09 +01006551 protected:
6552 DEFAULT_COPY_CONSTRUCTOR(Throw);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006553};
6554
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006555/**
6556 * Implementation strategies for the code generator of a HInstanceOf
6557 * or `HCheckCast`.
6558 */
6559enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006560 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006561 kExactCheck, // Can do a single class compare.
6562 kClassHierarchyCheck, // Can just walk the super class chain.
6563 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6564 kInterfaceCheck, // No optimization yet when checking against an interface.
6565 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006566 kArrayCheck, // No optimization yet when checking against a generic array.
6567 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006568};
6569
Roland Levillain86503782016-02-11 19:07:30 +00006570std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6571
Vladimir Markofcb503c2016-05-18 12:48:17 +01006572class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006573 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006574 HInstanceOf(HInstruction* object,
6575 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006576 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006577 uint32_t dex_pc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006578 : HExpression(DataType::Type::kBool,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006579 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006580 dex_pc) {
6581 SetPackedField<TypeCheckKindField>(check_kind);
6582 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006583 SetRawInputAt(0, object);
6584 SetRawInputAt(1, constant);
6585 }
6586
Artem Serovcced8ba2017-07-19 18:18:09 +01006587 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006588 bool CanBeMoved() const OVERRIDE { return true; }
6589
Vladimir Marko372f10e2016-05-17 16:30:10 +01006590 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006591 return true;
6592 }
6593
6594 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006595 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006596 }
6597
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006598 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006599 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6600 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6601 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6602 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006603
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006604 static bool CanCallRuntime(TypeCheckKind check_kind) {
6605 // Mips currently does runtime calls for any other checks.
6606 return check_kind != TypeCheckKind::kExactCheck;
6607 }
6608
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006609 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006610 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006611 }
6612
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006613 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006614
Artem Serovcced8ba2017-07-19 18:18:09 +01006615 protected:
6616 DEFAULT_COPY_CONSTRUCTOR(InstanceOf);
6617
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006618 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006619 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6620 static constexpr size_t kFieldTypeCheckKindSize =
6621 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6622 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6623 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6624 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6625 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006626};
6627
Vladimir Markofcb503c2016-05-18 12:48:17 +01006628class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006629 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07006630 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006631 : HExpression(DataType::Type::kReference, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006632 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6633 SetPackedFlag<kFlagUpperCanBeNull>(true);
6634 SetPackedFlag<kFlagCanBeNull>(true);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006635 DCHECK_EQ(input->GetType(), DataType::Type::kReference);
Calin Juravleb1498f62015-02-16 13:13:29 +00006636 SetRawInputAt(0, input);
6637 }
6638
Artem Serovcced8ba2017-07-19 18:18:09 +01006639 bool IsClonable() const OVERRIDE { return true; }
6640
David Brazdilf5552582015-12-27 13:36:12 +00006641 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006642 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006643 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006644 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006645
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006646 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006647 DCHECK(GetUpperCanBeNull() || !can_be_null);
6648 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006649 }
6650
Vladimir Markoa1de9182016-02-25 11:37:38 +00006651 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006652
Calin Juravleb1498f62015-02-16 13:13:29 +00006653 DECLARE_INSTRUCTION(BoundType);
6654
Artem Serovcced8ba2017-07-19 18:18:09 +01006655 protected:
6656 DEFAULT_COPY_CONSTRUCTOR(BoundType);
6657
Calin Juravleb1498f62015-02-16 13:13:29 +00006658 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006659 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6660 // is false then CanBeNull() cannot be true).
6661 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6662 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6663 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6664 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6665
Calin Juravleb1498f62015-02-16 13:13:29 +00006666 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006667 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6668 // It is used to bound the type in cases like:
6669 // if (x instanceof ClassX) {
6670 // // uper_bound_ will be ClassX
6671 // }
David Brazdilf5552582015-12-27 13:36:12 +00006672 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006673};
6674
Vladimir Markofcb503c2016-05-18 12:48:17 +01006675class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006676 public:
6677 HCheckCast(HInstruction* object,
6678 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006679 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006680 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006681 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6682 SetPackedField<TypeCheckKindField>(check_kind);
6683 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006684 SetRawInputAt(0, object);
6685 SetRawInputAt(1, constant);
6686 }
6687
Artem Serovcced8ba2017-07-19 18:18:09 +01006688 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006689 bool CanBeMoved() const OVERRIDE { return true; }
6690
Vladimir Marko372f10e2016-05-17 16:30:10 +01006691 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006692 return true;
6693 }
6694
6695 bool NeedsEnvironment() const OVERRIDE {
6696 // Instruction may throw a CheckCastError.
6697 return true;
6698 }
6699
6700 bool CanThrow() const OVERRIDE { return true; }
6701
Vladimir Markoa1de9182016-02-25 11:37:38 +00006702 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6703 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6704 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6705 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006706
6707 DECLARE_INSTRUCTION(CheckCast);
6708
Artem Serovcced8ba2017-07-19 18:18:09 +01006709 protected:
6710 DEFAULT_COPY_CONSTRUCTOR(CheckCast);
6711
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006712 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006713 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6714 static constexpr size_t kFieldTypeCheckKindSize =
6715 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6716 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6717 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6718 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6719 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006720};
6721
Andreas Gampe26de38b2016-07-27 17:53:11 -07006722/**
6723 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
6724 * @details We define the combined barrier types that are actually required
6725 * by the Java Memory Model, rather than using exactly the terminology from
6726 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
6727 * primitives. Note that the JSR-133 cookbook generally does not deal with
6728 * store atomicity issues, and the recipes there are not always entirely sufficient.
6729 * The current recipe is as follows:
6730 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
6731 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
6732 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
6733 * -# Use StoreStore barrier after all stores but before return from any constructor whose
6734 * class has final fields.
6735 * -# Use NTStoreStore to order non-temporal stores with respect to all later
6736 * store-to-memory instructions. Only generated together with non-temporal stores.
6737 */
6738enum MemBarrierKind {
6739 kAnyStore,
6740 kLoadAny,
6741 kStoreStore,
6742 kAnyAny,
6743 kNTStoreStore,
6744 kLastBarrierKind = kNTStoreStore
6745};
6746std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
6747
Vladimir Markofcb503c2016-05-18 12:48:17 +01006748class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006749 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006750 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006751 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006752 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6753 SetPackedField<BarrierKindField>(barrier_kind);
6754 }
Calin Juravle27df7582015-04-17 19:12:31 +01006755
Artem Serovcced8ba2017-07-19 18:18:09 +01006756 bool IsClonable() const OVERRIDE { return true; }
6757
Vladimir Markoa1de9182016-02-25 11:37:38 +00006758 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006759
6760 DECLARE_INSTRUCTION(MemoryBarrier);
6761
Artem Serovcced8ba2017-07-19 18:18:09 +01006762 protected:
6763 DEFAULT_COPY_CONSTRUCTOR(MemoryBarrier);
6764
Calin Juravle27df7582015-04-17 19:12:31 +01006765 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006766 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6767 static constexpr size_t kFieldBarrierKindSize =
6768 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6769 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6770 kFieldBarrierKind + kFieldBarrierKindSize;
6771 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6772 "Too many packed fields.");
6773 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006774};
6775
Igor Murashkind01745e2017-04-05 16:40:31 -07006776// A constructor fence orders all prior stores to fields that could be accessed via a final field of
6777// the specified object(s), with respect to any subsequent store that might "publish"
6778// (i.e. make visible) the specified object to another thread.
6779//
6780// JLS 17.5.1 "Semantics of final fields" states that a freeze action happens
6781// for all final fields (that were set) at the end of the invoked constructor.
6782//
6783// The constructor fence models the freeze actions for the final fields of an object
6784// being constructed (semantically at the end of the constructor). Constructor fences
6785// have a per-object affinity; two separate objects being constructed get two separate
6786// constructor fences.
6787//
6788// (Note: that if calling a super-constructor or forwarding to another constructor,
6789// the freezes would happen at the end of *that* constructor being invoked).
6790//
6791// The memory model guarantees that when the object being constructed is "published" after
6792// constructor completion (i.e. escapes the current thread via a store), then any final field
6793// writes must be observable on other threads (once they observe that publication).
6794//
6795// Further, anything written before the freeze, and read by dereferencing through the final field,
6796// must also be visible (so final object field could itself have an object with non-final fields;
6797// yet the freeze must also extend to them).
6798//
6799// Constructor example:
6800//
6801// class HasFinal {
6802// final int field; Optimizing IR for <init>()V:
6803// HasFinal() {
6804// field = 123; HInstanceFieldSet(this, HasFinal.field, 123)
6805// // freeze(this.field); HConstructorFence(this)
6806// } HReturn
6807// }
6808//
6809// HConstructorFence can serve double duty as a fence for new-instance/new-array allocations of
6810// already-initialized classes; in that case the allocation must act as a "default-initializer"
6811// of the object which effectively writes the class pointer "final field".
6812//
6813// For example, we can model default-initialiation as roughly the equivalent of the following:
6814//
6815// class Object {
6816// private final Class header;
6817// }
6818//
6819// Java code: Optimizing IR:
6820//
6821// T new_instance<T>() {
6822// Object obj = allocate_memory(T.class.size); obj = HInvoke(art_quick_alloc_object, T)
6823// obj.header = T.class; // header write is done by above call.
6824// // freeze(obj.header) HConstructorFence(obj)
6825// return (T)obj;
6826// }
6827//
6828// See also:
6829// * CompilerDriver::RequiresConstructorBarrier
6830// * QuasiAtomic::ThreadFenceForConstructor
6831//
6832class HConstructorFence FINAL : public HVariableInputSizeInstruction {
6833 // A fence has variable inputs because the inputs can be removed
6834 // after prepare_for_register_allocation phase.
6835 // (TODO: In the future a fence could freeze multiple objects
6836 // after merging two fences together.)
6837 public:
6838 // `fence_object` is the reference that needs to be protected for correct publication.
6839 //
6840 // It makes sense in the following situations:
6841 // * <init> constructors, it's the "this" parameter (i.e. HParameterValue, s.t. IsThis() == true).
6842 // * new-instance-like instructions, it's the return value (i.e. HNewInstance).
6843 //
6844 // After construction the `fence_object` becomes the 0th input.
6845 // This is not an input in a real sense, but just a convenient place to stash the information
6846 // about the associated object.
6847 HConstructorFence(HInstruction* fence_object,
6848 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01006849 ArenaAllocator* allocator)
Igor Murashkind01745e2017-04-05 16:40:31 -07006850 // We strongly suspect there is not a more accurate way to describe the fine-grained reordering
6851 // constraints described in the class header. We claim that these SideEffects constraints
6852 // enforce a superset of the real constraints.
6853 //
6854 // The ordering described above is conservatively modeled with SideEffects as follows:
6855 //
6856 // * To prevent reordering of the publication stores:
6857 // ----> "Reads of objects" is the initial SideEffect.
6858 // * For every primitive final field store in the constructor:
6859 // ----> Union that field's type as a read (e.g. "Read of T") into the SideEffect.
6860 // * If there are any stores to reference final fields in the constructor:
6861 // ----> Use a more conservative "AllReads" SideEffect because any stores to any references
6862 // that are reachable from `fence_object` also need to be prevented for reordering
6863 // (and we do not want to do alias analysis to figure out what those stores are).
6864 //
6865 // In the implementation, this initially starts out as an "all reads" side effect; this is an
6866 // even more conservative approach than the one described above, and prevents all of the
6867 // above reordering without analyzing any of the instructions in the constructor.
6868 //
6869 // If in a later phase we discover that there are no writes to reference final fields,
6870 // we can refine the side effect to a smaller set of type reads (see above constraints).
6871 : HVariableInputSizeInstruction(SideEffects::AllReads(),
6872 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01006873 allocator,
Igor Murashkind01745e2017-04-05 16:40:31 -07006874 /* number_of_inputs */ 1,
6875 kArenaAllocConstructorFenceInputs) {
6876 DCHECK(fence_object != nullptr);
6877 SetRawInputAt(0, fence_object);
6878 }
6879
6880 // The object associated with this constructor fence.
6881 //
6882 // (Note: This will be null after the prepare_for_register_allocation phase,
6883 // as all constructor fence inputs are removed there).
6884 HInstruction* GetFenceObject() const {
6885 return InputAt(0);
6886 }
6887
6888 // Find all the HConstructorFence uses (`fence_use`) for `this` and:
6889 // - Delete `fence_use` from `this`'s use list.
6890 // - Delete `this` from `fence_use`'s inputs list.
6891 // - If the `fence_use` is dead, remove it from the graph.
6892 //
6893 // A fence is considered dead once it no longer has any uses
6894 // and all of the inputs are dead.
6895 //
6896 // This must *not* be called during/after prepare_for_register_allocation,
6897 // because that removes all the inputs to the fences but the fence is actually
6898 // still considered live.
Igor Murashkin6ef45672017-08-08 13:59:55 -07006899 //
6900 // Returns how many HConstructorFence instructions were removed from graph.
6901 static size_t RemoveConstructorFences(HInstruction* instruction);
Igor Murashkind01745e2017-04-05 16:40:31 -07006902
Igor Murashkindd018df2017-08-09 10:38:31 -07006903 // Combine all inputs of `this` and `other` instruction and remove
6904 // `other` from the graph.
6905 //
6906 // Inputs are unique after the merge.
6907 //
6908 // Requirement: `this` must not be the same as `other.
6909 void Merge(HConstructorFence* other);
6910
Igor Murashkin79d8fa72017-04-18 09:37:23 -07006911 // Check if this constructor fence is protecting
6912 // an HNewInstance or HNewArray that is also the immediate
6913 // predecessor of `this`.
6914 //
Igor Murashkindd018df2017-08-09 10:38:31 -07006915 // If `ignore_inputs` is true, then the immediate predecessor doesn't need
6916 // to be one of the inputs of `this`.
6917 //
Igor Murashkin79d8fa72017-04-18 09:37:23 -07006918 // Returns the associated HNewArray or HNewInstance,
6919 // or null otherwise.
Igor Murashkindd018df2017-08-09 10:38:31 -07006920 HInstruction* GetAssociatedAllocation(bool ignore_inputs = false);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07006921
Igor Murashkind01745e2017-04-05 16:40:31 -07006922 DECLARE_INSTRUCTION(ConstructorFence);
6923
Artem Serovcced8ba2017-07-19 18:18:09 +01006924 protected:
6925 DEFAULT_COPY_CONSTRUCTOR(ConstructorFence);
Igor Murashkind01745e2017-04-05 16:40:31 -07006926};
6927
Vladimir Markofcb503c2016-05-18 12:48:17 +01006928class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006929 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006930 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006931 kEnter,
6932 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006933 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006934 };
6935
6936 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006937 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006938 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6939 dex_pc) {
6940 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006941 SetRawInputAt(0, object);
6942 }
6943
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006944 // Instruction may go into runtime, so we need an environment.
6945 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006946
6947 bool CanThrow() const OVERRIDE {
6948 // Verifier guarantees that monitor-exit cannot throw.
6949 // This is important because it allows the HGraphBuilder to remove
6950 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6951 return IsEnter();
6952 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006953
Vladimir Markoa1de9182016-02-25 11:37:38 +00006954 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6955 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006956
6957 DECLARE_INSTRUCTION(MonitorOperation);
6958
Artem Serovcced8ba2017-07-19 18:18:09 +01006959 protected:
6960 DEFAULT_COPY_CONSTRUCTOR(MonitorOperation);
6961
Calin Juravle52c48962014-12-16 17:02:57 +00006962 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006963 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6964 static constexpr size_t kFieldOperationKindSize =
6965 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6966 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6967 kFieldOperationKind + kFieldOperationKindSize;
6968 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6969 "Too many packed fields.");
6970 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006971};
6972
Vladimir Markofcb503c2016-05-18 12:48:17 +01006973class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00006974 public:
6975 HSelect(HInstruction* condition,
6976 HInstruction* true_value,
6977 HInstruction* false_value,
6978 uint32_t dex_pc)
6979 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6980 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6981
6982 // First input must be `true_value` or `false_value` to allow codegens to
6983 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6984 // that architectures which implement HSelect as a conditional move also
6985 // will not need to invert the condition.
6986 SetRawInputAt(0, false_value);
6987 SetRawInputAt(1, true_value);
6988 SetRawInputAt(2, condition);
6989 }
6990
Artem Serovcced8ba2017-07-19 18:18:09 +01006991 bool IsClonable() const OVERRIDE { return true; }
David Brazdil74eb1b22015-12-14 11:44:01 +00006992 HInstruction* GetFalseValue() const { return InputAt(0); }
6993 HInstruction* GetTrueValue() const { return InputAt(1); }
6994 HInstruction* GetCondition() const { return InputAt(2); }
6995
6996 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006997 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6998 return true;
6999 }
David Brazdil74eb1b22015-12-14 11:44:01 +00007000
7001 bool CanBeNull() const OVERRIDE {
7002 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
7003 }
7004
7005 DECLARE_INSTRUCTION(Select);
7006
Artem Serovcced8ba2017-07-19 18:18:09 +01007007 protected:
7008 DEFAULT_COPY_CONSTRUCTOR(Select);
David Brazdil74eb1b22015-12-14 11:44:01 +00007009};
7010
Vladimir Markof9f64412015-09-02 14:05:49 +01007011class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007012 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01007013 MoveOperands(Location source,
7014 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007015 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007016 HInstruction* instruction)
7017 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007018
7019 Location GetSource() const { return source_; }
7020 Location GetDestination() const { return destination_; }
7021
7022 void SetSource(Location value) { source_ = value; }
7023 void SetDestination(Location value) { destination_ = value; }
7024
7025 // The parallel move resolver marks moves as "in-progress" by clearing the
7026 // destination (but not the source).
7027 Location MarkPending() {
7028 DCHECK(!IsPending());
7029 Location dest = destination_;
7030 destination_ = Location::NoLocation();
7031 return dest;
7032 }
7033
7034 void ClearPending(Location dest) {
7035 DCHECK(IsPending());
7036 destination_ = dest;
7037 }
7038
7039 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00007040 DCHECK(source_.IsValid() || destination_.IsInvalid());
7041 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007042 }
7043
7044 // True if this blocks a move from the given location.
7045 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08007046 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007047 }
7048
7049 // A move is redundant if it's been eliminated, if its source and
7050 // destination are the same, or if its destination is unneeded.
7051 bool IsRedundant() const {
7052 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
7053 }
7054
7055 // We clear both operands to indicate move that's been eliminated.
7056 void Eliminate() {
7057 source_ = destination_ = Location::NoLocation();
7058 }
7059
7060 bool IsEliminated() const {
7061 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
7062 return source_.IsInvalid();
7063 }
7064
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007065 DataType::Type GetType() const { return type_; }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007066
Nicolas Geoffray90218252015-04-15 11:56:51 +01007067 bool Is64BitMove() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007068 return DataType::Is64BitType(type_);
Nicolas Geoffray90218252015-04-15 11:56:51 +01007069 }
7070
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007071 HInstruction* GetInstruction() const { return instruction_; }
7072
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007073 private:
7074 Location source_;
7075 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01007076 // The type this move is for.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007077 DataType::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007078 // The instruction this move is assocatied with. Null when this move is
7079 // for moving an input in the expected locations of user (including a phi user).
7080 // This is only used in debug mode, to ensure we do not connect interval siblings
7081 // in the same parallel move.
7082 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007083};
7084
Roland Levillainc9285912015-12-18 10:38:42 +00007085std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
7086
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007087static constexpr size_t kDefaultNumberOfMoves = 4;
7088
Vladimir Markofcb503c2016-05-18 12:48:17 +01007089class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007090 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007091 explicit HParallelMove(ArenaAllocator* allocator, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01007092 : HTemplateInstruction(SideEffects::None(), dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007093 moves_(allocator->Adapter(kArenaAllocMoveOperands)) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007094 moves_.reserve(kDefaultNumberOfMoves);
7095 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007096
Nicolas Geoffray90218252015-04-15 11:56:51 +01007097 void AddMove(Location source,
7098 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007099 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007100 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00007101 DCHECK(source.IsValid());
7102 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007103 if (kIsDebugBuild) {
7104 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007105 for (const MoveOperands& move : moves_) {
7106 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007107 // Special case the situation where the move is for the spill slot
7108 // of the instruction.
7109 if ((GetPrevious() == instruction)
7110 || ((GetPrevious() == nullptr)
7111 && instruction->IsPhi()
7112 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007113 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007114 << "Doing parallel moves for the same instruction.";
7115 } else {
7116 DCHECK(false) << "Doing parallel moves for the same instruction.";
7117 }
7118 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00007119 }
7120 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007121 for (const MoveOperands& move : moves_) {
7122 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007123 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01007124 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007125 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007126 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007127 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007128 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007129 }
7130
Vladimir Marko225b6462015-09-28 12:17:40 +01007131 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007132 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007133 }
7134
Vladimir Marko225b6462015-09-28 12:17:40 +01007135 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007136
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01007137 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007138
Artem Serovcced8ba2017-07-19 18:18:09 +01007139 protected:
7140 DEFAULT_COPY_CONSTRUCTOR(ParallelMove);
7141
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007142 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01007143 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007144};
7145
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007146// This instruction computes an intermediate address pointing in the 'middle' of an object. The
7147// result pointer cannot be handled by GC, so extra care is taken to make sure that this value is
7148// never used across anything that can trigger GC.
7149// The result of this instruction is not a pointer in the sense of `DataType::Type::kreference`.
7150// So we represent it by the type `DataType::Type::kInt`.
7151class HIntermediateAddress FINAL : public HExpression<2> {
7152 public:
7153 HIntermediateAddress(HInstruction* base_address, HInstruction* offset, uint32_t dex_pc)
7154 : HExpression(DataType::Type::kInt32, SideEffects::DependsOnGC(), dex_pc) {
7155 DCHECK_EQ(DataType::Size(DataType::Type::kInt32),
7156 DataType::Size(DataType::Type::kReference))
7157 << "kPrimInt and kPrimNot have different sizes.";
7158 SetRawInputAt(0, base_address);
7159 SetRawInputAt(1, offset);
7160 }
7161
Artem Serovcced8ba2017-07-19 18:18:09 +01007162 bool IsClonable() const OVERRIDE { return true; }
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007163 bool CanBeMoved() const OVERRIDE { return true; }
7164 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
7165 return true;
7166 }
7167 bool IsActualObject() const OVERRIDE { return false; }
7168
7169 HInstruction* GetBaseAddress() const { return InputAt(0); }
7170 HInstruction* GetOffset() const { return InputAt(1); }
7171
7172 DECLARE_INSTRUCTION(IntermediateAddress);
7173
Artem Serovcced8ba2017-07-19 18:18:09 +01007174 protected:
7175 DEFAULT_COPY_CONSTRUCTOR(IntermediateAddress);
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007176};
7177
7178
Mark Mendell0616ae02015-04-17 12:49:27 -04007179} // namespace art
7180
Aart Bikf8f5a162017-02-06 15:35:29 -08007181#include "nodes_vector.h"
7182
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007183#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
7184#include "nodes_shared.h"
7185#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007186#ifdef ART_ENABLE_CODEGEN_mips
7187#include "nodes_mips.h"
7188#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04007189#ifdef ART_ENABLE_CODEGEN_x86
7190#include "nodes_x86.h"
7191#endif
7192
7193namespace art {
7194
Igor Murashkin6ef45672017-08-08 13:59:55 -07007195class OptimizingCompilerStats;
7196
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007197class HGraphVisitor : public ValueObject {
7198 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007199 explicit HGraphVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7200 : stats_(stats),
7201 graph_(graph) {}
Dave Allison20dfc792014-06-16 20:44:29 -07007202 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007203
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007204 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007205 virtual void VisitBasicBlock(HBasicBlock* block);
7206
Roland Levillain633021e2014-10-01 14:12:25 +01007207 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007208 void VisitInsertionOrder();
7209
Roland Levillain633021e2014-10-01 14:12:25 +01007210 // Visit the graph following dominator tree reverse post-order.
7211 void VisitReversePostOrder();
7212
Nicolas Geoffray787c3072014-03-17 10:20:19 +00007213 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007214
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007215 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007216#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007217 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
7218
7219 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7220
7221#undef DECLARE_VISIT_INSTRUCTION
7222
Igor Murashkin6ef45672017-08-08 13:59:55 -07007223 protected:
7224 OptimizingCompilerStats* stats_;
7225
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007226 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07007227 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007228
7229 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
7230};
7231
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007232class HGraphDelegateVisitor : public HGraphVisitor {
7233 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007234 explicit HGraphDelegateVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7235 : HGraphVisitor(graph, stats) {}
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007236 virtual ~HGraphDelegateVisitor() {}
7237
7238 // Visit functions that delegate to to super class.
7239#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00007240 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007241
7242 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7243
7244#undef DECLARE_VISIT_INSTRUCTION
7245
7246 private:
7247 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
7248};
7249
Artem Serovcced8ba2017-07-19 18:18:09 +01007250// Create a clone of the instruction, insert it into the graph; replace the old one with a new
7251// and remove the old instruction.
7252HInstruction* ReplaceInstrOrPhiByClone(HInstruction* instr);
7253
7254// Create a clone for each clonable instructions/phis and replace the original with the clone.
7255//
7256// Used for testing individual instruction cloner.
7257class CloneAndReplaceInstructionVisitor : public HGraphDelegateVisitor {
7258 public:
7259 explicit CloneAndReplaceInstructionVisitor(HGraph* graph)
7260 : HGraphDelegateVisitor(graph), instr_replaced_by_clones_count(0) {}
7261
7262 void VisitInstruction(HInstruction* instruction) OVERRIDE {
7263 if (instruction->IsClonable()) {
7264 ReplaceInstrOrPhiByClone(instruction);
7265 instr_replaced_by_clones_count++;
7266 }
7267 }
7268
7269 size_t GetInstrReplacedByClonesCount() const { return instr_replaced_by_clones_count; }
7270
7271 private:
7272 size_t instr_replaced_by_clones_count;
7273
7274 DISALLOW_COPY_AND_ASSIGN(CloneAndReplaceInstructionVisitor);
7275};
7276
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007277// Iterator over the blocks that art part of the loop. Includes blocks part
7278// of an inner loop. The order in which the blocks are iterated is on their
7279// block id.
7280class HBlocksInLoopIterator : public ValueObject {
7281 public:
7282 explicit HBlocksInLoopIterator(const HLoopInformation& info)
7283 : blocks_in_loop_(info.GetBlocks()),
7284 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
7285 index_(0) {
7286 if (!blocks_in_loop_.IsBitSet(index_)) {
7287 Advance();
7288 }
7289 }
7290
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007291 bool Done() const { return index_ == blocks_.size(); }
7292 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007293 void Advance() {
7294 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007295 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007296 if (blocks_in_loop_.IsBitSet(index_)) {
7297 break;
7298 }
7299 }
7300 }
7301
7302 private:
7303 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007304 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007305 size_t index_;
7306
7307 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
7308};
7309
Mingyao Yang3584bce2015-05-19 16:01:59 -07007310// Iterator over the blocks that art part of the loop. Includes blocks part
7311// of an inner loop. The order in which the blocks are iterated is reverse
7312// post order.
7313class HBlocksInLoopReversePostOrderIterator : public ValueObject {
7314 public:
7315 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
7316 : blocks_in_loop_(info.GetBlocks()),
7317 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
7318 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007319 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007320 Advance();
7321 }
7322 }
7323
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007324 bool Done() const { return index_ == blocks_.size(); }
7325 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07007326 void Advance() {
7327 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007328 for (size_t e = blocks_.size(); index_ < e; ++index_) {
7329 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007330 break;
7331 }
7332 }
7333 }
7334
7335 private:
7336 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007337 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07007338 size_t index_;
7339
7340 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
7341};
7342
Aart Bikf3e61ee2017-04-12 17:09:20 -07007343// Returns int64_t value of a properly typed constant.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007344inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00007345 if (constant->IsIntConstant()) {
7346 return constant->AsIntConstant()->GetValue();
7347 } else if (constant->IsLongConstant()) {
7348 return constant->AsLongConstant()->GetValue();
7349 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00007350 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00007351 return 0;
7352 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007353}
7354
Aart Bikf3e61ee2017-04-12 17:09:20 -07007355// Returns true iff instruction is an integral constant (and sets value on success).
7356inline bool IsInt64AndGet(HInstruction* instruction, /*out*/ int64_t* value) {
7357 if (instruction->IsIntConstant()) {
7358 *value = instruction->AsIntConstant()->GetValue();
7359 return true;
7360 } else if (instruction->IsLongConstant()) {
7361 *value = instruction->AsLongConstant()->GetValue();
7362 return true;
7363 } else if (instruction->IsNullConstant()) {
7364 *value = 0;
7365 return true;
7366 }
7367 return false;
7368}
7369
Aart Bik0148de42017-09-05 09:25:01 -07007370// Returns true iff instruction is the given integral constant.
7371inline bool IsInt64Value(HInstruction* instruction, int64_t value) {
7372 int64_t val = 0;
7373 return IsInt64AndGet(instruction, &val) && val == value;
7374}
7375
7376// Returns true iff instruction is a zero bit pattern.
7377inline bool IsZeroBitPattern(HInstruction* instruction) {
7378 return instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern();
7379}
7380
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007381#define INSTRUCTION_TYPE_CHECK(type, super) \
7382 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
7383 inline const H##type* HInstruction::As##type() const { \
7384 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
7385 } \
7386 inline H##type* HInstruction::As##type() { \
7387 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
7388 }
7389
7390 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
7391#undef INSTRUCTION_TYPE_CHECK
7392
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00007393// Create space in `blocks` for adding `number_of_new_blocks` entries
7394// starting at location `at`. Blocks after `at` are moved accordingly.
7395inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
7396 size_t number_of_new_blocks,
7397 size_t after) {
7398 DCHECK_LT(after, blocks->size());
7399 size_t old_size = blocks->size();
7400 size_t new_size = old_size + number_of_new_blocks;
7401 blocks->resize(new_size);
7402 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
7403}
7404
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00007405/*
7406 * Hunt "under the hood" of array lengths (leading to array references),
7407 * null checks (also leading to array references), and new arrays
7408 * (leading to the actual length). This makes it more likely related
7409 * instructions become actually comparable.
7410 */
7411inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
7412 while (instruction->IsArrayLength() ||
7413 instruction->IsNullCheck() ||
7414 instruction->IsNewArray()) {
7415 instruction = instruction->IsNewArray()
7416 ? instruction->AsNewArray()->GetLength()
7417 : instruction->InputAt(0);
7418 }
7419 return instruction;
7420}
7421
Artem Serov21c7e6f2017-07-27 16:04:42 +01007422void RemoveEnvironmentUses(HInstruction* instruction);
7423bool HasEnvironmentUsedByOthers(HInstruction* instruction);
7424void ResetEnvironmentInputRecords(HInstruction* instruction);
7425
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007426} // namespace art
7427
7428#endif // ART_COMPILER_OPTIMIZING_NODES_H_