blob: f83abd708a85b8405146cdc2a2d3a9557bbb7a58 [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);
Artem Serov09faaea2017-12-07 14:36:01 +0000426
427 // Transform a loop into a format with a single preheader.
428 //
429 // Each phi in the header should be split: original one in the header should only hold
430 // inputs reachable from the back edges and a single input from the preheader. The newly created
431 // phi in the preheader should collate the inputs from the original multiple incoming blocks.
432 //
433 // Loops in the graph typically have a single preheader, so this method is used to "repair" loops
434 // that no longer have this property.
435 void TransformLoopToSinglePreheaderFormat(HBasicBlock* header);
436
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100437 void SimplifyLoop(HBasicBlock* header);
438
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000439 int32_t GetNextInstructionId() {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100440 CHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000441 return current_instruction_id_++;
442 }
443
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000444 int32_t GetCurrentInstructionId() const {
445 return current_instruction_id_;
446 }
447
448 void SetCurrentInstructionId(int32_t id) {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100449 CHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000450 current_instruction_id_ = id;
451 }
452
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100453 uint16_t GetMaximumNumberOfOutVRegs() const {
454 return maximum_number_of_out_vregs_;
455 }
456
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000457 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
458 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100459 }
460
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100461 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
462 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
463 }
464
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000465 void UpdateTemporariesVRegSlots(size_t slots) {
466 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100467 }
468
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000469 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100470 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000471 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100472 }
473
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100474 void SetNumberOfVRegs(uint16_t number_of_vregs) {
475 number_of_vregs_ = number_of_vregs;
476 }
477
478 uint16_t GetNumberOfVRegs() const {
479 return number_of_vregs_;
480 }
481
482 void SetNumberOfInVRegs(uint16_t value) {
483 number_of_in_vregs_ = value;
484 }
485
David Brazdildee58d62016-04-07 09:54:26 +0000486 uint16_t GetNumberOfInVRegs() const {
487 return number_of_in_vregs_;
488 }
489
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100490 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100491 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100492 return number_of_vregs_ - number_of_in_vregs_;
493 }
494
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100495 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100496 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100497 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100498
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100499 ArrayRef<HBasicBlock* const> GetReversePostOrderSkipEntryBlock() {
500 DCHECK(GetReversePostOrder()[0] == entry_block_);
501 return ArrayRef<HBasicBlock* const>(GetReversePostOrder()).SubArray(1);
502 }
503
504 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetPostOrder() const {
505 return ReverseRange(GetReversePostOrder());
506 }
507
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100508 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100509 return linear_order_;
510 }
511
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100512 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetLinearPostOrder() const {
513 return ReverseRange(GetLinearOrder());
514 }
515
Mark Mendell1152c922015-04-24 17:06:35 -0400516 bool HasBoundsChecks() const {
517 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800518 }
519
Mark Mendell1152c922015-04-24 17:06:35 -0400520 void SetHasBoundsChecks(bool value) {
521 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800522 }
523
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000524 bool IsDebuggable() const { return debuggable_; }
525
David Brazdil8d5b8b22015-03-24 10:51:52 +0000526 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000527 // already, it is created and inserted into the graph. This method is only for
528 // integral types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100529 HConstant* GetConstant(DataType::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000530
531 // TODO: This is problematic for the consistency of reference type propagation
532 // because it can be created anytime after the pass and thus it will be left
533 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600534 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000535
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600536 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
537 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000538 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600539 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
540 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000541 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600542 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
543 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000544 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600545 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
546 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000547 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000548
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100549 HCurrentMethod* GetCurrentMethod();
550
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100551 const DexFile& GetDexFile() const {
552 return dex_file_;
553 }
554
555 uint32_t GetMethodIdx() const {
556 return method_idx_;
557 }
558
Igor Murashkind01745e2017-04-05 16:40:31 -0700559 // Get the method name (without the signature), e.g. "<init>"
560 const char* GetMethodName() const;
561
562 // Get the pretty method name (class + name + optionally signature).
563 std::string PrettyMethod(bool with_signature = true) const;
564
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100565 InvokeType GetInvokeType() const {
566 return invoke_type_;
567 }
568
Mark Mendellc4701932015-04-10 13:18:51 -0400569 InstructionSet GetInstructionSet() const {
570 return instruction_set_;
571 }
572
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000573 bool IsCompilingOsr() const { return osr_; }
574
Mingyao Yang063fc772016-08-02 11:02:54 -0700575 ArenaSet<ArtMethod*>& GetCHASingleImplementationList() {
576 return cha_single_implementation_list_;
577 }
578
579 void AddCHASingleImplementationDependency(ArtMethod* method) {
580 cha_single_implementation_list_.insert(method);
581 }
582
583 bool HasShouldDeoptimizeFlag() const {
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800584 return number_of_cha_guards_ != 0;
Mingyao Yang063fc772016-08-02 11:02:54 -0700585 }
586
David Brazdil77a48ae2015-09-15 12:34:04 +0000587 bool HasTryCatch() const { return has_try_catch_; }
588 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100589
Aart Bikb13c65b2017-03-21 20:14:07 -0700590 bool HasSIMD() const { return has_simd_; }
591 void SetHasSIMD(bool value) { has_simd_ = value; }
592
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800593 bool HasLoops() const { return has_loops_; }
594 void SetHasLoops(bool value) { has_loops_ = value; }
595
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000596 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
597 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
598
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100599 ArtMethod* GetArtMethod() const { return art_method_; }
600 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
601
Roland Levillain5e8d5f02016-10-18 18:03:43 +0100602 // Returns an instruction with the opposite Boolean value from 'cond'.
Mark Mendellf6529172015-11-17 11:16:56 -0500603 // The instruction has been inserted into the graph, either as a constant, or
604 // before cursor.
605 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
606
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000607 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
608
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800609 uint32_t GetNumberOfCHAGuards() { return number_of_cha_guards_; }
610 void SetNumberOfCHAGuards(uint32_t num) { number_of_cha_guards_ = num; }
611 void IncrementNumberOfCHAGuards() { number_of_cha_guards_++; }
612
David Brazdil2d7352b2015-04-20 14:52:42 +0100613 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000614 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100615 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000616
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000617 template <class InstructionType, typename ValueType>
618 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600619 ArenaSafeMap<ValueType, InstructionType*>* cache,
620 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000621 // Try to find an existing constant of the given value.
622 InstructionType* constant = nullptr;
623 auto cached_constant = cache->find(value);
624 if (cached_constant != cache->end()) {
625 constant = cached_constant->second;
626 }
627
628 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100629 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000630 if (constant == nullptr || constant->GetBlock() == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100631 constant = new (allocator_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000632 cache->Overwrite(value, constant);
633 InsertConstant(constant);
634 }
635 return constant;
636 }
637
David Brazdil8d5b8b22015-03-24 10:51:52 +0000638 void InsertConstant(HConstant* instruction);
639
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000640 // Cache a float constant into the graph. This method should only be
641 // called by the SsaBuilder when creating "equivalent" instructions.
642 void CacheFloatConstant(HFloatConstant* constant);
643
644 // See CacheFloatConstant comment.
645 void CacheDoubleConstant(HDoubleConstant* constant);
646
Vladimir Markoca6fff82017-10-03 14:49:14 +0100647 ArenaAllocator* const allocator_;
648 ArenaStack* const arena_stack_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000649
650 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100651 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000652
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100653 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100654 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000655
Aart Bik281c6812016-08-26 11:31:48 -0700656 // List of blocks to perform a linear order tree traversal. Unlike the reverse
657 // post order, this order is not incrementally kept up-to-date.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100658 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100659
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000660 HBasicBlock* entry_block_;
661 HBasicBlock* exit_block_;
662
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100663 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100664 uint16_t maximum_number_of_out_vregs_;
665
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100666 // The number of virtual registers in this method. Contains the parameters.
667 uint16_t number_of_vregs_;
668
669 // The number of virtual registers used by parameters of this method.
670 uint16_t number_of_in_vregs_;
671
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000672 // Number of vreg size slots that the temporaries use (used in baseline compiler).
673 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100674
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800675 // Flag whether there are bounds checks in the graph. We can skip
676 // BCE if it's false. It's only best effort to keep it up to date in
677 // the presence of code elimination so there might be false positives.
Mark Mendell1152c922015-04-24 17:06:35 -0400678 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800679
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800680 // Flag whether there are try/catch blocks in the graph. We will skip
681 // try/catch-related passes if it's false. It's only best effort to keep
682 // it up to date in the presence of code elimination so there might be
683 // false positives.
David Brazdil77a48ae2015-09-15 12:34:04 +0000684 bool has_try_catch_;
685
Aart Bikb13c65b2017-03-21 20:14:07 -0700686 // Flag whether SIMD instructions appear in the graph. If true, the
687 // code generators may have to be more careful spilling the wider
688 // contents of SIMD registers.
689 bool has_simd_;
690
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800691 // Flag whether there are any loops in the graph. We can skip loop
692 // optimization if it's false. It's only best effort to keep it up
693 // to date in the presence of code elimination so there might be false
694 // positives.
695 bool has_loops_;
696
697 // Flag whether there are any irreducible loops in the graph. It's only
698 // best effort to keep it up to date in the presence of code elimination
699 // so there might be false positives.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000700 bool has_irreducible_loops_;
701
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000702 // Indicates whether the graph should be compiled in a way that
703 // ensures full debuggability. If false, we can apply more
704 // aggressive optimizations that may limit the level of debugging.
705 const bool debuggable_;
706
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000707 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000708 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000709
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100710 // The dex file from which the method is from.
711 const DexFile& dex_file_;
712
713 // The method index in the dex file.
714 const uint32_t method_idx_;
715
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100716 // If inlined, this encodes how the callee is being invoked.
717 const InvokeType invoke_type_;
718
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100719 // Whether the graph has been transformed to SSA form. Only used
720 // in debug mode to ensure we are not using properties only valid
721 // for non-SSA form (like the number of temporaries).
722 bool in_ssa_form_;
723
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800724 // Number of CHA guards in the graph. Used to short-circuit the
725 // CHA guard optimization pass when there is no CHA guard left.
726 uint32_t number_of_cha_guards_;
727
Mathieu Chartiere401d142015-04-22 13:56:20 -0700728 const InstructionSet instruction_set_;
729
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000730 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000731 HNullConstant* cached_null_constant_;
732 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000733 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000734 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000735 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000736
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100737 HCurrentMethod* cached_current_method_;
738
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100739 // The ArtMethod this graph is for. Note that for AOT, it may be null,
740 // for example for methods whose declaring class could not be resolved
741 // (such as when the superclass could not be found).
742 ArtMethod* art_method_;
743
David Brazdil4833f5a2015-12-16 10:37:39 +0000744 // Keep the RTI of inexact Object to avoid having to pass stack handle
745 // collection pointer to passes which may create NullConstant.
746 ReferenceTypeInfo inexact_object_rti_;
747
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000748 // Whether we are compiling this graph for on stack replacement: this will
749 // make all loops seen as irreducible and emit special stack maps to mark
750 // compiled code entries which the interpreter can directly jump to.
751 const bool osr_;
752
Mingyao Yang063fc772016-08-02 11:02:54 -0700753 // List of methods that are assumed to have single implementation.
754 ArenaSet<ArtMethod*> cha_single_implementation_list_;
755
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000756 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100757 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000758 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000759 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000760 DISALLOW_COPY_AND_ASSIGN(HGraph);
761};
762
Vladimir Markof9f64412015-09-02 14:05:49 +0100763class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000764 public:
765 HLoopInformation(HBasicBlock* header, HGraph* graph)
766 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100767 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000768 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100769 contains_irreducible_loop_(false),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100770 back_edges_(graph->GetAllocator()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100771 // Make bit vector growable, as the number of blocks may change.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100772 blocks_(graph->GetAllocator(),
773 graph->GetBlocks().size(),
774 true,
775 kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100776 back_edges_.reserve(kDefaultNumberOfBackEdges);
777 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100778
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000779 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100780 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000781
782 void Dump(std::ostream& os);
783
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100784 HBasicBlock* GetHeader() const {
785 return header_;
786 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000787
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000788 void SetHeader(HBasicBlock* block) {
789 header_ = block;
790 }
791
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100792 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
793 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
794 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
795
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000796 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100797 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000798 }
799
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100800 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100801 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100802 }
803
David Brazdil46e2a392015-03-16 17:31:52 +0000804 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100805 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100806 }
807
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000808 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100809 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000810 }
811
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100812 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100813
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100814 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100815 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100816 }
817
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100818 // Returns the lifetime position of the back edge that has the
819 // greatest lifetime position.
820 size_t GetLifetimeEnd() const;
821
822 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100823 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100824 }
825
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000826 // Finds blocks that are part of this loop.
827 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100828
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100829 // Returns whether this loop information contains `block`.
830 // Note that this loop information *must* be populated before entering this function.
831 bool Contains(const HBasicBlock& block) const;
832
833 // Returns whether this loop information is an inner loop of `other`.
834 // Note that `other` *must* be populated before entering this function.
835 bool IsIn(const HLoopInformation& other) const;
836
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800837 // Returns true if instruction is not defined within this loop.
838 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700839
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100840 const ArenaBitVector& GetBlocks() const { return blocks_; }
841
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000842 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000843 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000844
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000845 void ClearAllBlocks() {
846 blocks_.ClearAllBits();
847 }
848
David Brazdil3f4a5222016-05-06 12:46:21 +0100849 bool HasBackEdgeNotDominatedByHeader() const;
850
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100851 bool IsPopulated() const {
852 return blocks_.GetHighestBitSet() != -1;
853 }
854
Anton Shaminf89381f2016-05-16 16:44:13 +0600855 bool DominatesAllBackEdges(HBasicBlock* block);
856
David Sehrc757dec2016-11-04 15:48:34 -0700857 bool HasExitEdge() const;
858
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000859 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100860 // Internal recursive implementation of `Populate`.
861 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100862 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100863
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000864 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100865 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000866 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100867 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100868 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100869 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000870
871 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
872};
873
David Brazdilec16f792015-08-19 15:04:01 +0100874// Stores try/catch information for basic blocks.
875// Note that HGraph is constructed so that catch blocks cannot simultaneously
876// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100877class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100878 public:
879 // Try block information constructor.
880 explicit TryCatchInformation(const HTryBoundary& try_entry)
881 : try_entry_(&try_entry),
882 catch_dex_file_(nullptr),
883 catch_type_index_(DexFile::kDexNoIndex16) {
884 DCHECK(try_entry_ != nullptr);
885 }
886
887 // Catch block information constructor.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800888 TryCatchInformation(dex::TypeIndex catch_type_index, const DexFile& dex_file)
David Brazdilec16f792015-08-19 15:04:01 +0100889 : try_entry_(nullptr),
890 catch_dex_file_(&dex_file),
891 catch_type_index_(catch_type_index) {}
892
893 bool IsTryBlock() const { return try_entry_ != nullptr; }
894
895 const HTryBoundary& GetTryEntry() const {
896 DCHECK(IsTryBlock());
897 return *try_entry_;
898 }
899
900 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
901
902 bool IsCatchAllTypeIndex() const {
903 DCHECK(IsCatchBlock());
Andreas Gampea5b09a62016-11-17 15:21:22 -0800904 return !catch_type_index_.IsValid();
David Brazdilec16f792015-08-19 15:04:01 +0100905 }
906
Andreas Gampea5b09a62016-11-17 15:21:22 -0800907 dex::TypeIndex GetCatchTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +0100908 DCHECK(IsCatchBlock());
909 return catch_type_index_;
910 }
911
912 const DexFile& GetCatchDexFile() const {
913 DCHECK(IsCatchBlock());
914 return *catch_dex_file_;
915 }
916
917 private:
918 // One of possibly several TryBoundary instructions entering the block's try.
919 // Only set for try blocks.
920 const HTryBoundary* try_entry_;
921
922 // Exception type information. Only set for catch blocks.
923 const DexFile* catch_dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800924 const dex::TypeIndex catch_type_index_;
David Brazdilec16f792015-08-19 15:04:01 +0100925};
926
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100927static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100928static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100929
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000930// A block in a method. Contains the list of instructions represented
931// as a double linked list. Each block knows its predecessors and
932// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100933
Vladimir Markof9f64412015-09-02 14:05:49 +0100934class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000935 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700936 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000937 : graph_(graph),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100938 predecessors_(graph->GetAllocator()->Adapter(kArenaAllocPredecessors)),
939 successors_(graph->GetAllocator()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000940 loop_information_(nullptr),
941 dominator_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100942 dominated_blocks_(graph->GetAllocator()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100943 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100944 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100945 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000946 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000947 try_catch_information_(nullptr) {
948 predecessors_.reserve(kDefaultNumberOfPredecessors);
949 successors_.reserve(kDefaultNumberOfSuccessors);
950 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
951 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000952
Vladimir Marko60584552015-09-03 13:35:12 +0000953 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100954 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000955 }
956
Vladimir Marko60584552015-09-03 13:35:12 +0000957 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100958 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000959 }
960
David Brazdild26a4112015-11-10 11:07:31 +0000961 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
962 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
963
Vladimir Marko60584552015-09-03 13:35:12 +0000964 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
965 return ContainsElement(successors_, block, start_from);
966 }
967
968 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100969 return dominated_blocks_;
970 }
971
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100972 bool IsEntryBlock() const {
973 return graph_->GetEntryBlock() == this;
974 }
975
976 bool IsExitBlock() const {
977 return graph_->GetExitBlock() == this;
978 }
979
David Brazdil46e2a392015-03-16 17:31:52 +0000980 bool IsSingleGoto() const;
Mads Ager16e52892017-07-14 13:11:37 +0200981 bool IsSingleReturn() const;
Mingyao Yang46721ef2017-10-05 14:45:17 -0700982 bool IsSingleReturnOrReturnVoidAllowingPhis() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000983 bool IsSingleTryBoundary() const;
984
985 // Returns true if this block emits nothing but a jump.
986 bool IsSingleJump() const {
987 HLoopInformation* loop_info = GetLoopInformation();
988 return (IsSingleGoto() || IsSingleTryBoundary())
989 // Back edges generate a suspend check.
990 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
991 }
David Brazdil46e2a392015-03-16 17:31:52 +0000992
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000993 void AddBackEdge(HBasicBlock* back_edge) {
994 if (loop_information_ == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100995 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000996 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100997 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000998 loop_information_->AddBackEdge(back_edge);
999 }
1000
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001001 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001002 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001003
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001004 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001005 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001006 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001007
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001008 HBasicBlock* GetDominator() const { return dominator_; }
1009 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +00001010 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
1011
1012 void RemoveDominatedBlock(HBasicBlock* block) {
1013 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +01001014 }
Vladimir Marko60584552015-09-03 13:35:12 +00001015
1016 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
1017 ReplaceElement(dominated_blocks_, existing, new_block);
1018 }
1019
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001020 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001021
1022 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001023 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001024 }
1025
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001026 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
1027 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +01001028 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001029 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +01001030 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
1031 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001032
Nicolas Geoffray09aa1472016-01-19 10:52:54 +00001033 HInstruction* GetFirstInstructionDisregardMoves() const;
1034
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001035 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001036 successors_.push_back(block);
1037 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001038 }
1039
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001040 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
1041 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001042 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001043 new_block->predecessors_.push_back(this);
1044 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001045 }
1046
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001047 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
1048 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001049 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001050 new_block->successors_.push_back(this);
1051 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001052 }
1053
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001054 // Insert `this` between `predecessor` and `successor. This method
1055 // preserves the indicies, and will update the first edge found between
1056 // `predecessor` and `successor`.
1057 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
1058 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001059 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +00001060 successor->predecessors_[predecessor_index] = this;
1061 predecessor->successors_[successor_index] = this;
1062 successors_.push_back(successor);
1063 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001064 }
1065
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001066 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001067 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001068 }
1069
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001070 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001071 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001072 }
1073
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001074 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001075 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001076 }
1077
1078 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001079 predecessors_.push_back(block);
1080 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001081 }
1082
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001083 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001084 DCHECK_EQ(predecessors_.size(), 2u);
1085 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001086 }
1087
David Brazdil769c9e52015-04-27 13:54:09 +01001088 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001089 DCHECK_EQ(successors_.size(), 2u);
1090 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +01001091 }
1092
David Brazdilfc6a86a2015-06-26 10:33:45 +00001093 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001094 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001095 }
1096
David Brazdilfc6a86a2015-06-26 10:33:45 +00001097 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001098 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001099 }
1100
David Brazdilfc6a86a2015-06-26 10:33:45 +00001101 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001102 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001103 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001104 }
1105
1106 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001107 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001108 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001109 }
1110
1111 // Returns whether the first occurrence of `predecessor` in the list of
1112 // predecessors is at index `idx`.
1113 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001114 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001115 return GetPredecessorIndexOf(predecessor) == idx;
1116 }
1117
David Brazdild7558da2015-09-22 13:04:14 +01001118 // Create a new block between this block and its predecessors. The new block
1119 // is added to the graph, all predecessor edges are relinked to it and an edge
1120 // is created to `this`. Returns the new empty block. Reverse post order or
1121 // loop and try/catch information are not updated.
1122 HBasicBlock* CreateImmediateDominator();
1123
David Brazdilfc6a86a2015-06-26 10:33:45 +00001124 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001125 // created, latter block. Note that this method will add the block to the
1126 // graph, create a Goto at the end of the former block and will create an edge
1127 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001128 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001129 HBasicBlock* SplitBefore(HInstruction* cursor);
1130
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001131 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001132 // created block. Note that this method just updates raw block information,
1133 // like predecessors, successors, dominators, and instruction list. It does not
1134 // update the graph, reverse post order, loop information, nor make sure the
1135 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001136 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1137
1138 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1139 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001140
1141 // Merge `other` at the end of `this`. Successors and dominated blocks of
1142 // `other` are changed to be successors and dominated blocks of `this`. Note
1143 // that this method does not update the graph, reverse post order, loop
1144 // information, nor make sure the blocks are consistent (for example ending
1145 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001146 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001147
1148 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1149 // of `this` are moved to `other`.
1150 // Note that this method does not update the graph, reverse post order, loop
1151 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001152 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001153 void ReplaceWith(HBasicBlock* other);
1154
Aart Bik6b69e0a2017-01-11 10:20:43 -08001155 // Merges the instructions of `other` at the end of `this`.
1156 void MergeInstructionsWith(HBasicBlock* other);
1157
David Brazdil2d7352b2015-04-20 14:52:42 +01001158 // Merge `other` at the end of `this`. This method updates loops, reverse post
1159 // order, links to predecessors, successors, dominators and deletes the block
1160 // from the graph. The two blocks must be successive, i.e. `this` the only
1161 // predecessor of `other` and vice versa.
1162 void MergeWith(HBasicBlock* other);
1163
1164 // Disconnects `this` from all its predecessors, successors and dominator,
1165 // removes it from all loops it is included in and eventually from the graph.
1166 // The block must not dominate any other block. Predecessors and successors
1167 // are safely updated.
1168 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001169
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001170 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001171 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001172 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001173 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Artem Serovcced8ba2017-07-19 18:18:09 +01001174 // Replace phi `initial` with `replacement` within this block.
1175 void ReplaceAndRemovePhiWith(HPhi* initial, HPhi* replacement);
Roland Levillainccc07a92014-09-16 14:48:16 +01001176 // Replace instruction `initial` with `replacement` within this block.
1177 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1178 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001179 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001180 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001181 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1182 // instruction list. With 'ensure_safety' set to true, it verifies that the
1183 // instruction is not in use and removes it from the use lists of its inputs.
1184 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1185 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001186 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001187
1188 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001189 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001190 }
1191
Roland Levillain6b879dd2014-09-22 17:13:44 +01001192 bool IsLoopPreHeaderFirstPredecessor() const {
1193 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001194 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001195 }
1196
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001197 bool IsFirstPredecessorBackEdge() const {
1198 DCHECK(IsLoopHeader());
1199 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1200 }
1201
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001202 HLoopInformation* GetLoopInformation() const {
1203 return loop_information_;
1204 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001205
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001206 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001207 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001208 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001209 void SetInLoop(HLoopInformation* info) {
1210 if (IsLoopHeader()) {
1211 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001212 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001213 loop_information_ = info;
1214 } else if (loop_information_->Contains(*info->GetHeader())) {
1215 // Block is currently part of an outer loop. Make it part of this inner loop.
1216 // Note that a non loop header having a loop information means this loop information
1217 // has already been populated
1218 loop_information_ = info;
1219 } else {
1220 // Block is part of an inner loop. Do not update the loop information.
1221 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1222 // at this point, because this method is being called while populating `info`.
1223 }
1224 }
1225
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001226 // Raw update of the loop information.
1227 void SetLoopInformation(HLoopInformation* info) {
1228 loop_information_ = info;
1229 }
1230
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001231 bool IsInLoop() const { return loop_information_ != nullptr; }
1232
David Brazdilec16f792015-08-19 15:04:01 +01001233 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1234
1235 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1236 try_catch_information_ = try_catch_information;
1237 }
1238
1239 bool IsTryBlock() const {
1240 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1241 }
1242
1243 bool IsCatchBlock() const {
1244 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1245 }
David Brazdilffee3d32015-07-06 11:48:53 +01001246
1247 // Returns the try entry that this block's successors should have. They will
1248 // be in the same try, unless the block ends in a try boundary. In that case,
1249 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001250 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001251
David Brazdild7558da2015-09-22 13:04:14 +01001252 bool HasThrowingInstructions() const;
1253
David Brazdila4b8c212015-05-07 09:59:30 +01001254 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001255 bool Dominates(HBasicBlock* block) const;
1256
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001257 size_t GetLifetimeStart() const { return lifetime_start_; }
1258 size_t GetLifetimeEnd() const { return lifetime_end_; }
1259
1260 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1261 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1262
David Brazdil8d5b8b22015-03-24 10:51:52 +00001263 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001264 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001265 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001266 bool HasSinglePhi() const;
1267
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001268 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001269 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001270 ArenaVector<HBasicBlock*> predecessors_;
1271 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001272 HInstructionList instructions_;
1273 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001274 HLoopInformation* loop_information_;
1275 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001276 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001277 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001278 // The dex program counter of the first instruction of this block.
1279 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001280 size_t lifetime_start_;
1281 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001282 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001283
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001284 friend class HGraph;
1285 friend class HInstruction;
1286
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001287 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1288};
1289
David Brazdilb2bd1c52015-03-25 11:17:37 +00001290// Iterates over the LoopInformation of all loops which contain 'block'
1291// from the innermost to the outermost.
1292class HLoopInformationOutwardIterator : public ValueObject {
1293 public:
1294 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1295 : current_(block.GetLoopInformation()) {}
1296
1297 bool Done() const { return current_ == nullptr; }
1298
1299 void Advance() {
1300 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001301 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001302 }
1303
1304 HLoopInformation* Current() const {
1305 DCHECK(!Done());
1306 return current_;
1307 }
1308
1309 private:
1310 HLoopInformation* current_;
1311
1312 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1313};
1314
Alexandre Ramesef20f712015-06-09 10:29:30 +01001315#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001316 M(Above, Condition) \
1317 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001318 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001319 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001320 M(ArrayGet, Instruction) \
1321 M(ArrayLength, Instruction) \
1322 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001323 M(Below, Condition) \
1324 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001325 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001326 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001327 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001328 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001329 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001330 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001331 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001332 M(Compare, BinaryOperation) \
Igor Murashkind01745e2017-04-05 16:40:31 -07001333 M(ConstructorFence, Instruction) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001334 M(CurrentMethod, Instruction) \
Mingyao Yang063fc772016-08-02 11:02:54 -07001335 M(ShouldDeoptimizeFlag, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001336 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001337 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001338 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001339 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001340 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001341 M(Exit, Instruction) \
1342 M(FloatConstant, Constant) \
1343 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001344 M(GreaterThan, Condition) \
1345 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001346 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001347 M(InstanceFieldGet, Instruction) \
1348 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001349 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001350 M(IntConstant, Constant) \
xueliang.zhonge0eb4832017-10-30 13:43:14 +00001351 M(IntermediateAddress, Instruction) \
Calin Juravle175dc732015-08-25 15:42:32 +01001352 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001353 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001354 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001355 M(InvokeVirtual, Invoke) \
Orion Hodsonac141392017-01-13 11:53:47 +00001356 M(InvokePolymorphic, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001357 M(LessThan, Condition) \
1358 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001359 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001360 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001361 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001362 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001363 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001364 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001365 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001366 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001367 M(Neg, UnaryOperation) \
1368 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001369 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001370 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001371 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001372 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001373 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001374 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001375 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001376 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001377 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001378 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001379 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001380 M(Return, Instruction) \
1381 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001382 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001383 M(Shl, BinaryOperation) \
1384 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001385 M(StaticFieldGet, Instruction) \
1386 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001387 M(UnresolvedInstanceFieldGet, Instruction) \
1388 M(UnresolvedInstanceFieldSet, Instruction) \
1389 M(UnresolvedStaticFieldGet, Instruction) \
1390 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001391 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001392 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001393 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001394 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001395 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001396 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001397 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001398 M(Xor, BinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001399 M(VecReplicateScalar, VecUnaryOperation) \
Aart Bik0148de42017-09-05 09:25:01 -07001400 M(VecExtractScalar, VecUnaryOperation) \
1401 M(VecReduce, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001402 M(VecCnv, VecUnaryOperation) \
1403 M(VecNeg, VecUnaryOperation) \
Aart Bik6daebeb2017-04-03 14:35:41 -07001404 M(VecAbs, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001405 M(VecNot, VecUnaryOperation) \
1406 M(VecAdd, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001407 M(VecHalvingAdd, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001408 M(VecSub, VecBinaryOperation) \
1409 M(VecMul, VecBinaryOperation) \
1410 M(VecDiv, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001411 M(VecMin, VecBinaryOperation) \
1412 M(VecMax, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001413 M(VecAnd, VecBinaryOperation) \
1414 M(VecAndNot, VecBinaryOperation) \
1415 M(VecOr, VecBinaryOperation) \
1416 M(VecXor, VecBinaryOperation) \
1417 M(VecShl, VecBinaryOperation) \
1418 M(VecShr, VecBinaryOperation) \
1419 M(VecUShr, VecBinaryOperation) \
Aart Bik8de59162017-04-21 09:42:01 -07001420 M(VecSetScalars, VecOperation) \
Artem Serovf34dd202017-04-10 17:41:46 +01001421 M(VecMultiplyAccumulate, VecOperation) \
Aart Bikdbbac8f2017-09-01 13:06:08 -07001422 M(VecSADAccumulate, VecOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001423 M(VecLoad, VecMemoryOperation) \
1424 M(VecStore, VecMemoryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001425
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001426/*
1427 * Instructions, shared across several (not all) architectures.
1428 */
1429#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1430#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1431#else
1432#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001433 M(BitwiseNegatedRight, Instruction) \
Anton Kirilov74234da2017-01-13 14:42:47 +00001434 M(DataProcWithShifterOp, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001435 M(MultiplyAccumulate, Instruction) \
Artem Serove1811ed2017-04-27 16:50:47 +01001436 M(IntermediateAddressIndex, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001437#endif
1438
Alexandre Ramesef20f712015-06-09 10:29:30 +01001439#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1440
1441#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1442
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001443#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001444#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001445#else
1446#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1447 M(MipsComputeBaseMethodAddress, Instruction) \
Lena Djokica2901602017-09-21 13:50:52 +02001448 M(MipsPackedSwitch, Instruction) \
1449 M(IntermediateArrayAddressIndex, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001450#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001451
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001452#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1453
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001454#ifndef ART_ENABLE_CODEGEN_x86
1455#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1456#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001457#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1458 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001459 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001460 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001461 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001462#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001463
1464#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1465
1466#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1467 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001468 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001469 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1470 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001471 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001472 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001473 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1474 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1475
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001476#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1477 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001478 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001479 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001480 M(BinaryOperation, Instruction) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001481 M(Invoke, Instruction) \
1482 M(VecOperation, Instruction) \
1483 M(VecUnaryOperation, VecOperation) \
1484 M(VecBinaryOperation, VecOperation) \
1485 M(VecMemoryOperation, VecOperation)
Dave Allison20dfc792014-06-16 20:44:29 -07001486
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001487#define FOR_EACH_INSTRUCTION(M) \
1488 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1489 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1490
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001491#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001492FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1493#undef FORWARD_DECLARATION
1494
Vladimir Marko372f10e2016-05-17 16:30:10 +01001495#define DECLARE_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001496 private: \
1497 H##type& operator=(const H##type&) = delete; \
1498 public: \
Vladimir Marko372f10e2016-05-17 16:30:10 +01001499 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
1500 const char* DebugName() const OVERRIDE { return #type; } \
1501 bool InstructionTypeEquals(const HInstruction* other) const OVERRIDE { \
1502 return other->Is##type(); \
1503 } \
Artem Serovcced8ba2017-07-19 18:18:09 +01001504 HInstruction* Clone(ArenaAllocator* arena) const OVERRIDE { \
1505 DCHECK(IsClonable()); \
1506 return new (arena) H##type(*this->As##type()); \
1507 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001508 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001509
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001510#define DECLARE_ABSTRACT_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001511 private: \
1512 H##type& operator=(const H##type&) = delete; \
1513 public: \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001514 bool Is##type() const { return As##type() != nullptr; } \
1515 const H##type* As##type() const { return this; } \
1516 H##type* As##type() { return this; }
1517
Artem Serovcced8ba2017-07-19 18:18:09 +01001518#define DEFAULT_COPY_CONSTRUCTOR(type) \
1519 explicit H##type(const H##type& other) = default;
1520
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001521template <typename T>
Vladimir Marko82b07402017-03-01 19:02:04 +00001522class HUseListNode : public ArenaObject<kArenaAllocUseListNode>,
1523 public IntrusiveForwardListNode<HUseListNode<T>> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001524 public:
Igor Murashkind01745e2017-04-05 16:40:31 -07001525 // Get the instruction which has this use as one of the inputs.
David Brazdiled596192015-01-23 10:39:45 +00001526 T GetUser() const { return user_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001527 // Get the position of the input record that this use corresponds to.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001528 size_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001529 // Set the position of the input record that this use corresponds to.
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001530 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001531
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001532 private:
David Brazdiled596192015-01-23 10:39:45 +00001533 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001534 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001535
1536 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001537 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001538
Vladimir Marko46817b82016-03-29 12:21:58 +01001539 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001540
1541 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1542};
1543
David Brazdiled596192015-01-23 10:39:45 +00001544template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001545using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001546
David Brazdil1abb4192015-02-17 18:33:36 +00001547// This class is used by HEnvironment and HInstruction classes to record the
1548// instructions they use and pointers to the corresponding HUseListNodes kept
1549// by the used instructions.
1550template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001551class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001552 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001553 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1554 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001555
Vladimir Marko46817b82016-03-29 12:21:58 +01001556 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1557 : HUserRecord(old_record.instruction_, before_use_node) {}
1558 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1559 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001560 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001561 }
1562
1563 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001564 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1565 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001566
1567 private:
1568 // Instruction used by the user.
1569 HInstruction* instruction_;
1570
Vladimir Marko46817b82016-03-29 12:21:58 +01001571 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1572 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001573};
1574
Vladimir Markoe9004912016-06-16 16:50:52 +01001575// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1576// This is used for HInstruction::GetInputs() to return a container wrapper providing
1577// HInstruction* values even though the underlying container has HUserRecord<>s.
1578struct HInputExtractor {
1579 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1580 return record.GetInstruction();
1581 }
1582 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1583 return record.GetInstruction();
1584 }
1585};
1586
1587using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1588using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1589
Aart Bik854a02b2015-07-14 16:07:00 -07001590/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001591 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001592 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001593 * For write/read dependences on fields/arrays, the dependence analysis uses
1594 * type disambiguation (e.g. a float field write cannot modify the value of an
1595 * integer field read) and the access type (e.g. a reference array write cannot
1596 * modify the value of a reference field read [although it may modify the
1597 * reference fetch prior to reading the field, which is represented by its own
1598 * write/read dependence]). The analysis makes conservative points-to
1599 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1600 * the same, and any reference read depends on any reference read without
1601 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001602 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001603 * The internal representation uses 38-bit and is described in the table below.
1604 * The first line indicates the side effect, and for field/array accesses the
1605 * second line indicates the type of the access (in the order of the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001606 * DataType::Type enum).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001607 * The two numbered lines below indicate the bit position in the bitfield (read
1608 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001609 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001610 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1611 * +-------------+---------+---------+--------------+---------+---------+
1612 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1613 * | 3 |333333322|222222221| 1 |111111110|000000000|
1614 * | 7 |654321098|765432109| 8 |765432109|876543210|
1615 *
1616 * Note that, to ease the implementation, 'changes' bits are least significant
1617 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001618 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001619class SideEffects : public ValueObject {
1620 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001621 SideEffects() : flags_(0) {}
1622
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001623 static SideEffects None() {
1624 return SideEffects(0);
1625 }
1626
1627 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001628 return SideEffects(kAllChangeBits | kAllDependOnBits);
1629 }
1630
1631 static SideEffects AllChanges() {
1632 return SideEffects(kAllChangeBits);
1633 }
1634
1635 static SideEffects AllDependencies() {
1636 return SideEffects(kAllDependOnBits);
1637 }
1638
1639 static SideEffects AllExceptGCDependency() {
1640 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1641 }
1642
1643 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001644 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001645 }
1646
Aart Bik34c3ba92015-07-20 14:08:59 -07001647 static SideEffects AllWrites() {
1648 return SideEffects(kAllWrites);
1649 }
1650
1651 static SideEffects AllReads() {
1652 return SideEffects(kAllReads);
1653 }
1654
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001655 static SideEffects FieldWriteOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001656 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001657 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001658 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001659 }
1660
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001661 static SideEffects ArrayWriteOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001662 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001663 }
1664
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001665 static SideEffects FieldReadOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001666 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001667 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001668 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001669 }
1670
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001671 static SideEffects ArrayReadOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001672 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001673 }
1674
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001675 static SideEffects CanTriggerGC() {
1676 return SideEffects(1ULL << kCanTriggerGCBit);
1677 }
1678
1679 static SideEffects DependsOnGC() {
1680 return SideEffects(1ULL << kDependsOnGCBit);
1681 }
1682
Aart Bik854a02b2015-07-14 16:07:00 -07001683 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001684 SideEffects Union(SideEffects other) const {
1685 return SideEffects(flags_ | other.flags_);
1686 }
1687
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001688 SideEffects Exclusion(SideEffects other) const {
1689 return SideEffects(flags_ & ~other.flags_);
1690 }
1691
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001692 void Add(SideEffects other) {
1693 flags_ |= other.flags_;
1694 }
1695
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001696 bool Includes(SideEffects other) const {
1697 return (other.flags_ & flags_) == other.flags_;
1698 }
1699
1700 bool HasSideEffects() const {
1701 return (flags_ & kAllChangeBits);
1702 }
1703
1704 bool HasDependencies() const {
1705 return (flags_ & kAllDependOnBits);
1706 }
1707
1708 // Returns true if there are no side effects or dependencies.
1709 bool DoesNothing() const {
1710 return flags_ == 0;
1711 }
1712
Aart Bik854a02b2015-07-14 16:07:00 -07001713 // Returns true if something is written.
1714 bool DoesAnyWrite() const {
1715 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001716 }
1717
Aart Bik854a02b2015-07-14 16:07:00 -07001718 // Returns true if something is read.
1719 bool DoesAnyRead() const {
1720 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001721 }
1722
Aart Bik854a02b2015-07-14 16:07:00 -07001723 // Returns true if potentially everything is written and read
1724 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001725 bool DoesAllReadWrite() const {
1726 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1727 }
1728
Aart Bik854a02b2015-07-14 16:07:00 -07001729 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001730 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001731 }
1732
Roland Levillain0d5a2812015-11-13 10:07:31 +00001733 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001734 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001735 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1736 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001737 }
1738
1739 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001740 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001741 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001742 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001743 for (int s = kLastBit; s >= 0; s--) {
1744 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1745 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1746 // This is a bit for the GC side effect.
1747 if (current_bit_is_set) {
1748 flags += "GC";
1749 }
Aart Bik854a02b2015-07-14 16:07:00 -07001750 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001751 } else {
1752 // This is a bit for the array/field analysis.
1753 // The underscore character stands for the 'can trigger GC' bit.
1754 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1755 if (current_bit_is_set) {
1756 flags += kDebug[s];
1757 }
1758 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1759 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1760 flags += "|";
1761 }
1762 }
Aart Bik854a02b2015-07-14 16:07:00 -07001763 }
1764 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001765 }
1766
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001767 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001768
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001769 private:
1770 static constexpr int kFieldArrayAnalysisBits = 9;
1771
1772 static constexpr int kFieldWriteOffset = 0;
1773 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1774 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1775 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1776
1777 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1778
1779 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1780 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1781 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1782 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1783
1784 static constexpr int kLastBit = kDependsOnGCBit;
1785 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1786
1787 // Aliases.
1788
1789 static_assert(kChangeBits == kDependOnBits,
1790 "the 'change' bits should match the 'depend on' bits.");
1791
1792 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1793 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1794 static constexpr uint64_t kAllWrites =
1795 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1796 static constexpr uint64_t kAllReads =
1797 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001798
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001799 // Translates type to bit flag. The type must correspond to a Java type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001800 static uint64_t TypeFlag(DataType::Type type, int offset) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001801 int shift;
1802 switch (type) {
1803 case DataType::Type::kReference: shift = 0; break;
1804 case DataType::Type::kBool: shift = 1; break;
1805 case DataType::Type::kInt8: shift = 2; break;
1806 case DataType::Type::kUint16: shift = 3; break;
1807 case DataType::Type::kInt16: shift = 4; break;
1808 case DataType::Type::kInt32: shift = 5; break;
1809 case DataType::Type::kInt64: shift = 6; break;
1810 case DataType::Type::kFloat32: shift = 7; break;
1811 case DataType::Type::kFloat64: shift = 8; break;
1812 default:
1813 LOG(FATAL) << "Unexpected data type " << type;
1814 UNREACHABLE();
1815 }
Aart Bik854a02b2015-07-14 16:07:00 -07001816 DCHECK_LE(kFieldWriteOffset, shift);
1817 DCHECK_LT(shift, kArrayWriteOffset);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001818 return UINT64_C(1) << (shift + offset);
Aart Bik854a02b2015-07-14 16:07:00 -07001819 }
1820
1821 // Private constructor on direct flags value.
1822 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1823
1824 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001825};
1826
David Brazdiled596192015-01-23 10:39:45 +00001827// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001828class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001829 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001830 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
Mingyao Yang01b47b02017-02-03 12:09:57 -08001831 size_t number_of_vregs,
1832 ArtMethod* method,
1833 uint32_t dex_pc,
1834 HInstruction* holder)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001835 : vregs_(number_of_vregs, allocator->Adapter(kArenaAllocEnvironmentVRegs)),
1836 locations_(allocator->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001837 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001838 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001839 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001840 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001841 }
1842
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001843 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
1844 const HEnvironment& to_copy,
1845 HInstruction* holder)
1846 : HEnvironment(allocator,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001847 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001848 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001849 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001850 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001851
Vladimir Markoec32f642017-06-02 10:51:55 +01001852 void AllocateLocations() {
1853 DCHECK(locations_.empty());
1854 locations_.resize(vregs_.size());
1855 }
1856
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001857 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001858 if (parent_ != nullptr) {
1859 parent_->SetAndCopyParentChain(allocator, parent);
1860 } else {
1861 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1862 parent_->CopyFrom(parent);
1863 if (parent->GetParent() != nullptr) {
1864 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1865 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001866 }
David Brazdiled596192015-01-23 10:39:45 +00001867 }
1868
Vladimir Marko69d310e2017-10-09 14:12:23 +01001869 void CopyFrom(ArrayRef<HInstruction* const> locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001870 void CopyFrom(HEnvironment* environment);
1871
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001872 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1873 // input to the loop phi instead. This is for inserting instructions that
1874 // require an environment (like HDeoptimization) in the loop pre-header.
1875 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001876
1877 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001878 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001879 }
1880
1881 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001882 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001883 }
1884
David Brazdil1abb4192015-02-17 18:33:36 +00001885 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001886
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001887 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001888
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001889 HEnvironment* GetParent() const { return parent_; }
1890
1891 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001892 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001893 }
1894
1895 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001896 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001897 }
1898
1899 uint32_t GetDexPc() const {
1900 return dex_pc_;
1901 }
1902
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001903 ArtMethod* GetMethod() const {
1904 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001905 }
1906
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001907 HInstruction* GetHolder() const {
1908 return holder_;
1909 }
1910
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001911
1912 bool IsFromInlinedInvoke() const {
1913 return GetParent() != nullptr;
1914 }
1915
David Brazdiled596192015-01-23 10:39:45 +00001916 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001917 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1918 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001919 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001920 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001921 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00001922
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001923 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001924 HInstruction* const holder_;
1925
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001926 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001927
1928 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1929};
1930
Vladimir Markof9f64412015-09-02 14:05:49 +01001931class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001932 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001933 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001934 : previous_(nullptr),
1935 next_(nullptr),
1936 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001937 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001938 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001939 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001940 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001941 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001942 locations_(nullptr),
1943 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001944 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001945 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001946 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1947 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1948 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001949
Dave Allison20dfc792014-06-16 20:44:29 -07001950 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001951
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001952#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001953 enum InstructionKind {
1954 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1955 };
1956#undef DECLARE_KIND
1957
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001958 HInstruction* GetNext() const { return next_; }
1959 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001960
Calin Juravle77520bc2015-01-12 18:45:46 +00001961 HInstruction* GetNextDisregardingMoves() const;
1962 HInstruction* GetPreviousDisregardingMoves() const;
1963
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001964 HBasicBlock* GetBlock() const { return block_; }
Vladimir Markoca6fff82017-10-03 14:49:14 +01001965 ArenaAllocator* GetAllocator() const { return block_->GetGraph()->GetAllocator(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001966 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001967 bool IsInBlock() const { return block_ != nullptr; }
1968 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001969 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1970 bool IsIrreducibleLoopHeaderPhi() const {
1971 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1972 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001973
Vladimir Marko372f10e2016-05-17 16:30:10 +01001974 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
1975
1976 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
1977 // One virtual method is enough, just const_cast<> and then re-add the const.
1978 return ArrayRef<const HUserRecord<HInstruction*>>(
1979 const_cast<HInstruction*>(this)->GetInputRecords());
1980 }
1981
Vladimir Markoe9004912016-06-16 16:50:52 +01001982 HInputsRef GetInputs() {
1983 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001984 }
1985
Vladimir Markoe9004912016-06-16 16:50:52 +01001986 HConstInputsRef GetInputs() const {
1987 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001988 }
1989
1990 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001991 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001992
Aart Bik2767f4b2016-10-28 15:03:53 -07001993 bool HasInput(HInstruction* input) const {
1994 for (const HInstruction* i : GetInputs()) {
1995 if (i == input) {
1996 return true;
1997 }
1998 }
1999 return false;
2000 }
2001
Vladimir Marko372f10e2016-05-17 16:30:10 +01002002 void SetRawInputAt(size_t index, HInstruction* input) {
2003 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
2004 }
2005
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002006 virtual void Accept(HGraphVisitor* visitor) = 0;
2007 virtual const char* DebugName() const = 0;
2008
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002009 virtual DataType::Type GetType() const { return DataType::Type::kVoid; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002010
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002011 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002012
2013 uint32_t GetDexPc() const { return dex_pc_; }
2014
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002015 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01002016
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002017 // Can the instruction throw?
2018 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
2019 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01002020 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01002021 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002022
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002023 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002024 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002025
Calin Juravle10e244f2015-01-26 18:54:32 +00002026 // Does not apply for all instructions, but having this at top level greatly
2027 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00002028 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002029 virtual bool CanBeNull() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002030 DCHECK_EQ(GetType(), DataType::Type::kReference) << "CanBeNull only applies to reference types";
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002031 return true;
2032 }
Calin Juravle10e244f2015-01-26 18:54:32 +00002033
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002034 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01002035 return false;
2036 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002037
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002038 virtual bool IsActualObject() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002039 return GetType() == DataType::Type::kReference;
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002040 }
2041
Calin Juravle2e768302015-07-28 14:41:11 +00002042 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00002043
Calin Juravle61d544b2015-02-23 16:46:57 +00002044 ReferenceTypeInfo GetReferenceTypeInfo() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002045 DCHECK_EQ(GetType(), DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002046 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00002047 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00002048 }
Calin Juravleacf735c2015-02-12 15:25:22 +00002049
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002050 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00002051 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002052 // Note: fixup_end remains valid across push_front().
2053 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
2054 HUseListNode<HInstruction*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002055 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HInstruction*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002056 uses_.push_front(*new_node);
2057 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002058 }
2059
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002060 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01002061 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002062 // Note: env_fixup_end remains valid across push_front().
2063 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
2064 HUseListNode<HEnvironment*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002065 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HEnvironment*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002066 env_uses_.push_front(*new_node);
2067 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002068 }
2069
David Brazdil1abb4192015-02-17 18:33:36 +00002070 void RemoveAsUserOfInput(size_t input) {
2071 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002072 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2073 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2074 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00002075 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002076
Vladimir Marko372f10e2016-05-17 16:30:10 +01002077 void RemoveAsUserOfAllInputs() {
2078 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2079 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2080 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2081 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
2082 }
2083 }
2084
David Brazdil1abb4192015-02-17 18:33:36 +00002085 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2086 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002087
Vladimir Marko46817b82016-03-29 12:21:58 +01002088 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2089 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2090 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002091 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002092 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002093 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002094
Aart Bikcc42be02016-10-20 16:14:16 -07002095 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002096 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002097 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002098 !CanThrow() &&
2099 !IsSuspendCheck() &&
2100 !IsControlFlow() &&
2101 !IsNativeDebugInfo() &&
2102 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002103 // If we added an explicit barrier then we should keep it.
Igor Murashkind01745e2017-04-05 16:40:31 -07002104 !IsMemoryBarrier() &&
2105 !IsConstructorFence();
Aart Bik482095d2016-10-10 15:39:10 -07002106 }
2107
Aart Bikcc42be02016-10-20 16:14:16 -07002108 bool IsDeadAndRemovable() const {
2109 return IsRemovable() && !HasUses();
2110 }
2111
Artem Serov1de1e112017-07-20 16:33:59 +01002112 // Does this instruction dominate (strictly or in regular sense depending on 'strictly')
2113 // `other_instruction`?
2114 // Returns '!strictly' if this instruction and `other_instruction` are the same.
Roland Levillain6c82d402014-10-13 16:10:27 +01002115 // Aborts if this instruction and `other_instruction` are both phis.
Artem Serov1de1e112017-07-20 16:33:59 +01002116 bool Dominates(HInstruction* other_instruction, bool strictly) const;
2117
2118 // Return 'Dominates(other_instruction, /*strictly*/ true)'.
Roland Levillain6c82d402014-10-13 16:10:27 +01002119 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002120
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002121 int GetId() const { return id_; }
2122 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002123
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002124 int GetSsaIndex() const { return ssa_index_; }
2125 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2126 bool HasSsaIndex() const { return ssa_index_ != -1; }
2127
2128 bool HasEnvironment() const { return environment_ != nullptr; }
2129 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002130 // Set the `environment_` field. Raw because this method does not
2131 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002132 void SetRawEnvironment(HEnvironment* environment) {
2133 DCHECK(environment_ == nullptr);
2134 DCHECK_EQ(environment->GetHolder(), this);
2135 environment_ = environment;
2136 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002137
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002138 void InsertRawEnvironment(HEnvironment* environment) {
2139 DCHECK(environment_ != nullptr);
2140 DCHECK_EQ(environment->GetHolder(), this);
2141 DCHECK(environment->GetParent() == nullptr);
2142 environment->parent_ = environment_;
2143 environment_ = environment;
2144 }
2145
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002146 void RemoveEnvironment();
2147
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002148 // Set the environment of this instruction, copying it from `environment`. While
2149 // copying, the uses lists are being updated.
2150 void CopyEnvironmentFrom(HEnvironment* environment) {
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 Geoffray3dcd58c2015-04-03 11:02:38 +01002154 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002155 if (environment->GetParent() != nullptr) {
2156 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2157 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002158 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002159
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002160 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2161 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002162 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002163 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002164 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002165 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002166 if (environment->GetParent() != nullptr) {
2167 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2168 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002169 }
2170
Nicolas Geoffray39468442014-09-02 15:17:15 +01002171 // Returns the number of entries in the environment. Typically, that is the
2172 // number of dex registers in a method. It could be more in case of inlining.
2173 size_t EnvironmentSize() const;
2174
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002175 LocationSummary* GetLocations() const { return locations_; }
2176 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002177
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002178 void ReplaceWith(HInstruction* instruction);
Artem Serov1de1e112017-07-20 16:33:59 +01002179
2180 // Replace all uses of the instruction which are dominated by 'dominator' with 'replacement'.
2181 // 'strictly' determines whether strict or regular domination relation should be checked.
2182 void ReplaceUsesDominatedBy(HInstruction* dominator,
2183 HInstruction* replacement,
2184 bool strictly = true);
2185
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002186 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002187
Alexandre Rames188d4312015-04-09 18:30:21 +01002188 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2189 // uses of this instruction by `other` are *not* updated.
2190 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2191 ReplaceWith(other);
2192 other->ReplaceInput(this, use_index);
2193 }
2194
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002195 // Move `this` instruction before `cursor`
2196 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002197
Vladimir Markofb337ea2015-11-25 15:25:10 +00002198 // Move `this` before its first user and out of any loops. If there is no
2199 // out-of-loop user that dominates all other users, move the instruction
2200 // to the end of the out-of-loop common dominator of the user's blocks.
2201 //
2202 // This can be used only on non-throwing instructions with no side effects that
2203 // have at least one use but no environment uses.
2204 void MoveBeforeFirstUserAndOutOfLoops();
2205
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002206#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002207 bool Is##type() const; \
2208 const H##type* As##type() const; \
2209 H##type* As##type();
2210
2211 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2212#undef INSTRUCTION_TYPE_CHECK
2213
2214#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002215 bool Is##type() const { return (As##type() != nullptr); } \
2216 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002217 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002218 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002219#undef INSTRUCTION_TYPE_CHECK
2220
Artem Serovcced8ba2017-07-19 18:18:09 +01002221 // Return a clone of the instruction if it is clonable (shallow copy by default, custom copy
2222 // if a custom copy-constructor is provided for a particular type). If IsClonable() is false for
2223 // the instruction then the behaviour of this function is undefined.
2224 //
2225 // Note: It is semantically valid to create a clone of the instruction only until
2226 // prepare_for_register_allocator phase as lifetime, intervals and codegen info are not
2227 // copied.
2228 //
2229 // Note: HEnvironment and some other fields are not copied and are set to default values, see
2230 // 'explicit HInstruction(const HInstruction& other)' for details.
2231 virtual HInstruction* Clone(ArenaAllocator* arena ATTRIBUTE_UNUSED) const {
2232 LOG(FATAL) << "Cloning is not implemented for the instruction " <<
2233 DebugName() << " " << GetId();
2234 UNREACHABLE();
2235 }
2236
2237 // Return whether instruction can be cloned (copied).
2238 virtual bool IsClonable() const { return false; }
2239
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002240 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002241 // TODO: this method is used by LICM and GVN with possibly different
2242 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002243 virtual bool CanBeMoved() const { return false; }
2244
2245 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002246 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002247 return false;
2248 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002249
2250 // Returns whether any data encoded in the two instructions is equal.
2251 // This method does not look at the inputs. Both instructions must be
2252 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002253 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002254 return false;
2255 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002256
2257 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002258 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002259 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002260 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002261
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002262 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2263 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2264 // the virtual function because the __attribute__((__pure__)) doesn't really
2265 // apply the strong requirement for virtual functions, preventing optimizations.
2266 InstructionKind GetKind() const PURE;
2267 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002268
2269 virtual size_t ComputeHashCode() const {
2270 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002271 for (const HInstruction* input : GetInputs()) {
2272 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002273 }
2274 return result;
2275 }
2276
2277 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002278 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002279 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002280
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002281 size_t GetLifetimePosition() const { return lifetime_position_; }
2282 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2283 LiveInterval* GetLiveInterval() const { return live_interval_; }
2284 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2285 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2286
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002287 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2288
2289 // Returns whether the code generation of the instruction will require to have access
2290 // to the current method. Such instructions are:
2291 // (1): Instructions that require an environment, as calling the runtime requires
2292 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002293 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2294 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002295 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002296 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002297 }
2298
Vladimir Markodc151b22015-10-15 18:02:30 +01002299 // Returns whether the code generation of the instruction will require to have access
2300 // to the dex cache of the current method's declaring class via the current method.
2301 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002302
Mark Mendellc4701932015-04-10 13:18:51 -04002303 // Does this instruction have any use in an environment before
2304 // control flow hits 'other'?
2305 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2306
2307 // Remove all references to environment uses of this instruction.
2308 // The caller must ensure that this is safe to do.
2309 void RemoveEnvironmentUsers();
2310
Vladimir Markoa1de9182016-02-25 11:37:38 +00002311 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2312 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002313
David Brazdil1abb4192015-02-17 18:33:36 +00002314 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002315 // If set, the machine code for this instruction is assumed to be generated by
2316 // its users. Used by liveness analysis to compute use positions accordingly.
2317 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2318 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2319 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2320 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2321
Vladimir Marko372f10e2016-05-17 16:30:10 +01002322 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2323 return GetInputRecords()[i];
2324 }
2325
2326 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2327 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2328 input_records[index] = input;
2329 }
David Brazdil1abb4192015-02-17 18:33:36 +00002330
Vladimir Markoa1de9182016-02-25 11:37:38 +00002331 uint32_t GetPackedFields() const {
2332 return packed_fields_;
2333 }
2334
2335 template <size_t flag>
2336 bool GetPackedFlag() const {
2337 return (packed_fields_ & (1u << flag)) != 0u;
2338 }
2339
2340 template <size_t flag>
2341 void SetPackedFlag(bool value = true) {
2342 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2343 }
2344
2345 template <typename BitFieldType>
2346 typename BitFieldType::value_type GetPackedField() const {
2347 return BitFieldType::Decode(packed_fields_);
2348 }
2349
2350 template <typename BitFieldType>
2351 void SetPackedField(typename BitFieldType::value_type value) {
2352 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2353 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2354 }
2355
Artem Serovcced8ba2017-07-19 18:18:09 +01002356 // Copy construction for the instruction (used for Clone function).
2357 //
2358 // Fields (e.g. lifetime, intervals and codegen info) associated with phases starting from
2359 // prepare_for_register_allocator are not copied (set to default values).
2360 //
2361 // Copy constructors must be provided for every HInstruction type; default copy constructor is
2362 // fine for most of them. However for some of the instructions a custom copy constructor must be
2363 // specified (when instruction has non-trivially copyable fields and must have a special behaviour
2364 // for copying them).
2365 explicit HInstruction(const HInstruction& other)
2366 : previous_(nullptr),
2367 next_(nullptr),
2368 block_(nullptr),
2369 dex_pc_(other.dex_pc_),
2370 id_(-1),
2371 ssa_index_(-1),
2372 packed_fields_(other.packed_fields_),
2373 environment_(nullptr),
2374 locations_(nullptr),
2375 live_interval_(nullptr),
2376 lifetime_position_(kNoLifetime),
2377 side_effects_(other.side_effects_),
2378 reference_type_handle_(other.reference_type_handle_) {}
2379
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002380 private:
Vladimir Marko46817b82016-03-29 12:21:58 +01002381 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2382 auto before_use_node = uses_.before_begin();
2383 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2384 HInstruction* user = use_node->GetUser();
2385 size_t input_index = use_node->GetIndex();
2386 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2387 before_use_node = use_node;
2388 }
2389 }
2390
2391 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2392 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2393 if (next != uses_.end()) {
2394 HInstruction* next_user = next->GetUser();
2395 size_t next_index = next->GetIndex();
2396 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2397 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2398 }
2399 }
2400
2401 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2402 auto before_env_use_node = env_uses_.before_begin();
2403 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2404 HEnvironment* user = env_use_node->GetUser();
2405 size_t input_index = env_use_node->GetIndex();
2406 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2407 before_env_use_node = env_use_node;
2408 }
2409 }
2410
2411 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2412 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2413 if (next != env_uses_.end()) {
2414 HEnvironment* next_user = next->GetUser();
2415 size_t next_index = next->GetIndex();
2416 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2417 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2418 }
2419 }
David Brazdil1abb4192015-02-17 18:33:36 +00002420
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002421 HInstruction* previous_;
2422 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002423 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002424 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002425
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002426 // An instruction gets an id when it is added to the graph.
2427 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002428 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002429 int id_;
2430
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002431 // When doing liveness analysis, instructions that have uses get an SSA index.
2432 int ssa_index_;
2433
Vladimir Markoa1de9182016-02-25 11:37:38 +00002434 // Packed fields.
2435 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002436
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002437 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002438 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002439
2440 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002441 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002442
Nicolas Geoffray39468442014-09-02 15:17:15 +01002443 // The environment associated with this instruction. Not null if the instruction
2444 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002445 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002446
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002447 // Set by the code generator.
2448 LocationSummary* locations_;
2449
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002450 // Set by the liveness analysis.
2451 LiveInterval* live_interval_;
2452
2453 // Set by the liveness analysis, this is the position in a linear
2454 // order of blocks where this instruction's live interval start.
2455 size_t lifetime_position_;
2456
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002457 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002458
Vladimir Markoa1de9182016-02-25 11:37:38 +00002459 // The reference handle part of the reference type info.
2460 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002461 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002462 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002463
David Brazdil1abb4192015-02-17 18:33:36 +00002464 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002465 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002466 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002467 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002468 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002469};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002470std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002471
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002472// Iterates over the instructions, while preserving the next instruction
2473// in case the current instruction gets removed from the list by the user
2474// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002475class HInstructionIterator : public ValueObject {
2476 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002477 explicit HInstructionIterator(const HInstructionList& instructions)
2478 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002479 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002480 }
2481
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002482 bool Done() const { return instruction_ == nullptr; }
2483 HInstruction* Current() const { return instruction_; }
2484 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002485 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002486 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002487 }
2488
2489 private:
2490 HInstruction* instruction_;
2491 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002492
2493 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002494};
2495
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002496// Iterates over the instructions without saving the next instruction,
2497// therefore handling changes in the graph potentially made by the user
2498// of this iterator.
2499class HInstructionIteratorHandleChanges : public ValueObject {
2500 public:
2501 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2502 : instruction_(instructions.first_instruction_) {
2503 }
2504
2505 bool Done() const { return instruction_ == nullptr; }
2506 HInstruction* Current() const { return instruction_; }
2507 void Advance() {
2508 instruction_ = instruction_->GetNext();
2509 }
2510
2511 private:
2512 HInstruction* instruction_;
2513
2514 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2515};
2516
2517
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002518class HBackwardInstructionIterator : public ValueObject {
2519 public:
2520 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2521 : instruction_(instructions.last_instruction_) {
2522 next_ = Done() ? nullptr : instruction_->GetPrevious();
2523 }
2524
2525 bool Done() const { return instruction_ == nullptr; }
2526 HInstruction* Current() const { return instruction_; }
2527 void Advance() {
2528 instruction_ = next_;
2529 next_ = Done() ? nullptr : instruction_->GetPrevious();
2530 }
2531
2532 private:
2533 HInstruction* instruction_;
2534 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002535
2536 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002537};
2538
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002539class HVariableInputSizeInstruction : public HInstruction {
2540 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002541 using HInstruction::GetInputRecords; // Keep the const version visible.
2542 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
2543 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2544 }
2545
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002546 void AddInput(HInstruction* input);
2547 void InsertInputAt(size_t index, HInstruction* input);
2548 void RemoveInputAt(size_t index);
2549
Igor Murashkind01745e2017-04-05 16:40:31 -07002550 // Removes all the inputs.
2551 // Also removes this instructions from each input's use list
2552 // (for non-environment uses only).
2553 void RemoveAllInputs();
2554
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002555 protected:
2556 HVariableInputSizeInstruction(SideEffects side_effects,
2557 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002558 ArenaAllocator* allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002559 size_t number_of_inputs,
2560 ArenaAllocKind kind)
2561 : HInstruction(side_effects, dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002562 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002563
Artem Serovcced8ba2017-07-19 18:18:09 +01002564 DEFAULT_COPY_CONSTRUCTOR(VariableInputSizeInstruction);
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002565
Artem Serovcced8ba2017-07-19 18:18:09 +01002566 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002567};
2568
Vladimir Markof9f64412015-09-02 14:05:49 +01002569template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002570class HTemplateInstruction: public HInstruction {
2571 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002572 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002573 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002574 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002575
Vladimir Marko372f10e2016-05-17 16:30:10 +01002576 using HInstruction::GetInputRecords; // Keep the const version visible.
2577 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2578 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002579 }
2580
Artem Serovcced8ba2017-07-19 18:18:09 +01002581 protected:
2582 DEFAULT_COPY_CONSTRUCTOR(TemplateInstruction<N>);
2583
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002584 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002585 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002586
2587 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002588};
2589
Vladimir Markof9f64412015-09-02 14:05:49 +01002590// HTemplateInstruction specialization for N=0.
2591template<>
2592class HTemplateInstruction<0>: public HInstruction {
2593 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002594 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002595 : HInstruction(side_effects, dex_pc) {}
2596
Vladimir Markof9f64412015-09-02 14:05:49 +01002597 virtual ~HTemplateInstruction() {}
2598
Vladimir Marko372f10e2016-05-17 16:30:10 +01002599 using HInstruction::GetInputRecords; // Keep the const version visible.
2600 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2601 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002602 }
2603
Artem Serovcced8ba2017-07-19 18:18:09 +01002604 protected:
2605 DEFAULT_COPY_CONSTRUCTOR(TemplateInstruction<0>);
2606
Vladimir Markof9f64412015-09-02 14:05:49 +01002607 private:
2608 friend class SsaBuilder;
2609};
2610
Dave Allison20dfc792014-06-16 20:44:29 -07002611template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002612class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002613 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002614 HExpression<N>(DataType::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002615 : HTemplateInstruction<N>(side_effects, dex_pc) {
2616 this->template SetPackedField<TypeField>(type);
2617 }
Dave Allison20dfc792014-06-16 20:44:29 -07002618 virtual ~HExpression() {}
2619
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002620 DataType::Type GetType() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00002621 return TypeField::Decode(this->GetPackedFields());
2622 }
Dave Allison20dfc792014-06-16 20:44:29 -07002623
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002624 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002625 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2626 static constexpr size_t kFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002627 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00002628 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2629 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2630 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002631 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
Artem Serovcced8ba2017-07-19 18:18:09 +01002632 DEFAULT_COPY_CONSTRUCTOR(Expression<N>);
Dave Allison20dfc792014-06-16 20:44:29 -07002633};
2634
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002635// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2636// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002637class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002638 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002639 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2640 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002641
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002642 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002643
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002644 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002645
Artem Serovcced8ba2017-07-19 18:18:09 +01002646 protected:
2647 DEFAULT_COPY_CONSTRUCTOR(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002648};
2649
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002650// Represents dex's RETURN opcodes. A HReturn is a control flow
2651// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002652class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002653 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002654 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2655 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002656 SetRawInputAt(0, value);
2657 }
2658
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002659 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002660
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002661 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002662
Artem Serovcced8ba2017-07-19 18:18:09 +01002663 protected:
2664 DEFAULT_COPY_CONSTRUCTOR(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002665};
2666
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002667class HPhi FINAL : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002668 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002669 HPhi(ArenaAllocator* allocator,
David Brazdildee58d62016-04-07 09:54:26 +00002670 uint32_t reg_number,
2671 size_t number_of_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002672 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +00002673 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002674 : HVariableInputSizeInstruction(
2675 SideEffects::None(),
2676 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002677 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002678 number_of_inputs,
2679 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002680 reg_number_(reg_number) {
2681 SetPackedField<TypeField>(ToPhiType(type));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002682 DCHECK_NE(GetType(), DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +00002683 // Phis are constructed live and marked dead if conflicting or unused.
2684 // Individual steps of SsaBuilder should assume that if a phi has been
2685 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2686 SetPackedFlag<kFlagIsLive>(true);
2687 SetPackedFlag<kFlagCanBeNull>(true);
2688 }
2689
Artem Serovcced8ba2017-07-19 18:18:09 +01002690 bool IsClonable() const OVERRIDE { return true; }
2691
David Brazdildee58d62016-04-07 09:54:26 +00002692 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002693 static DataType::Type ToPhiType(DataType::Type type) {
2694 return DataType::Kind(type);
David Brazdildee58d62016-04-07 09:54:26 +00002695 }
2696
2697 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2698
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002699 DataType::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2700 void SetType(DataType::Type new_type) {
David Brazdildee58d62016-04-07 09:54:26 +00002701 // Make sure that only valid type changes occur. The following are allowed:
2702 // (1) int -> float/ref (primitive type propagation),
2703 // (2) long -> double (primitive type propagation).
2704 DCHECK(GetType() == new_type ||
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002705 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kFloat32) ||
2706 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kReference) ||
2707 (GetType() == DataType::Type::kInt64 && new_type == DataType::Type::kFloat64));
David Brazdildee58d62016-04-07 09:54:26 +00002708 SetPackedField<TypeField>(new_type);
2709 }
2710
2711 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2712 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2713
2714 uint32_t GetRegNumber() const { return reg_number_; }
2715
2716 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2717 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2718 bool IsDead() const { return !IsLive(); }
2719 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2720
Vladimir Markoe9004912016-06-16 16:50:52 +01002721 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002722 return other != nullptr
2723 && other->IsPhi()
2724 && other->AsPhi()->GetBlock() == GetBlock()
2725 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2726 }
2727
Nicolas Geoffrayf57c1ae2017-06-28 17:40:18 +01002728 bool HasEquivalentPhi() const {
2729 if (GetPrevious() != nullptr && GetPrevious()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2730 return true;
2731 }
2732 if (GetNext() != nullptr && GetNext()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2733 return true;
2734 }
2735 return false;
2736 }
2737
David Brazdildee58d62016-04-07 09:54:26 +00002738 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2739 // An equivalent phi is a phi having the same dex register and type.
2740 // It assumes that phis with the same dex register are adjacent.
2741 HPhi* GetNextEquivalentPhiWithSameType() {
2742 HInstruction* next = GetNext();
2743 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2744 if (next->GetType() == GetType()) {
2745 return next->AsPhi();
2746 }
2747 next = next->GetNext();
2748 }
2749 return nullptr;
2750 }
2751
2752 DECLARE_INSTRUCTION(Phi);
2753
Artem Serovcced8ba2017-07-19 18:18:09 +01002754 protected:
2755 DEFAULT_COPY_CONSTRUCTOR(Phi);
2756
David Brazdildee58d62016-04-07 09:54:26 +00002757 private:
2758 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2759 static constexpr size_t kFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002760 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
David Brazdildee58d62016-04-07 09:54:26 +00002761 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2762 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2763 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2764 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002765 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
David Brazdildee58d62016-04-07 09:54:26 +00002766
David Brazdildee58d62016-04-07 09:54:26 +00002767 const uint32_t reg_number_;
David Brazdildee58d62016-04-07 09:54:26 +00002768};
2769
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002770// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002771// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002772// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002773class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002774 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002775 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002776
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002777 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002778
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002779 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002780
Artem Serovcced8ba2017-07-19 18:18:09 +01002781 protected:
2782 DEFAULT_COPY_CONSTRUCTOR(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002783};
2784
2785// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002786class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002787 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002788 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002789
Artem Serovcced8ba2017-07-19 18:18:09 +01002790 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002791 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002792
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002793 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002794 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002795 }
2796
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002797 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002798
Artem Serovcced8ba2017-07-19 18:18:09 +01002799 protected:
2800 DEFAULT_COPY_CONSTRUCTOR(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002801};
2802
Roland Levillain9867bc72015-08-05 10:21:34 +01002803class HConstant : public HExpression<0> {
2804 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002805 explicit HConstant(DataType::Type type, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002806 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002807
2808 bool CanBeMoved() const OVERRIDE { return true; }
2809
Roland Levillain1a653882016-03-18 18:05:57 +00002810 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002811 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002812 // Is this constant 0 in the arithmetic sense?
2813 virtual bool IsArithmeticZero() const { return false; }
2814 // Is this constant a 0-bit pattern?
2815 virtual bool IsZeroBitPattern() const { return false; }
2816 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002817 virtual bool IsOne() const { return false; }
2818
David Brazdil77a48ae2015-09-15 12:34:04 +00002819 virtual uint64_t GetValueAsUint64() const = 0;
2820
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002821 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002822
Artem Serovcced8ba2017-07-19 18:18:09 +01002823 protected:
2824 DEFAULT_COPY_CONSTRUCTOR(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002825};
2826
Vladimir Markofcb503c2016-05-18 12:48:17 +01002827class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002828 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002829 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002830 return true;
2831 }
2832
David Brazdil77a48ae2015-09-15 12:34:04 +00002833 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2834
Roland Levillain9867bc72015-08-05 10:21:34 +01002835 size_t ComputeHashCode() const OVERRIDE { return 0; }
2836
Roland Levillain1a653882016-03-18 18:05:57 +00002837 // The null constant representation is a 0-bit pattern.
2838 virtual bool IsZeroBitPattern() const { return true; }
2839
Roland Levillain9867bc72015-08-05 10:21:34 +01002840 DECLARE_INSTRUCTION(NullConstant);
2841
Artem Serovcced8ba2017-07-19 18:18:09 +01002842 protected:
2843 DEFAULT_COPY_CONSTRUCTOR(NullConstant);
2844
Roland Levillain9867bc72015-08-05 10:21:34 +01002845 private:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002846 explicit HNullConstant(uint32_t dex_pc = kNoDexPc)
2847 : HConstant(DataType::Type::kReference, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002848
2849 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01002850};
2851
2852// Constants of the type int. Those can be from Dex instructions, or
2853// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002854class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002855 public:
2856 int32_t GetValue() const { return value_; }
2857
David Brazdil9f389d42015-10-01 14:32:56 +01002858 uint64_t GetValueAsUint64() const OVERRIDE {
2859 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2860 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002861
Vladimir Marko372f10e2016-05-17 16:30:10 +01002862 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002863 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002864 return other->AsIntConstant()->value_ == value_;
2865 }
2866
2867 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2868
2869 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002870 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2871 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002872 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2873
Roland Levillain1a653882016-03-18 18:05:57 +00002874 // Integer constants are used to encode Boolean values as well,
2875 // where 1 means true and 0 means false.
2876 bool IsTrue() const { return GetValue() == 1; }
2877 bool IsFalse() const { return GetValue() == 0; }
2878
Roland Levillain9867bc72015-08-05 10:21:34 +01002879 DECLARE_INSTRUCTION(IntConstant);
2880
Artem Serovcced8ba2017-07-19 18:18:09 +01002881 protected:
2882 DEFAULT_COPY_CONSTRUCTOR(IntConstant);
2883
Roland Levillain9867bc72015-08-05 10:21:34 +01002884 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002885 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002886 : HConstant(DataType::Type::kInt32, dex_pc), value_(value) {}
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002887 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002888 : HConstant(DataType::Type::kInt32, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002889
2890 const int32_t value_;
2891
2892 friend class HGraph;
2893 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2894 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Roland Levillain9867bc72015-08-05 10:21:34 +01002895};
2896
Vladimir Markofcb503c2016-05-18 12:48:17 +01002897class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002898 public:
2899 int64_t GetValue() const { return value_; }
2900
David Brazdil77a48ae2015-09-15 12:34:04 +00002901 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2902
Vladimir Marko372f10e2016-05-17 16:30:10 +01002903 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002904 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002905 return other->AsLongConstant()->value_ == value_;
2906 }
2907
2908 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2909
2910 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002911 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2912 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002913 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2914
2915 DECLARE_INSTRUCTION(LongConstant);
2916
Artem Serovcced8ba2017-07-19 18:18:09 +01002917 protected:
2918 DEFAULT_COPY_CONSTRUCTOR(LongConstant);
2919
Roland Levillain9867bc72015-08-05 10:21:34 +01002920 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002921 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002922 : HConstant(DataType::Type::kInt64, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002923
2924 const int64_t value_;
2925
2926 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01002927};
Dave Allison20dfc792014-06-16 20:44:29 -07002928
Vladimir Markofcb503c2016-05-18 12:48:17 +01002929class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002930 public:
2931 float GetValue() const { return value_; }
2932
2933 uint64_t GetValueAsUint64() const OVERRIDE {
2934 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2935 }
2936
Vladimir Marko372f10e2016-05-17 16:30:10 +01002937 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002938 DCHECK(other->IsFloatConstant()) << other->DebugName();
2939 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2940 }
2941
2942 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2943
2944 bool IsMinusOne() const OVERRIDE {
2945 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2946 }
Roland Levillain1a653882016-03-18 18:05:57 +00002947 bool IsArithmeticZero() const OVERRIDE {
2948 return std::fpclassify(value_) == FP_ZERO;
2949 }
2950 bool IsArithmeticPositiveZero() const {
2951 return IsArithmeticZero() && !std::signbit(value_);
2952 }
2953 bool IsArithmeticNegativeZero() const {
2954 return IsArithmeticZero() && std::signbit(value_);
2955 }
2956 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002957 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002958 }
2959 bool IsOne() const OVERRIDE {
2960 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2961 }
2962 bool IsNaN() const {
2963 return std::isnan(value_);
2964 }
2965
2966 DECLARE_INSTRUCTION(FloatConstant);
2967
Artem Serovcced8ba2017-07-19 18:18:09 +01002968 protected:
2969 DEFAULT_COPY_CONSTRUCTOR(FloatConstant);
2970
Roland Levillain31dd3d62016-02-16 12:21:02 +00002971 private:
2972 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002973 : HConstant(DataType::Type::kFloat32, dex_pc), value_(value) {}
Roland Levillain31dd3d62016-02-16 12:21:02 +00002974 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002975 : HConstant(DataType::Type::kFloat32, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
Roland Levillain31dd3d62016-02-16 12:21:02 +00002976
2977 const float value_;
2978
2979 // Only the SsaBuilder and HGraph can create floating-point constants.
2980 friend class SsaBuilder;
2981 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00002982};
2983
Vladimir Markofcb503c2016-05-18 12:48:17 +01002984class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002985 public:
2986 double GetValue() const { return value_; }
2987
2988 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2989
Vladimir Marko372f10e2016-05-17 16:30:10 +01002990 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002991 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2992 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2993 }
2994
2995 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2996
2997 bool IsMinusOne() const OVERRIDE {
2998 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2999 }
Roland Levillain1a653882016-03-18 18:05:57 +00003000 bool IsArithmeticZero() const OVERRIDE {
3001 return std::fpclassify(value_) == FP_ZERO;
3002 }
3003 bool IsArithmeticPositiveZero() const {
3004 return IsArithmeticZero() && !std::signbit(value_);
3005 }
3006 bool IsArithmeticNegativeZero() const {
3007 return IsArithmeticZero() && std::signbit(value_);
3008 }
3009 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003010 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00003011 }
3012 bool IsOne() const OVERRIDE {
3013 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3014 }
3015 bool IsNaN() const {
3016 return std::isnan(value_);
3017 }
3018
3019 DECLARE_INSTRUCTION(DoubleConstant);
3020
Artem Serovcced8ba2017-07-19 18:18:09 +01003021 protected:
3022 DEFAULT_COPY_CONSTRUCTOR(DoubleConstant);
3023
Roland Levillain31dd3d62016-02-16 12:21:02 +00003024 private:
3025 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003026 : HConstant(DataType::Type::kFloat64, dex_pc), value_(value) {}
Roland Levillain31dd3d62016-02-16 12:21:02 +00003027 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003028 : HConstant(DataType::Type::kFloat64, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
Roland Levillain31dd3d62016-02-16 12:21:02 +00003029
3030 const double value_;
3031
3032 // Only the SsaBuilder and HGraph can create floating-point constants.
3033 friend class SsaBuilder;
3034 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003035};
3036
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003037// Conditional branch. A block ending with an HIf instruction must have
3038// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003039class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003040 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003041 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
3042 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003043 SetRawInputAt(0, input);
3044 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003045
Artem Serovcced8ba2017-07-19 18:18:09 +01003046 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003047 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003048
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003049 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003050 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003051 }
3052
3053 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003054 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003055 }
3056
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003057 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003058
Artem Serovcced8ba2017-07-19 18:18:09 +01003059 protected:
3060 DEFAULT_COPY_CONSTRUCTOR(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003061};
3062
David Brazdilfc6a86a2015-06-26 10:33:45 +00003063
3064// Abstract instruction which marks the beginning and/or end of a try block and
3065// links it to the respective exception handlers. Behaves the same as a Goto in
3066// non-exceptional control flow.
3067// Normal-flow successor is stored at index zero, exception handlers under
3068// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003069class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003070 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003071 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01003072 kEntry,
3073 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003074 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01003075 };
3076
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003077 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003078 : HTemplateInstruction(SideEffects::None(), dex_pc) {
3079 SetPackedField<BoundaryKindField>(kind);
3080 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003081
3082 bool IsControlFlow() const OVERRIDE { return true; }
3083
3084 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01003085 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003086
David Brazdild26a4112015-11-10 11:07:31 +00003087 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
3088 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
3089 }
3090
David Brazdilfc6a86a2015-06-26 10:33:45 +00003091 // Returns whether `handler` is among its exception handlers (non-zero index
3092 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01003093 bool HasExceptionHandler(const HBasicBlock& handler) const {
3094 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00003095 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00003096 }
3097
3098 // If not present already, adds `handler` to its block's list of exception
3099 // handlers.
3100 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01003101 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003102 GetBlock()->AddSuccessor(handler);
3103 }
3104 }
3105
Vladimir Markoa1de9182016-02-25 11:37:38 +00003106 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
3107 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003108
David Brazdilffee3d32015-07-06 11:48:53 +01003109 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
3110
David Brazdilfc6a86a2015-06-26 10:33:45 +00003111 DECLARE_INSTRUCTION(TryBoundary);
3112
Artem Serovcced8ba2017-07-19 18:18:09 +01003113 protected:
3114 DEFAULT_COPY_CONSTRUCTOR(TryBoundary);
3115
David Brazdilfc6a86a2015-06-26 10:33:45 +00003116 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003117 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
3118 static constexpr size_t kFieldBoundaryKindSize =
3119 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
3120 static constexpr size_t kNumberOfTryBoundaryPackedBits =
3121 kFieldBoundaryKind + kFieldBoundaryKindSize;
3122 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
3123 "Too many packed fields.");
3124 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00003125};
3126
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003127// Deoptimize to interpreter, upon checking a condition.
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003128class HDeoptimize FINAL : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003129 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003130 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
3131 // across.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003132 HDeoptimize(ArenaAllocator* allocator,
3133 HInstruction* cond,
3134 DeoptimizationKind kind,
3135 uint32_t dex_pc)
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003136 : HVariableInputSizeInstruction(
3137 SideEffects::All(),
3138 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003139 allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003140 /* number_of_inputs */ 1,
3141 kArenaAllocMisc) {
3142 SetPackedFlag<kFieldCanBeMoved>(false);
3143 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003144 SetRawInputAt(0, cond);
3145 }
3146
Artem Serovcced8ba2017-07-19 18:18:09 +01003147 bool IsClonable() const OVERRIDE { return true; }
3148
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003149 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
3150 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
3151 // instead of `guard`.
3152 // We set CanTriggerGC to prevent any intermediate address to be live
3153 // at the point of the `HDeoptimize`.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003154 HDeoptimize(ArenaAllocator* allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003155 HInstruction* cond,
3156 HInstruction* guard,
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003157 DeoptimizationKind kind,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003158 uint32_t dex_pc)
3159 : HVariableInputSizeInstruction(
3160 SideEffects::CanTriggerGC(),
3161 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003162 allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003163 /* number_of_inputs */ 2,
3164 kArenaAllocMisc) {
3165 SetPackedFlag<kFieldCanBeMoved>(true);
3166 SetPackedField<DeoptimizeKindField>(kind);
3167 SetRawInputAt(0, cond);
3168 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003169 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003170
3171 bool CanBeMoved() const OVERRIDE { return GetPackedFlag<kFieldCanBeMoved>(); }
3172
3173 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
3174 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
3175 }
3176
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003177 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003178
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003179 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003180
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003181 DeoptimizationKind GetDeoptimizationKind() const { return GetPackedField<DeoptimizeKindField>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003182
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003183 DataType::Type GetType() const OVERRIDE {
3184 return GuardsAnInput() ? GuardedInput()->GetType() : DataType::Type::kVoid;
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003185 }
3186
3187 bool GuardsAnInput() const {
3188 return InputCount() == 2;
3189 }
3190
3191 HInstruction* GuardedInput() const {
3192 DCHECK(GuardsAnInput());
3193 return InputAt(1);
3194 }
3195
3196 void RemoveGuard() {
3197 RemoveInputAt(1);
3198 }
3199
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003200 DECLARE_INSTRUCTION(Deoptimize);
3201
Artem Serovcced8ba2017-07-19 18:18:09 +01003202 protected:
3203 DEFAULT_COPY_CONSTRUCTOR(Deoptimize);
3204
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003205 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003206 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3207 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3208 static constexpr size_t kFieldDeoptimizeKindSize =
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003209 MinimumBitsToStore(static_cast<size_t>(DeoptimizationKind::kLast));
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003210 static constexpr size_t kNumberOfDeoptimizePackedBits =
3211 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3212 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3213 "Too many packed fields.");
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003214 using DeoptimizeKindField =
3215 BitField<DeoptimizationKind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003216};
3217
Mingyao Yang063fc772016-08-02 11:02:54 -07003218// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3219// The compiled code checks this flag value in a guard before devirtualized call and
3220// if it's true, starts to do deoptimization.
3221// It has a 4-byte slot on stack.
3222// TODO: allocate a register for this flag.
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003223class HShouldDeoptimizeFlag FINAL : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003224 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003225 // CHA guards are only optimized in a separate pass and it has no side effects
3226 // with regard to other passes.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003227 HShouldDeoptimizeFlag(ArenaAllocator* allocator, uint32_t dex_pc)
3228 : HVariableInputSizeInstruction(SideEffects::None(), dex_pc, allocator, 0, kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003229 }
3230
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003231 DataType::Type GetType() const OVERRIDE { return DataType::Type::kInt32; }
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003232
3233 // We do all CHA guard elimination/motion in a single pass, after which there is no
3234 // further guard elimination/motion since a guard might have been used for justification
3235 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3236 // to avoid other optimizations trying to move it.
Mingyao Yang063fc772016-08-02 11:02:54 -07003237 bool CanBeMoved() const OVERRIDE { return false; }
3238
3239 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3240
Artem Serovcced8ba2017-07-19 18:18:09 +01003241 protected:
3242 DEFAULT_COPY_CONSTRUCTOR(ShouldDeoptimizeFlag);
Mingyao Yang063fc772016-08-02 11:02:54 -07003243};
3244
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003245// Represents the ArtMethod that was passed as a first argument to
3246// the method. It is used by instructions that depend on it, like
3247// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003248class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003249 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003250 explicit HCurrentMethod(DataType::Type type, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003251 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003252
3253 DECLARE_INSTRUCTION(CurrentMethod);
3254
Artem Serovcced8ba2017-07-19 18:18:09 +01003255 protected:
3256 DEFAULT_COPY_CONSTRUCTOR(CurrentMethod);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003257};
3258
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003259// Fetches an ArtMethod from the virtual table or the interface method table
3260// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003261class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003262 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003263 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003264 kVTable,
3265 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003266 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003267 };
3268 HClassTableGet(HInstruction* cls,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003269 DataType::Type type,
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003270 TableKind kind,
3271 size_t index,
3272 uint32_t dex_pc)
3273 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003274 index_(index) {
3275 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003276 SetRawInputAt(0, cls);
3277 }
3278
Artem Serovcced8ba2017-07-19 18:18:09 +01003279 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003280 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003281 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003282 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003283 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003284 }
3285
Vladimir Markoa1de9182016-02-25 11:37:38 +00003286 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003287 size_t GetIndex() const { return index_; }
3288
3289 DECLARE_INSTRUCTION(ClassTableGet);
3290
Artem Serovcced8ba2017-07-19 18:18:09 +01003291 protected:
3292 DEFAULT_COPY_CONSTRUCTOR(ClassTableGet);
3293
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003294 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003295 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
3296 static constexpr size_t kFieldTableKindSize =
3297 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3298 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3299 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3300 "Too many packed fields.");
3301 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3302
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003303 // The index of the ArtMethod in the table.
3304 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003305};
3306
Mark Mendellfe57faa2015-09-18 09:26:15 -04003307// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3308// have one successor for each entry in the switch table, and the final successor
3309// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003310class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003311 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003312 HPackedSwitch(int32_t start_value,
3313 uint32_t num_entries,
3314 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003315 uint32_t dex_pc = kNoDexPc)
3316 : HTemplateInstruction(SideEffects::None(), dex_pc),
3317 start_value_(start_value),
3318 num_entries_(num_entries) {
3319 SetRawInputAt(0, input);
3320 }
3321
Artem Serovcced8ba2017-07-19 18:18:09 +01003322 bool IsClonable() const OVERRIDE { return true; }
3323
Mark Mendellfe57faa2015-09-18 09:26:15 -04003324 bool IsControlFlow() const OVERRIDE { return true; }
3325
3326 int32_t GetStartValue() const { return start_value_; }
3327
Vladimir Marko211c2112015-09-24 16:52:33 +01003328 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003329
3330 HBasicBlock* GetDefaultBlock() const {
3331 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003332 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003333 }
3334 DECLARE_INSTRUCTION(PackedSwitch);
3335
Artem Serovcced8ba2017-07-19 18:18:09 +01003336 protected:
3337 DEFAULT_COPY_CONSTRUCTOR(PackedSwitch);
3338
Mark Mendellfe57faa2015-09-18 09:26:15 -04003339 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003340 const int32_t start_value_;
3341 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003342};
3343
Roland Levillain88cb1752014-10-20 16:36:47 +01003344class HUnaryOperation : public HExpression<1> {
3345 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003346 HUnaryOperation(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003347 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003348 SetRawInputAt(0, input);
3349 }
3350
Artem Serovcced8ba2017-07-19 18:18:09 +01003351 // All of the UnaryOperation instructions are clonable.
3352 bool IsClonable() const OVERRIDE { return true; }
3353
Roland Levillain88cb1752014-10-20 16:36:47 +01003354 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003355 DataType::Type GetResultType() const { return GetType(); }
Roland Levillain88cb1752014-10-20 16:36:47 +01003356
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003357 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003358 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003359 return true;
3360 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003361
Roland Levillain31dd3d62016-02-16 12:21:02 +00003362 // Try to statically evaluate `this` and return a HConstant
3363 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003364 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003365 HConstant* TryStaticEvaluation() const;
3366
3367 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003368 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3369 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003370 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3371 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003372
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003373 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003374
Artem Serovcced8ba2017-07-19 18:18:09 +01003375 protected:
3376 DEFAULT_COPY_CONSTRUCTOR(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003377};
3378
Dave Allison20dfc792014-06-16 20:44:29 -07003379class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003380 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003381 HBinaryOperation(DataType::Type result_type,
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003382 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003383 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003384 SideEffects side_effects = SideEffects::None(),
3385 uint32_t dex_pc = kNoDexPc)
3386 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003387 SetRawInputAt(0, left);
3388 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003389 }
3390
Artem Serovcced8ba2017-07-19 18:18:09 +01003391 // All of the BinaryOperation instructions are clonable.
3392 bool IsClonable() const OVERRIDE { return true; }
3393
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003394 HInstruction* GetLeft() const { return InputAt(0); }
3395 HInstruction* GetRight() const { return InputAt(1); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003396 DataType::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003397
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003398 virtual bool IsCommutative() const { return false; }
3399
3400 // Put constant on the right.
3401 // Returns whether order is changed.
3402 bool OrderInputsWithConstantOnTheRight() {
3403 HInstruction* left = InputAt(0);
3404 HInstruction* right = InputAt(1);
3405 if (left->IsConstant() && !right->IsConstant()) {
3406 ReplaceInput(right, 0);
3407 ReplaceInput(left, 1);
3408 return true;
3409 }
3410 return false;
3411 }
3412
3413 // Order inputs by instruction id, but favor constant on the right side.
3414 // This helps GVN for commutative ops.
3415 void OrderInputs() {
3416 DCHECK(IsCommutative());
3417 HInstruction* left = InputAt(0);
3418 HInstruction* right = InputAt(1);
3419 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3420 return;
3421 }
3422 if (OrderInputsWithConstantOnTheRight()) {
3423 return;
3424 }
3425 // Order according to instruction id.
3426 if (left->GetId() > right->GetId()) {
3427 ReplaceInput(right, 0);
3428 ReplaceInput(left, 1);
3429 }
3430 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003431
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003432 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003433 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003434 return true;
3435 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003436
Roland Levillain31dd3d62016-02-16 12:21:02 +00003437 // Try to statically evaluate `this` and return a HConstant
3438 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003439 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003440 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003441
3442 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003443 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3444 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003445 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3446 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003447 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003448 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3449 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003450 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3451 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003452 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3453 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003454 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003455 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3456 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003457
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003458 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003459 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003460 HConstant* GetConstantRight() const;
3461
3462 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003463 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003464 HInstruction* GetLeastConstantLeft() const;
3465
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003466 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003467
Artem Serovcced8ba2017-07-19 18:18:09 +01003468 protected:
3469 DEFAULT_COPY_CONSTRUCTOR(BinaryOperation);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003470};
3471
Mark Mendellc4701932015-04-10 13:18:51 -04003472// The comparison bias applies for floating point operations and indicates how NaN
3473// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003474enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003475 kNoBias, // bias is not applicable (i.e. for long operation)
3476 kGtBias, // return 1 for NaN comparisons
3477 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003478 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003479};
3480
Roland Levillain31dd3d62016-02-16 12:21:02 +00003481std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3482
Dave Allison20dfc792014-06-16 20:44:29 -07003483class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003484 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003485 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003486 : HBinaryOperation(DataType::Type::kBool, first, second, SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003487 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3488 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003489
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003490 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003491 // `instruction`, and disregard moves in between.
3492 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003493
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003494 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003495
3496 virtual IfCondition GetCondition() const = 0;
3497
Mark Mendellc4701932015-04-10 13:18:51 -04003498 virtual IfCondition GetOppositeCondition() const = 0;
3499
Vladimir Markoa1de9182016-02-25 11:37:38 +00003500 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003501 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3502
Vladimir Markoa1de9182016-02-25 11:37:38 +00003503 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3504 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003505
Vladimir Marko372f10e2016-05-17 16:30:10 +01003506 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003507 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003508 }
3509
Roland Levillain4fa13f62015-07-06 18:11:54 +01003510 bool IsFPConditionTrueIfNaN() 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 == kCondNE) {
3514 return true;
3515 } else if (if_cond == kCondEQ) {
3516 return false;
3517 }
3518 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003519 }
3520
3521 bool IsFPConditionFalseIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003522 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003523 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003524 if (if_cond == kCondEQ) {
3525 return true;
3526 } else if (if_cond == kCondNE) {
3527 return false;
3528 }
3529 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003530 }
3531
Roland Levillain31dd3d62016-02-16 12:21:02 +00003532 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003533 // Needed if we merge a HCompare into a HCondition.
3534 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3535 static constexpr size_t kFieldComparisonBiasSize =
3536 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3537 static constexpr size_t kNumberOfConditionPackedBits =
3538 kFieldComparisonBias + kFieldComparisonBiasSize;
3539 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3540 using ComparisonBiasField =
3541 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3542
Roland Levillain31dd3d62016-02-16 12:21:02 +00003543 template <typename T>
3544 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3545
3546 template <typename T>
3547 int32_t CompareFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003548 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003549 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3550 // Handle the bias.
3551 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3552 }
3553
3554 // Return an integer constant containing the result of a condition evaluated at compile time.
3555 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3556 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3557 }
3558
Artem Serovcced8ba2017-07-19 18:18:09 +01003559 DEFAULT_COPY_CONSTRUCTOR(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003560};
3561
3562// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003563class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003564 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003565 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3566 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003567
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003568 bool IsCommutative() const OVERRIDE { return true; }
3569
Vladimir Marko9e23df52015-11-10 17:14:35 +00003570 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3571 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003572 return MakeConstantCondition(true, GetDexPc());
3573 }
3574 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3575 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3576 }
3577 // In the following Evaluate methods, a HCompare instruction has
3578 // been merged into this HEqual instruction; evaluate it as
3579 // `Compare(x, y) == 0`.
3580 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3581 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3582 GetDexPc());
3583 }
3584 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3585 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3586 }
3587 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3588 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003589 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003590
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003591 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003592
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003593 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003594 return kCondEQ;
3595 }
3596
Mark Mendellc4701932015-04-10 13:18:51 -04003597 IfCondition GetOppositeCondition() const OVERRIDE {
3598 return kCondNE;
3599 }
3600
Artem Serovcced8ba2017-07-19 18:18:09 +01003601 protected:
3602 DEFAULT_COPY_CONSTRUCTOR(Equal);
3603
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003604 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003605 template <typename T> static bool Compute(T x, T y) { return x == y; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003606};
3607
Vladimir Markofcb503c2016-05-18 12:48:17 +01003608class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003609 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003610 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3611 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003612
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003613 bool IsCommutative() const OVERRIDE { return true; }
3614
Vladimir Marko9e23df52015-11-10 17:14:35 +00003615 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3616 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003617 return MakeConstantCondition(false, GetDexPc());
3618 }
3619 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3620 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3621 }
3622 // In the following Evaluate methods, a HCompare instruction has
3623 // been merged into this HNotEqual instruction; evaluate it as
3624 // `Compare(x, y) != 0`.
3625 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3626 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3627 }
3628 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3629 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3630 }
3631 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3632 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003633 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003634
Dave Allison20dfc792014-06-16 20:44:29 -07003635 DECLARE_INSTRUCTION(NotEqual);
3636
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003637 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003638 return kCondNE;
3639 }
3640
Mark Mendellc4701932015-04-10 13:18:51 -04003641 IfCondition GetOppositeCondition() const OVERRIDE {
3642 return kCondEQ;
3643 }
3644
Artem Serovcced8ba2017-07-19 18:18:09 +01003645 protected:
3646 DEFAULT_COPY_CONSTRUCTOR(NotEqual);
3647
Dave Allison20dfc792014-06-16 20:44:29 -07003648 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003649 template <typename T> static bool Compute(T x, T y) { return x != y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003650};
3651
Vladimir Markofcb503c2016-05-18 12:48:17 +01003652class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003653 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003654 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3655 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003656
Roland Levillain9867bc72015-08-05 10:21:34 +01003657 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003658 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003659 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003660 // In the following Evaluate methods, a HCompare instruction has
3661 // been merged into this HLessThan instruction; evaluate it as
3662 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003663 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003664 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3665 }
3666 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3667 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3668 }
3669 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3670 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003671 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003672
Dave Allison20dfc792014-06-16 20:44:29 -07003673 DECLARE_INSTRUCTION(LessThan);
3674
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003675 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003676 return kCondLT;
3677 }
3678
Mark Mendellc4701932015-04-10 13:18:51 -04003679 IfCondition GetOppositeCondition() const OVERRIDE {
3680 return kCondGE;
3681 }
3682
Artem Serovcced8ba2017-07-19 18:18:09 +01003683 protected:
3684 DEFAULT_COPY_CONSTRUCTOR(LessThan);
3685
Dave Allison20dfc792014-06-16 20:44:29 -07003686 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003687 template <typename T> static bool Compute(T x, T y) { return x < y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003688};
3689
Vladimir Markofcb503c2016-05-18 12:48:17 +01003690class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003691 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003692 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3693 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003694
Roland Levillain9867bc72015-08-05 10:21:34 +01003695 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003696 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003697 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003698 // In the following Evaluate methods, a HCompare instruction has
3699 // been merged into this HLessThanOrEqual instruction; evaluate it as
3700 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003701 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003702 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3703 }
3704 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3705 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3706 }
3707 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3708 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003709 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003710
Dave Allison20dfc792014-06-16 20:44:29 -07003711 DECLARE_INSTRUCTION(LessThanOrEqual);
3712
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003713 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003714 return kCondLE;
3715 }
3716
Mark Mendellc4701932015-04-10 13:18:51 -04003717 IfCondition GetOppositeCondition() const OVERRIDE {
3718 return kCondGT;
3719 }
3720
Artem Serovcced8ba2017-07-19 18:18:09 +01003721 protected:
3722 DEFAULT_COPY_CONSTRUCTOR(LessThanOrEqual);
3723
Dave Allison20dfc792014-06-16 20:44:29 -07003724 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003725 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003726};
3727
Vladimir Markofcb503c2016-05-18 12:48:17 +01003728class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003729 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003730 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3731 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003732
Roland Levillain9867bc72015-08-05 10:21:34 +01003733 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003734 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003735 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003736 // In the following Evaluate methods, a HCompare instruction has
3737 // been merged into this HGreaterThan instruction; evaluate it as
3738 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003739 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003740 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3741 }
3742 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3743 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3744 }
3745 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3746 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003747 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003748
Dave Allison20dfc792014-06-16 20:44:29 -07003749 DECLARE_INSTRUCTION(GreaterThan);
3750
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003751 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003752 return kCondGT;
3753 }
3754
Mark Mendellc4701932015-04-10 13:18:51 -04003755 IfCondition GetOppositeCondition() const OVERRIDE {
3756 return kCondLE;
3757 }
3758
Artem Serovcced8ba2017-07-19 18:18:09 +01003759 protected:
3760 DEFAULT_COPY_CONSTRUCTOR(GreaterThan);
3761
Dave Allison20dfc792014-06-16 20:44:29 -07003762 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003763 template <typename T> static bool Compute(T x, T y) { return x > y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003764};
3765
Vladimir Markofcb503c2016-05-18 12:48:17 +01003766class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003767 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003768 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3769 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003770
Roland Levillain9867bc72015-08-05 10:21:34 +01003771 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003772 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003773 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003774 // In the following Evaluate methods, a HCompare instruction has
3775 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3776 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003777 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003778 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3779 }
3780 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3781 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3782 }
3783 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3784 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003785 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003786
Dave Allison20dfc792014-06-16 20:44:29 -07003787 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3788
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003789 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003790 return kCondGE;
3791 }
3792
Mark Mendellc4701932015-04-10 13:18:51 -04003793 IfCondition GetOppositeCondition() const OVERRIDE {
3794 return kCondLT;
3795 }
3796
Artem Serovcced8ba2017-07-19 18:18:09 +01003797 protected:
3798 DEFAULT_COPY_CONSTRUCTOR(GreaterThanOrEqual);
3799
Dave Allison20dfc792014-06-16 20:44:29 -07003800 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003801 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003802};
3803
Vladimir Markofcb503c2016-05-18 12:48:17 +01003804class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003805 public:
3806 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3807 : HCondition(first, second, dex_pc) {}
3808
3809 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003810 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003811 }
3812 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003813 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3814 }
3815 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3816 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3817 LOG(FATAL) << DebugName() << " is not defined for float values";
3818 UNREACHABLE();
3819 }
3820 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3821 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3822 LOG(FATAL) << DebugName() << " is not defined for double values";
3823 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003824 }
3825
3826 DECLARE_INSTRUCTION(Below);
3827
3828 IfCondition GetCondition() const OVERRIDE {
3829 return kCondB;
3830 }
3831
3832 IfCondition GetOppositeCondition() const OVERRIDE {
3833 return kCondAE;
3834 }
3835
Artem Serovcced8ba2017-07-19 18:18:09 +01003836 protected:
3837 DEFAULT_COPY_CONSTRUCTOR(Below);
3838
Aart Bike9f37602015-10-09 11:15:55 -07003839 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003840 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003841 return MakeUnsigned(x) < MakeUnsigned(y);
3842 }
Aart Bike9f37602015-10-09 11:15:55 -07003843};
3844
Vladimir Markofcb503c2016-05-18 12:48:17 +01003845class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003846 public:
3847 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3848 : HCondition(first, second, dex_pc) {}
3849
3850 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003851 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003852 }
3853 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003854 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3855 }
3856 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3857 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3858 LOG(FATAL) << DebugName() << " is not defined for float values";
3859 UNREACHABLE();
3860 }
3861 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3862 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3863 LOG(FATAL) << DebugName() << " is not defined for double values";
3864 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003865 }
3866
3867 DECLARE_INSTRUCTION(BelowOrEqual);
3868
3869 IfCondition GetCondition() const OVERRIDE {
3870 return kCondBE;
3871 }
3872
3873 IfCondition GetOppositeCondition() const OVERRIDE {
3874 return kCondA;
3875 }
3876
Artem Serovcced8ba2017-07-19 18:18:09 +01003877 protected:
3878 DEFAULT_COPY_CONSTRUCTOR(BelowOrEqual);
3879
Aart Bike9f37602015-10-09 11:15:55 -07003880 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003881 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003882 return MakeUnsigned(x) <= MakeUnsigned(y);
3883 }
Aart Bike9f37602015-10-09 11:15:55 -07003884};
3885
Vladimir Markofcb503c2016-05-18 12:48:17 +01003886class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003887 public:
3888 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3889 : HCondition(first, second, dex_pc) {}
3890
3891 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003892 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003893 }
3894 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003895 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3896 }
3897 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3898 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3899 LOG(FATAL) << DebugName() << " is not defined for float values";
3900 UNREACHABLE();
3901 }
3902 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3903 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3904 LOG(FATAL) << DebugName() << " is not defined for double values";
3905 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003906 }
3907
3908 DECLARE_INSTRUCTION(Above);
3909
3910 IfCondition GetCondition() const OVERRIDE {
3911 return kCondA;
3912 }
3913
3914 IfCondition GetOppositeCondition() const OVERRIDE {
3915 return kCondBE;
3916 }
3917
Artem Serovcced8ba2017-07-19 18:18:09 +01003918 protected:
3919 DEFAULT_COPY_CONSTRUCTOR(Above);
3920
Aart Bike9f37602015-10-09 11:15:55 -07003921 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003922 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003923 return MakeUnsigned(x) > MakeUnsigned(y);
3924 }
Aart Bike9f37602015-10-09 11:15:55 -07003925};
3926
Vladimir Markofcb503c2016-05-18 12:48:17 +01003927class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003928 public:
3929 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3930 : HCondition(first, second, dex_pc) {}
3931
3932 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003933 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003934 }
3935 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003936 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3937 }
3938 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3939 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3940 LOG(FATAL) << DebugName() << " is not defined for float values";
3941 UNREACHABLE();
3942 }
3943 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3944 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3945 LOG(FATAL) << DebugName() << " is not defined for double values";
3946 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003947 }
3948
3949 DECLARE_INSTRUCTION(AboveOrEqual);
3950
3951 IfCondition GetCondition() const OVERRIDE {
3952 return kCondAE;
3953 }
3954
3955 IfCondition GetOppositeCondition() const OVERRIDE {
3956 return kCondB;
3957 }
3958
Artem Serovcced8ba2017-07-19 18:18:09 +01003959 protected:
3960 DEFAULT_COPY_CONSTRUCTOR(AboveOrEqual);
3961
Aart Bike9f37602015-10-09 11:15:55 -07003962 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003963 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003964 return MakeUnsigned(x) >= MakeUnsigned(y);
3965 }
Aart Bike9f37602015-10-09 11:15:55 -07003966};
Dave Allison20dfc792014-06-16 20:44:29 -07003967
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003968// Instruction to check how two inputs compare to each other.
3969// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003970class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003971 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003972 // Note that `comparison_type` is the type of comparison performed
3973 // between the comparison's inputs, not the type of the instantiated
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003974 // HCompare instruction (which is always DataType::Type::kInt).
3975 HCompare(DataType::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003976 HInstruction* first,
3977 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003978 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003979 uint32_t dex_pc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003980 : HBinaryOperation(DataType::Type::kInt32,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003981 first,
3982 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003983 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003984 dex_pc) {
3985 SetPackedField<ComparisonBiasField>(bias);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003986 DCHECK_EQ(comparison_type, DataType::Kind(first->GetType()));
3987 DCHECK_EQ(comparison_type, DataType::Kind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003988 }
3989
Roland Levillain9867bc72015-08-05 10:21:34 +01003990 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003991 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3992
3993 template <typename T>
3994 int32_t ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003995 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003996 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3997 // Handle the bias.
3998 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3999 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004000
Roland Levillain9867bc72015-08-05 10:21:34 +01004001 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004002 // Note that there is no "cmp-int" Dex instruction so we shouldn't
4003 // reach this code path when processing a freshly built HIR
4004 // graph. However HCompare integer instructions can be synthesized
4005 // by the instruction simplifier to implement IntegerCompare and
4006 // IntegerSignum intrinsics, so we have to handle this case.
4007 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004008 }
4009 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004010 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4011 }
4012 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4013 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4014 }
4015 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4016 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01004017 }
4018
Vladimir Marko372f10e2016-05-17 16:30:10 +01004019 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004020 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00004021 }
4022
Vladimir Markoa1de9182016-02-25 11:37:38 +00004023 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04004024
Roland Levillain31dd3d62016-02-16 12:21:02 +00004025 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00004026 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00004027 bool IsGtBias() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004028 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004029 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00004030 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004031
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004032 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type type ATTRIBUTE_UNUSED) {
Roland Levillain1693a1f2016-03-15 14:57:31 +00004033 // Comparisons do not require a runtime call in any back end.
4034 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004035 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004036
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004037 DECLARE_INSTRUCTION(Compare);
4038
Roland Levillain31dd3d62016-02-16 12:21:02 +00004039 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004040 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
4041 static constexpr size_t kFieldComparisonBiasSize =
4042 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
4043 static constexpr size_t kNumberOfComparePackedBits =
4044 kFieldComparisonBias + kFieldComparisonBiasSize;
4045 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4046 using ComparisonBiasField =
4047 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
4048
Roland Levillain31dd3d62016-02-16 12:21:02 +00004049 // Return an integer constant containing the result of a comparison evaluated at compile time.
4050 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
4051 DCHECK(value == -1 || value == 0 || value == 1) << value;
4052 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
4053 }
4054
Artem Serovcced8ba2017-07-19 18:18:09 +01004055 DEFAULT_COPY_CONSTRUCTOR(Compare);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004056};
4057
Nicolas Geoffray2b615ba2017-01-06 14:40:07 +00004058class HNewInstance FINAL : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00004059 public:
4060 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00004061 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004062 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00004063 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00004064 bool finalizable,
4065 QuickEntrypointEnum entrypoint)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004066 : HExpression(DataType::Type::kReference, SideEffects::CanTriggerGC(), dex_pc),
David Brazdil6de19382016-01-08 17:37:10 +00004067 type_index_(type_index),
4068 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00004069 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004070 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00004071 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00004072 }
4073
Artem Serovcced8ba2017-07-19 18:18:09 +01004074 bool IsClonable() const OVERRIDE { return true; }
4075
Andreas Gampea5b09a62016-11-17 15:21:22 -08004076 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00004077 const DexFile& GetDexFile() const { return dex_file_; }
4078
4079 // Calls runtime so needs an environment.
4080 bool NeedsEnvironment() const OVERRIDE { return true; }
4081
Mingyao Yang062157f2016-03-02 10:15:36 -08004082 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
4083 bool CanThrow() const OVERRIDE { return true; }
4084
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004085 bool NeedsChecks() const {
4086 return entrypoint_ == kQuickAllocObjectWithChecks;
4087 }
David Brazdil6de19382016-01-08 17:37:10 +00004088
Vladimir Markoa1de9182016-02-25 11:37:38 +00004089 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00004090
4091 bool CanBeNull() const OVERRIDE { return false; }
4092
4093 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4094
4095 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
4096 entrypoint_ = entrypoint;
4097 }
4098
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004099 HLoadClass* GetLoadClass() const {
4100 HInstruction* input = InputAt(0);
4101 if (input->IsClinitCheck()) {
4102 input = input->InputAt(0);
4103 }
4104 DCHECK(input->IsLoadClass());
4105 return input->AsLoadClass();
4106 }
4107
David Brazdil6de19382016-01-08 17:37:10 +00004108 bool IsStringAlloc() const;
4109
4110 DECLARE_INSTRUCTION(NewInstance);
4111
Artem Serovcced8ba2017-07-19 18:18:09 +01004112 protected:
4113 DEFAULT_COPY_CONSTRUCTOR(NewInstance);
4114
David Brazdil6de19382016-01-08 17:37:10 +00004115 private:
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004116 static constexpr size_t kFlagFinalizable = kNumberOfExpressionPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004117 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
4118 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
4119 "Too many packed fields.");
4120
Andreas Gampea5b09a62016-11-17 15:21:22 -08004121 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00004122 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00004123 QuickEntrypointEnum entrypoint_;
David Brazdil6de19382016-01-08 17:37:10 +00004124};
4125
Agi Csaki05f20562015-08-19 14:58:14 -07004126enum IntrinsicNeedsEnvironmentOrCache {
4127 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
4128 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07004129};
4130
Aart Bik5d75afe2015-12-14 11:57:01 -08004131enum IntrinsicSideEffects {
4132 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
4133 kReadSideEffects, // Intrinsic may read heap memory.
4134 kWriteSideEffects, // Intrinsic may write heap memory.
4135 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
4136};
4137
4138enum IntrinsicExceptions {
4139 kNoThrow, // Intrinsic does not throw any exceptions.
4140 kCanThrow // Intrinsic may throw exceptions.
4141};
4142
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004143class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004144 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004145 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004146
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01004147 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004148 SetRawInputAt(index, argument);
4149 }
4150
Roland Levillain3e3d7332015-04-28 11:00:54 +01004151 // Return the number of arguments. This number can be lower than
4152 // the number of inputs returned by InputCount(), as some invoke
4153 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
4154 // inputs at the end of their list of inputs.
4155 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
4156
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004157 DataType::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004158
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004159 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4160
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004161 InvokeType GetInvokeType() const {
4162 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004163 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004164
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01004165 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004166 return intrinsic_;
4167 }
4168
Aart Bik5d75afe2015-12-14 11:57:01 -08004169 void SetIntrinsic(Intrinsics intrinsic,
4170 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
4171 IntrinsicSideEffects side_effects,
4172 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004173
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004174 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00004175 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004176 }
4177
Aart Bikff7d89c2016-11-07 08:49:28 -08004178 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4179
Vladimir Markoa1de9182016-02-25 11:37:38 +00004180 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004181
Aart Bik71bf7b42016-11-16 10:17:46 -08004182 bool CanBeMoved() const OVERRIDE { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004183
Vladimir Marko372f10e2016-05-17 16:30:10 +01004184 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08004185 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4186 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004187
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004188 uint32_t* GetIntrinsicOptimizations() {
4189 return &intrinsic_optimizations_;
4190 }
4191
4192 const uint32_t* GetIntrinsicOptimizations() const {
4193 return &intrinsic_optimizations_;
4194 }
4195
4196 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4197
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004198 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004199 void SetResolvedMethod(ArtMethod* method) { resolved_method_ = method; }
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004200
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004201 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004202
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004203 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004204 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4205 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004206 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4207 static constexpr size_t kFieldReturnType =
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004208 kFieldInvokeType + kFieldInvokeTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004209 static constexpr size_t kFieldReturnTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004210 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00004211 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
4212 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
4213 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004214 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004215 using ReturnTypeField = BitField<DataType::Type, kFieldReturnType, kFieldReturnTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004216
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004217 HInvoke(ArenaAllocator* allocator,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004218 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004219 uint32_t number_of_other_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004220 DataType::Type return_type,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004221 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004222 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004223 ArtMethod* resolved_method,
4224 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004225 : HVariableInputSizeInstruction(
4226 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4227 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004228 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004229 number_of_arguments + number_of_other_inputs,
4230 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004231 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004232 resolved_method_(resolved_method),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004233 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07004234 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004235 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004236 SetPackedField<ReturnTypeField>(return_type);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004237 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004238 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004239 }
4240
Artem Serovcced8ba2017-07-19 18:18:09 +01004241 DEFAULT_COPY_CONSTRUCTOR(Invoke);
4242
Roland Levillain3e3d7332015-04-28 11:00:54 +01004243 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004244 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004245 const uint32_t dex_method_index_;
4246 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004247
4248 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4249 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004250};
4251
Vladimir Markofcb503c2016-05-18 12:48:17 +01004252class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004253 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004254 HInvokeUnresolved(ArenaAllocator* allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004255 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004256 DataType::Type return_type,
Calin Juravle175dc732015-08-25 15:42:32 +01004257 uint32_t dex_pc,
4258 uint32_t dex_method_index,
4259 InvokeType invoke_type)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004260 : HInvoke(allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004261 number_of_arguments,
4262 0u /* number_of_other_inputs */,
4263 return_type,
4264 dex_pc,
4265 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004266 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01004267 invoke_type) {
4268 }
4269
Artem Serovcced8ba2017-07-19 18:18:09 +01004270 bool IsClonable() const OVERRIDE { return true; }
4271
Calin Juravle175dc732015-08-25 15:42:32 +01004272 DECLARE_INSTRUCTION(InvokeUnresolved);
4273
Artem Serovcced8ba2017-07-19 18:18:09 +01004274 protected:
4275 DEFAULT_COPY_CONSTRUCTOR(InvokeUnresolved);
Calin Juravle175dc732015-08-25 15:42:32 +01004276};
4277
Orion Hodsonac141392017-01-13 11:53:47 +00004278class HInvokePolymorphic FINAL : public HInvoke {
4279 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004280 HInvokePolymorphic(ArenaAllocator* allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004281 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004282 DataType::Type return_type,
Orion Hodsonac141392017-01-13 11:53:47 +00004283 uint32_t dex_pc,
4284 uint32_t dex_method_index)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004285 : HInvoke(allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004286 number_of_arguments,
4287 0u /* number_of_other_inputs */,
4288 return_type,
4289 dex_pc,
4290 dex_method_index,
4291 nullptr,
4292 kVirtual) {}
4293
Artem Serovcced8ba2017-07-19 18:18:09 +01004294 bool IsClonable() const OVERRIDE { return true; }
4295
Orion Hodsonac141392017-01-13 11:53:47 +00004296 DECLARE_INSTRUCTION(InvokePolymorphic);
4297
Artem Serovcced8ba2017-07-19 18:18:09 +01004298 protected:
4299 DEFAULT_COPY_CONSTRUCTOR(InvokePolymorphic);
Orion Hodsonac141392017-01-13 11:53:47 +00004300};
4301
Vladimir Markofcb503c2016-05-18 12:48:17 +01004302class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004303 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004304 // Requirements of this method call regarding the class
4305 // initialization (clinit) check of its declaring class.
4306 enum class ClinitCheckRequirement {
4307 kNone, // Class already initialized.
4308 kExplicit, // Static call having explicit clinit check as last input.
4309 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004310 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004311 };
4312
Vladimir Marko58155012015-08-19 12:49:41 +00004313 // Determines how to load the target ArtMethod*.
4314 enum class MethodLoadKind {
4315 // Use a String init ArtMethod* loaded from Thread entrypoints.
4316 kStringInit,
4317
4318 // Use the method's own ArtMethod* loaded by the register allocator.
4319 kRecursive,
4320
Vladimir Marko65979462017-05-19 17:25:12 +01004321 // Use PC-relative boot image ArtMethod* address that will be known at link time.
4322 // Used for boot image methods referenced by boot image code.
4323 kBootImageLinkTimePcRelative,
4324
Vladimir Marko58155012015-08-19 12:49:41 +00004325 // Use ArtMethod* at a known address, embed the direct address in the code.
4326 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
4327 kDirectAddress,
4328
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004329 // Load from an entry in the .bss section using a PC-relative load.
4330 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
4331 kBssEntry,
Vladimir Marko58155012015-08-19 12:49:41 +00004332
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004333 // Make a runtime call to resolve and call the method. This is the last-resort-kind
4334 // used when other kinds are unimplemented on a particular architecture.
4335 kRuntimeCall,
Vladimir Marko58155012015-08-19 12:49:41 +00004336 };
4337
4338 // Determines the location of the code pointer.
4339 enum class CodePtrLocation {
4340 // Recursive call, use local PC-relative call instruction.
4341 kCallSelf,
4342
Vladimir Marko58155012015-08-19 12:49:41 +00004343 // Use code pointer from the ArtMethod*.
4344 // Used when we don't know the target code. This is also the last-resort-kind used when
4345 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4346 kCallArtMethod,
4347 };
4348
4349 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004350 MethodLoadKind method_load_kind;
4351 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004352 // The method load data holds
4353 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4354 // Note that there are multiple string init methods, each having its own offset.
4355 // - the method address for kDirectAddress
Vladimir Markodc151b22015-10-15 18:02:30 +01004356 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004357 };
4358
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004359 HInvokeStaticOrDirect(ArenaAllocator* allocator,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004360 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004361 DataType::Type return_type,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004362 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004363 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004364 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004365 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004366 InvokeType invoke_type,
4367 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004368 ClinitCheckRequirement clinit_check_requirement)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004369 : HInvoke(allocator,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004370 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004371 // There is potentially one extra argument for the HCurrentMethod node, and
4372 // potentially one other if the clinit check is explicit, and potentially
4373 // one other if the method is a string factory.
4374 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004375 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004376 return_type,
4377 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004378 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004379 resolved_method,
4380 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004381 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004382 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004383 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4384 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004385
Artem Serovcced8ba2017-07-19 18:18:09 +01004386 bool IsClonable() const OVERRIDE { return true; }
4387
Vladimir Markodc151b22015-10-15 18:02:30 +01004388 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004389 bool had_current_method_input = HasCurrentMethodInput();
4390 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4391
4392 // Using the current method is the default and once we find a better
4393 // method load kind, we should not go back to using the current method.
4394 DCHECK(had_current_method_input || !needs_current_method_input);
4395
4396 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004397 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4398 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004399 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004400 dispatch_info_ = dispatch_info;
4401 }
4402
Aart Bik6daebeb2017-04-03 14:35:41 -07004403 DispatchInfo GetDispatchInfo() const {
4404 return dispatch_info_;
4405 }
4406
Vladimir Markoc53c0792015-11-19 15:48:33 +00004407 void AddSpecialInput(HInstruction* input) {
4408 // We allow only one special input.
4409 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4410 DCHECK(InputCount() == GetSpecialInputIndex() ||
4411 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4412 InsertInputAt(GetSpecialInputIndex(), input);
4413 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004414
Vladimir Marko372f10e2016-05-17 16:30:10 +01004415 using HInstruction::GetInputRecords; // Keep the const version visible.
4416 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
4417 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4418 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4419 DCHECK(!input_records.empty());
4420 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4421 HInstruction* last_input = input_records.back().GetInstruction();
4422 // Note: `last_input` may be null during arguments setup.
4423 if (last_input != nullptr) {
4424 // `last_input` is the last input of a static invoke marked as having
4425 // an explicit clinit check. It must either be:
4426 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4427 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4428 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4429 }
4430 }
4431 return input_records;
4432 }
4433
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004434 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004435 // We access the method via the dex cache so we can't do an implicit null check.
4436 // TODO: for intrinsics we can generate implicit null checks.
4437 return false;
4438 }
4439
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004440 bool CanBeNull() const OVERRIDE {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004441 return GetPackedField<ReturnTypeField>() == DataType::Type::kReference && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004442 }
4443
Vladimir Markoc53c0792015-11-19 15:48:33 +00004444 // Get the index of the special input, if any.
4445 //
David Brazdil6de19382016-01-08 17:37:10 +00004446 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4447 // method pointer; otherwise there may be one platform-specific special input,
4448 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004449 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004450 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004451
Vladimir Marko58155012015-08-19 12:49:41 +00004452 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4453 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4454 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004455 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004456 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004457 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko65979462017-05-19 17:25:12 +01004458 bool HasPcRelativeMethodLoadKind() const {
4459 return GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004460 GetMethodLoadKind() == MethodLoadKind::kBssEntry;
Vladimir Marko65979462017-05-19 17:25:12 +01004461 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004462 bool HasCurrentMethodInput() const {
4463 // This function can be called only after the invoke has been fully initialized by the builder.
4464 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004465 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004466 return true;
4467 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004468 DCHECK(InputCount() == GetSpecialInputIndex() ||
4469 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004470 return false;
4471 }
4472 }
Vladimir Marko58155012015-08-19 12:49:41 +00004473
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004474 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004475 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004476 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004477 }
4478
4479 uint64_t GetMethodAddress() const {
4480 DCHECK(HasMethodAddress());
4481 return dispatch_info_.method_load_data;
4482 }
4483
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004484 const DexFile& GetDexFileForPcRelativeDexCache() const;
4485
Vladimir Markoa1de9182016-02-25 11:37:38 +00004486 ClinitCheckRequirement GetClinitCheckRequirement() const {
4487 return GetPackedField<ClinitCheckRequirementField>();
4488 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004489
Roland Levillain4c0eb422015-04-24 16:43:49 +01004490 // Is this instruction a call to a static method?
4491 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004492 return GetInvokeType() == kStatic;
4493 }
4494
4495 MethodReference GetTargetMethod() const {
4496 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004497 }
4498
Vladimir Markofbb184a2015-11-13 14:47:00 +00004499 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4500 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4501 // instruction; only relevant for static calls with explicit clinit check.
4502 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004503 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004504 size_t last_input_index = inputs_.size() - 1u;
4505 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004506 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004507 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004508 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004509 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004510 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004511 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004512 }
4513
4514 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004515 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004516 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004517 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004518 }
4519
4520 // Is this a call to a static method whose declaring class has an
4521 // implicit intialization check requirement?
4522 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004523 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004524 }
4525
Vladimir Markob554b5a2015-11-06 12:57:55 +00004526 // Does this method load kind need the current method as an input?
4527 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004528 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kRuntimeCall;
Vladimir Markob554b5a2015-11-06 12:57:55 +00004529 }
4530
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004531 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004532
Artem Serovcced8ba2017-07-19 18:18:09 +01004533 protected:
4534 DEFAULT_COPY_CONSTRUCTOR(InvokeStaticOrDirect);
4535
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004536 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004537 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004538 static constexpr size_t kFieldClinitCheckRequirementSize =
4539 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4540 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4541 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4542 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4543 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004544 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4545 kFieldClinitCheckRequirement,
4546 kFieldClinitCheckRequirementSize>;
4547
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004548 // Cached values of the resolved method, to avoid needing the mutator lock.
Vladimir Marko58155012015-08-19 12:49:41 +00004549 MethodReference target_method_;
4550 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004551};
Vladimir Markof64242a2015-12-01 14:58:23 +00004552std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004553std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004554
Vladimir Markofcb503c2016-05-18 12:48:17 +01004555class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004556 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004557 HInvokeVirtual(ArenaAllocator* allocator,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004558 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004559 DataType::Type return_type,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004560 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004561 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004562 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004563 uint32_t vtable_index)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004564 : HInvoke(allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004565 number_of_arguments,
4566 0u,
4567 return_type,
4568 dex_pc,
4569 dex_method_index,
4570 resolved_method,
4571 kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004572 vtable_index_(vtable_index) {}
4573
Artem Serovcced8ba2017-07-19 18:18:09 +01004574 bool IsClonable() const OVERRIDE { return true; }
4575
Aart Bik71bf7b42016-11-16 10:17:46 -08004576 bool CanBeNull() const OVERRIDE {
4577 switch (GetIntrinsic()) {
4578 case Intrinsics::kThreadCurrentThread:
4579 case Intrinsics::kStringBufferAppend:
4580 case Intrinsics::kStringBufferToString:
4581 case Intrinsics::kStringBuilderAppend:
4582 case Intrinsics::kStringBuilderToString:
4583 return false;
4584 default:
4585 return HInvoke::CanBeNull();
4586 }
4587 }
4588
Calin Juravle641547a2015-04-21 22:08:51 +01004589 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004590 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004591 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004592 }
4593
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004594 uint32_t GetVTableIndex() const { return vtable_index_; }
4595
4596 DECLARE_INSTRUCTION(InvokeVirtual);
4597
Artem Serovcced8ba2017-07-19 18:18:09 +01004598 protected:
4599 DEFAULT_COPY_CONSTRUCTOR(InvokeVirtual);
4600
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004601 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004602 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004603 const uint32_t vtable_index_;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004604};
4605
Vladimir Markofcb503c2016-05-18 12:48:17 +01004606class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004607 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004608 HInvokeInterface(ArenaAllocator* allocator,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004609 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004610 DataType::Type return_type,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004611 uint32_t dex_pc,
4612 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004613 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004614 uint32_t imt_index)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004615 : HInvoke(allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004616 number_of_arguments,
4617 0u,
4618 return_type,
4619 dex_pc,
4620 dex_method_index,
4621 resolved_method,
4622 kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004623 imt_index_(imt_index) {}
4624
Artem Serovcced8ba2017-07-19 18:18:09 +01004625 bool IsClonable() const OVERRIDE { return true; }
4626
Calin Juravle641547a2015-04-21 22:08:51 +01004627 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004628 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004629 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004630 }
4631
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004632 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
4633 // The assembly stub currently needs it.
4634 return true;
4635 }
4636
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004637 uint32_t GetImtIndex() const { return imt_index_; }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004638
4639 DECLARE_INSTRUCTION(InvokeInterface);
4640
Artem Serovcced8ba2017-07-19 18:18:09 +01004641 protected:
4642 DEFAULT_COPY_CONSTRUCTOR(InvokeInterface);
4643
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004644 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004645 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004646 const uint32_t imt_index_;
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004647};
4648
Vladimir Markofcb503c2016-05-18 12:48:17 +01004649class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004650 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004651 HNeg(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004652 : HUnaryOperation(result_type, input, dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004653 DCHECK_EQ(result_type, DataType::Kind(input->GetType()));
Roland Levillain937e6cd2016-03-22 11:54:37 +00004654 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004655
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004656 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004657
4658 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004659 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004660 }
4661 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004662 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004663 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004664 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4665 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4666 }
4667 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4668 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4669 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004670
Roland Levillain88cb1752014-10-20 16:36:47 +01004671 DECLARE_INSTRUCTION(Neg);
4672
Artem Serovcced8ba2017-07-19 18:18:09 +01004673 protected:
4674 DEFAULT_COPY_CONSTRUCTOR(Neg);
Roland Levillain88cb1752014-10-20 16:36:47 +01004675};
4676
Vladimir Markofcb503c2016-05-18 12:48:17 +01004677class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004678 public:
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004679 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004680 : HExpression(DataType::Type::kReference, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004681 SetRawInputAt(0, cls);
4682 SetRawInputAt(1, length);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004683 }
4684
Artem Serovcced8ba2017-07-19 18:18:09 +01004685 bool IsClonable() const OVERRIDE { return true; }
4686
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004687 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004688 bool NeedsEnvironment() const OVERRIDE { return true; }
4689
Mingyao Yang0c365e62015-03-31 15:09:29 -07004690 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4691 bool CanThrow() const OVERRIDE { return true; }
4692
Calin Juravle10e244f2015-01-26 18:54:32 +00004693 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004694
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004695 HLoadClass* GetLoadClass() const {
4696 DCHECK(InputAt(0)->IsLoadClass());
4697 return InputAt(0)->AsLoadClass();
4698 }
4699
4700 HInstruction* GetLength() const {
4701 return InputAt(1);
4702 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004703
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004704 DECLARE_INSTRUCTION(NewArray);
4705
Artem Serovcced8ba2017-07-19 18:18:09 +01004706 protected:
4707 DEFAULT_COPY_CONSTRUCTOR(NewArray);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004708};
4709
Vladimir Markofcb503c2016-05-18 12:48:17 +01004710class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004711 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004712 HAdd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004713 HInstruction* left,
4714 HInstruction* right,
4715 uint32_t dex_pc = kNoDexPc)
4716 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004717
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004718 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004719
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004720 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004721
4722 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004723 return GetBlock()->GetGraph()->GetIntConstant(
4724 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004725 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004726 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004727 return GetBlock()->GetGraph()->GetLongConstant(
4728 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004729 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004730 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4731 return GetBlock()->GetGraph()->GetFloatConstant(
4732 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4733 }
4734 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4735 return GetBlock()->GetGraph()->GetDoubleConstant(
4736 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4737 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004738
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004739 DECLARE_INSTRUCTION(Add);
4740
Artem Serovcced8ba2017-07-19 18:18:09 +01004741 protected:
4742 DEFAULT_COPY_CONSTRUCTOR(Add);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004743};
4744
Vladimir Markofcb503c2016-05-18 12:48:17 +01004745class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004746 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004747 HSub(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004748 HInstruction* left,
4749 HInstruction* right,
4750 uint32_t dex_pc = kNoDexPc)
4751 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004752
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004753 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004754
4755 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004756 return GetBlock()->GetGraph()->GetIntConstant(
4757 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004758 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004759 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004760 return GetBlock()->GetGraph()->GetLongConstant(
4761 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004762 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004763 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4764 return GetBlock()->GetGraph()->GetFloatConstant(
4765 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4766 }
4767 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4768 return GetBlock()->GetGraph()->GetDoubleConstant(
4769 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4770 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004771
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004772 DECLARE_INSTRUCTION(Sub);
4773
Artem Serovcced8ba2017-07-19 18:18:09 +01004774 protected:
4775 DEFAULT_COPY_CONSTRUCTOR(Sub);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004776};
4777
Vladimir Markofcb503c2016-05-18 12:48:17 +01004778class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004779 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004780 HMul(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004781 HInstruction* left,
4782 HInstruction* right,
4783 uint32_t dex_pc = kNoDexPc)
4784 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004785
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004786 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004787
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004788 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004789
4790 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004791 return GetBlock()->GetGraph()->GetIntConstant(
4792 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004793 }
4794 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004795 return GetBlock()->GetGraph()->GetLongConstant(
4796 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004797 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004798 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4799 return GetBlock()->GetGraph()->GetFloatConstant(
4800 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4801 }
4802 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4803 return GetBlock()->GetGraph()->GetDoubleConstant(
4804 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4805 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004806
4807 DECLARE_INSTRUCTION(Mul);
4808
Artem Serovcced8ba2017-07-19 18:18:09 +01004809 protected:
4810 DEFAULT_COPY_CONSTRUCTOR(Mul);
Calin Juravle34bacdf2014-10-07 20:23:36 +01004811};
4812
Vladimir Markofcb503c2016-05-18 12:48:17 +01004813class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004814 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004815 HDiv(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004816 HInstruction* left,
4817 HInstruction* right,
4818 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004819 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004820
Roland Levillain9867bc72015-08-05 10:21:34 +01004821 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004822 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004823 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004824 // Our graph structure ensures we never have 0 for `y` during
4825 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004826 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004827 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004828 return (y == -1) ? -x : x / y;
4829 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004830
Roland Levillain31dd3d62016-02-16 12:21:02 +00004831 template <typename T>
4832 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004833 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004834 return x / y;
4835 }
4836
Roland Levillain9867bc72015-08-05 10:21:34 +01004837 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004838 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004839 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004840 }
4841 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004842 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004843 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4844 }
4845 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4846 return GetBlock()->GetGraph()->GetFloatConstant(
4847 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4848 }
4849 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4850 return GetBlock()->GetGraph()->GetDoubleConstant(
4851 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004852 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004853
4854 DECLARE_INSTRUCTION(Div);
4855
Artem Serovcced8ba2017-07-19 18:18:09 +01004856 protected:
4857 DEFAULT_COPY_CONSTRUCTOR(Div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00004858};
4859
Vladimir Markofcb503c2016-05-18 12:48:17 +01004860class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004861 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004862 HRem(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004863 HInstruction* left,
4864 HInstruction* right,
4865 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004866 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004867
Roland Levillain9867bc72015-08-05 10:21:34 +01004868 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004869 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004870 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004871 // Our graph structure ensures we never have 0 for `y` during
4872 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004873 DCHECK_NE(y, 0);
4874 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4875 return (y == -1) ? 0 : x % y;
4876 }
4877
Roland Levillain31dd3d62016-02-16 12:21:02 +00004878 template <typename T>
4879 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004880 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004881 return std::fmod(x, y);
4882 }
4883
Roland Levillain9867bc72015-08-05 10:21:34 +01004884 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004885 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004886 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004887 }
4888 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004889 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004890 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004891 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004892 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4893 return GetBlock()->GetGraph()->GetFloatConstant(
4894 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4895 }
4896 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4897 return GetBlock()->GetGraph()->GetDoubleConstant(
4898 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4899 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004900
4901 DECLARE_INSTRUCTION(Rem);
4902
Artem Serovcced8ba2017-07-19 18:18:09 +01004903 protected:
4904 DEFAULT_COPY_CONSTRUCTOR(Rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00004905};
4906
Vladimir Markofcb503c2016-05-18 12:48:17 +01004907class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00004908 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004909 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4910 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004911 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004912 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004913 SetRawInputAt(0, value);
4914 }
4915
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004916 DataType::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004917
Calin Juravled0d48522014-11-04 16:40:20 +00004918 bool CanBeMoved() const OVERRIDE { return true; }
4919
Vladimir Marko372f10e2016-05-17 16:30:10 +01004920 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004921 return true;
4922 }
4923
4924 bool NeedsEnvironment() const OVERRIDE { return true; }
4925 bool CanThrow() const OVERRIDE { return true; }
4926
Calin Juravled0d48522014-11-04 16:40:20 +00004927 DECLARE_INSTRUCTION(DivZeroCheck);
4928
Artem Serovcced8ba2017-07-19 18:18:09 +01004929 protected:
4930 DEFAULT_COPY_CONSTRUCTOR(DivZeroCheck);
Calin Juravled0d48522014-11-04 16:40:20 +00004931};
4932
Vladimir Markofcb503c2016-05-18 12:48:17 +01004933class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004934 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004935 HShl(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004936 HInstruction* value,
4937 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004938 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004939 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004940 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
4941 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004942 }
4943
Roland Levillain5b5b9312016-03-22 14:57:31 +00004944 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004945 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004946 return value << (distance & max_shift_distance);
4947 }
4948
4949 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004950 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004951 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004952 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004953 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004954 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004955 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004956 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004957 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4958 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4959 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4960 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004961 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004962 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4963 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004964 LOG(FATAL) << DebugName() << " is not defined for float values";
4965 UNREACHABLE();
4966 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004967 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4968 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004969 LOG(FATAL) << DebugName() << " is not defined for double values";
4970 UNREACHABLE();
4971 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004972
4973 DECLARE_INSTRUCTION(Shl);
4974
Artem Serovcced8ba2017-07-19 18:18:09 +01004975 protected:
4976 DEFAULT_COPY_CONSTRUCTOR(Shl);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004977};
4978
Vladimir Markofcb503c2016-05-18 12:48:17 +01004979class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004980 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004981 HShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004982 HInstruction* value,
4983 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004984 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004985 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004986 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
4987 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004988 }
4989
Roland Levillain5b5b9312016-03-22 14:57:31 +00004990 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004991 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004992 return value >> (distance & max_shift_distance);
4993 }
4994
4995 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004996 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004997 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004998 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004999 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005000 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005001 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005002 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005003 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5004 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5005 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5006 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005007 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005008 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5009 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005010 LOG(FATAL) << DebugName() << " is not defined for float values";
5011 UNREACHABLE();
5012 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005013 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5014 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005015 LOG(FATAL) << DebugName() << " is not defined for double values";
5016 UNREACHABLE();
5017 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005018
5019 DECLARE_INSTRUCTION(Shr);
5020
Artem Serovcced8ba2017-07-19 18:18:09 +01005021 protected:
5022 DEFAULT_COPY_CONSTRUCTOR(Shr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005023};
5024
Vladimir Markofcb503c2016-05-18 12:48:17 +01005025class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005026 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005027 HUShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005028 HInstruction* value,
5029 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005030 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00005031 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005032 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5033 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00005034 }
5035
Roland Levillain5b5b9312016-03-22 14:57:31 +00005036 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005037 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005038 typedef typename std::make_unsigned<T>::type V;
5039 V ux = static_cast<V>(value);
5040 return static_cast<T>(ux >> (distance & max_shift_distance));
5041 }
5042
5043 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005044 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005045 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005046 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005047 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005048 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005049 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005050 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005051 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5052 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5053 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5054 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00005055 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005056 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5057 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005058 LOG(FATAL) << DebugName() << " is not defined for float values";
5059 UNREACHABLE();
5060 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005061 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5062 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005063 LOG(FATAL) << DebugName() << " is not defined for double values";
5064 UNREACHABLE();
5065 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005066
5067 DECLARE_INSTRUCTION(UShr);
5068
Artem Serovcced8ba2017-07-19 18:18:09 +01005069 protected:
5070 DEFAULT_COPY_CONSTRUCTOR(UShr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005071};
5072
Vladimir Markofcb503c2016-05-18 12:48:17 +01005073class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005074 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005075 HAnd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005076 HInstruction* left,
5077 HInstruction* right,
5078 uint32_t dex_pc = kNoDexPc)
5079 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005080
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005081 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005082
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005083 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005084
5085 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005086 return GetBlock()->GetGraph()->GetIntConstant(
5087 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005088 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005089 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005090 return GetBlock()->GetGraph()->GetLongConstant(
5091 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005092 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005093 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5094 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5095 LOG(FATAL) << DebugName() << " is not defined for float values";
5096 UNREACHABLE();
5097 }
5098 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5099 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5100 LOG(FATAL) << DebugName() << " is not defined for double values";
5101 UNREACHABLE();
5102 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005103
5104 DECLARE_INSTRUCTION(And);
5105
Artem Serovcced8ba2017-07-19 18:18:09 +01005106 protected:
5107 DEFAULT_COPY_CONSTRUCTOR(And);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005108};
5109
Vladimir Markofcb503c2016-05-18 12:48:17 +01005110class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005111 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005112 HOr(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005113 HInstruction* left,
5114 HInstruction* right,
5115 uint32_t dex_pc = kNoDexPc)
5116 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005117
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005118 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005119
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005120 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005121
5122 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005123 return GetBlock()->GetGraph()->GetIntConstant(
5124 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005125 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005126 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005127 return GetBlock()->GetGraph()->GetLongConstant(
5128 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005129 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005130 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5131 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5132 LOG(FATAL) << DebugName() << " is not defined for float values";
5133 UNREACHABLE();
5134 }
5135 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5136 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5137 LOG(FATAL) << DebugName() << " is not defined for double values";
5138 UNREACHABLE();
5139 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005140
5141 DECLARE_INSTRUCTION(Or);
5142
Artem Serovcced8ba2017-07-19 18:18:09 +01005143 protected:
5144 DEFAULT_COPY_CONSTRUCTOR(Or);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005145};
5146
Vladimir Markofcb503c2016-05-18 12:48:17 +01005147class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005148 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005149 HXor(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005150 HInstruction* left,
5151 HInstruction* right,
5152 uint32_t dex_pc = kNoDexPc)
5153 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005154
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005155 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005156
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005157 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005158
5159 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005160 return GetBlock()->GetGraph()->GetIntConstant(
5161 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005162 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005163 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005164 return GetBlock()->GetGraph()->GetLongConstant(
5165 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005166 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005167 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5168 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5169 LOG(FATAL) << DebugName() << " is not defined for float values";
5170 UNREACHABLE();
5171 }
5172 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5173 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5174 LOG(FATAL) << DebugName() << " is not defined for double values";
5175 UNREACHABLE();
5176 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005177
5178 DECLARE_INSTRUCTION(Xor);
5179
Artem Serovcced8ba2017-07-19 18:18:09 +01005180 protected:
5181 DEFAULT_COPY_CONSTRUCTOR(Xor);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005182};
5183
Vladimir Markofcb503c2016-05-18 12:48:17 +01005184class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005185 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005186 HRor(DataType::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00005187 : HBinaryOperation(result_type, value, distance) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005188 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5189 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00005190 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005191
Roland Levillain5b5b9312016-03-22 14:57:31 +00005192 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005193 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005194 typedef typename std::make_unsigned<T>::type V;
5195 V ux = static_cast<V>(value);
5196 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005197 return static_cast<T>(ux);
5198 } else {
5199 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005200 return static_cast<T>(ux >> (distance & max_shift_value)) |
5201 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005202 }
5203 }
5204
Roland Levillain5b5b9312016-03-22 14:57:31 +00005205 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005206 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005207 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005208 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005209 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005210 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005211 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005212 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005213 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5214 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5215 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5216 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005217 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005218 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5219 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005220 LOG(FATAL) << DebugName() << " is not defined for float values";
5221 UNREACHABLE();
5222 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005223 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5224 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005225 LOG(FATAL) << DebugName() << " is not defined for double values";
5226 UNREACHABLE();
5227 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005228
5229 DECLARE_INSTRUCTION(Ror);
5230
Artem Serovcced8ba2017-07-19 18:18:09 +01005231 protected:
5232 DEFAULT_COPY_CONSTRUCTOR(Ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005233};
5234
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005235// The value of a parameter in this method. Its location depends on
5236// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01005237class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005238 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005239 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005240 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005241 uint8_t index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005242 DataType::Type parameter_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005243 bool is_this = false)
5244 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005245 dex_file_(dex_file),
5246 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005247 index_(index) {
5248 SetPackedFlag<kFlagIsThis>(is_this);
5249 SetPackedFlag<kFlagCanBeNull>(!is_this);
5250 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005251
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005252 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005253 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005254 uint8_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07005255 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005256
Vladimir Markoa1de9182016-02-25 11:37:38 +00005257 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
5258 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005259
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005260 DECLARE_INSTRUCTION(ParameterValue);
5261
Artem Serovcced8ba2017-07-19 18:18:09 +01005262 protected:
5263 DEFAULT_COPY_CONSTRUCTOR(ParameterValue);
5264
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005265 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005266 // Whether or not the parameter value corresponds to 'this' argument.
5267 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
5268 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5269 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5270 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5271 "Too many packed fields.");
5272
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005273 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005274 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005275 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005276 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005277 const uint8_t index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005278};
5279
Vladimir Markofcb503c2016-05-18 12:48:17 +01005280class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005281 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005282 HNot(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005283 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005284
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005285 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005286 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005287 return true;
5288 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005289
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005290 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005291
5292 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005293 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005294 }
5295 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005296 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005297 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005298 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5299 LOG(FATAL) << DebugName() << " is not defined for float values";
5300 UNREACHABLE();
5301 }
5302 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5303 LOG(FATAL) << DebugName() << " is not defined for double values";
5304 UNREACHABLE();
5305 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005306
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005307 DECLARE_INSTRUCTION(Not);
5308
Artem Serovcced8ba2017-07-19 18:18:09 +01005309 protected:
5310 DEFAULT_COPY_CONSTRUCTOR(Not);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005311};
5312
Vladimir Markofcb503c2016-05-18 12:48:17 +01005313class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005314 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005315 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005316 : HUnaryOperation(DataType::Type::kBool, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01005317
5318 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005319 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01005320 return true;
5321 }
5322
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005323 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005324 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005325 return !x;
5326 }
5327
Roland Levillain9867bc72015-08-05 10:21:34 +01005328 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005329 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005330 }
5331 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5332 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005333 UNREACHABLE();
5334 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005335 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5336 LOG(FATAL) << DebugName() << " is not defined for float values";
5337 UNREACHABLE();
5338 }
5339 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5340 LOG(FATAL) << DebugName() << " is not defined for double values";
5341 UNREACHABLE();
5342 }
David Brazdil66d126e2015-04-03 16:02:44 +01005343
5344 DECLARE_INSTRUCTION(BooleanNot);
5345
Artem Serovcced8ba2017-07-19 18:18:09 +01005346 protected:
5347 DEFAULT_COPY_CONSTRUCTOR(BooleanNot);
David Brazdil66d126e2015-04-03 16:02:44 +01005348};
5349
Vladimir Markofcb503c2016-05-18 12:48:17 +01005350class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005351 public:
5352 // Instantiate a type conversion of `input` to `result_type`.
Vladimir Markoc8fb2112017-10-03 11:37:52 +01005353 HTypeConversion(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005354 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005355 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005356 // Invariant: We should never generate a conversion to a Boolean value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005357 DCHECK_NE(DataType::Type::kBool, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005358 }
5359
5360 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005361 DataType::Type GetInputType() const { return GetInput()->GetType(); }
5362 DataType::Type GetResultType() const { return GetType(); }
Roland Levillaindff1f282014-11-05 14:15:05 +00005363
5364 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005365 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5366 return true;
5367 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005368
Mark Mendelle82549b2015-05-06 10:55:34 -04005369 // Try to statically evaluate the conversion and return a HConstant
5370 // containing the result. If the input cannot be converted, return nullptr.
5371 HConstant* TryStaticEvaluation() const;
5372
Roland Levillaindff1f282014-11-05 14:15:05 +00005373 DECLARE_INSTRUCTION(TypeConversion);
5374
Artem Serovcced8ba2017-07-19 18:18:09 +01005375 protected:
5376 DEFAULT_COPY_CONSTRUCTOR(TypeConversion);
Roland Levillaindff1f282014-11-05 14:15:05 +00005377};
5378
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005379static constexpr uint32_t kNoRegNumber = -1;
5380
Vladimir Markofcb503c2016-05-18 12:48:17 +01005381class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005382 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005383 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
5384 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005385 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005386 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005387 SetRawInputAt(0, value);
5388 }
5389
Artem Serovcced8ba2017-07-19 18:18:09 +01005390 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00005391 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005392 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005393 return true;
5394 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005395
Calin Juravle10e244f2015-01-26 18:54:32 +00005396 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005397
Calin Juravle10e244f2015-01-26 18:54:32 +00005398 bool CanThrow() const OVERRIDE { return true; }
5399
5400 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005401
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005402 DECLARE_INSTRUCTION(NullCheck);
5403
Artem Serovcced8ba2017-07-19 18:18:09 +01005404 protected:
5405 DEFAULT_COPY_CONSTRUCTOR(NullCheck);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005406};
5407
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005408// Embeds an ArtField and all the information required by the compiler. We cache
5409// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005410class FieldInfo : public ValueObject {
5411 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005412 FieldInfo(ArtField* field,
5413 MemberOffset field_offset,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005414 DataType::Type field_type,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005415 bool is_volatile,
5416 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005417 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005418 const DexFile& dex_file)
5419 : field_(field),
5420 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005421 field_type_(field_type),
5422 is_volatile_(is_volatile),
5423 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005424 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005425 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005426
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005427 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005428 MemberOffset GetFieldOffset() const { return field_offset_; }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005429 DataType::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005430 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005431 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005432 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005433 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005434
5435 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005436 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005437 const MemberOffset field_offset_;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005438 const DataType::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005439 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005440 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005441 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005442 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005443};
5444
Vladimir Markofcb503c2016-05-18 12:48:17 +01005445class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005446 public:
5447 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005448 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005449 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005450 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005451 bool is_volatile,
5452 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005453 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005454 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005455 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005456 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005457 field_info_(field,
5458 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005459 field_type,
5460 is_volatile,
5461 field_idx,
5462 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005463 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005464 SetRawInputAt(0, value);
5465 }
5466
Artem Serovcced8ba2017-07-19 18:18:09 +01005467 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005468 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005469
Vladimir Marko372f10e2016-05-17 16:30:10 +01005470 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5471 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005472 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005473 }
5474
Calin Juravle641547a2015-04-21 22:08:51 +01005475 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005476 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005477 }
5478
5479 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005480 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5481 }
5482
Calin Juravle52c48962014-12-16 17:02:57 +00005483 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005484 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005485 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005486 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005487
Vladimir Marko61b92282017-10-11 13:23:17 +01005488 void SetType(DataType::Type new_type) {
5489 DCHECK(DataType::IsIntegralType(GetType()));
5490 DCHECK(DataType::IsIntegralType(new_type));
5491 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5492 SetPackedField<TypeField>(new_type);
5493 }
5494
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005495 DECLARE_INSTRUCTION(InstanceFieldGet);
5496
Artem Serovcced8ba2017-07-19 18:18:09 +01005497 protected:
5498 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldGet);
5499
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005500 private:
5501 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005502};
5503
Vladimir Markofcb503c2016-05-18 12:48:17 +01005504class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005505 public:
5506 HInstanceFieldSet(HInstruction* object,
5507 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005508 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005509 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005510 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005511 bool is_volatile,
5512 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005513 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005514 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005515 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005516 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005517 field_info_(field,
5518 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005519 field_type,
5520 is_volatile,
5521 field_idx,
5522 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005523 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005524 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005525 SetRawInputAt(0, object);
5526 SetRawInputAt(1, value);
5527 }
5528
Artem Serovcced8ba2017-07-19 18:18:09 +01005529 bool IsClonable() const OVERRIDE { return true; }
5530
Calin Juravle641547a2015-04-21 22:08:51 +01005531 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005532 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005533 }
5534
Calin Juravle52c48962014-12-16 17:02:57 +00005535 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005536 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005537 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005538 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005539 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005540 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5541 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005542
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005543 DECLARE_INSTRUCTION(InstanceFieldSet);
5544
Artem Serovcced8ba2017-07-19 18:18:09 +01005545 protected:
5546 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldSet);
5547
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005548 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005549 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5550 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5551 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5552 "Too many packed fields.");
5553
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005554 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005555};
5556
Vladimir Markofcb503c2016-05-18 12:48:17 +01005557class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005558 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005559 HArrayGet(HInstruction* array,
5560 HInstruction* index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005561 DataType::Type type,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005562 uint32_t dex_pc)
5563 : HArrayGet(array,
5564 index,
5565 type,
5566 SideEffects::ArrayReadOfType(type),
5567 dex_pc,
5568 /* is_string_char_at */ false) {}
5569
5570 HArrayGet(HInstruction* array,
5571 HInstruction* index,
5572 DataType::Type type,
5573 SideEffects side_effects,
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005574 uint32_t dex_pc,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005575 bool is_string_char_at)
5576 : HExpression(type, side_effects, dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005577 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005578 SetRawInputAt(0, array);
5579 SetRawInputAt(1, index);
5580 }
5581
Artem Serovcced8ba2017-07-19 18:18:09 +01005582 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005583 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005584 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005585 return true;
5586 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005587 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005588 // TODO: We can be smarter here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01005589 // Currently, unless the array is the result of NewArray, the array access is always
5590 // preceded by some form of null NullCheck necessary for the bounds check, usually
5591 // implicit null check on the ArrayLength input to BoundsCheck or Deoptimize for
5592 // dynamic BCE. There are cases when these could be removed to produce better code.
5593 // If we ever add optimizations to do so we should allow an implicit check here
5594 // (as long as the address falls in the first page).
5595 //
5596 // As an example of such fancy optimization, we could eliminate BoundsCheck for
5597 // a = cond ? new int[1] : null;
5598 // a[0]; // The Phi does not need bounds check for either input.
Calin Juravle77520bc2015-01-12 18:45:46 +00005599 return false;
5600 }
5601
David Brazdil4833f5a2015-12-16 10:37:39 +00005602 bool IsEquivalentOf(HArrayGet* other) const {
5603 bool result = (GetDexPc() == other->GetDexPc());
5604 if (kIsDebugBuild && result) {
5605 DCHECK_EQ(GetBlock(), other->GetBlock());
5606 DCHECK_EQ(GetArray(), other->GetArray());
5607 DCHECK_EQ(GetIndex(), other->GetIndex());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005608 if (DataType::IsIntOrLongType(GetType())) {
5609 DCHECK(DataType::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005610 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005611 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
5612 DCHECK(DataType::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005613 }
5614 }
5615 return result;
5616 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005617
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005618 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5619
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005620 HInstruction* GetArray() const { return InputAt(0); }
5621 HInstruction* GetIndex() const { return InputAt(1); }
5622
Vladimir Marko61b92282017-10-11 13:23:17 +01005623 void SetType(DataType::Type new_type) {
5624 DCHECK(DataType::IsIntegralType(GetType()));
5625 DCHECK(DataType::IsIntegralType(new_type));
5626 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5627 SetPackedField<TypeField>(new_type);
5628 }
5629
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005630 DECLARE_INSTRUCTION(ArrayGet);
5631
Artem Serovcced8ba2017-07-19 18:18:09 +01005632 protected:
5633 DEFAULT_COPY_CONSTRUCTOR(ArrayGet);
5634
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005635 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005636 // We treat a String as an array, creating the HArrayGet from String.charAt()
5637 // intrinsic in the instruction simplifier. We can always determine whether
5638 // a particular HArrayGet is actually a String.charAt() by looking at the type
5639 // of the input but that requires holding the mutator lock, so we prefer to use
5640 // a flag, so that code generators don't need to do the locking.
5641 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
5642 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5643 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5644 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005645};
5646
Vladimir Markofcb503c2016-05-18 12:48:17 +01005647class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005648 public:
5649 HArraySet(HInstruction* array,
5650 HInstruction* index,
5651 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005652 DataType::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005653 uint32_t dex_pc)
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005654 : HArraySet(array,
5655 index,
5656 value,
5657 expected_component_type,
5658 // Make a best guess for side effects now, may be refined during SSA building.
5659 ComputeSideEffects(GetComponentType(value->GetType(), expected_component_type)),
5660 dex_pc) {}
5661
5662 HArraySet(HInstruction* array,
5663 HInstruction* index,
5664 HInstruction* value,
5665 DataType::Type expected_component_type,
5666 SideEffects side_effects,
5667 uint32_t dex_pc)
5668 : HTemplateInstruction(side_effects, dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005669 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005670 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005671 SetPackedFlag<kFlagValueCanBeNull>(true);
5672 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005673 SetRawInputAt(0, array);
5674 SetRawInputAt(1, index);
5675 SetRawInputAt(2, value);
5676 }
5677
Artem Serovcced8ba2017-07-19 18:18:09 +01005678 bool IsClonable() const OVERRIDE { return true; }
5679
Calin Juravle77520bc2015-01-12 18:45:46 +00005680 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005681 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005682 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005683 }
5684
Mingyao Yang81014cb2015-06-02 03:16:27 -07005685 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005686 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005687
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005688 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005689 // TODO: Same as for ArrayGet.
5690 return false;
5691 }
5692
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005693 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005694 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005695 }
5696
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005697 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005698 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005699 }
5700
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005701 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005702 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005703 }
5704
Vladimir Markoa1de9182016-02-25 11:37:38 +00005705 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5706 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5707 bool StaticTypeOfArrayIsObjectArray() const {
5708 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5709 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005710
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005711 HInstruction* GetArray() const { return InputAt(0); }
5712 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005713 HInstruction* GetValue() const { return InputAt(2); }
5714
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005715 DataType::Type GetComponentType() const {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005716 return GetComponentType(GetValue()->GetType(), GetRawExpectedComponentType());
5717 }
5718
5719 static DataType::Type GetComponentType(DataType::Type value_type,
5720 DataType::Type expected_component_type) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005721 // The Dex format does not type floating point index operations. Since the
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005722 // `expected_component_type` comes from SSA building and can therefore not
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005723 // be correct, we also check what is the value type. If it is a floating
5724 // point type, we must use that type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005725 return ((value_type == DataType::Type::kFloat32) || (value_type == DataType::Type::kFloat64))
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005726 ? value_type
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005727 : expected_component_type;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005728 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005729
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005730 DataType::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005731 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005732 }
5733
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005734 static SideEffects ComputeSideEffects(DataType::Type type) {
5735 return SideEffects::ArrayWriteOfType(type).Union(SideEffectsForArchRuntimeCalls(type));
Aart Bik18b36ab2016-04-13 16:41:35 -07005736 }
5737
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005738 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type value_type) {
5739 return (value_type == DataType::Type::kReference) ? SideEffects::CanTriggerGC()
5740 : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005741 }
5742
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005743 DECLARE_INSTRUCTION(ArraySet);
5744
Artem Serovcced8ba2017-07-19 18:18:09 +01005745 protected:
5746 DEFAULT_COPY_CONSTRUCTOR(ArraySet);
5747
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005748 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005749 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5750 static constexpr size_t kFieldExpectedComponentTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005751 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00005752 static constexpr size_t kFlagNeedsTypeCheck =
5753 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5754 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005755 // Cached information for the reference_type_info_ so that codegen
5756 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005757 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5758 static constexpr size_t kNumberOfArraySetPackedBits =
5759 kFlagStaticTypeOfArrayIsObjectArray + 1;
5760 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5761 using ExpectedComponentTypeField =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005762 BitField<DataType::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005763};
5764
Vladimir Markofcb503c2016-05-18 12:48:17 +01005765class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005766 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005767 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005768 : HExpression(DataType::Type::kInt32, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005769 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005770 // Note that arrays do not change length, so the instruction does not
5771 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005772 SetRawInputAt(0, array);
5773 }
5774
Artem Serovcced8ba2017-07-19 18:18:09 +01005775 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle77520bc2015-01-12 18:45:46 +00005776 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005777 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005778 return true;
5779 }
Calin Juravle641547a2015-04-21 22:08:51 +01005780 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5781 return obj == InputAt(0);
5782 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005783
Vladimir Markodce016e2016-04-28 13:10:02 +01005784 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5785
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005786 DECLARE_INSTRUCTION(ArrayLength);
5787
Artem Serovcced8ba2017-07-19 18:18:09 +01005788 protected:
5789 DEFAULT_COPY_CONSTRUCTOR(ArrayLength);
5790
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005791 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005792 // We treat a String as an array, creating the HArrayLength from String.length()
5793 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5794 // determine whether a particular HArrayLength is actually a String.length() by
5795 // looking at the type of the input but that requires holding the mutator lock, so
5796 // we prefer to use a flag, so that code generators don't need to do the locking.
5797 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5798 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5799 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5800 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005801};
5802
Vladimir Markofcb503c2016-05-18 12:48:17 +01005803class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005804 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005805 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5806 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005807 HBoundsCheck(HInstruction* index,
5808 HInstruction* length,
5809 uint32_t dex_pc,
Vladimir Marko0259c242017-12-04 11:27:47 +00005810 bool is_string_char_at = false)
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005811 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005812 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(index->GetType()));
Vladimir Marko0259c242017-12-04 11:27:47 +00005813 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005814 SetRawInputAt(0, index);
5815 SetRawInputAt(1, length);
5816 }
5817
Artem Serovcced8ba2017-07-19 18:18:09 +01005818 bool IsClonable() const OVERRIDE { return true; }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005819 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005820 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005821 return true;
5822 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005823
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005824 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005825
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005826 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005827
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005828 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005829
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005830 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005831
5832 DECLARE_INSTRUCTION(BoundsCheck);
5833
Artem Serovcced8ba2017-07-19 18:18:09 +01005834 protected:
5835 DEFAULT_COPY_CONSTRUCTOR(BoundsCheck);
5836
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005837 private:
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005838 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005839};
5840
Vladimir Markofcb503c2016-05-18 12:48:17 +01005841class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005842 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005843 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005844 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005845
Artem Serovcced8ba2017-07-19 18:18:09 +01005846 bool IsClonable() const OVERRIDE { return true; }
5847
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005848 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005849 return true;
5850 }
5851
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005852 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5853 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005854
5855 DECLARE_INSTRUCTION(SuspendCheck);
5856
Artem Serovcced8ba2017-07-19 18:18:09 +01005857 protected:
5858 DEFAULT_COPY_CONSTRUCTOR(SuspendCheck);
5859
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005860 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005861 // Only used for code generation, in order to share the same slow path between back edges
5862 // of a same loop.
5863 SlowPathCode* slow_path_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005864};
5865
David Srbecky0cf44932015-12-09 14:09:59 +00005866// Pseudo-instruction which provides the native debugger with mapping information.
5867// It ensures that we can generate line number and local variables at this point.
5868class HNativeDebugInfo : public HTemplateInstruction<0> {
5869 public:
5870 explicit HNativeDebugInfo(uint32_t dex_pc)
5871 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5872
5873 bool NeedsEnvironment() const OVERRIDE {
5874 return true;
5875 }
5876
5877 DECLARE_INSTRUCTION(NativeDebugInfo);
5878
Artem Serovcced8ba2017-07-19 18:18:09 +01005879 protected:
5880 DEFAULT_COPY_CONSTRUCTOR(NativeDebugInfo);
David Srbecky0cf44932015-12-09 14:09:59 +00005881};
5882
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005883/**
5884 * Instruction to load a Class object.
5885 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005886class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005887 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005888 // Determines how to load the Class.
5889 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005890 // We cannot load this class. See HSharpening::SharpenLoadClass.
5891 kInvalid = -1,
5892
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005893 // Use the Class* from the method's own ArtMethod*.
5894 kReferrersClass,
5895
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005896 // Use PC-relative boot image Class* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01005897 // Used for boot image classes referenced by boot image code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005898 kBootImageLinkTimePcRelative,
5899
5900 // Use a known boot image Class* address, embedded in the code by the codegen.
5901 // Used for boot image classes referenced by apps in AOT- and JIT-compiled code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005902 kBootImageAddress,
5903
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005904 // Use a PC-relative load from a boot image ClassTable mmapped into the .bss
5905 // of the oat file.
5906 kBootImageClassTable,
5907
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005908 // Load from an entry in the .bss section using a PC-relative load.
5909 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
5910 kBssEntry,
5911
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005912 // Load from the root table associated with the JIT compiled method.
5913 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005914
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005915 // Load using a simple runtime call. This is the fall-back load kind when
5916 // the codegen is unable to use another appropriate kind.
5917 kRuntimeCall,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005918
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005919 kLast = kRuntimeCall
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005920 };
5921
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005922 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005923 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005924 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005925 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005926 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005927 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00005928 bool needs_access_check)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005929 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5930 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005931 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005932 dex_file_(dex_file),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005933 klass_(klass),
Calin Juravle2e768302015-07-28 14:41:11 +00005934 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005935 // Referrers class should not need access check. We never inline unverified
5936 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005937 DCHECK(!is_referrers_class || !needs_access_check);
5938
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005939 SetPackedField<LoadKindField>(
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005940 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kRuntimeCall);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005941 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00005942 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005943 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005944 }
5945
Artem Serovcced8ba2017-07-19 18:18:09 +01005946 bool IsClonable() const OVERRIDE { return true; }
5947
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005948 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005949
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005950 LoadKind GetLoadKind() const {
5951 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005952 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005953
5954 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005955
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005956 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005957
Andreas Gampea5b09a62016-11-17 15:21:22 -08005958 size_t ComputeHashCode() const OVERRIDE { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005959
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005960 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005961
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005962 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005963 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005964 }
5965
Calin Juravle0ba218d2015-05-19 18:46:01 +01005966 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005967 // The entrypoint the code generator is going to call does not do
5968 // clinit of the class.
5969 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005970 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005971 }
5972
5973 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00005974 return NeedsAccessCheck() ||
5975 MustGenerateClinitCheck() ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005976 GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005977 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01005978 }
5979
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005980 bool CanThrow() const OVERRIDE {
Vladimir Marko41559982017-01-06 14:04:23 +00005981 return NeedsAccessCheck() ||
5982 MustGenerateClinitCheck() ||
5983 // If the class is in the boot image, the lookup in the runtime call cannot throw.
5984 // This keeps CanThrow() consistent between non-PIC (using kBootImageAddress) and
5985 // PIC and subsequently avoids a DCE behavior dependency on the PIC option.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005986 ((GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005987 GetLoadKind() == LoadKind::kBssEntry) &&
5988 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005989 }
5990
Calin Juravleacf735c2015-02-12 15:25:22 +00005991 ReferenceTypeInfo GetLoadedClassRTI() {
5992 return loaded_class_rti_;
5993 }
5994
5995 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5996 // Make sure we only set exact types (the loaded class should never be merged).
5997 DCHECK(rti.IsExact());
5998 loaded_class_rti_ = rti;
5999 }
6000
Andreas Gampea5b09a62016-11-17 15:21:22 -08006001 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006002 const DexFile& GetDexFile() const { return dex_file_; }
6003
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006004 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006005 return GetLoadKind() == LoadKind::kRuntimeCall;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006006 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00006007
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006008 static SideEffects SideEffectsForArchRuntimeCalls() {
6009 return SideEffects::CanTriggerGC();
6010 }
6011
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006012 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006013 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006014 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006015 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006016
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006017 void MarkInBootImage() {
6018 SetPackedFlag<kFlagIsInBootImage>(true);
6019 }
6020
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006021 void AddSpecialInput(HInstruction* special_input);
6022
6023 using HInstruction::GetInputRecords; // Keep the const version visible.
6024 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
6025 return ArrayRef<HUserRecord<HInstruction*>>(
6026 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6027 }
6028
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006029 DataType::Type GetType() const OVERRIDE {
6030 return DataType::Type::kReference;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006031 }
6032
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006033 Handle<mirror::Class> GetClass() const {
6034 return klass_;
6035 }
6036
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006037 DECLARE_INSTRUCTION(LoadClass);
6038
Artem Serovcced8ba2017-07-19 18:18:09 +01006039 protected:
6040 DEFAULT_COPY_CONSTRUCTOR(LoadClass);
6041
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006042 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006043 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00006044 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006045 // Whether this instruction must generate the initialization check.
6046 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006047 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006048 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
6049 static constexpr size_t kFieldLoadKindSize =
6050 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
6051 static constexpr size_t kNumberOfLoadClassPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006052 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006053 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
6054
6055 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006056 return load_kind == LoadKind::kReferrersClass ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006057 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006058 load_kind == LoadKind::kBootImageClassTable ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006059 load_kind == LoadKind::kBssEntry ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006060 load_kind == LoadKind::kRuntimeCall;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006061 }
6062
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006063 void SetLoadKindInternal(LoadKind load_kind);
6064
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006065 // The special input is the HCurrentMethod for kRuntimeCall or kReferrersClass.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006066 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006067 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006068 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006069
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006070 // A type index and dex file where the class can be accessed. The dex file can be:
6071 // - The compiling method's dex file if the class is defined there too.
6072 // - The compiling method's dex file if the class is referenced there.
6073 // - The dex file where the class is defined. When the load kind can only be
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006074 // kBssEntry or kRuntimeCall, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08006075 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006076 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006077
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006078 Handle<mirror::Class> klass_;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006079
Calin Juravleacf735c2015-02-12 15:25:22 +00006080 ReferenceTypeInfo loaded_class_rti_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006081};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006082std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
6083
6084// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006085inline void HLoadClass::SetLoadKind(LoadKind load_kind) {
6086 // The load kind should be determined before inserting the instruction to the graph.
6087 DCHECK(GetBlock() == nullptr);
6088 DCHECK(GetEnvironment() == nullptr);
6089 SetPackedField<LoadKindField>(load_kind);
6090 if (load_kind != LoadKind::kRuntimeCall && load_kind != LoadKind::kReferrersClass) {
6091 special_input_ = HUserRecord<HInstruction*>(nullptr);
6092 }
6093 if (!NeedsEnvironment()) {
6094 SetSideEffects(SideEffects::None());
6095 }
6096}
6097
6098// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006099inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006100 // The special input is used for PC-relative loads on some architectures,
6101 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006102 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006103 GetLoadKind() == LoadKind::kBootImageAddress ||
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006104 GetLoadKind() == LoadKind::kBootImageClassTable ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006105 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006106 DCHECK(special_input_.GetInstruction() == nullptr);
6107 special_input_ = HUserRecord<HInstruction*>(special_input);
6108 special_input->AddUseAt(this, 0);
6109}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006110
Vladimir Marko372f10e2016-05-17 16:30:10 +01006111class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006112 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006113 // Determines how to load the String.
6114 enum class LoadKind {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006115 // Use PC-relative boot image String* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006116 // Used for boot image strings referenced by boot image code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006117 kBootImageLinkTimePcRelative,
6118
6119 // Use a known boot image String* address, embedded in the code by the codegen.
6120 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006121 kBootImageAddress,
6122
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006123 // Use a PC-relative load from a boot image InternTable mmapped into the .bss
6124 // of the oat file.
6125 kBootImageInternTable,
6126
Vladimir Markoaad75c62016-10-03 08:46:48 +00006127 // Load from an entry in the .bss section using a PC-relative load.
6128 // Used for strings outside boot image when .bss is accessible with a PC-relative load.
6129 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006130
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006131 // Load from the root table associated with the JIT compiled method.
6132 kJitTableAddress,
6133
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006134 // Load using a simple runtime call. This is the fall-back load kind when
6135 // the codegen is unable to use another appropriate kind.
6136 kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006137
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006138 kLast = kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006139 };
6140
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006141 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006142 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006143 const DexFile& dex_file,
6144 uint32_t dex_pc)
Vladimir Marko372f10e2016-05-17 16:30:10 +01006145 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
6146 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006147 string_index_(string_index),
6148 dex_file_(dex_file) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006149 SetPackedField<LoadKindField>(LoadKind::kRuntimeCall);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006150 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006151
Artem Serovcced8ba2017-07-19 18:18:09 +01006152 bool IsClonable() const OVERRIDE { return true; }
6153
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006154 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006155
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006156 LoadKind GetLoadKind() const {
6157 return GetPackedField<LoadKindField>();
6158 }
6159
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006160 const DexFile& GetDexFile() const {
6161 return dex_file_;
6162 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006163
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006164 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006165 return string_index_;
6166 }
6167
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006168 Handle<mirror::String> GetString() const {
6169 return string_;
6170 }
6171
6172 void SetString(Handle<mirror::String> str) {
6173 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006174 }
6175
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006176 bool CanBeMoved() const OVERRIDE { return true; }
6177
Vladimir Marko372f10e2016-05-17 16:30:10 +01006178 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006179
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006180 size_t ComputeHashCode() const OVERRIDE { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006181
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006182 // Will call the runtime if we need to load the string through
6183 // the dex cache and the string is not guaranteed to be there yet.
6184 bool NeedsEnvironment() const OVERRIDE {
6185 LoadKind load_kind = GetLoadKind();
Vladimir Marko764d4542017-05-16 10:31:41 +01006186 if (load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006187 load_kind == LoadKind::kBootImageAddress ||
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006188 load_kind == LoadKind::kBootImageInternTable ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006189 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006190 return false;
6191 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006192 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006193 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006194
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006195 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006196 return GetLoadKind() == LoadKind::kRuntimeCall;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006197 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006198
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07006199 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006200 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006201
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006202 static SideEffects SideEffectsForArchRuntimeCalls() {
6203 return SideEffects::CanTriggerGC();
6204 }
6205
Vladimir Marko372f10e2016-05-17 16:30:10 +01006206 void AddSpecialInput(HInstruction* special_input);
6207
6208 using HInstruction::GetInputRecords; // Keep the const version visible.
6209 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
6210 return ArrayRef<HUserRecord<HInstruction*>>(
6211 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006212 }
6213
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006214 DataType::Type GetType() const OVERRIDE {
6215 return DataType::Type::kReference;
Vladimir Marko372f10e2016-05-17 16:30:10 +01006216 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006217
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006218 DECLARE_INSTRUCTION(LoadString);
6219
Artem Serovcced8ba2017-07-19 18:18:09 +01006220 protected:
6221 DEFAULT_COPY_CONSTRUCTOR(LoadString);
6222
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006223 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006224 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006225 static constexpr size_t kFieldLoadKindSize =
6226 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
6227 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006228 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006229 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006230
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006231 void SetLoadKindInternal(LoadKind load_kind);
6232
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006233 // The special input is the HCurrentMethod for kRuntimeCall.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006234 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00006235 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01006236 HUserRecord<HInstruction*> special_input_;
6237
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006238 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006239 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006240
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006241 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006242};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006243std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
6244
6245// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006246inline void HLoadString::SetLoadKind(LoadKind load_kind) {
6247 // The load kind should be determined before inserting the instruction to the graph.
6248 DCHECK(GetBlock() == nullptr);
6249 DCHECK(GetEnvironment() == nullptr);
6250 DCHECK_EQ(GetLoadKind(), LoadKind::kRuntimeCall);
6251 SetPackedField<LoadKindField>(load_kind);
6252 if (load_kind != LoadKind::kRuntimeCall) {
6253 special_input_ = HUserRecord<HInstruction*>(nullptr);
6254 }
6255 if (!NeedsEnvironment()) {
6256 SetSideEffects(SideEffects::None());
6257 }
6258}
6259
6260// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006261inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006262 // The special input is used for PC-relative loads on some architectures,
6263 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006264 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006265 GetLoadKind() == LoadKind::kBootImageAddress ||
6266 GetLoadKind() == LoadKind::kBootImageInternTable ||
6267 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01006268 // HLoadString::GetInputRecords() returns an empty array at this point,
6269 // so use the GetInputRecords() from the base class to set the input record.
6270 DCHECK(special_input_.GetInstruction() == nullptr);
6271 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006272 special_input->AddUseAt(this, 0);
6273}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006274
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006275/**
6276 * Performs an initialization check on its Class object input.
6277 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01006278class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006279 public:
Roland Levillain3887c462015-08-12 18:15:42 +01006280 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07006281 : HExpression(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006282 DataType::Type::kReference,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006283 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
6284 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006285 SetRawInputAt(0, constant);
6286 }
6287
Artem Serovcced8ba2017-07-19 18:18:09 +01006288 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006289 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006290 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006291 return true;
6292 }
6293
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006294 bool NeedsEnvironment() const OVERRIDE {
6295 // May call runtime to initialize the class.
6296 return true;
6297 }
6298
Nicolas Geoffray729645a2015-11-19 13:29:02 +00006299 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006300
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006301 HLoadClass* GetLoadClass() const {
6302 DCHECK(InputAt(0)->IsLoadClass());
6303 return InputAt(0)->AsLoadClass();
6304 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006305
6306 DECLARE_INSTRUCTION(ClinitCheck);
6307
Artem Serovcced8ba2017-07-19 18:18:09 +01006308
6309 protected:
6310 DEFAULT_COPY_CONSTRUCTOR(ClinitCheck);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006311};
6312
Vladimir Markofcb503c2016-05-18 12:48:17 +01006313class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006314 public:
6315 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006316 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006317 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006318 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006319 bool is_volatile,
6320 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006321 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006322 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006323 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01006324 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006325 field_info_(field,
6326 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006327 field_type,
6328 is_volatile,
6329 field_idx,
6330 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006331 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006332 SetRawInputAt(0, cls);
6333 }
6334
Calin Juravle52c48962014-12-16 17:02:57 +00006335
Artem Serovcced8ba2017-07-19 18:18:09 +01006336 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle10c9cbe2014-12-19 10:50:19 +00006337 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006338
Vladimir Marko372f10e2016-05-17 16:30:10 +01006339 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
6340 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006341 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006342 }
6343
6344 size_t ComputeHashCode() const OVERRIDE {
6345 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6346 }
6347
Calin Juravle52c48962014-12-16 17:02:57 +00006348 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006349 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006350 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006351 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006352
Vladimir Marko61b92282017-10-11 13:23:17 +01006353 void SetType(DataType::Type new_type) {
6354 DCHECK(DataType::IsIntegralType(GetType()));
6355 DCHECK(DataType::IsIntegralType(new_type));
6356 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6357 SetPackedField<TypeField>(new_type);
6358 }
6359
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006360 DECLARE_INSTRUCTION(StaticFieldGet);
6361
Artem Serovcced8ba2017-07-19 18:18:09 +01006362 protected:
6363 DEFAULT_COPY_CONSTRUCTOR(StaticFieldGet);
6364
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006365 private:
6366 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006367};
6368
Vladimir Markofcb503c2016-05-18 12:48:17 +01006369class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006370 public:
6371 HStaticFieldSet(HInstruction* cls,
6372 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006373 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006374 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006375 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006376 bool is_volatile,
6377 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006378 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006379 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006380 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01006381 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006382 field_info_(field,
6383 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006384 field_type,
6385 is_volatile,
6386 field_idx,
6387 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006388 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006389 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006390 SetRawInputAt(0, cls);
6391 SetRawInputAt(1, value);
6392 }
6393
Artem Serovcced8ba2017-07-19 18:18:09 +01006394 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle52c48962014-12-16 17:02:57 +00006395 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006396 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006397 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006398 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006399
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006400 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006401 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6402 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006403
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006404 DECLARE_INSTRUCTION(StaticFieldSet);
6405
Artem Serovcced8ba2017-07-19 18:18:09 +01006406 protected:
6407 DEFAULT_COPY_CONSTRUCTOR(StaticFieldSet);
6408
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006409 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006410 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6411 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6412 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6413 "Too many packed fields.");
6414
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006415 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006416};
6417
Vladimir Markofcb503c2016-05-18 12:48:17 +01006418class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006419 public:
6420 HUnresolvedInstanceFieldGet(HInstruction* obj,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006421 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006422 uint32_t field_index,
6423 uint32_t dex_pc)
6424 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6425 field_index_(field_index) {
6426 SetRawInputAt(0, obj);
6427 }
6428
Artem Serovcced8ba2017-07-19 18:18:09 +01006429 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006430 bool NeedsEnvironment() const OVERRIDE { return true; }
6431 bool CanThrow() const OVERRIDE { return true; }
6432
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006433 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006434 uint32_t GetFieldIndex() const { return field_index_; }
6435
6436 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6437
Artem Serovcced8ba2017-07-19 18:18:09 +01006438 protected:
6439 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldGet);
6440
Calin Juravlee460d1d2015-09-29 04:52:17 +01006441 private:
6442 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006443};
6444
Vladimir Markofcb503c2016-05-18 12:48:17 +01006445class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006446 public:
6447 HUnresolvedInstanceFieldSet(HInstruction* obj,
6448 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006449 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006450 uint32_t field_index,
6451 uint32_t dex_pc)
6452 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006453 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006454 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006455 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006456 SetRawInputAt(0, obj);
6457 SetRawInputAt(1, value);
6458 }
6459
Artem Serovcced8ba2017-07-19 18:18:09 +01006460 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006461 bool NeedsEnvironment() const OVERRIDE { return true; }
6462 bool CanThrow() const OVERRIDE { return true; }
6463
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006464 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006465 uint32_t GetFieldIndex() const { return field_index_; }
6466
6467 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6468
Artem Serovcced8ba2017-07-19 18:18:09 +01006469 protected:
6470 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldSet);
6471
Calin Juravlee460d1d2015-09-29 04:52:17 +01006472 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006473 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6474 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006475 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006476 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6477 kFieldFieldType + kFieldFieldTypeSize;
6478 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6479 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006480 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006481
Calin Juravlee460d1d2015-09-29 04:52:17 +01006482 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006483};
6484
Vladimir Markofcb503c2016-05-18 12:48:17 +01006485class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006486 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006487 HUnresolvedStaticFieldGet(DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006488 uint32_t field_index,
6489 uint32_t dex_pc)
6490 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6491 field_index_(field_index) {
6492 }
6493
Artem Serovcced8ba2017-07-19 18:18:09 +01006494 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006495 bool NeedsEnvironment() const OVERRIDE { return true; }
6496 bool CanThrow() const OVERRIDE { return true; }
6497
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006498 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006499 uint32_t GetFieldIndex() const { return field_index_; }
6500
6501 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6502
Artem Serovcced8ba2017-07-19 18:18:09 +01006503 protected:
6504 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldGet);
6505
Calin Juravlee460d1d2015-09-29 04:52:17 +01006506 private:
6507 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006508};
6509
Vladimir Markofcb503c2016-05-18 12:48:17 +01006510class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006511 public:
6512 HUnresolvedStaticFieldSet(HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006513 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006514 uint32_t field_index,
6515 uint32_t dex_pc)
6516 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006517 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006518 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006519 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006520 SetRawInputAt(0, value);
6521 }
6522
Artem Serovcced8ba2017-07-19 18:18:09 +01006523 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006524 bool NeedsEnvironment() const OVERRIDE { return true; }
6525 bool CanThrow() const OVERRIDE { return true; }
6526
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006527 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006528 uint32_t GetFieldIndex() const { return field_index_; }
6529
6530 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6531
Artem Serovcced8ba2017-07-19 18:18:09 +01006532 protected:
6533 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldSet);
6534
Calin Juravlee460d1d2015-09-29 04:52:17 +01006535 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006536 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6537 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006538 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006539 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6540 kFieldFieldType + kFieldFieldTypeSize;
6541 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6542 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006543 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006544
Calin Juravlee460d1d2015-09-29 04:52:17 +01006545 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006546};
6547
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006548// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006549class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006550 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006551 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006552 : HExpression(DataType::Type::kReference, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006553
David Brazdilbbd733e2015-08-18 17:48:17 +01006554 bool CanBeNull() const OVERRIDE { return false; }
6555
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006556 DECLARE_INSTRUCTION(LoadException);
6557
Artem Serovcced8ba2017-07-19 18:18:09 +01006558 protected:
6559 DEFAULT_COPY_CONSTRUCTOR(LoadException);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006560};
6561
David Brazdilcb1c0552015-08-04 16:22:25 +01006562// Implicit part of move-exception which clears thread-local exception storage.
6563// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006564class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006565 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006566 explicit HClearException(uint32_t dex_pc = kNoDexPc)
6567 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01006568
6569 DECLARE_INSTRUCTION(ClearException);
6570
Artem Serovcced8ba2017-07-19 18:18:09 +01006571 protected:
6572 DEFAULT_COPY_CONSTRUCTOR(ClearException);
David Brazdilcb1c0552015-08-04 16:22:25 +01006573};
6574
Vladimir Markofcb503c2016-05-18 12:48:17 +01006575class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006576 public:
6577 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006578 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006579 SetRawInputAt(0, exception);
6580 }
6581
6582 bool IsControlFlow() const OVERRIDE { return true; }
6583
6584 bool NeedsEnvironment() const OVERRIDE { return true; }
6585
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006586 bool CanThrow() const OVERRIDE { return true; }
6587
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006588 DECLARE_INSTRUCTION(Throw);
6589
Artem Serovcced8ba2017-07-19 18:18:09 +01006590 protected:
6591 DEFAULT_COPY_CONSTRUCTOR(Throw);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006592};
6593
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006594/**
6595 * Implementation strategies for the code generator of a HInstanceOf
6596 * or `HCheckCast`.
6597 */
6598enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006599 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006600 kExactCheck, // Can do a single class compare.
6601 kClassHierarchyCheck, // Can just walk the super class chain.
6602 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6603 kInterfaceCheck, // No optimization yet when checking against an interface.
6604 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006605 kArrayCheck, // No optimization yet when checking against a generic array.
6606 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006607};
6608
Roland Levillain86503782016-02-11 19:07:30 +00006609std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6610
Vladimir Markofcb503c2016-05-18 12:48:17 +01006611class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006612 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006613 HInstanceOf(HInstruction* object,
6614 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006615 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006616 uint32_t dex_pc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006617 : HExpression(DataType::Type::kBool,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006618 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006619 dex_pc) {
6620 SetPackedField<TypeCheckKindField>(check_kind);
6621 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006622 SetRawInputAt(0, object);
6623 SetRawInputAt(1, constant);
6624 }
6625
Artem Serovcced8ba2017-07-19 18:18:09 +01006626 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006627 bool CanBeMoved() const OVERRIDE { return true; }
6628
Vladimir Marko372f10e2016-05-17 16:30:10 +01006629 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006630 return true;
6631 }
6632
6633 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006634 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006635 }
6636
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006637 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006638 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6639 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6640 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6641 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006642
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006643 static bool CanCallRuntime(TypeCheckKind check_kind) {
6644 // Mips currently does runtime calls for any other checks.
6645 return check_kind != TypeCheckKind::kExactCheck;
6646 }
6647
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006648 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006649 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006650 }
6651
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006652 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006653
Artem Serovcced8ba2017-07-19 18:18:09 +01006654 protected:
6655 DEFAULT_COPY_CONSTRUCTOR(InstanceOf);
6656
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006657 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006658 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6659 static constexpr size_t kFieldTypeCheckKindSize =
6660 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6661 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6662 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6663 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6664 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006665};
6666
Vladimir Markofcb503c2016-05-18 12:48:17 +01006667class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006668 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07006669 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006670 : HExpression(DataType::Type::kReference, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006671 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6672 SetPackedFlag<kFlagUpperCanBeNull>(true);
6673 SetPackedFlag<kFlagCanBeNull>(true);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006674 DCHECK_EQ(input->GetType(), DataType::Type::kReference);
Calin Juravleb1498f62015-02-16 13:13:29 +00006675 SetRawInputAt(0, input);
6676 }
6677
Artem Serovcced8ba2017-07-19 18:18:09 +01006678 bool IsClonable() const OVERRIDE { return true; }
6679
David Brazdilf5552582015-12-27 13:36:12 +00006680 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006681 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006682 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006683 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006684
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006685 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006686 DCHECK(GetUpperCanBeNull() || !can_be_null);
6687 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006688 }
6689
Vladimir Markoa1de9182016-02-25 11:37:38 +00006690 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006691
Calin Juravleb1498f62015-02-16 13:13:29 +00006692 DECLARE_INSTRUCTION(BoundType);
6693
Artem Serovcced8ba2017-07-19 18:18:09 +01006694 protected:
6695 DEFAULT_COPY_CONSTRUCTOR(BoundType);
6696
Calin Juravleb1498f62015-02-16 13:13:29 +00006697 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006698 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6699 // is false then CanBeNull() cannot be true).
6700 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6701 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6702 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6703 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6704
Calin Juravleb1498f62015-02-16 13:13:29 +00006705 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006706 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6707 // It is used to bound the type in cases like:
6708 // if (x instanceof ClassX) {
6709 // // uper_bound_ will be ClassX
6710 // }
David Brazdilf5552582015-12-27 13:36:12 +00006711 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006712};
6713
Vladimir Markofcb503c2016-05-18 12:48:17 +01006714class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006715 public:
6716 HCheckCast(HInstruction* object,
6717 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006718 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006719 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006720 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6721 SetPackedField<TypeCheckKindField>(check_kind);
6722 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006723 SetRawInputAt(0, object);
6724 SetRawInputAt(1, constant);
6725 }
6726
Artem Serovcced8ba2017-07-19 18:18:09 +01006727 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006728 bool CanBeMoved() const OVERRIDE { return true; }
6729
Vladimir Marko372f10e2016-05-17 16:30:10 +01006730 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006731 return true;
6732 }
6733
6734 bool NeedsEnvironment() const OVERRIDE {
6735 // Instruction may throw a CheckCastError.
6736 return true;
6737 }
6738
6739 bool CanThrow() const OVERRIDE { return true; }
6740
Vladimir Markoa1de9182016-02-25 11:37:38 +00006741 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6742 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6743 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6744 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006745
6746 DECLARE_INSTRUCTION(CheckCast);
6747
Artem Serovcced8ba2017-07-19 18:18:09 +01006748 protected:
6749 DEFAULT_COPY_CONSTRUCTOR(CheckCast);
6750
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006751 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006752 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6753 static constexpr size_t kFieldTypeCheckKindSize =
6754 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6755 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6756 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6757 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6758 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006759};
6760
Andreas Gampe26de38b2016-07-27 17:53:11 -07006761/**
6762 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
6763 * @details We define the combined barrier types that are actually required
6764 * by the Java Memory Model, rather than using exactly the terminology from
6765 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
6766 * primitives. Note that the JSR-133 cookbook generally does not deal with
6767 * store atomicity issues, and the recipes there are not always entirely sufficient.
6768 * The current recipe is as follows:
6769 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
6770 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
6771 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
6772 * -# Use StoreStore barrier after all stores but before return from any constructor whose
6773 * class has final fields.
6774 * -# Use NTStoreStore to order non-temporal stores with respect to all later
6775 * store-to-memory instructions. Only generated together with non-temporal stores.
6776 */
6777enum MemBarrierKind {
6778 kAnyStore,
6779 kLoadAny,
6780 kStoreStore,
6781 kAnyAny,
6782 kNTStoreStore,
6783 kLastBarrierKind = kNTStoreStore
6784};
6785std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
6786
Vladimir Markofcb503c2016-05-18 12:48:17 +01006787class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006788 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006789 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006790 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006791 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6792 SetPackedField<BarrierKindField>(barrier_kind);
6793 }
Calin Juravle27df7582015-04-17 19:12:31 +01006794
Artem Serovcced8ba2017-07-19 18:18:09 +01006795 bool IsClonable() const OVERRIDE { return true; }
6796
Vladimir Markoa1de9182016-02-25 11:37:38 +00006797 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006798
6799 DECLARE_INSTRUCTION(MemoryBarrier);
6800
Artem Serovcced8ba2017-07-19 18:18:09 +01006801 protected:
6802 DEFAULT_COPY_CONSTRUCTOR(MemoryBarrier);
6803
Calin Juravle27df7582015-04-17 19:12:31 +01006804 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006805 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6806 static constexpr size_t kFieldBarrierKindSize =
6807 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6808 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6809 kFieldBarrierKind + kFieldBarrierKindSize;
6810 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6811 "Too many packed fields.");
6812 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006813};
6814
Igor Murashkind01745e2017-04-05 16:40:31 -07006815// A constructor fence orders all prior stores to fields that could be accessed via a final field of
6816// the specified object(s), with respect to any subsequent store that might "publish"
6817// (i.e. make visible) the specified object to another thread.
6818//
6819// JLS 17.5.1 "Semantics of final fields" states that a freeze action happens
6820// for all final fields (that were set) at the end of the invoked constructor.
6821//
6822// The constructor fence models the freeze actions for the final fields of an object
6823// being constructed (semantically at the end of the constructor). Constructor fences
6824// have a per-object affinity; two separate objects being constructed get two separate
6825// constructor fences.
6826//
6827// (Note: that if calling a super-constructor or forwarding to another constructor,
6828// the freezes would happen at the end of *that* constructor being invoked).
6829//
6830// The memory model guarantees that when the object being constructed is "published" after
6831// constructor completion (i.e. escapes the current thread via a store), then any final field
6832// writes must be observable on other threads (once they observe that publication).
6833//
6834// Further, anything written before the freeze, and read by dereferencing through the final field,
6835// must also be visible (so final object field could itself have an object with non-final fields;
6836// yet the freeze must also extend to them).
6837//
6838// Constructor example:
6839//
6840// class HasFinal {
6841// final int field; Optimizing IR for <init>()V:
6842// HasFinal() {
6843// field = 123; HInstanceFieldSet(this, HasFinal.field, 123)
6844// // freeze(this.field); HConstructorFence(this)
6845// } HReturn
6846// }
6847//
6848// HConstructorFence can serve double duty as a fence for new-instance/new-array allocations of
6849// already-initialized classes; in that case the allocation must act as a "default-initializer"
6850// of the object which effectively writes the class pointer "final field".
6851//
6852// For example, we can model default-initialiation as roughly the equivalent of the following:
6853//
6854// class Object {
6855// private final Class header;
6856// }
6857//
6858// Java code: Optimizing IR:
6859//
6860// T new_instance<T>() {
6861// Object obj = allocate_memory(T.class.size); obj = HInvoke(art_quick_alloc_object, T)
6862// obj.header = T.class; // header write is done by above call.
6863// // freeze(obj.header) HConstructorFence(obj)
6864// return (T)obj;
6865// }
6866//
6867// See also:
6868// * CompilerDriver::RequiresConstructorBarrier
6869// * QuasiAtomic::ThreadFenceForConstructor
6870//
6871class HConstructorFence FINAL : public HVariableInputSizeInstruction {
6872 // A fence has variable inputs because the inputs can be removed
6873 // after prepare_for_register_allocation phase.
6874 // (TODO: In the future a fence could freeze multiple objects
6875 // after merging two fences together.)
6876 public:
6877 // `fence_object` is the reference that needs to be protected for correct publication.
6878 //
6879 // It makes sense in the following situations:
6880 // * <init> constructors, it's the "this" parameter (i.e. HParameterValue, s.t. IsThis() == true).
6881 // * new-instance-like instructions, it's the return value (i.e. HNewInstance).
6882 //
6883 // After construction the `fence_object` becomes the 0th input.
6884 // This is not an input in a real sense, but just a convenient place to stash the information
6885 // about the associated object.
6886 HConstructorFence(HInstruction* fence_object,
6887 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01006888 ArenaAllocator* allocator)
Igor Murashkind01745e2017-04-05 16:40:31 -07006889 // We strongly suspect there is not a more accurate way to describe the fine-grained reordering
6890 // constraints described in the class header. We claim that these SideEffects constraints
6891 // enforce a superset of the real constraints.
6892 //
6893 // The ordering described above is conservatively modeled with SideEffects as follows:
6894 //
6895 // * To prevent reordering of the publication stores:
6896 // ----> "Reads of objects" is the initial SideEffect.
6897 // * For every primitive final field store in the constructor:
6898 // ----> Union that field's type as a read (e.g. "Read of T") into the SideEffect.
6899 // * If there are any stores to reference final fields in the constructor:
6900 // ----> Use a more conservative "AllReads" SideEffect because any stores to any references
6901 // that are reachable from `fence_object` also need to be prevented for reordering
6902 // (and we do not want to do alias analysis to figure out what those stores are).
6903 //
6904 // In the implementation, this initially starts out as an "all reads" side effect; this is an
6905 // even more conservative approach than the one described above, and prevents all of the
6906 // above reordering without analyzing any of the instructions in the constructor.
6907 //
6908 // If in a later phase we discover that there are no writes to reference final fields,
6909 // we can refine the side effect to a smaller set of type reads (see above constraints).
6910 : HVariableInputSizeInstruction(SideEffects::AllReads(),
6911 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01006912 allocator,
Igor Murashkind01745e2017-04-05 16:40:31 -07006913 /* number_of_inputs */ 1,
6914 kArenaAllocConstructorFenceInputs) {
6915 DCHECK(fence_object != nullptr);
6916 SetRawInputAt(0, fence_object);
6917 }
6918
6919 // The object associated with this constructor fence.
6920 //
6921 // (Note: This will be null after the prepare_for_register_allocation phase,
6922 // as all constructor fence inputs are removed there).
6923 HInstruction* GetFenceObject() const {
6924 return InputAt(0);
6925 }
6926
6927 // Find all the HConstructorFence uses (`fence_use`) for `this` and:
6928 // - Delete `fence_use` from `this`'s use list.
6929 // - Delete `this` from `fence_use`'s inputs list.
6930 // - If the `fence_use` is dead, remove it from the graph.
6931 //
6932 // A fence is considered dead once it no longer has any uses
6933 // and all of the inputs are dead.
6934 //
6935 // This must *not* be called during/after prepare_for_register_allocation,
6936 // because that removes all the inputs to the fences but the fence is actually
6937 // still considered live.
Igor Murashkin6ef45672017-08-08 13:59:55 -07006938 //
6939 // Returns how many HConstructorFence instructions were removed from graph.
6940 static size_t RemoveConstructorFences(HInstruction* instruction);
Igor Murashkind01745e2017-04-05 16:40:31 -07006941
Igor Murashkindd018df2017-08-09 10:38:31 -07006942 // Combine all inputs of `this` and `other` instruction and remove
6943 // `other` from the graph.
6944 //
6945 // Inputs are unique after the merge.
6946 //
6947 // Requirement: `this` must not be the same as `other.
6948 void Merge(HConstructorFence* other);
6949
Igor Murashkin79d8fa72017-04-18 09:37:23 -07006950 // Check if this constructor fence is protecting
6951 // an HNewInstance or HNewArray that is also the immediate
6952 // predecessor of `this`.
6953 //
Igor Murashkindd018df2017-08-09 10:38:31 -07006954 // If `ignore_inputs` is true, then the immediate predecessor doesn't need
6955 // to be one of the inputs of `this`.
6956 //
Igor Murashkin79d8fa72017-04-18 09:37:23 -07006957 // Returns the associated HNewArray or HNewInstance,
6958 // or null otherwise.
Igor Murashkindd018df2017-08-09 10:38:31 -07006959 HInstruction* GetAssociatedAllocation(bool ignore_inputs = false);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07006960
Igor Murashkind01745e2017-04-05 16:40:31 -07006961 DECLARE_INSTRUCTION(ConstructorFence);
6962
Artem Serovcced8ba2017-07-19 18:18:09 +01006963 protected:
6964 DEFAULT_COPY_CONSTRUCTOR(ConstructorFence);
Igor Murashkind01745e2017-04-05 16:40:31 -07006965};
6966
Vladimir Markofcb503c2016-05-18 12:48:17 +01006967class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006968 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006969 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006970 kEnter,
6971 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006972 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006973 };
6974
6975 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006976 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006977 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6978 dex_pc) {
6979 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006980 SetRawInputAt(0, object);
6981 }
6982
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006983 // Instruction may go into runtime, so we need an environment.
6984 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006985
6986 bool CanThrow() const OVERRIDE {
6987 // Verifier guarantees that monitor-exit cannot throw.
6988 // This is important because it allows the HGraphBuilder to remove
6989 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6990 return IsEnter();
6991 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006992
Vladimir Markoa1de9182016-02-25 11:37:38 +00006993 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6994 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006995
6996 DECLARE_INSTRUCTION(MonitorOperation);
6997
Artem Serovcced8ba2017-07-19 18:18:09 +01006998 protected:
6999 DEFAULT_COPY_CONSTRUCTOR(MonitorOperation);
7000
Calin Juravle52c48962014-12-16 17:02:57 +00007001 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007002 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
7003 static constexpr size_t kFieldOperationKindSize =
7004 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
7005 static constexpr size_t kNumberOfMonitorOperationPackedBits =
7006 kFieldOperationKind + kFieldOperationKindSize;
7007 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7008 "Too many packed fields.");
7009 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007010};
7011
Vladimir Markofcb503c2016-05-18 12:48:17 +01007012class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00007013 public:
7014 HSelect(HInstruction* condition,
7015 HInstruction* true_value,
7016 HInstruction* false_value,
7017 uint32_t dex_pc)
7018 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
7019 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
7020
7021 // First input must be `true_value` or `false_value` to allow codegens to
7022 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
7023 // that architectures which implement HSelect as a conditional move also
7024 // will not need to invert the condition.
7025 SetRawInputAt(0, false_value);
7026 SetRawInputAt(1, true_value);
7027 SetRawInputAt(2, condition);
7028 }
7029
Artem Serovcced8ba2017-07-19 18:18:09 +01007030 bool IsClonable() const OVERRIDE { return true; }
David Brazdil74eb1b22015-12-14 11:44:01 +00007031 HInstruction* GetFalseValue() const { return InputAt(0); }
7032 HInstruction* GetTrueValue() const { return InputAt(1); }
7033 HInstruction* GetCondition() const { return InputAt(2); }
7034
7035 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01007036 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
7037 return true;
7038 }
David Brazdil74eb1b22015-12-14 11:44:01 +00007039
7040 bool CanBeNull() const OVERRIDE {
7041 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
7042 }
7043
7044 DECLARE_INSTRUCTION(Select);
7045
Artem Serovcced8ba2017-07-19 18:18:09 +01007046 protected:
7047 DEFAULT_COPY_CONSTRUCTOR(Select);
David Brazdil74eb1b22015-12-14 11:44:01 +00007048};
7049
Vladimir Markof9f64412015-09-02 14:05:49 +01007050class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007051 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01007052 MoveOperands(Location source,
7053 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007054 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007055 HInstruction* instruction)
7056 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007057
7058 Location GetSource() const { return source_; }
7059 Location GetDestination() const { return destination_; }
7060
7061 void SetSource(Location value) { source_ = value; }
7062 void SetDestination(Location value) { destination_ = value; }
7063
7064 // The parallel move resolver marks moves as "in-progress" by clearing the
7065 // destination (but not the source).
7066 Location MarkPending() {
7067 DCHECK(!IsPending());
7068 Location dest = destination_;
7069 destination_ = Location::NoLocation();
7070 return dest;
7071 }
7072
7073 void ClearPending(Location dest) {
7074 DCHECK(IsPending());
7075 destination_ = dest;
7076 }
7077
7078 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00007079 DCHECK(source_.IsValid() || destination_.IsInvalid());
7080 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007081 }
7082
7083 // True if this blocks a move from the given location.
7084 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08007085 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007086 }
7087
7088 // A move is redundant if it's been eliminated, if its source and
7089 // destination are the same, or if its destination is unneeded.
7090 bool IsRedundant() const {
7091 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
7092 }
7093
7094 // We clear both operands to indicate move that's been eliminated.
7095 void Eliminate() {
7096 source_ = destination_ = Location::NoLocation();
7097 }
7098
7099 bool IsEliminated() const {
7100 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
7101 return source_.IsInvalid();
7102 }
7103
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007104 DataType::Type GetType() const { return type_; }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007105
Nicolas Geoffray90218252015-04-15 11:56:51 +01007106 bool Is64BitMove() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007107 return DataType::Is64BitType(type_);
Nicolas Geoffray90218252015-04-15 11:56:51 +01007108 }
7109
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007110 HInstruction* GetInstruction() const { return instruction_; }
7111
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007112 private:
7113 Location source_;
7114 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01007115 // The type this move is for.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007116 DataType::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007117 // The instruction this move is assocatied with. Null when this move is
7118 // for moving an input in the expected locations of user (including a phi user).
7119 // This is only used in debug mode, to ensure we do not connect interval siblings
7120 // in the same parallel move.
7121 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007122};
7123
Roland Levillainc9285912015-12-18 10:38:42 +00007124std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
7125
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007126static constexpr size_t kDefaultNumberOfMoves = 4;
7127
Vladimir Markofcb503c2016-05-18 12:48:17 +01007128class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007129 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007130 explicit HParallelMove(ArenaAllocator* allocator, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01007131 : HTemplateInstruction(SideEffects::None(), dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007132 moves_(allocator->Adapter(kArenaAllocMoveOperands)) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007133 moves_.reserve(kDefaultNumberOfMoves);
7134 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007135
Nicolas Geoffray90218252015-04-15 11:56:51 +01007136 void AddMove(Location source,
7137 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007138 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007139 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00007140 DCHECK(source.IsValid());
7141 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007142 if (kIsDebugBuild) {
7143 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007144 for (const MoveOperands& move : moves_) {
7145 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007146 // Special case the situation where the move is for the spill slot
7147 // of the instruction.
7148 if ((GetPrevious() == instruction)
7149 || ((GetPrevious() == nullptr)
7150 && instruction->IsPhi()
7151 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007152 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007153 << "Doing parallel moves for the same instruction.";
7154 } else {
7155 DCHECK(false) << "Doing parallel moves for the same instruction.";
7156 }
7157 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00007158 }
7159 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007160 for (const MoveOperands& move : moves_) {
7161 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007162 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01007163 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007164 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007165 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007166 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007167 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007168 }
7169
Vladimir Marko225b6462015-09-28 12:17:40 +01007170 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007171 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007172 }
7173
Vladimir Marko225b6462015-09-28 12:17:40 +01007174 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007175
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01007176 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007177
Artem Serovcced8ba2017-07-19 18:18:09 +01007178 protected:
7179 DEFAULT_COPY_CONSTRUCTOR(ParallelMove);
7180
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007181 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01007182 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007183};
7184
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007185// This instruction computes an intermediate address pointing in the 'middle' of an object. The
7186// result pointer cannot be handled by GC, so extra care is taken to make sure that this value is
7187// never used across anything that can trigger GC.
7188// The result of this instruction is not a pointer in the sense of `DataType::Type::kreference`.
7189// So we represent it by the type `DataType::Type::kInt`.
7190class HIntermediateAddress FINAL : public HExpression<2> {
7191 public:
7192 HIntermediateAddress(HInstruction* base_address, HInstruction* offset, uint32_t dex_pc)
7193 : HExpression(DataType::Type::kInt32, SideEffects::DependsOnGC(), dex_pc) {
7194 DCHECK_EQ(DataType::Size(DataType::Type::kInt32),
7195 DataType::Size(DataType::Type::kReference))
7196 << "kPrimInt and kPrimNot have different sizes.";
7197 SetRawInputAt(0, base_address);
7198 SetRawInputAt(1, offset);
7199 }
7200
Artem Serovcced8ba2017-07-19 18:18:09 +01007201 bool IsClonable() const OVERRIDE { return true; }
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007202 bool CanBeMoved() const OVERRIDE { return true; }
7203 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
7204 return true;
7205 }
7206 bool IsActualObject() const OVERRIDE { return false; }
7207
7208 HInstruction* GetBaseAddress() const { return InputAt(0); }
7209 HInstruction* GetOffset() const { return InputAt(1); }
7210
7211 DECLARE_INSTRUCTION(IntermediateAddress);
7212
Artem Serovcced8ba2017-07-19 18:18:09 +01007213 protected:
7214 DEFAULT_COPY_CONSTRUCTOR(IntermediateAddress);
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007215};
7216
7217
Mark Mendell0616ae02015-04-17 12:49:27 -04007218} // namespace art
7219
Aart Bikf8f5a162017-02-06 15:35:29 -08007220#include "nodes_vector.h"
7221
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007222#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
7223#include "nodes_shared.h"
7224#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007225#ifdef ART_ENABLE_CODEGEN_mips
7226#include "nodes_mips.h"
7227#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04007228#ifdef ART_ENABLE_CODEGEN_x86
7229#include "nodes_x86.h"
7230#endif
7231
7232namespace art {
7233
Igor Murashkin6ef45672017-08-08 13:59:55 -07007234class OptimizingCompilerStats;
7235
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007236class HGraphVisitor : public ValueObject {
7237 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007238 explicit HGraphVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7239 : stats_(stats),
7240 graph_(graph) {}
Dave Allison20dfc792014-06-16 20:44:29 -07007241 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007242
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007243 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007244 virtual void VisitBasicBlock(HBasicBlock* block);
7245
Roland Levillain633021e2014-10-01 14:12:25 +01007246 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007247 void VisitInsertionOrder();
7248
Roland Levillain633021e2014-10-01 14:12:25 +01007249 // Visit the graph following dominator tree reverse post-order.
7250 void VisitReversePostOrder();
7251
Nicolas Geoffray787c3072014-03-17 10:20:19 +00007252 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007253
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007254 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007255#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007256 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
7257
7258 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7259
7260#undef DECLARE_VISIT_INSTRUCTION
7261
Igor Murashkin6ef45672017-08-08 13:59:55 -07007262 protected:
7263 OptimizingCompilerStats* stats_;
7264
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007265 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07007266 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007267
7268 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
7269};
7270
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007271class HGraphDelegateVisitor : public HGraphVisitor {
7272 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007273 explicit HGraphDelegateVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7274 : HGraphVisitor(graph, stats) {}
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007275 virtual ~HGraphDelegateVisitor() {}
7276
7277 // Visit functions that delegate to to super class.
7278#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00007279 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007280
7281 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7282
7283#undef DECLARE_VISIT_INSTRUCTION
7284
7285 private:
7286 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
7287};
7288
Artem Serovcced8ba2017-07-19 18:18:09 +01007289// Create a clone of the instruction, insert it into the graph; replace the old one with a new
7290// and remove the old instruction.
7291HInstruction* ReplaceInstrOrPhiByClone(HInstruction* instr);
7292
7293// Create a clone for each clonable instructions/phis and replace the original with the clone.
7294//
7295// Used for testing individual instruction cloner.
7296class CloneAndReplaceInstructionVisitor : public HGraphDelegateVisitor {
7297 public:
7298 explicit CloneAndReplaceInstructionVisitor(HGraph* graph)
7299 : HGraphDelegateVisitor(graph), instr_replaced_by_clones_count(0) {}
7300
7301 void VisitInstruction(HInstruction* instruction) OVERRIDE {
7302 if (instruction->IsClonable()) {
7303 ReplaceInstrOrPhiByClone(instruction);
7304 instr_replaced_by_clones_count++;
7305 }
7306 }
7307
7308 size_t GetInstrReplacedByClonesCount() const { return instr_replaced_by_clones_count; }
7309
7310 private:
7311 size_t instr_replaced_by_clones_count;
7312
7313 DISALLOW_COPY_AND_ASSIGN(CloneAndReplaceInstructionVisitor);
7314};
7315
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007316// Iterator over the blocks that art part of the loop. Includes blocks part
7317// of an inner loop. The order in which the blocks are iterated is on their
7318// block id.
7319class HBlocksInLoopIterator : public ValueObject {
7320 public:
7321 explicit HBlocksInLoopIterator(const HLoopInformation& info)
7322 : blocks_in_loop_(info.GetBlocks()),
7323 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
7324 index_(0) {
7325 if (!blocks_in_loop_.IsBitSet(index_)) {
7326 Advance();
7327 }
7328 }
7329
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007330 bool Done() const { return index_ == blocks_.size(); }
7331 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007332 void Advance() {
7333 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007334 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007335 if (blocks_in_loop_.IsBitSet(index_)) {
7336 break;
7337 }
7338 }
7339 }
7340
7341 private:
7342 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007343 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007344 size_t index_;
7345
7346 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
7347};
7348
Mingyao Yang3584bce2015-05-19 16:01:59 -07007349// Iterator over the blocks that art part of the loop. Includes blocks part
7350// of an inner loop. The order in which the blocks are iterated is reverse
7351// post order.
7352class HBlocksInLoopReversePostOrderIterator : public ValueObject {
7353 public:
7354 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
7355 : blocks_in_loop_(info.GetBlocks()),
7356 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
7357 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007358 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007359 Advance();
7360 }
7361 }
7362
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007363 bool Done() const { return index_ == blocks_.size(); }
7364 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07007365 void Advance() {
7366 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007367 for (size_t e = blocks_.size(); index_ < e; ++index_) {
7368 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007369 break;
7370 }
7371 }
7372 }
7373
7374 private:
7375 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007376 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07007377 size_t index_;
7378
7379 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
7380};
7381
Aart Bikf3e61ee2017-04-12 17:09:20 -07007382// Returns int64_t value of a properly typed constant.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007383inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00007384 if (constant->IsIntConstant()) {
7385 return constant->AsIntConstant()->GetValue();
7386 } else if (constant->IsLongConstant()) {
7387 return constant->AsLongConstant()->GetValue();
7388 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00007389 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00007390 return 0;
7391 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007392}
7393
Aart Bikf3e61ee2017-04-12 17:09:20 -07007394// Returns true iff instruction is an integral constant (and sets value on success).
7395inline bool IsInt64AndGet(HInstruction* instruction, /*out*/ int64_t* value) {
7396 if (instruction->IsIntConstant()) {
7397 *value = instruction->AsIntConstant()->GetValue();
7398 return true;
7399 } else if (instruction->IsLongConstant()) {
7400 *value = instruction->AsLongConstant()->GetValue();
7401 return true;
7402 } else if (instruction->IsNullConstant()) {
7403 *value = 0;
7404 return true;
7405 }
7406 return false;
7407}
7408
Aart Bik0148de42017-09-05 09:25:01 -07007409// Returns true iff instruction is the given integral constant.
7410inline bool IsInt64Value(HInstruction* instruction, int64_t value) {
7411 int64_t val = 0;
7412 return IsInt64AndGet(instruction, &val) && val == value;
7413}
7414
7415// Returns true iff instruction is a zero bit pattern.
7416inline bool IsZeroBitPattern(HInstruction* instruction) {
7417 return instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern();
7418}
7419
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007420#define INSTRUCTION_TYPE_CHECK(type, super) \
7421 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
7422 inline const H##type* HInstruction::As##type() const { \
7423 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
7424 } \
7425 inline H##type* HInstruction::As##type() { \
7426 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
7427 }
7428
7429 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
7430#undef INSTRUCTION_TYPE_CHECK
7431
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00007432// Create space in `blocks` for adding `number_of_new_blocks` entries
7433// starting at location `at`. Blocks after `at` are moved accordingly.
7434inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
7435 size_t number_of_new_blocks,
7436 size_t after) {
7437 DCHECK_LT(after, blocks->size());
7438 size_t old_size = blocks->size();
7439 size_t new_size = old_size + number_of_new_blocks;
7440 blocks->resize(new_size);
7441 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
7442}
7443
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00007444/*
7445 * Hunt "under the hood" of array lengths (leading to array references),
7446 * null checks (also leading to array references), and new arrays
7447 * (leading to the actual length). This makes it more likely related
7448 * instructions become actually comparable.
7449 */
7450inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
7451 while (instruction->IsArrayLength() ||
7452 instruction->IsNullCheck() ||
7453 instruction->IsNewArray()) {
7454 instruction = instruction->IsNewArray()
7455 ? instruction->AsNewArray()->GetLength()
7456 : instruction->InputAt(0);
7457 }
7458 return instruction;
7459}
7460
Artem Serov21c7e6f2017-07-27 16:04:42 +01007461void RemoveEnvironmentUses(HInstruction* instruction);
7462bool HasEnvironmentUsedByOthers(HInstruction* instruction);
7463void ResetEnvironmentInputRecords(HInstruction* instruction);
7464
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007465} // namespace art
7466
7467#endif // ART_COMPILER_OPTIMIZING_NODES_H_