blob: f91d37b3ac371fd996bf1a441c022ecec86ea9ce [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"
David Sehr9e734c72018-01-04 17:56:19 -080033#include "dex/dex_file.h"
34#include "dex/dex_file_types.h"
David Sehr8c0961f2018-01-23 16:11:38 -080035#include "dex/invoke_type.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000036#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000037#include "handle.h"
38#include "handle_scope.h"
Nicolas Geoffray762869d2016-07-15 15:28:35 +010039#include "intrinsics_enum.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
Artem Serov7f4aff62017-06-21 17:02:18 +0100829 // Updates blocks population of the loop and all of its outer' ones recursively after the
830 // population of the inner loop is updated.
831 void PopulateInnerLoopUpwards(HLoopInformation* inner_loop);
832
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100833 // Returns whether this loop information contains `block`.
834 // Note that this loop information *must* be populated before entering this function.
835 bool Contains(const HBasicBlock& block) const;
836
837 // Returns whether this loop information is an inner loop of `other`.
838 // Note that `other` *must* be populated before entering this function.
839 bool IsIn(const HLoopInformation& other) const;
840
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800841 // Returns true if instruction is not defined within this loop.
842 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700843
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100844 const ArenaBitVector& GetBlocks() const { return blocks_; }
845
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000846 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000847 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000848
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000849 void ClearAllBlocks() {
850 blocks_.ClearAllBits();
851 }
852
David Brazdil3f4a5222016-05-06 12:46:21 +0100853 bool HasBackEdgeNotDominatedByHeader() const;
854
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100855 bool IsPopulated() const {
856 return blocks_.GetHighestBitSet() != -1;
857 }
858
Anton Shaminf89381f2016-05-16 16:44:13 +0600859 bool DominatesAllBackEdges(HBasicBlock* block);
860
David Sehrc757dec2016-11-04 15:48:34 -0700861 bool HasExitEdge() const;
862
Artem Serov7f4aff62017-06-21 17:02:18 +0100863 // Resets back edge and blocks-in-loop data.
864 void ResetBasicBlockData() {
865 back_edges_.clear();
866 ClearAllBlocks();
867 }
868
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000869 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100870 // Internal recursive implementation of `Populate`.
871 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100872 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100873
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000874 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100875 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000876 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100877 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100878 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100879 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000880
881 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
882};
883
David Brazdilec16f792015-08-19 15:04:01 +0100884// Stores try/catch information for basic blocks.
885// Note that HGraph is constructed so that catch blocks cannot simultaneously
886// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100887class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100888 public:
889 // Try block information constructor.
890 explicit TryCatchInformation(const HTryBoundary& try_entry)
891 : try_entry_(&try_entry),
892 catch_dex_file_(nullptr),
893 catch_type_index_(DexFile::kDexNoIndex16) {
894 DCHECK(try_entry_ != nullptr);
895 }
896
897 // Catch block information constructor.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800898 TryCatchInformation(dex::TypeIndex catch_type_index, const DexFile& dex_file)
David Brazdilec16f792015-08-19 15:04:01 +0100899 : try_entry_(nullptr),
900 catch_dex_file_(&dex_file),
901 catch_type_index_(catch_type_index) {}
902
903 bool IsTryBlock() const { return try_entry_ != nullptr; }
904
905 const HTryBoundary& GetTryEntry() const {
906 DCHECK(IsTryBlock());
907 return *try_entry_;
908 }
909
910 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
911
912 bool IsCatchAllTypeIndex() const {
913 DCHECK(IsCatchBlock());
Andreas Gampea5b09a62016-11-17 15:21:22 -0800914 return !catch_type_index_.IsValid();
David Brazdilec16f792015-08-19 15:04:01 +0100915 }
916
Andreas Gampea5b09a62016-11-17 15:21:22 -0800917 dex::TypeIndex GetCatchTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +0100918 DCHECK(IsCatchBlock());
919 return catch_type_index_;
920 }
921
922 const DexFile& GetCatchDexFile() const {
923 DCHECK(IsCatchBlock());
924 return *catch_dex_file_;
925 }
926
927 private:
928 // One of possibly several TryBoundary instructions entering the block's try.
929 // Only set for try blocks.
930 const HTryBoundary* try_entry_;
931
932 // Exception type information. Only set for catch blocks.
933 const DexFile* catch_dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800934 const dex::TypeIndex catch_type_index_;
David Brazdilec16f792015-08-19 15:04:01 +0100935};
936
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100937static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100938static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100939
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000940// A block in a method. Contains the list of instructions represented
941// as a double linked list. Each block knows its predecessors and
942// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100943
Vladimir Markof9f64412015-09-02 14:05:49 +0100944class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000945 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700946 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000947 : graph_(graph),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100948 predecessors_(graph->GetAllocator()->Adapter(kArenaAllocPredecessors)),
949 successors_(graph->GetAllocator()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000950 loop_information_(nullptr),
951 dominator_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100952 dominated_blocks_(graph->GetAllocator()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100953 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100954 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100955 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000956 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000957 try_catch_information_(nullptr) {
958 predecessors_.reserve(kDefaultNumberOfPredecessors);
959 successors_.reserve(kDefaultNumberOfSuccessors);
960 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
961 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000962
Vladimir Marko60584552015-09-03 13:35:12 +0000963 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100964 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000965 }
966
Vladimir Marko60584552015-09-03 13:35:12 +0000967 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100968 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000969 }
970
David Brazdild26a4112015-11-10 11:07:31 +0000971 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
972 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
973
Vladimir Marko60584552015-09-03 13:35:12 +0000974 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
975 return ContainsElement(successors_, block, start_from);
976 }
977
978 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100979 return dominated_blocks_;
980 }
981
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100982 bool IsEntryBlock() const {
983 return graph_->GetEntryBlock() == this;
984 }
985
986 bool IsExitBlock() const {
987 return graph_->GetExitBlock() == this;
988 }
989
David Brazdil46e2a392015-03-16 17:31:52 +0000990 bool IsSingleGoto() const;
Mads Ager16e52892017-07-14 13:11:37 +0200991 bool IsSingleReturn() const;
Mingyao Yang46721ef2017-10-05 14:45:17 -0700992 bool IsSingleReturnOrReturnVoidAllowingPhis() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000993 bool IsSingleTryBoundary() const;
994
995 // Returns true if this block emits nothing but a jump.
996 bool IsSingleJump() const {
997 HLoopInformation* loop_info = GetLoopInformation();
998 return (IsSingleGoto() || IsSingleTryBoundary())
999 // Back edges generate a suspend check.
1000 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
1001 }
David Brazdil46e2a392015-03-16 17:31:52 +00001002
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001003 void AddBackEdge(HBasicBlock* back_edge) {
1004 if (loop_information_ == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001005 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001006 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001007 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001008 loop_information_->AddBackEdge(back_edge);
1009 }
1010
Artem Serov7f4aff62017-06-21 17:02:18 +01001011 // Registers a back edge; if the block was not a loop header before the call associates a newly
1012 // created loop info with it.
1013 //
1014 // Used in SuperblockCloner to preserve LoopInformation object instead of reseting loop
1015 // info for all blocks during back edges recalculation.
1016 void AddBackEdgeWhileUpdating(HBasicBlock* back_edge) {
1017 if (loop_information_ == nullptr || loop_information_->GetHeader() != this) {
1018 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
1019 }
1020 loop_information_->AddBackEdge(back_edge);
1021 }
1022
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001023 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001024 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001025
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001026 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001027 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001028 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001029
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001030 HBasicBlock* GetDominator() const { return dominator_; }
1031 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +00001032 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
1033
1034 void RemoveDominatedBlock(HBasicBlock* block) {
1035 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +01001036 }
Vladimir Marko60584552015-09-03 13:35:12 +00001037
1038 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
1039 ReplaceElement(dominated_blocks_, existing, new_block);
1040 }
1041
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001042 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001043
1044 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001045 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001046 }
1047
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001048 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
1049 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +01001050 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001051 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +01001052 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
1053 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001054
Nicolas Geoffray09aa1472016-01-19 10:52:54 +00001055 HInstruction* GetFirstInstructionDisregardMoves() const;
1056
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001057 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001058 successors_.push_back(block);
1059 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001060 }
1061
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001062 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
1063 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001064 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001065 new_block->predecessors_.push_back(this);
1066 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001067 }
1068
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001069 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
1070 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001071 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001072 new_block->successors_.push_back(this);
1073 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001074 }
1075
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001076 // Insert `this` between `predecessor` and `successor. This method
1077 // preserves the indicies, and will update the first edge found between
1078 // `predecessor` and `successor`.
1079 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
1080 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001081 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +00001082 successor->predecessors_[predecessor_index] = this;
1083 predecessor->successors_[successor_index] = this;
1084 successors_.push_back(successor);
1085 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001086 }
1087
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001088 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001089 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001090 }
1091
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001092 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001093 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001094 }
1095
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001096 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001097 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001098 }
1099
1100 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001101 predecessors_.push_back(block);
1102 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001103 }
1104
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001105 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001106 DCHECK_EQ(predecessors_.size(), 2u);
1107 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001108 }
1109
David Brazdil769c9e52015-04-27 13:54:09 +01001110 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001111 DCHECK_EQ(successors_.size(), 2u);
1112 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +01001113 }
1114
David Brazdilfc6a86a2015-06-26 10:33:45 +00001115 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001116 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001117 }
1118
David Brazdilfc6a86a2015-06-26 10:33:45 +00001119 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001120 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001121 }
1122
David Brazdilfc6a86a2015-06-26 10:33:45 +00001123 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001124 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001125 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001126 }
1127
1128 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001129 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001130 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001131 }
1132
1133 // Returns whether the first occurrence of `predecessor` in the list of
1134 // predecessors is at index `idx`.
1135 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001136 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001137 return GetPredecessorIndexOf(predecessor) == idx;
1138 }
1139
David Brazdild7558da2015-09-22 13:04:14 +01001140 // Create a new block between this block and its predecessors. The new block
1141 // is added to the graph, all predecessor edges are relinked to it and an edge
1142 // is created to `this`. Returns the new empty block. Reverse post order or
1143 // loop and try/catch information are not updated.
1144 HBasicBlock* CreateImmediateDominator();
1145
David Brazdilfc6a86a2015-06-26 10:33:45 +00001146 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001147 // created, latter block. Note that this method will add the block to the
1148 // graph, create a Goto at the end of the former block and will create an edge
1149 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001150 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001151 HBasicBlock* SplitBefore(HInstruction* cursor);
1152
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001153 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001154 // created block. Note that this method just updates raw block information,
1155 // like predecessors, successors, dominators, and instruction list. It does not
1156 // update the graph, reverse post order, loop information, nor make sure the
1157 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001158 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1159
1160 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1161 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001162
1163 // Merge `other` at the end of `this`. Successors and dominated blocks of
1164 // `other` are changed to be successors and dominated blocks of `this`. Note
1165 // that this method does not update the graph, reverse post order, loop
1166 // information, nor make sure the blocks are consistent (for example ending
1167 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001168 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001169
1170 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1171 // of `this` are moved to `other`.
1172 // Note that this method does not update the graph, reverse post order, loop
1173 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001174 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001175 void ReplaceWith(HBasicBlock* other);
1176
Aart Bik6b69e0a2017-01-11 10:20:43 -08001177 // Merges the instructions of `other` at the end of `this`.
1178 void MergeInstructionsWith(HBasicBlock* other);
1179
David Brazdil2d7352b2015-04-20 14:52:42 +01001180 // Merge `other` at the end of `this`. This method updates loops, reverse post
1181 // order, links to predecessors, successors, dominators and deletes the block
1182 // from the graph. The two blocks must be successive, i.e. `this` the only
1183 // predecessor of `other` and vice versa.
1184 void MergeWith(HBasicBlock* other);
1185
1186 // Disconnects `this` from all its predecessors, successors and dominator,
1187 // removes it from all loops it is included in and eventually from the graph.
1188 // The block must not dominate any other block. Predecessors and successors
1189 // are safely updated.
1190 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001191
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001192 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001193 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001194 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001195 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Artem Serovcced8ba2017-07-19 18:18:09 +01001196 // Replace phi `initial` with `replacement` within this block.
1197 void ReplaceAndRemovePhiWith(HPhi* initial, HPhi* replacement);
Roland Levillainccc07a92014-09-16 14:48:16 +01001198 // Replace instruction `initial` with `replacement` within this block.
1199 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1200 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001201 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001202 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001203 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1204 // instruction list. With 'ensure_safety' set to true, it verifies that the
1205 // instruction is not in use and removes it from the use lists of its inputs.
1206 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1207 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001208 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001209
1210 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001211 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001212 }
1213
Roland Levillain6b879dd2014-09-22 17:13:44 +01001214 bool IsLoopPreHeaderFirstPredecessor() const {
1215 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001216 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001217 }
1218
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001219 bool IsFirstPredecessorBackEdge() const {
1220 DCHECK(IsLoopHeader());
1221 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1222 }
1223
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001224 HLoopInformation* GetLoopInformation() const {
1225 return loop_information_;
1226 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001227
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001228 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001229 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001230 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001231 void SetInLoop(HLoopInformation* info) {
1232 if (IsLoopHeader()) {
1233 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001234 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001235 loop_information_ = info;
1236 } else if (loop_information_->Contains(*info->GetHeader())) {
1237 // Block is currently part of an outer loop. Make it part of this inner loop.
1238 // Note that a non loop header having a loop information means this loop information
1239 // has already been populated
1240 loop_information_ = info;
1241 } else {
1242 // Block is part of an inner loop. Do not update the loop information.
1243 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1244 // at this point, because this method is being called while populating `info`.
1245 }
1246 }
1247
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001248 // Raw update of the loop information.
1249 void SetLoopInformation(HLoopInformation* info) {
1250 loop_information_ = info;
1251 }
1252
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001253 bool IsInLoop() const { return loop_information_ != nullptr; }
1254
David Brazdilec16f792015-08-19 15:04:01 +01001255 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1256
1257 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1258 try_catch_information_ = try_catch_information;
1259 }
1260
1261 bool IsTryBlock() const {
1262 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1263 }
1264
1265 bool IsCatchBlock() const {
1266 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1267 }
David Brazdilffee3d32015-07-06 11:48:53 +01001268
1269 // Returns the try entry that this block's successors should have. They will
1270 // be in the same try, unless the block ends in a try boundary. In that case,
1271 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001272 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001273
David Brazdild7558da2015-09-22 13:04:14 +01001274 bool HasThrowingInstructions() const;
1275
David Brazdila4b8c212015-05-07 09:59:30 +01001276 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001277 bool Dominates(HBasicBlock* block) const;
1278
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001279 size_t GetLifetimeStart() const { return lifetime_start_; }
1280 size_t GetLifetimeEnd() const { return lifetime_end_; }
1281
1282 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1283 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1284
David Brazdil8d5b8b22015-03-24 10:51:52 +00001285 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001286 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001287 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001288 bool HasSinglePhi() const;
1289
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001290 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001291 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001292 ArenaVector<HBasicBlock*> predecessors_;
1293 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001294 HInstructionList instructions_;
1295 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001296 HLoopInformation* loop_information_;
1297 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001298 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001299 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001300 // The dex program counter of the first instruction of this block.
1301 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001302 size_t lifetime_start_;
1303 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001304 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001305
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001306 friend class HGraph;
1307 friend class HInstruction;
1308
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001309 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1310};
1311
David Brazdilb2bd1c52015-03-25 11:17:37 +00001312// Iterates over the LoopInformation of all loops which contain 'block'
1313// from the innermost to the outermost.
1314class HLoopInformationOutwardIterator : public ValueObject {
1315 public:
1316 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1317 : current_(block.GetLoopInformation()) {}
1318
1319 bool Done() const { return current_ == nullptr; }
1320
1321 void Advance() {
1322 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001323 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001324 }
1325
1326 HLoopInformation* Current() const {
1327 DCHECK(!Done());
1328 return current_;
1329 }
1330
1331 private:
1332 HLoopInformation* current_;
1333
1334 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1335};
1336
Alexandre Ramesef20f712015-06-09 10:29:30 +01001337#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001338 M(Above, Condition) \
1339 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001340 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001341 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001342 M(ArrayGet, Instruction) \
1343 M(ArrayLength, Instruction) \
1344 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001345 M(Below, Condition) \
1346 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001347 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001348 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001349 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001350 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001351 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001352 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001353 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001354 M(Compare, BinaryOperation) \
Igor Murashkind01745e2017-04-05 16:40:31 -07001355 M(ConstructorFence, Instruction) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001356 M(CurrentMethod, Instruction) \
Mingyao Yang063fc772016-08-02 11:02:54 -07001357 M(ShouldDeoptimizeFlag, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001358 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001359 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001360 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001361 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001362 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001363 M(Exit, Instruction) \
1364 M(FloatConstant, Constant) \
1365 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001366 M(GreaterThan, Condition) \
1367 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001368 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001369 M(InstanceFieldGet, Instruction) \
1370 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001371 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001372 M(IntConstant, Constant) \
xueliang.zhonge0eb4832017-10-30 13:43:14 +00001373 M(IntermediateAddress, Instruction) \
Calin Juravle175dc732015-08-25 15:42:32 +01001374 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001375 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001376 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001377 M(InvokeVirtual, Invoke) \
Orion Hodsonac141392017-01-13 11:53:47 +00001378 M(InvokePolymorphic, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001379 M(LessThan, Condition) \
1380 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001381 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001382 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001383 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001384 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001385 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001386 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001387 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001388 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001389 M(Neg, UnaryOperation) \
1390 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001391 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001392 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001393 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001394 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001395 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001396 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001397 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001398 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001399 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001400 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001401 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001402 M(Return, Instruction) \
1403 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001404 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001405 M(Shl, BinaryOperation) \
1406 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001407 M(StaticFieldGet, Instruction) \
1408 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001409 M(UnresolvedInstanceFieldGet, Instruction) \
1410 M(UnresolvedInstanceFieldSet, Instruction) \
1411 M(UnresolvedStaticFieldGet, Instruction) \
1412 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001413 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001414 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001415 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001416 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001417 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001418 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001419 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001420 M(Xor, BinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001421 M(VecReplicateScalar, VecUnaryOperation) \
Aart Bik0148de42017-09-05 09:25:01 -07001422 M(VecExtractScalar, VecUnaryOperation) \
1423 M(VecReduce, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001424 M(VecCnv, VecUnaryOperation) \
1425 M(VecNeg, VecUnaryOperation) \
Aart Bik6daebeb2017-04-03 14:35:41 -07001426 M(VecAbs, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001427 M(VecNot, VecUnaryOperation) \
1428 M(VecAdd, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001429 M(VecHalvingAdd, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001430 M(VecSub, VecBinaryOperation) \
1431 M(VecMul, VecBinaryOperation) \
1432 M(VecDiv, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001433 M(VecMin, VecBinaryOperation) \
1434 M(VecMax, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001435 M(VecAnd, VecBinaryOperation) \
1436 M(VecAndNot, VecBinaryOperation) \
1437 M(VecOr, VecBinaryOperation) \
1438 M(VecXor, VecBinaryOperation) \
1439 M(VecShl, VecBinaryOperation) \
1440 M(VecShr, VecBinaryOperation) \
1441 M(VecUShr, VecBinaryOperation) \
Aart Bik8de59162017-04-21 09:42:01 -07001442 M(VecSetScalars, VecOperation) \
Artem Serovf34dd202017-04-10 17:41:46 +01001443 M(VecMultiplyAccumulate, VecOperation) \
Aart Bikdbbac8f2017-09-01 13:06:08 -07001444 M(VecSADAccumulate, VecOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001445 M(VecLoad, VecMemoryOperation) \
1446 M(VecStore, VecMemoryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001447
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001448/*
1449 * Instructions, shared across several (not all) architectures.
1450 */
1451#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1452#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1453#else
1454#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001455 M(BitwiseNegatedRight, Instruction) \
Anton Kirilov74234da2017-01-13 14:42:47 +00001456 M(DataProcWithShifterOp, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001457 M(MultiplyAccumulate, Instruction) \
Artem Serove1811ed2017-04-27 16:50:47 +01001458 M(IntermediateAddressIndex, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001459#endif
1460
Alexandre Ramesef20f712015-06-09 10:29:30 +01001461#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1462
1463#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1464
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001465#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001466#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001467#else
1468#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1469 M(MipsComputeBaseMethodAddress, Instruction) \
Lena Djokica2901602017-09-21 13:50:52 +02001470 M(MipsPackedSwitch, Instruction) \
1471 M(IntermediateArrayAddressIndex, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001472#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001473
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001474#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1475
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001476#ifndef ART_ENABLE_CODEGEN_x86
1477#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1478#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001479#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1480 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001481 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001482 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001483 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001484#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001485
1486#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1487
1488#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1489 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001490 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001491 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1492 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001493 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001494 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001495 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1496 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1497
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001498#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1499 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001500 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001501 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001502 M(BinaryOperation, Instruction) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001503 M(Invoke, Instruction) \
1504 M(VecOperation, Instruction) \
1505 M(VecUnaryOperation, VecOperation) \
1506 M(VecBinaryOperation, VecOperation) \
1507 M(VecMemoryOperation, VecOperation)
Dave Allison20dfc792014-06-16 20:44:29 -07001508
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001509#define FOR_EACH_INSTRUCTION(M) \
1510 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1511 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1512
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001513#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001514FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1515#undef FORWARD_DECLARATION
1516
Vladimir Marko372f10e2016-05-17 16:30:10 +01001517#define DECLARE_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001518 private: \
1519 H##type& operator=(const H##type&) = delete; \
1520 public: \
Vladimir Marko372f10e2016-05-17 16:30:10 +01001521 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
1522 const char* DebugName() const OVERRIDE { return #type; } \
1523 bool InstructionTypeEquals(const HInstruction* other) const OVERRIDE { \
1524 return other->Is##type(); \
1525 } \
Artem Serovcced8ba2017-07-19 18:18:09 +01001526 HInstruction* Clone(ArenaAllocator* arena) const OVERRIDE { \
1527 DCHECK(IsClonable()); \
1528 return new (arena) H##type(*this->As##type()); \
1529 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001530 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001531
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001532#define DECLARE_ABSTRACT_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001533 private: \
1534 H##type& operator=(const H##type&) = delete; \
1535 public: \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001536 bool Is##type() const { return As##type() != nullptr; } \
1537 const H##type* As##type() const { return this; } \
1538 H##type* As##type() { return this; }
1539
Artem Serovcced8ba2017-07-19 18:18:09 +01001540#define DEFAULT_COPY_CONSTRUCTOR(type) \
1541 explicit H##type(const H##type& other) = default;
1542
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001543template <typename T>
Vladimir Marko82b07402017-03-01 19:02:04 +00001544class HUseListNode : public ArenaObject<kArenaAllocUseListNode>,
1545 public IntrusiveForwardListNode<HUseListNode<T>> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001546 public:
Igor Murashkind01745e2017-04-05 16:40:31 -07001547 // Get the instruction which has this use as one of the inputs.
David Brazdiled596192015-01-23 10:39:45 +00001548 T GetUser() const { return user_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001549 // Get the position of the input record that this use corresponds to.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001550 size_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001551 // Set the position of the input record that this use corresponds to.
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001552 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001553
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001554 private:
David Brazdiled596192015-01-23 10:39:45 +00001555 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001556 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001557
1558 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001559 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001560
Vladimir Marko46817b82016-03-29 12:21:58 +01001561 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001562
1563 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1564};
1565
David Brazdiled596192015-01-23 10:39:45 +00001566template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001567using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001568
David Brazdil1abb4192015-02-17 18:33:36 +00001569// This class is used by HEnvironment and HInstruction classes to record the
1570// instructions they use and pointers to the corresponding HUseListNodes kept
1571// by the used instructions.
1572template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001573class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001574 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001575 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1576 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001577
Vladimir Marko46817b82016-03-29 12:21:58 +01001578 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1579 : HUserRecord(old_record.instruction_, before_use_node) {}
1580 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1581 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001582 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001583 }
1584
1585 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001586 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1587 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001588
1589 private:
1590 // Instruction used by the user.
1591 HInstruction* instruction_;
1592
Vladimir Marko46817b82016-03-29 12:21:58 +01001593 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1594 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001595};
1596
Vladimir Markoe9004912016-06-16 16:50:52 +01001597// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1598// This is used for HInstruction::GetInputs() to return a container wrapper providing
1599// HInstruction* values even though the underlying container has HUserRecord<>s.
1600struct HInputExtractor {
1601 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1602 return record.GetInstruction();
1603 }
1604 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1605 return record.GetInstruction();
1606 }
1607};
1608
1609using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1610using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1611
Aart Bik854a02b2015-07-14 16:07:00 -07001612/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001613 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001614 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001615 * For write/read dependences on fields/arrays, the dependence analysis uses
1616 * type disambiguation (e.g. a float field write cannot modify the value of an
1617 * integer field read) and the access type (e.g. a reference array write cannot
1618 * modify the value of a reference field read [although it may modify the
1619 * reference fetch prior to reading the field, which is represented by its own
1620 * write/read dependence]). The analysis makes conservative points-to
1621 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1622 * the same, and any reference read depends on any reference read without
1623 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001624 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001625 * The internal representation uses 38-bit and is described in the table below.
1626 * The first line indicates the side effect, and for field/array accesses the
1627 * second line indicates the type of the access (in the order of the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001628 * DataType::Type enum).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001629 * The two numbered lines below indicate the bit position in the bitfield (read
1630 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001631 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001632 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1633 * +-------------+---------+---------+--------------+---------+---------+
1634 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1635 * | 3 |333333322|222222221| 1 |111111110|000000000|
1636 * | 7 |654321098|765432109| 8 |765432109|876543210|
1637 *
1638 * Note that, to ease the implementation, 'changes' bits are least significant
1639 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001640 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001641class SideEffects : public ValueObject {
1642 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001643 SideEffects() : flags_(0) {}
1644
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001645 static SideEffects None() {
1646 return SideEffects(0);
1647 }
1648
1649 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001650 return SideEffects(kAllChangeBits | kAllDependOnBits);
1651 }
1652
1653 static SideEffects AllChanges() {
1654 return SideEffects(kAllChangeBits);
1655 }
1656
1657 static SideEffects AllDependencies() {
1658 return SideEffects(kAllDependOnBits);
1659 }
1660
1661 static SideEffects AllExceptGCDependency() {
1662 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1663 }
1664
1665 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001666 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001667 }
1668
Aart Bik34c3ba92015-07-20 14:08:59 -07001669 static SideEffects AllWrites() {
1670 return SideEffects(kAllWrites);
1671 }
1672
1673 static SideEffects AllReads() {
1674 return SideEffects(kAllReads);
1675 }
1676
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001677 static SideEffects FieldWriteOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001678 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001679 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001680 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001681 }
1682
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001683 static SideEffects ArrayWriteOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001684 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001685 }
1686
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001687 static SideEffects FieldReadOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001688 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001689 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001690 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001691 }
1692
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001693 static SideEffects ArrayReadOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001694 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001695 }
1696
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001697 static SideEffects CanTriggerGC() {
1698 return SideEffects(1ULL << kCanTriggerGCBit);
1699 }
1700
1701 static SideEffects DependsOnGC() {
1702 return SideEffects(1ULL << kDependsOnGCBit);
1703 }
1704
Aart Bik854a02b2015-07-14 16:07:00 -07001705 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001706 SideEffects Union(SideEffects other) const {
1707 return SideEffects(flags_ | other.flags_);
1708 }
1709
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001710 SideEffects Exclusion(SideEffects other) const {
1711 return SideEffects(flags_ & ~other.flags_);
1712 }
1713
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001714 void Add(SideEffects other) {
1715 flags_ |= other.flags_;
1716 }
1717
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001718 bool Includes(SideEffects other) const {
1719 return (other.flags_ & flags_) == other.flags_;
1720 }
1721
1722 bool HasSideEffects() const {
1723 return (flags_ & kAllChangeBits);
1724 }
1725
1726 bool HasDependencies() const {
1727 return (flags_ & kAllDependOnBits);
1728 }
1729
1730 // Returns true if there are no side effects or dependencies.
1731 bool DoesNothing() const {
1732 return flags_ == 0;
1733 }
1734
Aart Bik854a02b2015-07-14 16:07:00 -07001735 // Returns true if something is written.
1736 bool DoesAnyWrite() const {
1737 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001738 }
1739
Aart Bik854a02b2015-07-14 16:07:00 -07001740 // Returns true if something is read.
1741 bool DoesAnyRead() const {
1742 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001743 }
1744
Aart Bik854a02b2015-07-14 16:07:00 -07001745 // Returns true if potentially everything is written and read
1746 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001747 bool DoesAllReadWrite() const {
1748 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1749 }
1750
Aart Bik854a02b2015-07-14 16:07:00 -07001751 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001752 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001753 }
1754
Roland Levillain0d5a2812015-11-13 10:07:31 +00001755 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001756 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001757 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1758 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001759 }
1760
1761 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001762 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001763 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001764 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001765 for (int s = kLastBit; s >= 0; s--) {
1766 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1767 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1768 // This is a bit for the GC side effect.
1769 if (current_bit_is_set) {
1770 flags += "GC";
1771 }
Aart Bik854a02b2015-07-14 16:07:00 -07001772 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001773 } else {
1774 // This is a bit for the array/field analysis.
1775 // The underscore character stands for the 'can trigger GC' bit.
1776 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1777 if (current_bit_is_set) {
1778 flags += kDebug[s];
1779 }
1780 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1781 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1782 flags += "|";
1783 }
1784 }
Aart Bik854a02b2015-07-14 16:07:00 -07001785 }
1786 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001787 }
1788
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001789 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001790
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001791 private:
1792 static constexpr int kFieldArrayAnalysisBits = 9;
1793
1794 static constexpr int kFieldWriteOffset = 0;
1795 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1796 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1797 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1798
1799 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1800
1801 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1802 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1803 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1804 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1805
1806 static constexpr int kLastBit = kDependsOnGCBit;
1807 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1808
1809 // Aliases.
1810
1811 static_assert(kChangeBits == kDependOnBits,
1812 "the 'change' bits should match the 'depend on' bits.");
1813
1814 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1815 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1816 static constexpr uint64_t kAllWrites =
1817 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1818 static constexpr uint64_t kAllReads =
1819 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001820
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001821 // Translates type to bit flag. The type must correspond to a Java type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001822 static uint64_t TypeFlag(DataType::Type type, int offset) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001823 int shift;
1824 switch (type) {
1825 case DataType::Type::kReference: shift = 0; break;
1826 case DataType::Type::kBool: shift = 1; break;
1827 case DataType::Type::kInt8: shift = 2; break;
1828 case DataType::Type::kUint16: shift = 3; break;
1829 case DataType::Type::kInt16: shift = 4; break;
1830 case DataType::Type::kInt32: shift = 5; break;
1831 case DataType::Type::kInt64: shift = 6; break;
1832 case DataType::Type::kFloat32: shift = 7; break;
1833 case DataType::Type::kFloat64: shift = 8; break;
1834 default:
1835 LOG(FATAL) << "Unexpected data type " << type;
1836 UNREACHABLE();
1837 }
Aart Bik854a02b2015-07-14 16:07:00 -07001838 DCHECK_LE(kFieldWriteOffset, shift);
1839 DCHECK_LT(shift, kArrayWriteOffset);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001840 return UINT64_C(1) << (shift + offset);
Aart Bik854a02b2015-07-14 16:07:00 -07001841 }
1842
1843 // Private constructor on direct flags value.
1844 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1845
1846 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001847};
1848
David Brazdiled596192015-01-23 10:39:45 +00001849// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001850class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001851 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001852 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
Mingyao Yang01b47b02017-02-03 12:09:57 -08001853 size_t number_of_vregs,
1854 ArtMethod* method,
1855 uint32_t dex_pc,
1856 HInstruction* holder)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001857 : vregs_(number_of_vregs, allocator->Adapter(kArenaAllocEnvironmentVRegs)),
1858 locations_(allocator->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001859 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001860 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001861 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001862 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001863 }
1864
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001865 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
1866 const HEnvironment& to_copy,
1867 HInstruction* holder)
1868 : HEnvironment(allocator,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001869 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001870 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001871 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001872 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001873
Vladimir Markoec32f642017-06-02 10:51:55 +01001874 void AllocateLocations() {
1875 DCHECK(locations_.empty());
1876 locations_.resize(vregs_.size());
1877 }
1878
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001879 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001880 if (parent_ != nullptr) {
1881 parent_->SetAndCopyParentChain(allocator, parent);
1882 } else {
1883 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1884 parent_->CopyFrom(parent);
1885 if (parent->GetParent() != nullptr) {
1886 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1887 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001888 }
David Brazdiled596192015-01-23 10:39:45 +00001889 }
1890
Vladimir Marko69d310e2017-10-09 14:12:23 +01001891 void CopyFrom(ArrayRef<HInstruction* const> locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001892 void CopyFrom(HEnvironment* environment);
1893
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001894 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1895 // input to the loop phi instead. This is for inserting instructions that
1896 // require an environment (like HDeoptimization) in the loop pre-header.
1897 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001898
1899 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001900 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001901 }
1902
1903 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001904 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001905 }
1906
David Brazdil1abb4192015-02-17 18:33:36 +00001907 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001908
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001909 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001910
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001911 HEnvironment* GetParent() const { return parent_; }
1912
1913 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001914 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001915 }
1916
1917 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001918 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001919 }
1920
1921 uint32_t GetDexPc() const {
1922 return dex_pc_;
1923 }
1924
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001925 ArtMethod* GetMethod() const {
1926 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001927 }
1928
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001929 HInstruction* GetHolder() const {
1930 return holder_;
1931 }
1932
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001933
1934 bool IsFromInlinedInvoke() const {
1935 return GetParent() != nullptr;
1936 }
1937
David Brazdiled596192015-01-23 10:39:45 +00001938 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001939 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1940 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001941 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001942 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001943 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00001944
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001945 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001946 HInstruction* const holder_;
1947
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001948 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001949
1950 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1951};
1952
Vladimir Markof9f64412015-09-02 14:05:49 +01001953class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001954 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001955 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001956 : previous_(nullptr),
1957 next_(nullptr),
1958 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001959 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001960 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001961 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001962 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001963 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001964 locations_(nullptr),
1965 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001966 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001967 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001968 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1969 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1970 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001971
Dave Allison20dfc792014-06-16 20:44:29 -07001972 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001973
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001974#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001975 enum InstructionKind {
1976 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1977 };
1978#undef DECLARE_KIND
1979
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001980 HInstruction* GetNext() const { return next_; }
1981 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001982
Calin Juravle77520bc2015-01-12 18:45:46 +00001983 HInstruction* GetNextDisregardingMoves() const;
1984 HInstruction* GetPreviousDisregardingMoves() const;
1985
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001986 HBasicBlock* GetBlock() const { return block_; }
Vladimir Markoca6fff82017-10-03 14:49:14 +01001987 ArenaAllocator* GetAllocator() const { return block_->GetGraph()->GetAllocator(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001988 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001989 bool IsInBlock() const { return block_ != nullptr; }
1990 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001991 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1992 bool IsIrreducibleLoopHeaderPhi() const {
1993 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1994 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001995
Vladimir Marko372f10e2016-05-17 16:30:10 +01001996 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
1997
1998 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
1999 // One virtual method is enough, just const_cast<> and then re-add the const.
2000 return ArrayRef<const HUserRecord<HInstruction*>>(
2001 const_cast<HInstruction*>(this)->GetInputRecords());
2002 }
2003
Vladimir Markoe9004912016-06-16 16:50:52 +01002004 HInputsRef GetInputs() {
2005 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002006 }
2007
Vladimir Markoe9004912016-06-16 16:50:52 +01002008 HConstInputsRef GetInputs() const {
2009 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002010 }
2011
2012 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00002013 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002014
Aart Bik2767f4b2016-10-28 15:03:53 -07002015 bool HasInput(HInstruction* input) const {
2016 for (const HInstruction* i : GetInputs()) {
2017 if (i == input) {
2018 return true;
2019 }
2020 }
2021 return false;
2022 }
2023
Vladimir Marko372f10e2016-05-17 16:30:10 +01002024 void SetRawInputAt(size_t index, HInstruction* input) {
2025 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
2026 }
2027
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002028 virtual void Accept(HGraphVisitor* visitor) = 0;
2029 virtual const char* DebugName() const = 0;
2030
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002031 virtual DataType::Type GetType() const { return DataType::Type::kVoid; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002032
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002033 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002034
2035 uint32_t GetDexPc() const { return dex_pc_; }
2036
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002037 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01002038
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002039 // Can the instruction throw?
2040 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
2041 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01002042 virtual bool CanThrow() const { return false; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08002043
2044 // Does the instruction always throw an exception unconditionally?
2045 virtual bool AlwaysThrows() const { return false; }
2046
David Brazdilec16f792015-08-19 15:04:01 +01002047 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002048
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002049 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002050 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002051
Calin Juravle10e244f2015-01-26 18:54:32 +00002052 // Does not apply for all instructions, but having this at top level greatly
2053 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00002054 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002055 virtual bool CanBeNull() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002056 DCHECK_EQ(GetType(), DataType::Type::kReference) << "CanBeNull only applies to reference types";
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002057 return true;
2058 }
Calin Juravle10e244f2015-01-26 18:54:32 +00002059
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002060 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01002061 return false;
2062 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002063
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002064 virtual bool IsActualObject() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002065 return GetType() == DataType::Type::kReference;
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002066 }
2067
Calin Juravle2e768302015-07-28 14:41:11 +00002068 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00002069
Calin Juravle61d544b2015-02-23 16:46:57 +00002070 ReferenceTypeInfo GetReferenceTypeInfo() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002071 DCHECK_EQ(GetType(), DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002072 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00002073 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00002074 }
Calin Juravleacf735c2015-02-12 15:25:22 +00002075
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002076 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00002077 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002078 // Note: fixup_end remains valid across push_front().
2079 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
2080 HUseListNode<HInstruction*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002081 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HInstruction*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002082 uses_.push_front(*new_node);
2083 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002084 }
2085
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002086 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01002087 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002088 // Note: env_fixup_end remains valid across push_front().
2089 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
2090 HUseListNode<HEnvironment*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002091 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HEnvironment*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002092 env_uses_.push_front(*new_node);
2093 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002094 }
2095
David Brazdil1abb4192015-02-17 18:33:36 +00002096 void RemoveAsUserOfInput(size_t input) {
2097 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002098 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2099 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2100 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00002101 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002102
Vladimir Marko372f10e2016-05-17 16:30:10 +01002103 void RemoveAsUserOfAllInputs() {
2104 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2105 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2106 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2107 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
2108 }
2109 }
2110
David Brazdil1abb4192015-02-17 18:33:36 +00002111 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2112 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002113
Vladimir Marko46817b82016-03-29 12:21:58 +01002114 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2115 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2116 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002117 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002118 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002119 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002120
Aart Bikcc42be02016-10-20 16:14:16 -07002121 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002122 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002123 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002124 !CanThrow() &&
2125 !IsSuspendCheck() &&
2126 !IsControlFlow() &&
2127 !IsNativeDebugInfo() &&
2128 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002129 // If we added an explicit barrier then we should keep it.
Igor Murashkind01745e2017-04-05 16:40:31 -07002130 !IsMemoryBarrier() &&
2131 !IsConstructorFence();
Aart Bik482095d2016-10-10 15:39:10 -07002132 }
2133
Aart Bikcc42be02016-10-20 16:14:16 -07002134 bool IsDeadAndRemovable() const {
2135 return IsRemovable() && !HasUses();
2136 }
2137
Roland Levillain6c82d402014-10-13 16:10:27 +01002138 // Does this instruction strictly dominate `other_instruction`?
2139 // Returns false if this instruction and `other_instruction` are the same.
2140 // Aborts if this instruction and `other_instruction` are both phis.
2141 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002142
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002143 int GetId() const { return id_; }
2144 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002145
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002146 int GetSsaIndex() const { return ssa_index_; }
2147 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2148 bool HasSsaIndex() const { return ssa_index_ != -1; }
2149
2150 bool HasEnvironment() const { return environment_ != nullptr; }
2151 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002152 // Set the `environment_` field. Raw because this method does not
2153 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002154 void SetRawEnvironment(HEnvironment* environment) {
2155 DCHECK(environment_ == nullptr);
2156 DCHECK_EQ(environment->GetHolder(), this);
2157 environment_ = environment;
2158 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002159
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002160 void InsertRawEnvironment(HEnvironment* environment) {
2161 DCHECK(environment_ != nullptr);
2162 DCHECK_EQ(environment->GetHolder(), this);
2163 DCHECK(environment->GetParent() == nullptr);
2164 environment->parent_ = environment_;
2165 environment_ = environment;
2166 }
2167
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002168 void RemoveEnvironment();
2169
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002170 // Set the environment of this instruction, copying it from `environment`. While
2171 // copying, the uses lists are being updated.
2172 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002173 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002174 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002175 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002176 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002177 if (environment->GetParent() != nullptr) {
2178 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2179 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002180 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002181
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002182 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2183 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002184 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002185 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002186 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002187 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002188 if (environment->GetParent() != nullptr) {
2189 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2190 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002191 }
2192
Nicolas Geoffray39468442014-09-02 15:17:15 +01002193 // Returns the number of entries in the environment. Typically, that is the
2194 // number of dex registers in a method. It could be more in case of inlining.
2195 size_t EnvironmentSize() const;
2196
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002197 LocationSummary* GetLocations() const { return locations_; }
2198 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002199
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002200 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002201 void ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002202 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002203
Alexandre Rames188d4312015-04-09 18:30:21 +01002204 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2205 // uses of this instruction by `other` are *not* updated.
2206 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2207 ReplaceWith(other);
2208 other->ReplaceInput(this, use_index);
2209 }
2210
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002211 // Move `this` instruction before `cursor`
2212 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002213
Vladimir Markofb337ea2015-11-25 15:25:10 +00002214 // Move `this` before its first user and out of any loops. If there is no
2215 // out-of-loop user that dominates all other users, move the instruction
2216 // to the end of the out-of-loop common dominator of the user's blocks.
2217 //
2218 // This can be used only on non-throwing instructions with no side effects that
2219 // have at least one use but no environment uses.
2220 void MoveBeforeFirstUserAndOutOfLoops();
2221
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002222#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002223 bool Is##type() const; \
2224 const H##type* As##type() const; \
2225 H##type* As##type();
2226
2227 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2228#undef INSTRUCTION_TYPE_CHECK
2229
2230#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002231 bool Is##type() const { return (As##type() != nullptr); } \
2232 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002233 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002234 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002235#undef INSTRUCTION_TYPE_CHECK
2236
Artem Serovcced8ba2017-07-19 18:18:09 +01002237 // Return a clone of the instruction if it is clonable (shallow copy by default, custom copy
2238 // if a custom copy-constructor is provided for a particular type). If IsClonable() is false for
2239 // the instruction then the behaviour of this function is undefined.
2240 //
2241 // Note: It is semantically valid to create a clone of the instruction only until
2242 // prepare_for_register_allocator phase as lifetime, intervals and codegen info are not
2243 // copied.
2244 //
2245 // Note: HEnvironment and some other fields are not copied and are set to default values, see
2246 // 'explicit HInstruction(const HInstruction& other)' for details.
2247 virtual HInstruction* Clone(ArenaAllocator* arena ATTRIBUTE_UNUSED) const {
2248 LOG(FATAL) << "Cloning is not implemented for the instruction " <<
2249 DebugName() << " " << GetId();
2250 UNREACHABLE();
2251 }
2252
2253 // Return whether instruction can be cloned (copied).
2254 virtual bool IsClonable() const { return false; }
2255
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002256 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002257 // TODO: this method is used by LICM and GVN with possibly different
2258 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002259 virtual bool CanBeMoved() const { return false; }
2260
2261 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002262 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002263 return false;
2264 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002265
2266 // Returns whether any data encoded in the two instructions is equal.
2267 // This method does not look at the inputs. Both instructions must be
2268 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002269 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002270 return false;
2271 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002272
2273 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002274 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002275 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002276 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002277
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002278 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2279 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2280 // the virtual function because the __attribute__((__pure__)) doesn't really
2281 // apply the strong requirement for virtual functions, preventing optimizations.
2282 InstructionKind GetKind() const PURE;
2283 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002284
2285 virtual size_t ComputeHashCode() const {
2286 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002287 for (const HInstruction* input : GetInputs()) {
2288 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002289 }
2290 return result;
2291 }
2292
2293 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002294 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002295 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002296
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002297 size_t GetLifetimePosition() const { return lifetime_position_; }
2298 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2299 LiveInterval* GetLiveInterval() const { return live_interval_; }
2300 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2301 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2302
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002303 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2304
2305 // Returns whether the code generation of the instruction will require to have access
2306 // to the current method. Such instructions are:
2307 // (1): Instructions that require an environment, as calling the runtime requires
2308 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002309 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2310 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002311 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002312 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002313 }
2314
Vladimir Markodc151b22015-10-15 18:02:30 +01002315 // Returns whether the code generation of the instruction will require to have access
2316 // to the dex cache of the current method's declaring class via the current method.
2317 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002318
Mark Mendellc4701932015-04-10 13:18:51 -04002319 // Does this instruction have any use in an environment before
2320 // control flow hits 'other'?
2321 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2322
2323 // Remove all references to environment uses of this instruction.
2324 // The caller must ensure that this is safe to do.
2325 void RemoveEnvironmentUsers();
2326
Vladimir Markoa1de9182016-02-25 11:37:38 +00002327 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2328 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002329
David Brazdil1abb4192015-02-17 18:33:36 +00002330 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002331 // If set, the machine code for this instruction is assumed to be generated by
2332 // its users. Used by liveness analysis to compute use positions accordingly.
2333 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2334 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2335 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2336 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2337
Vladimir Marko372f10e2016-05-17 16:30:10 +01002338 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2339 return GetInputRecords()[i];
2340 }
2341
2342 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2343 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2344 input_records[index] = input;
2345 }
David Brazdil1abb4192015-02-17 18:33:36 +00002346
Vladimir Markoa1de9182016-02-25 11:37:38 +00002347 uint32_t GetPackedFields() const {
2348 return packed_fields_;
2349 }
2350
2351 template <size_t flag>
2352 bool GetPackedFlag() const {
2353 return (packed_fields_ & (1u << flag)) != 0u;
2354 }
2355
2356 template <size_t flag>
2357 void SetPackedFlag(bool value = true) {
2358 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2359 }
2360
2361 template <typename BitFieldType>
2362 typename BitFieldType::value_type GetPackedField() const {
2363 return BitFieldType::Decode(packed_fields_);
2364 }
2365
2366 template <typename BitFieldType>
2367 void SetPackedField(typename BitFieldType::value_type value) {
2368 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2369 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2370 }
2371
Artem Serovcced8ba2017-07-19 18:18:09 +01002372 // Copy construction for the instruction (used for Clone function).
2373 //
2374 // Fields (e.g. lifetime, intervals and codegen info) associated with phases starting from
2375 // prepare_for_register_allocator are not copied (set to default values).
2376 //
2377 // Copy constructors must be provided for every HInstruction type; default copy constructor is
2378 // fine for most of them. However for some of the instructions a custom copy constructor must be
2379 // specified (when instruction has non-trivially copyable fields and must have a special behaviour
2380 // for copying them).
2381 explicit HInstruction(const HInstruction& other)
2382 : previous_(nullptr),
2383 next_(nullptr),
2384 block_(nullptr),
2385 dex_pc_(other.dex_pc_),
2386 id_(-1),
2387 ssa_index_(-1),
2388 packed_fields_(other.packed_fields_),
2389 environment_(nullptr),
2390 locations_(nullptr),
2391 live_interval_(nullptr),
2392 lifetime_position_(kNoLifetime),
2393 side_effects_(other.side_effects_),
2394 reference_type_handle_(other.reference_type_handle_) {}
2395
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002396 private:
Vladimir Marko46817b82016-03-29 12:21:58 +01002397 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2398 auto before_use_node = uses_.before_begin();
2399 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2400 HInstruction* user = use_node->GetUser();
2401 size_t input_index = use_node->GetIndex();
2402 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2403 before_use_node = use_node;
2404 }
2405 }
2406
2407 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2408 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2409 if (next != uses_.end()) {
2410 HInstruction* next_user = next->GetUser();
2411 size_t next_index = next->GetIndex();
2412 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2413 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2414 }
2415 }
2416
2417 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2418 auto before_env_use_node = env_uses_.before_begin();
2419 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2420 HEnvironment* user = env_use_node->GetUser();
2421 size_t input_index = env_use_node->GetIndex();
2422 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2423 before_env_use_node = env_use_node;
2424 }
2425 }
2426
2427 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2428 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2429 if (next != env_uses_.end()) {
2430 HEnvironment* next_user = next->GetUser();
2431 size_t next_index = next->GetIndex();
2432 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2433 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2434 }
2435 }
David Brazdil1abb4192015-02-17 18:33:36 +00002436
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002437 HInstruction* previous_;
2438 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002439 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002440 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002441
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002442 // An instruction gets an id when it is added to the graph.
2443 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002444 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002445 int id_;
2446
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002447 // When doing liveness analysis, instructions that have uses get an SSA index.
2448 int ssa_index_;
2449
Vladimir Markoa1de9182016-02-25 11:37:38 +00002450 // Packed fields.
2451 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002452
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002453 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002454 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002455
2456 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002457 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002458
Nicolas Geoffray39468442014-09-02 15:17:15 +01002459 // The environment associated with this instruction. Not null if the instruction
2460 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002461 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002462
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002463 // Set by the code generator.
2464 LocationSummary* locations_;
2465
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002466 // Set by the liveness analysis.
2467 LiveInterval* live_interval_;
2468
2469 // Set by the liveness analysis, this is the position in a linear
2470 // order of blocks where this instruction's live interval start.
2471 size_t lifetime_position_;
2472
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002473 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002474
Vladimir Markoa1de9182016-02-25 11:37:38 +00002475 // The reference handle part of the reference type info.
2476 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002477 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002478 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002479
David Brazdil1abb4192015-02-17 18:33:36 +00002480 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002481 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002482 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002483 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002484 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002485};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002486std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002487
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002488// Iterates over the instructions, while preserving the next instruction
2489// in case the current instruction gets removed from the list by the user
2490// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002491class HInstructionIterator : public ValueObject {
2492 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002493 explicit HInstructionIterator(const HInstructionList& instructions)
2494 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002495 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002496 }
2497
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002498 bool Done() const { return instruction_ == nullptr; }
2499 HInstruction* Current() const { return instruction_; }
2500 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002501 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002502 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002503 }
2504
2505 private:
2506 HInstruction* instruction_;
2507 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002508
2509 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002510};
2511
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002512// Iterates over the instructions without saving the next instruction,
2513// therefore handling changes in the graph potentially made by the user
2514// of this iterator.
2515class HInstructionIteratorHandleChanges : public ValueObject {
2516 public:
2517 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2518 : instruction_(instructions.first_instruction_) {
2519 }
2520
2521 bool Done() const { return instruction_ == nullptr; }
2522 HInstruction* Current() const { return instruction_; }
2523 void Advance() {
2524 instruction_ = instruction_->GetNext();
2525 }
2526
2527 private:
2528 HInstruction* instruction_;
2529
2530 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2531};
2532
2533
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002534class HBackwardInstructionIterator : public ValueObject {
2535 public:
2536 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2537 : instruction_(instructions.last_instruction_) {
2538 next_ = Done() ? nullptr : instruction_->GetPrevious();
2539 }
2540
2541 bool Done() const { return instruction_ == nullptr; }
2542 HInstruction* Current() const { return instruction_; }
2543 void Advance() {
2544 instruction_ = next_;
2545 next_ = Done() ? nullptr : instruction_->GetPrevious();
2546 }
2547
2548 private:
2549 HInstruction* instruction_;
2550 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002551
2552 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002553};
2554
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002555class HVariableInputSizeInstruction : public HInstruction {
2556 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002557 using HInstruction::GetInputRecords; // Keep the const version visible.
2558 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
2559 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2560 }
2561
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002562 void AddInput(HInstruction* input);
2563 void InsertInputAt(size_t index, HInstruction* input);
2564 void RemoveInputAt(size_t index);
2565
Igor Murashkind01745e2017-04-05 16:40:31 -07002566 // Removes all the inputs.
2567 // Also removes this instructions from each input's use list
2568 // (for non-environment uses only).
2569 void RemoveAllInputs();
2570
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002571 protected:
2572 HVariableInputSizeInstruction(SideEffects side_effects,
2573 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002574 ArenaAllocator* allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002575 size_t number_of_inputs,
2576 ArenaAllocKind kind)
2577 : HInstruction(side_effects, dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002578 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002579
Artem Serovcced8ba2017-07-19 18:18:09 +01002580 DEFAULT_COPY_CONSTRUCTOR(VariableInputSizeInstruction);
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002581
Artem Serovcced8ba2017-07-19 18:18:09 +01002582 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002583};
2584
Vladimir Markof9f64412015-09-02 14:05:49 +01002585template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002586class HTemplateInstruction: public HInstruction {
2587 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002588 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002589 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002590 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002591
Vladimir Marko372f10e2016-05-17 16:30:10 +01002592 using HInstruction::GetInputRecords; // Keep the const version visible.
2593 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2594 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002595 }
2596
Artem Serovcced8ba2017-07-19 18:18:09 +01002597 protected:
2598 DEFAULT_COPY_CONSTRUCTOR(TemplateInstruction<N>);
2599
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002600 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002601 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002602
2603 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002604};
2605
Vladimir Markof9f64412015-09-02 14:05:49 +01002606// HTemplateInstruction specialization for N=0.
2607template<>
2608class HTemplateInstruction<0>: public HInstruction {
2609 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002610 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002611 : HInstruction(side_effects, dex_pc) {}
2612
Vladimir Markof9f64412015-09-02 14:05:49 +01002613 virtual ~HTemplateInstruction() {}
2614
Vladimir Marko372f10e2016-05-17 16:30:10 +01002615 using HInstruction::GetInputRecords; // Keep the const version visible.
2616 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2617 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002618 }
2619
Artem Serovcced8ba2017-07-19 18:18:09 +01002620 protected:
2621 DEFAULT_COPY_CONSTRUCTOR(TemplateInstruction<0>);
2622
Vladimir Markof9f64412015-09-02 14:05:49 +01002623 private:
2624 friend class SsaBuilder;
2625};
2626
Dave Allison20dfc792014-06-16 20:44:29 -07002627template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002628class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002629 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002630 HExpression<N>(DataType::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002631 : HTemplateInstruction<N>(side_effects, dex_pc) {
2632 this->template SetPackedField<TypeField>(type);
2633 }
Dave Allison20dfc792014-06-16 20:44:29 -07002634 virtual ~HExpression() {}
2635
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002636 DataType::Type GetType() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00002637 return TypeField::Decode(this->GetPackedFields());
2638 }
Dave Allison20dfc792014-06-16 20:44:29 -07002639
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002640 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002641 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2642 static constexpr size_t kFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002643 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00002644 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2645 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2646 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002647 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
Artem Serovcced8ba2017-07-19 18:18:09 +01002648 DEFAULT_COPY_CONSTRUCTOR(Expression<N>);
Dave Allison20dfc792014-06-16 20:44:29 -07002649};
2650
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002651// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2652// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002653class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002654 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002655 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2656 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002657
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002658 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002659
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002660 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002661
Artem Serovcced8ba2017-07-19 18:18:09 +01002662 protected:
2663 DEFAULT_COPY_CONSTRUCTOR(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002664};
2665
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002666// Represents dex's RETURN opcodes. A HReturn is a control flow
2667// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002668class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002669 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002670 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2671 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002672 SetRawInputAt(0, value);
2673 }
2674
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002675 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002676
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002677 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002678
Artem Serovcced8ba2017-07-19 18:18:09 +01002679 protected:
2680 DEFAULT_COPY_CONSTRUCTOR(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002681};
2682
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002683class HPhi FINAL : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002684 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002685 HPhi(ArenaAllocator* allocator,
David Brazdildee58d62016-04-07 09:54:26 +00002686 uint32_t reg_number,
2687 size_t number_of_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002688 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +00002689 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002690 : HVariableInputSizeInstruction(
2691 SideEffects::None(),
2692 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002693 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002694 number_of_inputs,
2695 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002696 reg_number_(reg_number) {
2697 SetPackedField<TypeField>(ToPhiType(type));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002698 DCHECK_NE(GetType(), DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +00002699 // Phis are constructed live and marked dead if conflicting or unused.
2700 // Individual steps of SsaBuilder should assume that if a phi has been
2701 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2702 SetPackedFlag<kFlagIsLive>(true);
2703 SetPackedFlag<kFlagCanBeNull>(true);
2704 }
2705
Artem Serovcced8ba2017-07-19 18:18:09 +01002706 bool IsClonable() const OVERRIDE { return true; }
2707
David Brazdildee58d62016-04-07 09:54:26 +00002708 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002709 static DataType::Type ToPhiType(DataType::Type type) {
2710 return DataType::Kind(type);
David Brazdildee58d62016-04-07 09:54:26 +00002711 }
2712
2713 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2714
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002715 DataType::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2716 void SetType(DataType::Type new_type) {
David Brazdildee58d62016-04-07 09:54:26 +00002717 // Make sure that only valid type changes occur. The following are allowed:
2718 // (1) int -> float/ref (primitive type propagation),
2719 // (2) long -> double (primitive type propagation).
2720 DCHECK(GetType() == new_type ||
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002721 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kFloat32) ||
2722 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kReference) ||
2723 (GetType() == DataType::Type::kInt64 && new_type == DataType::Type::kFloat64));
David Brazdildee58d62016-04-07 09:54:26 +00002724 SetPackedField<TypeField>(new_type);
2725 }
2726
2727 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2728 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2729
2730 uint32_t GetRegNumber() const { return reg_number_; }
2731
2732 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2733 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2734 bool IsDead() const { return !IsLive(); }
2735 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2736
Vladimir Markoe9004912016-06-16 16:50:52 +01002737 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002738 return other != nullptr
2739 && other->IsPhi()
2740 && other->AsPhi()->GetBlock() == GetBlock()
2741 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2742 }
2743
Nicolas Geoffrayf57c1ae2017-06-28 17:40:18 +01002744 bool HasEquivalentPhi() const {
2745 if (GetPrevious() != nullptr && GetPrevious()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2746 return true;
2747 }
2748 if (GetNext() != nullptr && GetNext()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2749 return true;
2750 }
2751 return false;
2752 }
2753
David Brazdildee58d62016-04-07 09:54:26 +00002754 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2755 // An equivalent phi is a phi having the same dex register and type.
2756 // It assumes that phis with the same dex register are adjacent.
2757 HPhi* GetNextEquivalentPhiWithSameType() {
2758 HInstruction* next = GetNext();
2759 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2760 if (next->GetType() == GetType()) {
2761 return next->AsPhi();
2762 }
2763 next = next->GetNext();
2764 }
2765 return nullptr;
2766 }
2767
2768 DECLARE_INSTRUCTION(Phi);
2769
Artem Serovcced8ba2017-07-19 18:18:09 +01002770 protected:
2771 DEFAULT_COPY_CONSTRUCTOR(Phi);
2772
David Brazdildee58d62016-04-07 09:54:26 +00002773 private:
2774 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2775 static constexpr size_t kFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002776 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
David Brazdildee58d62016-04-07 09:54:26 +00002777 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2778 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2779 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2780 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002781 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
David Brazdildee58d62016-04-07 09:54:26 +00002782
David Brazdildee58d62016-04-07 09:54:26 +00002783 const uint32_t reg_number_;
David Brazdildee58d62016-04-07 09:54:26 +00002784};
2785
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002786// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002787// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002788// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002789class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002790 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002791 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002792
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002793 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002794
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002795 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002796
Artem Serovcced8ba2017-07-19 18:18:09 +01002797 protected:
2798 DEFAULT_COPY_CONSTRUCTOR(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002799};
2800
2801// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002802class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002803 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002804 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002805
Artem Serovcced8ba2017-07-19 18:18:09 +01002806 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002807 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002808
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002809 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002810 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002811 }
2812
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002813 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002814
Artem Serovcced8ba2017-07-19 18:18:09 +01002815 protected:
2816 DEFAULT_COPY_CONSTRUCTOR(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002817};
2818
Roland Levillain9867bc72015-08-05 10:21:34 +01002819class HConstant : public HExpression<0> {
2820 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002821 explicit HConstant(DataType::Type type, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002822 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002823
2824 bool CanBeMoved() const OVERRIDE { return true; }
2825
Roland Levillain1a653882016-03-18 18:05:57 +00002826 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002827 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002828 // Is this constant 0 in the arithmetic sense?
2829 virtual bool IsArithmeticZero() const { return false; }
2830 // Is this constant a 0-bit pattern?
2831 virtual bool IsZeroBitPattern() const { return false; }
2832 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002833 virtual bool IsOne() const { return false; }
2834
David Brazdil77a48ae2015-09-15 12:34:04 +00002835 virtual uint64_t GetValueAsUint64() const = 0;
2836
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002837 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002838
Artem Serovcced8ba2017-07-19 18:18:09 +01002839 protected:
2840 DEFAULT_COPY_CONSTRUCTOR(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002841};
2842
Vladimir Markofcb503c2016-05-18 12:48:17 +01002843class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002844 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002845 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002846 return true;
2847 }
2848
David Brazdil77a48ae2015-09-15 12:34:04 +00002849 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2850
Roland Levillain9867bc72015-08-05 10:21:34 +01002851 size_t ComputeHashCode() const OVERRIDE { return 0; }
2852
Roland Levillain1a653882016-03-18 18:05:57 +00002853 // The null constant representation is a 0-bit pattern.
2854 virtual bool IsZeroBitPattern() const { return true; }
2855
Roland Levillain9867bc72015-08-05 10:21:34 +01002856 DECLARE_INSTRUCTION(NullConstant);
2857
Artem Serovcced8ba2017-07-19 18:18:09 +01002858 protected:
2859 DEFAULT_COPY_CONSTRUCTOR(NullConstant);
2860
Roland Levillain9867bc72015-08-05 10:21:34 +01002861 private:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002862 explicit HNullConstant(uint32_t dex_pc = kNoDexPc)
2863 : HConstant(DataType::Type::kReference, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002864
2865 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01002866};
2867
2868// Constants of the type int. Those can be from Dex instructions, or
2869// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002870class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002871 public:
2872 int32_t GetValue() const { return value_; }
2873
David Brazdil9f389d42015-10-01 14:32:56 +01002874 uint64_t GetValueAsUint64() const OVERRIDE {
2875 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2876 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002877
Vladimir Marko372f10e2016-05-17 16:30:10 +01002878 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002879 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002880 return other->AsIntConstant()->value_ == value_;
2881 }
2882
2883 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2884
2885 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002886 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2887 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002888 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2889
Roland Levillain1a653882016-03-18 18:05:57 +00002890 // Integer constants are used to encode Boolean values as well,
2891 // where 1 means true and 0 means false.
2892 bool IsTrue() const { return GetValue() == 1; }
2893 bool IsFalse() const { return GetValue() == 0; }
2894
Roland Levillain9867bc72015-08-05 10:21:34 +01002895 DECLARE_INSTRUCTION(IntConstant);
2896
Artem Serovcced8ba2017-07-19 18:18:09 +01002897 protected:
2898 DEFAULT_COPY_CONSTRUCTOR(IntConstant);
2899
Roland Levillain9867bc72015-08-05 10:21:34 +01002900 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002901 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002902 : HConstant(DataType::Type::kInt32, dex_pc), value_(value) {}
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002903 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002904 : HConstant(DataType::Type::kInt32, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002905
2906 const int32_t value_;
2907
2908 friend class HGraph;
2909 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2910 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Roland Levillain9867bc72015-08-05 10:21:34 +01002911};
2912
Vladimir Markofcb503c2016-05-18 12:48:17 +01002913class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002914 public:
2915 int64_t GetValue() const { return value_; }
2916
David Brazdil77a48ae2015-09-15 12:34:04 +00002917 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2918
Vladimir Marko372f10e2016-05-17 16:30:10 +01002919 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002920 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002921 return other->AsLongConstant()->value_ == value_;
2922 }
2923
2924 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2925
2926 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002927 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2928 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002929 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2930
2931 DECLARE_INSTRUCTION(LongConstant);
2932
Artem Serovcced8ba2017-07-19 18:18:09 +01002933 protected:
2934 DEFAULT_COPY_CONSTRUCTOR(LongConstant);
2935
Roland Levillain9867bc72015-08-05 10:21:34 +01002936 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002937 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002938 : HConstant(DataType::Type::kInt64, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002939
2940 const int64_t value_;
2941
2942 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01002943};
Dave Allison20dfc792014-06-16 20:44:29 -07002944
Vladimir Markofcb503c2016-05-18 12:48:17 +01002945class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002946 public:
2947 float GetValue() const { return value_; }
2948
2949 uint64_t GetValueAsUint64() const OVERRIDE {
2950 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2951 }
2952
Vladimir Marko372f10e2016-05-17 16:30:10 +01002953 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002954 DCHECK(other->IsFloatConstant()) << other->DebugName();
2955 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2956 }
2957
2958 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2959
2960 bool IsMinusOne() const OVERRIDE {
2961 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2962 }
Roland Levillain1a653882016-03-18 18:05:57 +00002963 bool IsArithmeticZero() const OVERRIDE {
2964 return std::fpclassify(value_) == FP_ZERO;
2965 }
2966 bool IsArithmeticPositiveZero() const {
2967 return IsArithmeticZero() && !std::signbit(value_);
2968 }
2969 bool IsArithmeticNegativeZero() const {
2970 return IsArithmeticZero() && std::signbit(value_);
2971 }
2972 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002973 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002974 }
2975 bool IsOne() const OVERRIDE {
2976 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2977 }
2978 bool IsNaN() const {
2979 return std::isnan(value_);
2980 }
2981
2982 DECLARE_INSTRUCTION(FloatConstant);
2983
Artem Serovcced8ba2017-07-19 18:18:09 +01002984 protected:
2985 DEFAULT_COPY_CONSTRUCTOR(FloatConstant);
2986
Roland Levillain31dd3d62016-02-16 12:21:02 +00002987 private:
2988 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002989 : HConstant(DataType::Type::kFloat32, dex_pc), value_(value) {}
Roland Levillain31dd3d62016-02-16 12:21:02 +00002990 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002991 : HConstant(DataType::Type::kFloat32, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
Roland Levillain31dd3d62016-02-16 12:21:02 +00002992
2993 const float value_;
2994
2995 // Only the SsaBuilder and HGraph can create floating-point constants.
2996 friend class SsaBuilder;
2997 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00002998};
2999
Vladimir Markofcb503c2016-05-18 12:48:17 +01003000class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003001 public:
3002 double GetValue() const { return value_; }
3003
3004 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
3005
Vladimir Marko372f10e2016-05-17 16:30:10 +01003006 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003007 DCHECK(other->IsDoubleConstant()) << other->DebugName();
3008 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
3009 }
3010
3011 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
3012
3013 bool IsMinusOne() const OVERRIDE {
3014 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
3015 }
Roland Levillain1a653882016-03-18 18:05:57 +00003016 bool IsArithmeticZero() const OVERRIDE {
3017 return std::fpclassify(value_) == FP_ZERO;
3018 }
3019 bool IsArithmeticPositiveZero() const {
3020 return IsArithmeticZero() && !std::signbit(value_);
3021 }
3022 bool IsArithmeticNegativeZero() const {
3023 return IsArithmeticZero() && std::signbit(value_);
3024 }
3025 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003026 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00003027 }
3028 bool IsOne() const OVERRIDE {
3029 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3030 }
3031 bool IsNaN() const {
3032 return std::isnan(value_);
3033 }
3034
3035 DECLARE_INSTRUCTION(DoubleConstant);
3036
Artem Serovcced8ba2017-07-19 18:18:09 +01003037 protected:
3038 DEFAULT_COPY_CONSTRUCTOR(DoubleConstant);
3039
Roland Levillain31dd3d62016-02-16 12:21:02 +00003040 private:
3041 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003042 : HConstant(DataType::Type::kFloat64, dex_pc), value_(value) {}
Roland Levillain31dd3d62016-02-16 12:21:02 +00003043 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003044 : HConstant(DataType::Type::kFloat64, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
Roland Levillain31dd3d62016-02-16 12:21:02 +00003045
3046 const double value_;
3047
3048 // Only the SsaBuilder and HGraph can create floating-point constants.
3049 friend class SsaBuilder;
3050 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003051};
3052
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003053// Conditional branch. A block ending with an HIf instruction must have
3054// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003055class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003056 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003057 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
3058 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003059 SetRawInputAt(0, input);
3060 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003061
Artem Serovcced8ba2017-07-19 18:18:09 +01003062 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003063 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003064
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003065 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003066 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003067 }
3068
3069 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003070 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003071 }
3072
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003073 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003074
Artem Serovcced8ba2017-07-19 18:18:09 +01003075 protected:
3076 DEFAULT_COPY_CONSTRUCTOR(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003077};
3078
David Brazdilfc6a86a2015-06-26 10:33:45 +00003079
3080// Abstract instruction which marks the beginning and/or end of a try block and
3081// links it to the respective exception handlers. Behaves the same as a Goto in
3082// non-exceptional control flow.
3083// Normal-flow successor is stored at index zero, exception handlers under
3084// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003085class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003086 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003087 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01003088 kEntry,
3089 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003090 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01003091 };
3092
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003093 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003094 : HTemplateInstruction(SideEffects::None(), dex_pc) {
3095 SetPackedField<BoundaryKindField>(kind);
3096 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003097
3098 bool IsControlFlow() const OVERRIDE { return true; }
3099
3100 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01003101 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003102
David Brazdild26a4112015-11-10 11:07:31 +00003103 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
3104 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
3105 }
3106
David Brazdilfc6a86a2015-06-26 10:33:45 +00003107 // Returns whether `handler` is among its exception handlers (non-zero index
3108 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01003109 bool HasExceptionHandler(const HBasicBlock& handler) const {
3110 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00003111 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00003112 }
3113
3114 // If not present already, adds `handler` to its block's list of exception
3115 // handlers.
3116 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01003117 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003118 GetBlock()->AddSuccessor(handler);
3119 }
3120 }
3121
Vladimir Markoa1de9182016-02-25 11:37:38 +00003122 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
3123 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003124
David Brazdilffee3d32015-07-06 11:48:53 +01003125 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
3126
David Brazdilfc6a86a2015-06-26 10:33:45 +00003127 DECLARE_INSTRUCTION(TryBoundary);
3128
Artem Serovcced8ba2017-07-19 18:18:09 +01003129 protected:
3130 DEFAULT_COPY_CONSTRUCTOR(TryBoundary);
3131
David Brazdilfc6a86a2015-06-26 10:33:45 +00003132 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003133 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
3134 static constexpr size_t kFieldBoundaryKindSize =
3135 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
3136 static constexpr size_t kNumberOfTryBoundaryPackedBits =
3137 kFieldBoundaryKind + kFieldBoundaryKindSize;
3138 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
3139 "Too many packed fields.");
3140 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00003141};
3142
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003143// Deoptimize to interpreter, upon checking a condition.
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003144class HDeoptimize FINAL : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003145 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003146 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
3147 // across.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003148 HDeoptimize(ArenaAllocator* allocator,
3149 HInstruction* cond,
3150 DeoptimizationKind kind,
3151 uint32_t dex_pc)
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003152 : HVariableInputSizeInstruction(
3153 SideEffects::All(),
3154 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003155 allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003156 /* number_of_inputs */ 1,
3157 kArenaAllocMisc) {
3158 SetPackedFlag<kFieldCanBeMoved>(false);
3159 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003160 SetRawInputAt(0, cond);
3161 }
3162
Artem Serovcced8ba2017-07-19 18:18:09 +01003163 bool IsClonable() const OVERRIDE { return true; }
3164
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003165 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
3166 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
3167 // instead of `guard`.
3168 // We set CanTriggerGC to prevent any intermediate address to be live
3169 // at the point of the `HDeoptimize`.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003170 HDeoptimize(ArenaAllocator* allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003171 HInstruction* cond,
3172 HInstruction* guard,
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003173 DeoptimizationKind kind,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003174 uint32_t dex_pc)
3175 : HVariableInputSizeInstruction(
3176 SideEffects::CanTriggerGC(),
3177 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003178 allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003179 /* number_of_inputs */ 2,
3180 kArenaAllocMisc) {
3181 SetPackedFlag<kFieldCanBeMoved>(true);
3182 SetPackedField<DeoptimizeKindField>(kind);
3183 SetRawInputAt(0, cond);
3184 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003185 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003186
3187 bool CanBeMoved() const OVERRIDE { return GetPackedFlag<kFieldCanBeMoved>(); }
3188
3189 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
3190 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
3191 }
3192
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003193 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003194
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003195 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003196
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003197 DeoptimizationKind GetDeoptimizationKind() const { return GetPackedField<DeoptimizeKindField>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003198
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003199 DataType::Type GetType() const OVERRIDE {
3200 return GuardsAnInput() ? GuardedInput()->GetType() : DataType::Type::kVoid;
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003201 }
3202
3203 bool GuardsAnInput() const {
3204 return InputCount() == 2;
3205 }
3206
3207 HInstruction* GuardedInput() const {
3208 DCHECK(GuardsAnInput());
3209 return InputAt(1);
3210 }
3211
3212 void RemoveGuard() {
3213 RemoveInputAt(1);
3214 }
3215
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003216 DECLARE_INSTRUCTION(Deoptimize);
3217
Artem Serovcced8ba2017-07-19 18:18:09 +01003218 protected:
3219 DEFAULT_COPY_CONSTRUCTOR(Deoptimize);
3220
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003221 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003222 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3223 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3224 static constexpr size_t kFieldDeoptimizeKindSize =
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003225 MinimumBitsToStore(static_cast<size_t>(DeoptimizationKind::kLast));
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003226 static constexpr size_t kNumberOfDeoptimizePackedBits =
3227 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3228 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3229 "Too many packed fields.");
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003230 using DeoptimizeKindField =
3231 BitField<DeoptimizationKind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003232};
3233
Mingyao Yang063fc772016-08-02 11:02:54 -07003234// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3235// The compiled code checks this flag value in a guard before devirtualized call and
3236// if it's true, starts to do deoptimization.
3237// It has a 4-byte slot on stack.
3238// TODO: allocate a register for this flag.
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003239class HShouldDeoptimizeFlag FINAL : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003240 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003241 // CHA guards are only optimized in a separate pass and it has no side effects
3242 // with regard to other passes.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003243 HShouldDeoptimizeFlag(ArenaAllocator* allocator, uint32_t dex_pc)
3244 : HVariableInputSizeInstruction(SideEffects::None(), dex_pc, allocator, 0, kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003245 }
3246
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003247 DataType::Type GetType() const OVERRIDE { return DataType::Type::kInt32; }
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003248
3249 // We do all CHA guard elimination/motion in a single pass, after which there is no
3250 // further guard elimination/motion since a guard might have been used for justification
3251 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3252 // to avoid other optimizations trying to move it.
Mingyao Yang063fc772016-08-02 11:02:54 -07003253 bool CanBeMoved() const OVERRIDE { return false; }
3254
3255 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3256
Artem Serovcced8ba2017-07-19 18:18:09 +01003257 protected:
3258 DEFAULT_COPY_CONSTRUCTOR(ShouldDeoptimizeFlag);
Mingyao Yang063fc772016-08-02 11:02:54 -07003259};
3260
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003261// Represents the ArtMethod that was passed as a first argument to
3262// the method. It is used by instructions that depend on it, like
3263// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003264class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003265 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003266 explicit HCurrentMethod(DataType::Type type, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003267 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003268
3269 DECLARE_INSTRUCTION(CurrentMethod);
3270
Artem Serovcced8ba2017-07-19 18:18:09 +01003271 protected:
3272 DEFAULT_COPY_CONSTRUCTOR(CurrentMethod);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003273};
3274
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003275// Fetches an ArtMethod from the virtual table or the interface method table
3276// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003277class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003278 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003279 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003280 kVTable,
3281 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003282 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003283 };
3284 HClassTableGet(HInstruction* cls,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003285 DataType::Type type,
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003286 TableKind kind,
3287 size_t index,
3288 uint32_t dex_pc)
3289 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003290 index_(index) {
3291 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003292 SetRawInputAt(0, cls);
3293 }
3294
Artem Serovcced8ba2017-07-19 18:18:09 +01003295 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003296 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003297 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003298 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003299 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003300 }
3301
Vladimir Markoa1de9182016-02-25 11:37:38 +00003302 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003303 size_t GetIndex() const { return index_; }
3304
3305 DECLARE_INSTRUCTION(ClassTableGet);
3306
Artem Serovcced8ba2017-07-19 18:18:09 +01003307 protected:
3308 DEFAULT_COPY_CONSTRUCTOR(ClassTableGet);
3309
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003310 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003311 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
3312 static constexpr size_t kFieldTableKindSize =
3313 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3314 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3315 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3316 "Too many packed fields.");
3317 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3318
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003319 // The index of the ArtMethod in the table.
3320 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003321};
3322
Mark Mendellfe57faa2015-09-18 09:26:15 -04003323// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3324// have one successor for each entry in the switch table, and the final successor
3325// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003326class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003327 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003328 HPackedSwitch(int32_t start_value,
3329 uint32_t num_entries,
3330 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003331 uint32_t dex_pc = kNoDexPc)
3332 : HTemplateInstruction(SideEffects::None(), dex_pc),
3333 start_value_(start_value),
3334 num_entries_(num_entries) {
3335 SetRawInputAt(0, input);
3336 }
3337
Artem Serovcced8ba2017-07-19 18:18:09 +01003338 bool IsClonable() const OVERRIDE { return true; }
3339
Mark Mendellfe57faa2015-09-18 09:26:15 -04003340 bool IsControlFlow() const OVERRIDE { return true; }
3341
3342 int32_t GetStartValue() const { return start_value_; }
3343
Vladimir Marko211c2112015-09-24 16:52:33 +01003344 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003345
3346 HBasicBlock* GetDefaultBlock() const {
3347 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003348 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003349 }
3350 DECLARE_INSTRUCTION(PackedSwitch);
3351
Artem Serovcced8ba2017-07-19 18:18:09 +01003352 protected:
3353 DEFAULT_COPY_CONSTRUCTOR(PackedSwitch);
3354
Mark Mendellfe57faa2015-09-18 09:26:15 -04003355 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003356 const int32_t start_value_;
3357 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003358};
3359
Roland Levillain88cb1752014-10-20 16:36:47 +01003360class HUnaryOperation : public HExpression<1> {
3361 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003362 HUnaryOperation(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003363 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003364 SetRawInputAt(0, input);
3365 }
3366
Artem Serovcced8ba2017-07-19 18:18:09 +01003367 // All of the UnaryOperation instructions are clonable.
3368 bool IsClonable() const OVERRIDE { return true; }
3369
Roland Levillain88cb1752014-10-20 16:36:47 +01003370 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003371 DataType::Type GetResultType() const { return GetType(); }
Roland Levillain88cb1752014-10-20 16:36:47 +01003372
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003373 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003374 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003375 return true;
3376 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003377
Roland Levillain31dd3d62016-02-16 12:21:02 +00003378 // Try to statically evaluate `this` and return a HConstant
3379 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003380 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003381 HConstant* TryStaticEvaluation() const;
3382
3383 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003384 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3385 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003386 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3387 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003388
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003389 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003390
Artem Serovcced8ba2017-07-19 18:18:09 +01003391 protected:
3392 DEFAULT_COPY_CONSTRUCTOR(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003393};
3394
Dave Allison20dfc792014-06-16 20:44:29 -07003395class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003396 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003397 HBinaryOperation(DataType::Type result_type,
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003398 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003399 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003400 SideEffects side_effects = SideEffects::None(),
3401 uint32_t dex_pc = kNoDexPc)
3402 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003403 SetRawInputAt(0, left);
3404 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003405 }
3406
Artem Serovcced8ba2017-07-19 18:18:09 +01003407 // All of the BinaryOperation instructions are clonable.
3408 bool IsClonable() const OVERRIDE { return true; }
3409
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003410 HInstruction* GetLeft() const { return InputAt(0); }
3411 HInstruction* GetRight() const { return InputAt(1); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003412 DataType::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003413
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003414 virtual bool IsCommutative() const { return false; }
3415
3416 // Put constant on the right.
3417 // Returns whether order is changed.
3418 bool OrderInputsWithConstantOnTheRight() {
3419 HInstruction* left = InputAt(0);
3420 HInstruction* right = InputAt(1);
3421 if (left->IsConstant() && !right->IsConstant()) {
3422 ReplaceInput(right, 0);
3423 ReplaceInput(left, 1);
3424 return true;
3425 }
3426 return false;
3427 }
3428
3429 // Order inputs by instruction id, but favor constant on the right side.
3430 // This helps GVN for commutative ops.
3431 void OrderInputs() {
3432 DCHECK(IsCommutative());
3433 HInstruction* left = InputAt(0);
3434 HInstruction* right = InputAt(1);
3435 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3436 return;
3437 }
3438 if (OrderInputsWithConstantOnTheRight()) {
3439 return;
3440 }
3441 // Order according to instruction id.
3442 if (left->GetId() > right->GetId()) {
3443 ReplaceInput(right, 0);
3444 ReplaceInput(left, 1);
3445 }
3446 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003447
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003448 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003449 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003450 return true;
3451 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003452
Roland Levillain31dd3d62016-02-16 12:21:02 +00003453 // Try to statically evaluate `this` and return a HConstant
3454 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003455 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003456 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003457
3458 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003459 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3460 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003461 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3462 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003463 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003464 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3465 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003466 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3467 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003468 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3469 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003470 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003471 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3472 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003473
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003474 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003475 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003476 HConstant* GetConstantRight() const;
3477
3478 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003479 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003480 HInstruction* GetLeastConstantLeft() const;
3481
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003482 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003483
Artem Serovcced8ba2017-07-19 18:18:09 +01003484 protected:
3485 DEFAULT_COPY_CONSTRUCTOR(BinaryOperation);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003486};
3487
Mark Mendellc4701932015-04-10 13:18:51 -04003488// The comparison bias applies for floating point operations and indicates how NaN
3489// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003490enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003491 kNoBias, // bias is not applicable (i.e. for long operation)
3492 kGtBias, // return 1 for NaN comparisons
3493 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003494 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003495};
3496
Roland Levillain31dd3d62016-02-16 12:21:02 +00003497std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3498
Dave Allison20dfc792014-06-16 20:44:29 -07003499class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003500 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003501 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003502 : HBinaryOperation(DataType::Type::kBool, first, second, SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003503 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3504 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003505
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003506 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003507 // `instruction`, and disregard moves in between.
3508 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003509
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003510 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003511
3512 virtual IfCondition GetCondition() const = 0;
3513
Mark Mendellc4701932015-04-10 13:18:51 -04003514 virtual IfCondition GetOppositeCondition() const = 0;
3515
Vladimir Markoa1de9182016-02-25 11:37:38 +00003516 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003517 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3518
Vladimir Markoa1de9182016-02-25 11:37:38 +00003519 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3520 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003521
Vladimir Marko372f10e2016-05-17 16:30:10 +01003522 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003523 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003524 }
3525
Roland Levillain4fa13f62015-07-06 18:11:54 +01003526 bool IsFPConditionTrueIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003527 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003528 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003529 if (if_cond == kCondNE) {
3530 return true;
3531 } else if (if_cond == kCondEQ) {
3532 return false;
3533 }
3534 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003535 }
3536
3537 bool IsFPConditionFalseIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003538 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003539 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003540 if (if_cond == kCondEQ) {
3541 return true;
3542 } else if (if_cond == kCondNE) {
3543 return false;
3544 }
3545 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003546 }
3547
Roland Levillain31dd3d62016-02-16 12:21:02 +00003548 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003549 // Needed if we merge a HCompare into a HCondition.
3550 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3551 static constexpr size_t kFieldComparisonBiasSize =
3552 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3553 static constexpr size_t kNumberOfConditionPackedBits =
3554 kFieldComparisonBias + kFieldComparisonBiasSize;
3555 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3556 using ComparisonBiasField =
3557 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3558
Roland Levillain31dd3d62016-02-16 12:21:02 +00003559 template <typename T>
3560 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3561
3562 template <typename T>
3563 int32_t CompareFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003564 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003565 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3566 // Handle the bias.
3567 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3568 }
3569
3570 // Return an integer constant containing the result of a condition evaluated at compile time.
3571 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3572 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3573 }
3574
Artem Serovcced8ba2017-07-19 18:18:09 +01003575 DEFAULT_COPY_CONSTRUCTOR(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003576};
3577
3578// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003579class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003580 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003581 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3582 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003583
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003584 bool IsCommutative() const OVERRIDE { return true; }
3585
Vladimir Marko9e23df52015-11-10 17:14:35 +00003586 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3587 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003588 return MakeConstantCondition(true, GetDexPc());
3589 }
3590 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3591 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3592 }
3593 // In the following Evaluate methods, a HCompare instruction has
3594 // been merged into this HEqual instruction; evaluate it as
3595 // `Compare(x, y) == 0`.
3596 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3597 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3598 GetDexPc());
3599 }
3600 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3601 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3602 }
3603 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3604 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003605 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003606
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003607 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003608
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003609 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003610 return kCondEQ;
3611 }
3612
Mark Mendellc4701932015-04-10 13:18:51 -04003613 IfCondition GetOppositeCondition() const OVERRIDE {
3614 return kCondNE;
3615 }
3616
Artem Serovcced8ba2017-07-19 18:18:09 +01003617 protected:
3618 DEFAULT_COPY_CONSTRUCTOR(Equal);
3619
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003620 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003621 template <typename T> static bool Compute(T x, T y) { return x == y; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003622};
3623
Vladimir Markofcb503c2016-05-18 12:48:17 +01003624class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003625 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003626 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3627 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003628
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003629 bool IsCommutative() const OVERRIDE { return true; }
3630
Vladimir Marko9e23df52015-11-10 17:14:35 +00003631 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3632 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003633 return MakeConstantCondition(false, GetDexPc());
3634 }
3635 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3636 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3637 }
3638 // In the following Evaluate methods, a HCompare instruction has
3639 // been merged into this HNotEqual instruction; evaluate it as
3640 // `Compare(x, y) != 0`.
3641 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3642 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3643 }
3644 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3645 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3646 }
3647 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3648 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003649 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003650
Dave Allison20dfc792014-06-16 20:44:29 -07003651 DECLARE_INSTRUCTION(NotEqual);
3652
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003653 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003654 return kCondNE;
3655 }
3656
Mark Mendellc4701932015-04-10 13:18:51 -04003657 IfCondition GetOppositeCondition() const OVERRIDE {
3658 return kCondEQ;
3659 }
3660
Artem Serovcced8ba2017-07-19 18:18:09 +01003661 protected:
3662 DEFAULT_COPY_CONSTRUCTOR(NotEqual);
3663
Dave Allison20dfc792014-06-16 20:44:29 -07003664 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003665 template <typename T> static bool Compute(T x, T y) { return x != y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003666};
3667
Vladimir Markofcb503c2016-05-18 12:48:17 +01003668class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003669 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003670 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3671 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003672
Roland Levillain9867bc72015-08-05 10:21:34 +01003673 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003674 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003675 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003676 // In the following Evaluate methods, a HCompare instruction has
3677 // been merged into this HLessThan instruction; evaluate it as
3678 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003679 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003680 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3681 }
3682 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3683 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3684 }
3685 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3686 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003687 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003688
Dave Allison20dfc792014-06-16 20:44:29 -07003689 DECLARE_INSTRUCTION(LessThan);
3690
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003691 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003692 return kCondLT;
3693 }
3694
Mark Mendellc4701932015-04-10 13:18:51 -04003695 IfCondition GetOppositeCondition() const OVERRIDE {
3696 return kCondGE;
3697 }
3698
Artem Serovcced8ba2017-07-19 18:18:09 +01003699 protected:
3700 DEFAULT_COPY_CONSTRUCTOR(LessThan);
3701
Dave Allison20dfc792014-06-16 20:44:29 -07003702 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003703 template <typename T> static bool Compute(T x, T y) { return x < y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003704};
3705
Vladimir Markofcb503c2016-05-18 12:48:17 +01003706class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003707 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003708 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3709 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003710
Roland Levillain9867bc72015-08-05 10:21:34 +01003711 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003712 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003713 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003714 // In the following Evaluate methods, a HCompare instruction has
3715 // been merged into this HLessThanOrEqual instruction; evaluate it as
3716 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003717 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003718 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3719 }
3720 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3721 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3722 }
3723 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3724 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003725 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003726
Dave Allison20dfc792014-06-16 20:44:29 -07003727 DECLARE_INSTRUCTION(LessThanOrEqual);
3728
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003729 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003730 return kCondLE;
3731 }
3732
Mark Mendellc4701932015-04-10 13:18:51 -04003733 IfCondition GetOppositeCondition() const OVERRIDE {
3734 return kCondGT;
3735 }
3736
Artem Serovcced8ba2017-07-19 18:18:09 +01003737 protected:
3738 DEFAULT_COPY_CONSTRUCTOR(LessThanOrEqual);
3739
Dave Allison20dfc792014-06-16 20:44:29 -07003740 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003741 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003742};
3743
Vladimir Markofcb503c2016-05-18 12:48:17 +01003744class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003745 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003746 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3747 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003748
Roland Levillain9867bc72015-08-05 10:21:34 +01003749 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003750 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003751 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003752 // In the following Evaluate methods, a HCompare instruction has
3753 // been merged into this HGreaterThan instruction; evaluate it as
3754 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003755 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003756 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3757 }
3758 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3759 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3760 }
3761 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3762 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003763 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003764
Dave Allison20dfc792014-06-16 20:44:29 -07003765 DECLARE_INSTRUCTION(GreaterThan);
3766
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003767 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003768 return kCondGT;
3769 }
3770
Mark Mendellc4701932015-04-10 13:18:51 -04003771 IfCondition GetOppositeCondition() const OVERRIDE {
3772 return kCondLE;
3773 }
3774
Artem Serovcced8ba2017-07-19 18:18:09 +01003775 protected:
3776 DEFAULT_COPY_CONSTRUCTOR(GreaterThan);
3777
Dave Allison20dfc792014-06-16 20:44:29 -07003778 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003779 template <typename T> static bool Compute(T x, T y) { return x > y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003780};
3781
Vladimir Markofcb503c2016-05-18 12:48:17 +01003782class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003783 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003784 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3785 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003786
Roland Levillain9867bc72015-08-05 10:21:34 +01003787 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003788 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003789 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003790 // In the following Evaluate methods, a HCompare instruction has
3791 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3792 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003793 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003794 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3795 }
3796 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3797 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3798 }
3799 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3800 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003801 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003802
Dave Allison20dfc792014-06-16 20:44:29 -07003803 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3804
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003805 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003806 return kCondGE;
3807 }
3808
Mark Mendellc4701932015-04-10 13:18:51 -04003809 IfCondition GetOppositeCondition() const OVERRIDE {
3810 return kCondLT;
3811 }
3812
Artem Serovcced8ba2017-07-19 18:18:09 +01003813 protected:
3814 DEFAULT_COPY_CONSTRUCTOR(GreaterThanOrEqual);
3815
Dave Allison20dfc792014-06-16 20:44:29 -07003816 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003817 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003818};
3819
Vladimir Markofcb503c2016-05-18 12:48:17 +01003820class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003821 public:
3822 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3823 : HCondition(first, second, dex_pc) {}
3824
3825 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003826 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003827 }
3828 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003829 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3830 }
3831 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3832 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3833 LOG(FATAL) << DebugName() << " is not defined for float values";
3834 UNREACHABLE();
3835 }
3836 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3837 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3838 LOG(FATAL) << DebugName() << " is not defined for double values";
3839 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003840 }
3841
3842 DECLARE_INSTRUCTION(Below);
3843
3844 IfCondition GetCondition() const OVERRIDE {
3845 return kCondB;
3846 }
3847
3848 IfCondition GetOppositeCondition() const OVERRIDE {
3849 return kCondAE;
3850 }
3851
Artem Serovcced8ba2017-07-19 18:18:09 +01003852 protected:
3853 DEFAULT_COPY_CONSTRUCTOR(Below);
3854
Aart Bike9f37602015-10-09 11:15:55 -07003855 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003856 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003857 return MakeUnsigned(x) < MakeUnsigned(y);
3858 }
Aart Bike9f37602015-10-09 11:15:55 -07003859};
3860
Vladimir Markofcb503c2016-05-18 12:48:17 +01003861class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003862 public:
3863 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3864 : HCondition(first, second, dex_pc) {}
3865
3866 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003867 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003868 }
3869 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003870 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3871 }
3872 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3873 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3874 LOG(FATAL) << DebugName() << " is not defined for float values";
3875 UNREACHABLE();
3876 }
3877 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3878 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3879 LOG(FATAL) << DebugName() << " is not defined for double values";
3880 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003881 }
3882
3883 DECLARE_INSTRUCTION(BelowOrEqual);
3884
3885 IfCondition GetCondition() const OVERRIDE {
3886 return kCondBE;
3887 }
3888
3889 IfCondition GetOppositeCondition() const OVERRIDE {
3890 return kCondA;
3891 }
3892
Artem Serovcced8ba2017-07-19 18:18:09 +01003893 protected:
3894 DEFAULT_COPY_CONSTRUCTOR(BelowOrEqual);
3895
Aart Bike9f37602015-10-09 11:15:55 -07003896 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003897 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003898 return MakeUnsigned(x) <= MakeUnsigned(y);
3899 }
Aart Bike9f37602015-10-09 11:15:55 -07003900};
3901
Vladimir Markofcb503c2016-05-18 12:48:17 +01003902class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003903 public:
3904 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3905 : HCondition(first, second, dex_pc) {}
3906
3907 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003908 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003909 }
3910 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003911 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3912 }
3913 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3914 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3915 LOG(FATAL) << DebugName() << " is not defined for float values";
3916 UNREACHABLE();
3917 }
3918 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3919 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3920 LOG(FATAL) << DebugName() << " is not defined for double values";
3921 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003922 }
3923
3924 DECLARE_INSTRUCTION(Above);
3925
3926 IfCondition GetCondition() const OVERRIDE {
3927 return kCondA;
3928 }
3929
3930 IfCondition GetOppositeCondition() const OVERRIDE {
3931 return kCondBE;
3932 }
3933
Artem Serovcced8ba2017-07-19 18:18:09 +01003934 protected:
3935 DEFAULT_COPY_CONSTRUCTOR(Above);
3936
Aart Bike9f37602015-10-09 11:15:55 -07003937 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003938 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003939 return MakeUnsigned(x) > MakeUnsigned(y);
3940 }
Aart Bike9f37602015-10-09 11:15:55 -07003941};
3942
Vladimir Markofcb503c2016-05-18 12:48:17 +01003943class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003944 public:
3945 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3946 : HCondition(first, second, dex_pc) {}
3947
3948 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003949 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003950 }
3951 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003952 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3953 }
3954 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3955 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3956 LOG(FATAL) << DebugName() << " is not defined for float values";
3957 UNREACHABLE();
3958 }
3959 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3960 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3961 LOG(FATAL) << DebugName() << " is not defined for double values";
3962 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003963 }
3964
3965 DECLARE_INSTRUCTION(AboveOrEqual);
3966
3967 IfCondition GetCondition() const OVERRIDE {
3968 return kCondAE;
3969 }
3970
3971 IfCondition GetOppositeCondition() const OVERRIDE {
3972 return kCondB;
3973 }
3974
Artem Serovcced8ba2017-07-19 18:18:09 +01003975 protected:
3976 DEFAULT_COPY_CONSTRUCTOR(AboveOrEqual);
3977
Aart Bike9f37602015-10-09 11:15:55 -07003978 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003979 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003980 return MakeUnsigned(x) >= MakeUnsigned(y);
3981 }
Aart Bike9f37602015-10-09 11:15:55 -07003982};
Dave Allison20dfc792014-06-16 20:44:29 -07003983
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003984// Instruction to check how two inputs compare to each other.
3985// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003986class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003987 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003988 // Note that `comparison_type` is the type of comparison performed
3989 // between the comparison's inputs, not the type of the instantiated
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003990 // HCompare instruction (which is always DataType::Type::kInt).
3991 HCompare(DataType::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003992 HInstruction* first,
3993 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003994 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003995 uint32_t dex_pc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003996 : HBinaryOperation(DataType::Type::kInt32,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003997 first,
3998 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003999 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004000 dex_pc) {
4001 SetPackedField<ComparisonBiasField>(bias);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004002 DCHECK_EQ(comparison_type, DataType::Kind(first->GetType()));
4003 DCHECK_EQ(comparison_type, DataType::Kind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004004 }
4005
Roland Levillain9867bc72015-08-05 10:21:34 +01004006 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004007 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
4008
4009 template <typename T>
4010 int32_t ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004011 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004012 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
4013 // Handle the bias.
4014 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
4015 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004016
Roland Levillain9867bc72015-08-05 10:21:34 +01004017 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004018 // Note that there is no "cmp-int" Dex instruction so we shouldn't
4019 // reach this code path when processing a freshly built HIR
4020 // graph. However HCompare integer instructions can be synthesized
4021 // by the instruction simplifier to implement IntegerCompare and
4022 // IntegerSignum intrinsics, so we have to handle this case.
4023 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004024 }
4025 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004026 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4027 }
4028 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4029 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4030 }
4031 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4032 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01004033 }
4034
Vladimir Marko372f10e2016-05-17 16:30:10 +01004035 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004036 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00004037 }
4038
Vladimir Markoa1de9182016-02-25 11:37:38 +00004039 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04004040
Roland Levillain31dd3d62016-02-16 12:21:02 +00004041 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00004042 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00004043 bool IsGtBias() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004044 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004045 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00004046 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004047
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004048 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type type ATTRIBUTE_UNUSED) {
Roland Levillain1693a1f2016-03-15 14:57:31 +00004049 // Comparisons do not require a runtime call in any back end.
4050 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004051 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004052
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004053 DECLARE_INSTRUCTION(Compare);
4054
Roland Levillain31dd3d62016-02-16 12:21:02 +00004055 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004056 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
4057 static constexpr size_t kFieldComparisonBiasSize =
4058 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
4059 static constexpr size_t kNumberOfComparePackedBits =
4060 kFieldComparisonBias + kFieldComparisonBiasSize;
4061 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4062 using ComparisonBiasField =
4063 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
4064
Roland Levillain31dd3d62016-02-16 12:21:02 +00004065 // Return an integer constant containing the result of a comparison evaluated at compile time.
4066 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
4067 DCHECK(value == -1 || value == 0 || value == 1) << value;
4068 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
4069 }
4070
Artem Serovcced8ba2017-07-19 18:18:09 +01004071 DEFAULT_COPY_CONSTRUCTOR(Compare);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004072};
4073
Nicolas Geoffray2b615ba2017-01-06 14:40:07 +00004074class HNewInstance FINAL : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00004075 public:
4076 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00004077 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004078 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00004079 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00004080 bool finalizable,
4081 QuickEntrypointEnum entrypoint)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004082 : HExpression(DataType::Type::kReference, SideEffects::CanTriggerGC(), dex_pc),
David Brazdil6de19382016-01-08 17:37:10 +00004083 type_index_(type_index),
4084 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00004085 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004086 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00004087 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00004088 }
4089
Artem Serovcced8ba2017-07-19 18:18:09 +01004090 bool IsClonable() const OVERRIDE { return true; }
4091
Andreas Gampea5b09a62016-11-17 15:21:22 -08004092 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00004093 const DexFile& GetDexFile() const { return dex_file_; }
4094
4095 // Calls runtime so needs an environment.
4096 bool NeedsEnvironment() const OVERRIDE { return true; }
4097
Mingyao Yang062157f2016-03-02 10:15:36 -08004098 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
4099 bool CanThrow() const OVERRIDE { return true; }
4100
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004101 bool NeedsChecks() const {
4102 return entrypoint_ == kQuickAllocObjectWithChecks;
4103 }
David Brazdil6de19382016-01-08 17:37:10 +00004104
Vladimir Markoa1de9182016-02-25 11:37:38 +00004105 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00004106
4107 bool CanBeNull() const OVERRIDE { return false; }
4108
4109 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4110
4111 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
4112 entrypoint_ = entrypoint;
4113 }
4114
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004115 HLoadClass* GetLoadClass() const {
4116 HInstruction* input = InputAt(0);
4117 if (input->IsClinitCheck()) {
4118 input = input->InputAt(0);
4119 }
4120 DCHECK(input->IsLoadClass());
4121 return input->AsLoadClass();
4122 }
4123
David Brazdil6de19382016-01-08 17:37:10 +00004124 bool IsStringAlloc() const;
4125
4126 DECLARE_INSTRUCTION(NewInstance);
4127
Artem Serovcced8ba2017-07-19 18:18:09 +01004128 protected:
4129 DEFAULT_COPY_CONSTRUCTOR(NewInstance);
4130
David Brazdil6de19382016-01-08 17:37:10 +00004131 private:
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004132 static constexpr size_t kFlagFinalizable = kNumberOfExpressionPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004133 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
4134 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
4135 "Too many packed fields.");
4136
Andreas Gampea5b09a62016-11-17 15:21:22 -08004137 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00004138 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00004139 QuickEntrypointEnum entrypoint_;
David Brazdil6de19382016-01-08 17:37:10 +00004140};
4141
Agi Csaki05f20562015-08-19 14:58:14 -07004142enum IntrinsicNeedsEnvironmentOrCache {
4143 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
4144 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07004145};
4146
Aart Bik5d75afe2015-12-14 11:57:01 -08004147enum IntrinsicSideEffects {
4148 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
4149 kReadSideEffects, // Intrinsic may read heap memory.
4150 kWriteSideEffects, // Intrinsic may write heap memory.
4151 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
4152};
4153
4154enum IntrinsicExceptions {
4155 kNoThrow, // Intrinsic does not throw any exceptions.
4156 kCanThrow // Intrinsic may throw exceptions.
4157};
4158
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004159class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004160 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004161 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004162
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01004163 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004164 SetRawInputAt(index, argument);
4165 }
4166
Roland Levillain3e3d7332015-04-28 11:00:54 +01004167 // Return the number of arguments. This number can be lower than
4168 // the number of inputs returned by InputCount(), as some invoke
4169 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
4170 // inputs at the end of their list of inputs.
4171 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
4172
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004173 DataType::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004174
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004175 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4176
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004177 InvokeType GetInvokeType() const {
4178 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004179 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004180
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01004181 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004182 return intrinsic_;
4183 }
4184
Aart Bik5d75afe2015-12-14 11:57:01 -08004185 void SetIntrinsic(Intrinsics intrinsic,
4186 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
4187 IntrinsicSideEffects side_effects,
4188 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004189
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004190 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00004191 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004192 }
4193
Aart Bikff7d89c2016-11-07 08:49:28 -08004194 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4195
Vladimir Markoa1de9182016-02-25 11:37:38 +00004196 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004197
Aart Bika8b8e9b2018-01-09 11:01:02 -08004198 void SetAlwaysThrows(bool always_throws) { SetPackedFlag<kFlagAlwaysThrows>(always_throws); }
4199
4200 bool AlwaysThrows() const OVERRIDE { return GetPackedFlag<kFlagAlwaysThrows>(); }
4201
Aart Bik71bf7b42016-11-16 10:17:46 -08004202 bool CanBeMoved() const OVERRIDE { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004203
Vladimir Marko372f10e2016-05-17 16:30:10 +01004204 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08004205 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4206 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004207
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004208 uint32_t* GetIntrinsicOptimizations() {
4209 return &intrinsic_optimizations_;
4210 }
4211
4212 const uint32_t* GetIntrinsicOptimizations() const {
4213 return &intrinsic_optimizations_;
4214 }
4215
4216 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4217
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004218 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004219 void SetResolvedMethod(ArtMethod* method) { resolved_method_ = method; }
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004220
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004221 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004222
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004223 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004224 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4225 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004226 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4227 static constexpr size_t kFieldReturnType =
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004228 kFieldInvokeType + kFieldInvokeTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004229 static constexpr size_t kFieldReturnTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004230 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00004231 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
Aart Bika8b8e9b2018-01-09 11:01:02 -08004232 static constexpr size_t kFlagAlwaysThrows = kFlagCanThrow + 1;
4233 static constexpr size_t kNumberOfInvokePackedBits = kFlagAlwaysThrows + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004234 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004235 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004236 using ReturnTypeField = BitField<DataType::Type, kFieldReturnType, kFieldReturnTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004237
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004238 HInvoke(ArenaAllocator* allocator,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004239 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004240 uint32_t number_of_other_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004241 DataType::Type return_type,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004242 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004243 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004244 ArtMethod* resolved_method,
4245 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004246 : HVariableInputSizeInstruction(
4247 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4248 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004249 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004250 number_of_arguments + number_of_other_inputs,
4251 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004252 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004253 resolved_method_(resolved_method),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004254 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07004255 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004256 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004257 SetPackedField<ReturnTypeField>(return_type);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004258 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004259 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004260 }
4261
Artem Serovcced8ba2017-07-19 18:18:09 +01004262 DEFAULT_COPY_CONSTRUCTOR(Invoke);
4263
Roland Levillain3e3d7332015-04-28 11:00:54 +01004264 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004265 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004266 const uint32_t dex_method_index_;
4267 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004268
4269 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4270 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004271};
4272
Vladimir Markofcb503c2016-05-18 12:48:17 +01004273class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004274 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004275 HInvokeUnresolved(ArenaAllocator* allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004276 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004277 DataType::Type return_type,
Calin Juravle175dc732015-08-25 15:42:32 +01004278 uint32_t dex_pc,
4279 uint32_t dex_method_index,
4280 InvokeType invoke_type)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004281 : HInvoke(allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004282 number_of_arguments,
4283 0u /* number_of_other_inputs */,
4284 return_type,
4285 dex_pc,
4286 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004287 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01004288 invoke_type) {
4289 }
4290
Artem Serovcced8ba2017-07-19 18:18:09 +01004291 bool IsClonable() const OVERRIDE { return true; }
4292
Calin Juravle175dc732015-08-25 15:42:32 +01004293 DECLARE_INSTRUCTION(InvokeUnresolved);
4294
Artem Serovcced8ba2017-07-19 18:18:09 +01004295 protected:
4296 DEFAULT_COPY_CONSTRUCTOR(InvokeUnresolved);
Calin Juravle175dc732015-08-25 15:42:32 +01004297};
4298
Orion Hodsonac141392017-01-13 11:53:47 +00004299class HInvokePolymorphic FINAL : public HInvoke {
4300 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004301 HInvokePolymorphic(ArenaAllocator* allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004302 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004303 DataType::Type return_type,
Orion Hodsonac141392017-01-13 11:53:47 +00004304 uint32_t dex_pc,
4305 uint32_t dex_method_index)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004306 : HInvoke(allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004307 number_of_arguments,
4308 0u /* number_of_other_inputs */,
4309 return_type,
4310 dex_pc,
4311 dex_method_index,
4312 nullptr,
4313 kVirtual) {}
4314
Artem Serovcced8ba2017-07-19 18:18:09 +01004315 bool IsClonable() const OVERRIDE { return true; }
4316
Orion Hodsonac141392017-01-13 11:53:47 +00004317 DECLARE_INSTRUCTION(InvokePolymorphic);
4318
Artem Serovcced8ba2017-07-19 18:18:09 +01004319 protected:
4320 DEFAULT_COPY_CONSTRUCTOR(InvokePolymorphic);
Orion Hodsonac141392017-01-13 11:53:47 +00004321};
4322
Vladimir Markofcb503c2016-05-18 12:48:17 +01004323class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004324 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004325 // Requirements of this method call regarding the class
4326 // initialization (clinit) check of its declaring class.
4327 enum class ClinitCheckRequirement {
4328 kNone, // Class already initialized.
4329 kExplicit, // Static call having explicit clinit check as last input.
4330 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004331 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004332 };
4333
Vladimir Marko58155012015-08-19 12:49:41 +00004334 // Determines how to load the target ArtMethod*.
4335 enum class MethodLoadKind {
4336 // Use a String init ArtMethod* loaded from Thread entrypoints.
4337 kStringInit,
4338
4339 // Use the method's own ArtMethod* loaded by the register allocator.
4340 kRecursive,
4341
Vladimir Marko65979462017-05-19 17:25:12 +01004342 // Use PC-relative boot image ArtMethod* address that will be known at link time.
4343 // Used for boot image methods referenced by boot image code.
4344 kBootImageLinkTimePcRelative,
4345
Vladimir Marko58155012015-08-19 12:49:41 +00004346 // Use ArtMethod* at a known address, embed the direct address in the code.
4347 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
4348 kDirectAddress,
4349
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004350 // Load from an entry in the .bss section using a PC-relative load.
4351 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
4352 kBssEntry,
Vladimir Marko58155012015-08-19 12:49:41 +00004353
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004354 // Make a runtime call to resolve and call the method. This is the last-resort-kind
4355 // used when other kinds are unimplemented on a particular architecture.
4356 kRuntimeCall,
Vladimir Marko58155012015-08-19 12:49:41 +00004357 };
4358
4359 // Determines the location of the code pointer.
4360 enum class CodePtrLocation {
4361 // Recursive call, use local PC-relative call instruction.
4362 kCallSelf,
4363
Vladimir Marko58155012015-08-19 12:49:41 +00004364 // Use code pointer from the ArtMethod*.
4365 // Used when we don't know the target code. This is also the last-resort-kind used when
4366 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4367 kCallArtMethod,
4368 };
4369
4370 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004371 MethodLoadKind method_load_kind;
4372 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004373 // The method load data holds
4374 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4375 // Note that there are multiple string init methods, each having its own offset.
4376 // - the method address for kDirectAddress
Vladimir Markodc151b22015-10-15 18:02:30 +01004377 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004378 };
4379
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004380 HInvokeStaticOrDirect(ArenaAllocator* allocator,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004381 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004382 DataType::Type return_type,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004383 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004384 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004385 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004386 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004387 InvokeType invoke_type,
4388 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004389 ClinitCheckRequirement clinit_check_requirement)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004390 : HInvoke(allocator,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004391 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004392 // There is potentially one extra argument for the HCurrentMethod node, and
4393 // potentially one other if the clinit check is explicit, and potentially
4394 // one other if the method is a string factory.
4395 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004396 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004397 return_type,
4398 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004399 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004400 resolved_method,
4401 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004402 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004403 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004404 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4405 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004406
Artem Serovcced8ba2017-07-19 18:18:09 +01004407 bool IsClonable() const OVERRIDE { return true; }
4408
Vladimir Markodc151b22015-10-15 18:02:30 +01004409 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004410 bool had_current_method_input = HasCurrentMethodInput();
4411 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4412
4413 // Using the current method is the default and once we find a better
4414 // method load kind, we should not go back to using the current method.
4415 DCHECK(had_current_method_input || !needs_current_method_input);
4416
4417 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004418 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4419 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004420 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004421 dispatch_info_ = dispatch_info;
4422 }
4423
Aart Bik6daebeb2017-04-03 14:35:41 -07004424 DispatchInfo GetDispatchInfo() const {
4425 return dispatch_info_;
4426 }
4427
Vladimir Markoc53c0792015-11-19 15:48:33 +00004428 void AddSpecialInput(HInstruction* input) {
4429 // We allow only one special input.
4430 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4431 DCHECK(InputCount() == GetSpecialInputIndex() ||
4432 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4433 InsertInputAt(GetSpecialInputIndex(), input);
4434 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004435
Vladimir Marko372f10e2016-05-17 16:30:10 +01004436 using HInstruction::GetInputRecords; // Keep the const version visible.
4437 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
4438 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4439 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4440 DCHECK(!input_records.empty());
4441 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4442 HInstruction* last_input = input_records.back().GetInstruction();
4443 // Note: `last_input` may be null during arguments setup.
4444 if (last_input != nullptr) {
4445 // `last_input` is the last input of a static invoke marked as having
4446 // an explicit clinit check. It must either be:
4447 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4448 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4449 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4450 }
4451 }
4452 return input_records;
4453 }
4454
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004455 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004456 // We access the method via the dex cache so we can't do an implicit null check.
4457 // TODO: for intrinsics we can generate implicit null checks.
4458 return false;
4459 }
4460
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004461 bool CanBeNull() const OVERRIDE {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004462 return GetPackedField<ReturnTypeField>() == DataType::Type::kReference && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004463 }
4464
Vladimir Markoc53c0792015-11-19 15:48:33 +00004465 // Get the index of the special input, if any.
4466 //
David Brazdil6de19382016-01-08 17:37:10 +00004467 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4468 // method pointer; otherwise there may be one platform-specific special input,
4469 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004470 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004471 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004472
Vladimir Marko58155012015-08-19 12:49:41 +00004473 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4474 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4475 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004476 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004477 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004478 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko65979462017-05-19 17:25:12 +01004479 bool HasPcRelativeMethodLoadKind() const {
4480 return GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004481 GetMethodLoadKind() == MethodLoadKind::kBssEntry;
Vladimir Marko65979462017-05-19 17:25:12 +01004482 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004483 bool HasCurrentMethodInput() const {
4484 // This function can be called only after the invoke has been fully initialized by the builder.
4485 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004486 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004487 return true;
4488 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004489 DCHECK(InputCount() == GetSpecialInputIndex() ||
4490 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004491 return false;
4492 }
4493 }
Vladimir Marko58155012015-08-19 12:49:41 +00004494
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004495 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004496 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004497 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004498 }
4499
4500 uint64_t GetMethodAddress() const {
4501 DCHECK(HasMethodAddress());
4502 return dispatch_info_.method_load_data;
4503 }
4504
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004505 const DexFile& GetDexFileForPcRelativeDexCache() const;
4506
Vladimir Markoa1de9182016-02-25 11:37:38 +00004507 ClinitCheckRequirement GetClinitCheckRequirement() const {
4508 return GetPackedField<ClinitCheckRequirementField>();
4509 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004510
Roland Levillain4c0eb422015-04-24 16:43:49 +01004511 // Is this instruction a call to a static method?
4512 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004513 return GetInvokeType() == kStatic;
4514 }
4515
4516 MethodReference GetTargetMethod() const {
4517 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004518 }
4519
Vladimir Markofbb184a2015-11-13 14:47:00 +00004520 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4521 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4522 // instruction; only relevant for static calls with explicit clinit check.
4523 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004524 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004525 size_t last_input_index = inputs_.size() - 1u;
4526 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004527 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004528 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004529 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004530 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004531 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004532 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004533 }
4534
4535 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004536 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004537 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004538 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004539 }
4540
4541 // Is this a call to a static method whose declaring class has an
4542 // implicit intialization check requirement?
4543 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004544 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004545 }
4546
Vladimir Markob554b5a2015-11-06 12:57:55 +00004547 // Does this method load kind need the current method as an input?
4548 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004549 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kRuntimeCall;
Vladimir Markob554b5a2015-11-06 12:57:55 +00004550 }
4551
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004552 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004553
Artem Serovcced8ba2017-07-19 18:18:09 +01004554 protected:
4555 DEFAULT_COPY_CONSTRUCTOR(InvokeStaticOrDirect);
4556
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004557 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004558 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004559 static constexpr size_t kFieldClinitCheckRequirementSize =
4560 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4561 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4562 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4563 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4564 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004565 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4566 kFieldClinitCheckRequirement,
4567 kFieldClinitCheckRequirementSize>;
4568
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004569 // Cached values of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffrayea179f42018-02-08 22:30:18 +00004570 const MethodReference target_method_;
Vladimir Marko58155012015-08-19 12:49:41 +00004571 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004572};
Vladimir Markof64242a2015-12-01 14:58:23 +00004573std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004574std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004575
Vladimir Markofcb503c2016-05-18 12:48:17 +01004576class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004577 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004578 HInvokeVirtual(ArenaAllocator* allocator,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004579 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004580 DataType::Type return_type,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004581 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004582 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004583 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004584 uint32_t vtable_index)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004585 : HInvoke(allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004586 number_of_arguments,
4587 0u,
4588 return_type,
4589 dex_pc,
4590 dex_method_index,
4591 resolved_method,
4592 kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004593 vtable_index_(vtable_index) {}
4594
Artem Serovcced8ba2017-07-19 18:18:09 +01004595 bool IsClonable() const OVERRIDE { return true; }
4596
Aart Bik71bf7b42016-11-16 10:17:46 -08004597 bool CanBeNull() const OVERRIDE {
4598 switch (GetIntrinsic()) {
4599 case Intrinsics::kThreadCurrentThread:
4600 case Intrinsics::kStringBufferAppend:
4601 case Intrinsics::kStringBufferToString:
4602 case Intrinsics::kStringBuilderAppend:
4603 case Intrinsics::kStringBuilderToString:
4604 return false;
4605 default:
4606 return HInvoke::CanBeNull();
4607 }
4608 }
4609
Calin Juravle641547a2015-04-21 22:08:51 +01004610 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004611 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004612 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004613 }
4614
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004615 uint32_t GetVTableIndex() const { return vtable_index_; }
4616
4617 DECLARE_INSTRUCTION(InvokeVirtual);
4618
Artem Serovcced8ba2017-07-19 18:18:09 +01004619 protected:
4620 DEFAULT_COPY_CONSTRUCTOR(InvokeVirtual);
4621
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004622 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004623 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004624 const uint32_t vtable_index_;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004625};
4626
Vladimir Markofcb503c2016-05-18 12:48:17 +01004627class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004628 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004629 HInvokeInterface(ArenaAllocator* allocator,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004630 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004631 DataType::Type return_type,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004632 uint32_t dex_pc,
4633 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004634 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004635 uint32_t imt_index)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004636 : HInvoke(allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004637 number_of_arguments,
4638 0u,
4639 return_type,
4640 dex_pc,
4641 dex_method_index,
4642 resolved_method,
4643 kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004644 imt_index_(imt_index) {}
4645
Artem Serovcced8ba2017-07-19 18:18:09 +01004646 bool IsClonable() const OVERRIDE { return true; }
4647
Calin Juravle641547a2015-04-21 22:08:51 +01004648 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004649 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004650 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004651 }
4652
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004653 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
4654 // The assembly stub currently needs it.
4655 return true;
4656 }
4657
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004658 uint32_t GetImtIndex() const { return imt_index_; }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004659
4660 DECLARE_INSTRUCTION(InvokeInterface);
4661
Artem Serovcced8ba2017-07-19 18:18:09 +01004662 protected:
4663 DEFAULT_COPY_CONSTRUCTOR(InvokeInterface);
4664
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004665 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004666 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004667 const uint32_t imt_index_;
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004668};
4669
Vladimir Markofcb503c2016-05-18 12:48:17 +01004670class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004671 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004672 HNeg(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004673 : HUnaryOperation(result_type, input, dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004674 DCHECK_EQ(result_type, DataType::Kind(input->GetType()));
Roland Levillain937e6cd2016-03-22 11:54:37 +00004675 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004676
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004677 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004678
4679 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004680 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004681 }
4682 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004683 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004684 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004685 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4686 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4687 }
4688 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4689 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4690 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004691
Roland Levillain88cb1752014-10-20 16:36:47 +01004692 DECLARE_INSTRUCTION(Neg);
4693
Artem Serovcced8ba2017-07-19 18:18:09 +01004694 protected:
4695 DEFAULT_COPY_CONSTRUCTOR(Neg);
Roland Levillain88cb1752014-10-20 16:36:47 +01004696};
4697
Vladimir Markofcb503c2016-05-18 12:48:17 +01004698class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004699 public:
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004700 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004701 : HExpression(DataType::Type::kReference, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004702 SetRawInputAt(0, cls);
4703 SetRawInputAt(1, length);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004704 }
4705
Artem Serovcced8ba2017-07-19 18:18:09 +01004706 bool IsClonable() const OVERRIDE { return true; }
4707
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004708 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004709 bool NeedsEnvironment() const OVERRIDE { return true; }
4710
Mingyao Yang0c365e62015-03-31 15:09:29 -07004711 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4712 bool CanThrow() const OVERRIDE { return true; }
4713
Calin Juravle10e244f2015-01-26 18:54:32 +00004714 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004715
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004716 HLoadClass* GetLoadClass() const {
4717 DCHECK(InputAt(0)->IsLoadClass());
4718 return InputAt(0)->AsLoadClass();
4719 }
4720
4721 HInstruction* GetLength() const {
4722 return InputAt(1);
4723 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004724
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004725 DECLARE_INSTRUCTION(NewArray);
4726
Artem Serovcced8ba2017-07-19 18:18:09 +01004727 protected:
4728 DEFAULT_COPY_CONSTRUCTOR(NewArray);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004729};
4730
Vladimir Markofcb503c2016-05-18 12:48:17 +01004731class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004732 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004733 HAdd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004734 HInstruction* left,
4735 HInstruction* right,
4736 uint32_t dex_pc = kNoDexPc)
4737 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004738
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004739 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004740
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004741 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004742
4743 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004744 return GetBlock()->GetGraph()->GetIntConstant(
4745 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004746 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004747 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004748 return GetBlock()->GetGraph()->GetLongConstant(
4749 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004750 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004751 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4752 return GetBlock()->GetGraph()->GetFloatConstant(
4753 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4754 }
4755 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4756 return GetBlock()->GetGraph()->GetDoubleConstant(
4757 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4758 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004759
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004760 DECLARE_INSTRUCTION(Add);
4761
Artem Serovcced8ba2017-07-19 18:18:09 +01004762 protected:
4763 DEFAULT_COPY_CONSTRUCTOR(Add);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004764};
4765
Vladimir Markofcb503c2016-05-18 12:48:17 +01004766class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004767 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004768 HSub(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004769 HInstruction* left,
4770 HInstruction* right,
4771 uint32_t dex_pc = kNoDexPc)
4772 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004773
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004774 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004775
4776 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004777 return GetBlock()->GetGraph()->GetIntConstant(
4778 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004779 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004780 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004781 return GetBlock()->GetGraph()->GetLongConstant(
4782 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004783 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004784 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4785 return GetBlock()->GetGraph()->GetFloatConstant(
4786 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4787 }
4788 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4789 return GetBlock()->GetGraph()->GetDoubleConstant(
4790 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4791 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004792
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004793 DECLARE_INSTRUCTION(Sub);
4794
Artem Serovcced8ba2017-07-19 18:18:09 +01004795 protected:
4796 DEFAULT_COPY_CONSTRUCTOR(Sub);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004797};
4798
Vladimir Markofcb503c2016-05-18 12:48:17 +01004799class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004800 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004801 HMul(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004802 HInstruction* left,
4803 HInstruction* right,
4804 uint32_t dex_pc = kNoDexPc)
4805 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004806
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004807 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004808
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004809 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004810
4811 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004812 return GetBlock()->GetGraph()->GetIntConstant(
4813 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004814 }
4815 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004816 return GetBlock()->GetGraph()->GetLongConstant(
4817 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004818 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004819 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4820 return GetBlock()->GetGraph()->GetFloatConstant(
4821 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4822 }
4823 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4824 return GetBlock()->GetGraph()->GetDoubleConstant(
4825 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4826 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004827
4828 DECLARE_INSTRUCTION(Mul);
4829
Artem Serovcced8ba2017-07-19 18:18:09 +01004830 protected:
4831 DEFAULT_COPY_CONSTRUCTOR(Mul);
Calin Juravle34bacdf2014-10-07 20:23:36 +01004832};
4833
Vladimir Markofcb503c2016-05-18 12:48:17 +01004834class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004835 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004836 HDiv(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004837 HInstruction* left,
4838 HInstruction* right,
4839 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004840 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004841
Roland Levillain9867bc72015-08-05 10:21:34 +01004842 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004843 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004844 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004845 // Our graph structure ensures we never have 0 for `y` during
4846 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004847 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004848 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004849 return (y == -1) ? -x : x / y;
4850 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004851
Roland Levillain31dd3d62016-02-16 12:21:02 +00004852 template <typename T>
4853 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004854 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004855 return x / y;
4856 }
4857
Roland Levillain9867bc72015-08-05 10:21:34 +01004858 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004859 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004860 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004861 }
4862 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004863 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004864 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4865 }
4866 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4867 return GetBlock()->GetGraph()->GetFloatConstant(
4868 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4869 }
4870 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4871 return GetBlock()->GetGraph()->GetDoubleConstant(
4872 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004873 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004874
4875 DECLARE_INSTRUCTION(Div);
4876
Artem Serovcced8ba2017-07-19 18:18:09 +01004877 protected:
4878 DEFAULT_COPY_CONSTRUCTOR(Div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00004879};
4880
Vladimir Markofcb503c2016-05-18 12:48:17 +01004881class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004882 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004883 HRem(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004884 HInstruction* left,
4885 HInstruction* right,
4886 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004887 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004888
Roland Levillain9867bc72015-08-05 10:21:34 +01004889 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004890 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004891 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004892 // Our graph structure ensures we never have 0 for `y` during
4893 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004894 DCHECK_NE(y, 0);
4895 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4896 return (y == -1) ? 0 : x % y;
4897 }
4898
Roland Levillain31dd3d62016-02-16 12:21:02 +00004899 template <typename T>
4900 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004901 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004902 return std::fmod(x, y);
4903 }
4904
Roland Levillain9867bc72015-08-05 10:21:34 +01004905 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004906 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004907 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004908 }
4909 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004910 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004911 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004912 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004913 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4914 return GetBlock()->GetGraph()->GetFloatConstant(
4915 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4916 }
4917 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4918 return GetBlock()->GetGraph()->GetDoubleConstant(
4919 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4920 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004921
4922 DECLARE_INSTRUCTION(Rem);
4923
Artem Serovcced8ba2017-07-19 18:18:09 +01004924 protected:
4925 DEFAULT_COPY_CONSTRUCTOR(Rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00004926};
4927
Vladimir Markofcb503c2016-05-18 12:48:17 +01004928class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00004929 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004930 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4931 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004932 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004933 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004934 SetRawInputAt(0, value);
4935 }
4936
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004937 DataType::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004938
Calin Juravled0d48522014-11-04 16:40:20 +00004939 bool CanBeMoved() const OVERRIDE { return true; }
4940
Vladimir Marko372f10e2016-05-17 16:30:10 +01004941 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004942 return true;
4943 }
4944
4945 bool NeedsEnvironment() const OVERRIDE { return true; }
4946 bool CanThrow() const OVERRIDE { return true; }
4947
Calin Juravled0d48522014-11-04 16:40:20 +00004948 DECLARE_INSTRUCTION(DivZeroCheck);
4949
Artem Serovcced8ba2017-07-19 18:18:09 +01004950 protected:
4951 DEFAULT_COPY_CONSTRUCTOR(DivZeroCheck);
Calin Juravled0d48522014-11-04 16:40:20 +00004952};
4953
Vladimir Markofcb503c2016-05-18 12:48:17 +01004954class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004955 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004956 HShl(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004957 HInstruction* value,
4958 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004959 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004960 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004961 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
4962 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004963 }
4964
Roland Levillain5b5b9312016-03-22 14:57:31 +00004965 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004966 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004967 return value << (distance & max_shift_distance);
4968 }
4969
4970 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004971 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004972 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004973 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004974 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004975 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004976 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004977 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004978 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4979 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4980 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4981 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004982 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004983 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4984 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004985 LOG(FATAL) << DebugName() << " is not defined for float values";
4986 UNREACHABLE();
4987 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004988 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4989 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004990 LOG(FATAL) << DebugName() << " is not defined for double values";
4991 UNREACHABLE();
4992 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004993
4994 DECLARE_INSTRUCTION(Shl);
4995
Artem Serovcced8ba2017-07-19 18:18:09 +01004996 protected:
4997 DEFAULT_COPY_CONSTRUCTOR(Shl);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004998};
4999
Vladimir Markofcb503c2016-05-18 12:48:17 +01005000class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005001 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005002 HShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005003 HInstruction* value,
5004 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005005 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00005006 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005007 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5008 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005009 }
5010
Roland Levillain5b5b9312016-03-22 14:57:31 +00005011 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005012 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005013 return value >> (distance & max_shift_distance);
5014 }
5015
5016 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005017 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005018 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005019 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005020 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005021 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005022 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005023 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005024 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5025 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5026 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5027 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005028 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005029 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5030 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005031 LOG(FATAL) << DebugName() << " is not defined for float values";
5032 UNREACHABLE();
5033 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005034 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5035 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005036 LOG(FATAL) << DebugName() << " is not defined for double values";
5037 UNREACHABLE();
5038 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005039
5040 DECLARE_INSTRUCTION(Shr);
5041
Artem Serovcced8ba2017-07-19 18:18:09 +01005042 protected:
5043 DEFAULT_COPY_CONSTRUCTOR(Shr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005044};
5045
Vladimir Markofcb503c2016-05-18 12:48:17 +01005046class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005047 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005048 HUShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005049 HInstruction* value,
5050 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005051 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00005052 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005053 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5054 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00005055 }
5056
Roland Levillain5b5b9312016-03-22 14:57:31 +00005057 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005058 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005059 typedef typename std::make_unsigned<T>::type V;
5060 V ux = static_cast<V>(value);
5061 return static_cast<T>(ux >> (distance & max_shift_distance));
5062 }
5063
5064 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005065 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005066 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005067 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005068 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005069 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005070 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005071 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005072 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5073 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5074 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5075 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00005076 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005077 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5078 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005079 LOG(FATAL) << DebugName() << " is not defined for float values";
5080 UNREACHABLE();
5081 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005082 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5083 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005084 LOG(FATAL) << DebugName() << " is not defined for double values";
5085 UNREACHABLE();
5086 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005087
5088 DECLARE_INSTRUCTION(UShr);
5089
Artem Serovcced8ba2017-07-19 18:18:09 +01005090 protected:
5091 DEFAULT_COPY_CONSTRUCTOR(UShr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005092};
5093
Vladimir Markofcb503c2016-05-18 12:48:17 +01005094class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005095 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005096 HAnd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005097 HInstruction* left,
5098 HInstruction* right,
5099 uint32_t dex_pc = kNoDexPc)
5100 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005101
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005102 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005103
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005104 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005105
5106 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005107 return GetBlock()->GetGraph()->GetIntConstant(
5108 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005109 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005110 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005111 return GetBlock()->GetGraph()->GetLongConstant(
5112 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005113 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005114 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5115 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5116 LOG(FATAL) << DebugName() << " is not defined for float values";
5117 UNREACHABLE();
5118 }
5119 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5120 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5121 LOG(FATAL) << DebugName() << " is not defined for double values";
5122 UNREACHABLE();
5123 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005124
5125 DECLARE_INSTRUCTION(And);
5126
Artem Serovcced8ba2017-07-19 18:18:09 +01005127 protected:
5128 DEFAULT_COPY_CONSTRUCTOR(And);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005129};
5130
Vladimir Markofcb503c2016-05-18 12:48:17 +01005131class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005132 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005133 HOr(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005134 HInstruction* left,
5135 HInstruction* right,
5136 uint32_t dex_pc = kNoDexPc)
5137 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005138
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005139 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005140
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005141 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005142
5143 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005144 return GetBlock()->GetGraph()->GetIntConstant(
5145 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005146 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005147 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005148 return GetBlock()->GetGraph()->GetLongConstant(
5149 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005150 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005151 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5152 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5153 LOG(FATAL) << DebugName() << " is not defined for float values";
5154 UNREACHABLE();
5155 }
5156 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5157 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5158 LOG(FATAL) << DebugName() << " is not defined for double values";
5159 UNREACHABLE();
5160 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005161
5162 DECLARE_INSTRUCTION(Or);
5163
Artem Serovcced8ba2017-07-19 18:18:09 +01005164 protected:
5165 DEFAULT_COPY_CONSTRUCTOR(Or);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005166};
5167
Vladimir Markofcb503c2016-05-18 12:48:17 +01005168class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005169 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005170 HXor(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005171 HInstruction* left,
5172 HInstruction* right,
5173 uint32_t dex_pc = kNoDexPc)
5174 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005175
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005176 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005177
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005178 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005179
5180 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005181 return GetBlock()->GetGraph()->GetIntConstant(
5182 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005183 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005184 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005185 return GetBlock()->GetGraph()->GetLongConstant(
5186 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005187 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005188 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5189 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5190 LOG(FATAL) << DebugName() << " is not defined for float values";
5191 UNREACHABLE();
5192 }
5193 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5194 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5195 LOG(FATAL) << DebugName() << " is not defined for double values";
5196 UNREACHABLE();
5197 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005198
5199 DECLARE_INSTRUCTION(Xor);
5200
Artem Serovcced8ba2017-07-19 18:18:09 +01005201 protected:
5202 DEFAULT_COPY_CONSTRUCTOR(Xor);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005203};
5204
Vladimir Markofcb503c2016-05-18 12:48:17 +01005205class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005206 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005207 HRor(DataType::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00005208 : HBinaryOperation(result_type, value, distance) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005209 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5210 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00005211 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005212
Roland Levillain5b5b9312016-03-22 14:57:31 +00005213 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005214 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005215 typedef typename std::make_unsigned<T>::type V;
5216 V ux = static_cast<V>(value);
5217 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005218 return static_cast<T>(ux);
5219 } else {
5220 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005221 return static_cast<T>(ux >> (distance & max_shift_value)) |
5222 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005223 }
5224 }
5225
Roland Levillain5b5b9312016-03-22 14:57:31 +00005226 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005227 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005228 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005229 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005230 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005231 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005232 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005233 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005234 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5235 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5236 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5237 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005238 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005239 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5240 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005241 LOG(FATAL) << DebugName() << " is not defined for float values";
5242 UNREACHABLE();
5243 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005244 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5245 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005246 LOG(FATAL) << DebugName() << " is not defined for double values";
5247 UNREACHABLE();
5248 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005249
5250 DECLARE_INSTRUCTION(Ror);
5251
Artem Serovcced8ba2017-07-19 18:18:09 +01005252 protected:
5253 DEFAULT_COPY_CONSTRUCTOR(Ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005254};
5255
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005256// The value of a parameter in this method. Its location depends on
5257// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01005258class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005259 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005260 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005261 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005262 uint8_t index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005263 DataType::Type parameter_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005264 bool is_this = false)
5265 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005266 dex_file_(dex_file),
5267 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005268 index_(index) {
5269 SetPackedFlag<kFlagIsThis>(is_this);
5270 SetPackedFlag<kFlagCanBeNull>(!is_this);
5271 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005272
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005273 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005274 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005275 uint8_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07005276 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005277
Vladimir Markoa1de9182016-02-25 11:37:38 +00005278 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
5279 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005280
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005281 DECLARE_INSTRUCTION(ParameterValue);
5282
Artem Serovcced8ba2017-07-19 18:18:09 +01005283 protected:
5284 DEFAULT_COPY_CONSTRUCTOR(ParameterValue);
5285
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005286 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005287 // Whether or not the parameter value corresponds to 'this' argument.
5288 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
5289 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5290 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5291 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5292 "Too many packed fields.");
5293
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005294 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005295 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005296 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005297 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005298 const uint8_t index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005299};
5300
Vladimir Markofcb503c2016-05-18 12:48:17 +01005301class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005302 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005303 HNot(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005304 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005305
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005306 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005307 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005308 return true;
5309 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005310
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005311 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005312
5313 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005314 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005315 }
5316 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005317 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005318 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005319 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5320 LOG(FATAL) << DebugName() << " is not defined for float values";
5321 UNREACHABLE();
5322 }
5323 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5324 LOG(FATAL) << DebugName() << " is not defined for double values";
5325 UNREACHABLE();
5326 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005327
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005328 DECLARE_INSTRUCTION(Not);
5329
Artem Serovcced8ba2017-07-19 18:18:09 +01005330 protected:
5331 DEFAULT_COPY_CONSTRUCTOR(Not);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005332};
5333
Vladimir Markofcb503c2016-05-18 12:48:17 +01005334class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005335 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005336 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005337 : HUnaryOperation(DataType::Type::kBool, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01005338
5339 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005340 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01005341 return true;
5342 }
5343
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005344 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005345 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005346 return !x;
5347 }
5348
Roland Levillain9867bc72015-08-05 10:21:34 +01005349 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005350 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005351 }
5352 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5353 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005354 UNREACHABLE();
5355 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005356 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5357 LOG(FATAL) << DebugName() << " is not defined for float values";
5358 UNREACHABLE();
5359 }
5360 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5361 LOG(FATAL) << DebugName() << " is not defined for double values";
5362 UNREACHABLE();
5363 }
David Brazdil66d126e2015-04-03 16:02:44 +01005364
5365 DECLARE_INSTRUCTION(BooleanNot);
5366
Artem Serovcced8ba2017-07-19 18:18:09 +01005367 protected:
5368 DEFAULT_COPY_CONSTRUCTOR(BooleanNot);
David Brazdil66d126e2015-04-03 16:02:44 +01005369};
5370
Vladimir Markofcb503c2016-05-18 12:48:17 +01005371class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005372 public:
5373 // Instantiate a type conversion of `input` to `result_type`.
Vladimir Markoc8fb2112017-10-03 11:37:52 +01005374 HTypeConversion(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005375 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005376 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005377 // Invariant: We should never generate a conversion to a Boolean value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005378 DCHECK_NE(DataType::Type::kBool, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005379 }
5380
5381 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005382 DataType::Type GetInputType() const { return GetInput()->GetType(); }
5383 DataType::Type GetResultType() const { return GetType(); }
Roland Levillaindff1f282014-11-05 14:15:05 +00005384
5385 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005386 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5387 return true;
5388 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005389
Mark Mendelle82549b2015-05-06 10:55:34 -04005390 // Try to statically evaluate the conversion and return a HConstant
5391 // containing the result. If the input cannot be converted, return nullptr.
5392 HConstant* TryStaticEvaluation() const;
5393
Roland Levillaindff1f282014-11-05 14:15:05 +00005394 DECLARE_INSTRUCTION(TypeConversion);
5395
Artem Serovcced8ba2017-07-19 18:18:09 +01005396 protected:
5397 DEFAULT_COPY_CONSTRUCTOR(TypeConversion);
Roland Levillaindff1f282014-11-05 14:15:05 +00005398};
5399
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005400static constexpr uint32_t kNoRegNumber = -1;
5401
Vladimir Markofcb503c2016-05-18 12:48:17 +01005402class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005403 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005404 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
5405 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005406 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005407 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005408 SetRawInputAt(0, value);
5409 }
5410
Artem Serovcced8ba2017-07-19 18:18:09 +01005411 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00005412 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005413 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005414 return true;
5415 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005416
Calin Juravle10e244f2015-01-26 18:54:32 +00005417 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005418
Calin Juravle10e244f2015-01-26 18:54:32 +00005419 bool CanThrow() const OVERRIDE { return true; }
5420
5421 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005422
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005423 DECLARE_INSTRUCTION(NullCheck);
5424
Artem Serovcced8ba2017-07-19 18:18:09 +01005425 protected:
5426 DEFAULT_COPY_CONSTRUCTOR(NullCheck);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005427};
5428
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005429// Embeds an ArtField and all the information required by the compiler. We cache
5430// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005431class FieldInfo : public ValueObject {
5432 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005433 FieldInfo(ArtField* field,
5434 MemberOffset field_offset,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005435 DataType::Type field_type,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005436 bool is_volatile,
5437 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005438 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005439 const DexFile& dex_file)
5440 : field_(field),
5441 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005442 field_type_(field_type),
5443 is_volatile_(is_volatile),
5444 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005445 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005446 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005447
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005448 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005449 MemberOffset GetFieldOffset() const { return field_offset_; }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005450 DataType::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005451 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005452 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005453 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005454 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005455
5456 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005457 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005458 const MemberOffset field_offset_;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005459 const DataType::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005460 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005461 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005462 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005463 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005464};
5465
Vladimir Markofcb503c2016-05-18 12:48:17 +01005466class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005467 public:
5468 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005469 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005470 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005471 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005472 bool is_volatile,
5473 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005474 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005475 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005476 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005477 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005478 field_info_(field,
5479 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005480 field_type,
5481 is_volatile,
5482 field_idx,
5483 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005484 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005485 SetRawInputAt(0, value);
5486 }
5487
Artem Serovcced8ba2017-07-19 18:18:09 +01005488 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005489 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005490
Vladimir Marko372f10e2016-05-17 16:30:10 +01005491 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5492 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005493 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005494 }
5495
Calin Juravle641547a2015-04-21 22:08:51 +01005496 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005497 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005498 }
5499
5500 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005501 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5502 }
5503
Calin Juravle52c48962014-12-16 17:02:57 +00005504 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005505 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005506 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005507 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005508
Vladimir Marko61b92282017-10-11 13:23:17 +01005509 void SetType(DataType::Type new_type) {
5510 DCHECK(DataType::IsIntegralType(GetType()));
5511 DCHECK(DataType::IsIntegralType(new_type));
5512 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5513 SetPackedField<TypeField>(new_type);
5514 }
5515
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005516 DECLARE_INSTRUCTION(InstanceFieldGet);
5517
Artem Serovcced8ba2017-07-19 18:18:09 +01005518 protected:
5519 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldGet);
5520
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005521 private:
5522 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005523};
5524
Vladimir Markofcb503c2016-05-18 12:48:17 +01005525class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005526 public:
5527 HInstanceFieldSet(HInstruction* object,
5528 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005529 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005530 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005531 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005532 bool is_volatile,
5533 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005534 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005535 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005536 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005537 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005538 field_info_(field,
5539 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005540 field_type,
5541 is_volatile,
5542 field_idx,
5543 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005544 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005545 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005546 SetRawInputAt(0, object);
5547 SetRawInputAt(1, value);
5548 }
5549
Artem Serovcced8ba2017-07-19 18:18:09 +01005550 bool IsClonable() const OVERRIDE { return true; }
5551
Calin Juravle641547a2015-04-21 22:08:51 +01005552 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005553 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005554 }
5555
Calin Juravle52c48962014-12-16 17:02:57 +00005556 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005557 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005558 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005559 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005560 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005561 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5562 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005563
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005564 DECLARE_INSTRUCTION(InstanceFieldSet);
5565
Artem Serovcced8ba2017-07-19 18:18:09 +01005566 protected:
5567 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldSet);
5568
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005569 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005570 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5571 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5572 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5573 "Too many packed fields.");
5574
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005575 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005576};
5577
Vladimir Markofcb503c2016-05-18 12:48:17 +01005578class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005579 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005580 HArrayGet(HInstruction* array,
5581 HInstruction* index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005582 DataType::Type type,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005583 uint32_t dex_pc)
5584 : HArrayGet(array,
5585 index,
5586 type,
5587 SideEffects::ArrayReadOfType(type),
5588 dex_pc,
5589 /* is_string_char_at */ false) {}
5590
5591 HArrayGet(HInstruction* array,
5592 HInstruction* index,
5593 DataType::Type type,
5594 SideEffects side_effects,
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005595 uint32_t dex_pc,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005596 bool is_string_char_at)
5597 : HExpression(type, side_effects, dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005598 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005599 SetRawInputAt(0, array);
5600 SetRawInputAt(1, index);
5601 }
5602
Artem Serovcced8ba2017-07-19 18:18:09 +01005603 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005604 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005605 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005606 return true;
5607 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005608 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005609 // TODO: We can be smarter here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01005610 // Currently, unless the array is the result of NewArray, the array access is always
5611 // preceded by some form of null NullCheck necessary for the bounds check, usually
5612 // implicit null check on the ArrayLength input to BoundsCheck or Deoptimize for
5613 // dynamic BCE. There are cases when these could be removed to produce better code.
5614 // If we ever add optimizations to do so we should allow an implicit check here
5615 // (as long as the address falls in the first page).
5616 //
5617 // As an example of such fancy optimization, we could eliminate BoundsCheck for
5618 // a = cond ? new int[1] : null;
5619 // a[0]; // The Phi does not need bounds check for either input.
Calin Juravle77520bc2015-01-12 18:45:46 +00005620 return false;
5621 }
5622
David Brazdil4833f5a2015-12-16 10:37:39 +00005623 bool IsEquivalentOf(HArrayGet* other) const {
5624 bool result = (GetDexPc() == other->GetDexPc());
5625 if (kIsDebugBuild && result) {
5626 DCHECK_EQ(GetBlock(), other->GetBlock());
5627 DCHECK_EQ(GetArray(), other->GetArray());
5628 DCHECK_EQ(GetIndex(), other->GetIndex());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005629 if (DataType::IsIntOrLongType(GetType())) {
5630 DCHECK(DataType::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005631 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005632 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
5633 DCHECK(DataType::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005634 }
5635 }
5636 return result;
5637 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005638
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005639 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5640
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005641 HInstruction* GetArray() const { return InputAt(0); }
5642 HInstruction* GetIndex() const { return InputAt(1); }
5643
Vladimir Marko61b92282017-10-11 13:23:17 +01005644 void SetType(DataType::Type new_type) {
5645 DCHECK(DataType::IsIntegralType(GetType()));
5646 DCHECK(DataType::IsIntegralType(new_type));
5647 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5648 SetPackedField<TypeField>(new_type);
5649 }
5650
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005651 DECLARE_INSTRUCTION(ArrayGet);
5652
Artem Serovcced8ba2017-07-19 18:18:09 +01005653 protected:
5654 DEFAULT_COPY_CONSTRUCTOR(ArrayGet);
5655
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005656 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005657 // We treat a String as an array, creating the HArrayGet from String.charAt()
5658 // intrinsic in the instruction simplifier. We can always determine whether
5659 // a particular HArrayGet is actually a String.charAt() by looking at the type
5660 // of the input but that requires holding the mutator lock, so we prefer to use
5661 // a flag, so that code generators don't need to do the locking.
5662 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
5663 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5664 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5665 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005666};
5667
Vladimir Markofcb503c2016-05-18 12:48:17 +01005668class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005669 public:
5670 HArraySet(HInstruction* array,
5671 HInstruction* index,
5672 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005673 DataType::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005674 uint32_t dex_pc)
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005675 : HArraySet(array,
5676 index,
5677 value,
5678 expected_component_type,
5679 // Make a best guess for side effects now, may be refined during SSA building.
5680 ComputeSideEffects(GetComponentType(value->GetType(), expected_component_type)),
5681 dex_pc) {}
5682
5683 HArraySet(HInstruction* array,
5684 HInstruction* index,
5685 HInstruction* value,
5686 DataType::Type expected_component_type,
5687 SideEffects side_effects,
5688 uint32_t dex_pc)
5689 : HTemplateInstruction(side_effects, dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005690 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005691 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005692 SetPackedFlag<kFlagValueCanBeNull>(true);
5693 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005694 SetRawInputAt(0, array);
5695 SetRawInputAt(1, index);
5696 SetRawInputAt(2, value);
5697 }
5698
Artem Serovcced8ba2017-07-19 18:18:09 +01005699 bool IsClonable() const OVERRIDE { return true; }
5700
Calin Juravle77520bc2015-01-12 18:45:46 +00005701 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005702 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005703 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005704 }
5705
Mingyao Yang81014cb2015-06-02 03:16:27 -07005706 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005707 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005708
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005709 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005710 // TODO: Same as for ArrayGet.
5711 return false;
5712 }
5713
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005714 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005715 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005716 }
5717
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005718 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005719 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005720 }
5721
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005722 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005723 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005724 }
5725
Vladimir Markoa1de9182016-02-25 11:37:38 +00005726 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5727 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5728 bool StaticTypeOfArrayIsObjectArray() const {
5729 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5730 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005731
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005732 HInstruction* GetArray() const { return InputAt(0); }
5733 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005734 HInstruction* GetValue() const { return InputAt(2); }
5735
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005736 DataType::Type GetComponentType() const {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005737 return GetComponentType(GetValue()->GetType(), GetRawExpectedComponentType());
5738 }
5739
5740 static DataType::Type GetComponentType(DataType::Type value_type,
5741 DataType::Type expected_component_type) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005742 // The Dex format does not type floating point index operations. Since the
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005743 // `expected_component_type` comes from SSA building and can therefore not
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005744 // be correct, we also check what is the value type. If it is a floating
5745 // point type, we must use that type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005746 return ((value_type == DataType::Type::kFloat32) || (value_type == DataType::Type::kFloat64))
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005747 ? value_type
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005748 : expected_component_type;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005749 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005750
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005751 DataType::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005752 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005753 }
5754
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005755 static SideEffects ComputeSideEffects(DataType::Type type) {
5756 return SideEffects::ArrayWriteOfType(type).Union(SideEffectsForArchRuntimeCalls(type));
Aart Bik18b36ab2016-04-13 16:41:35 -07005757 }
5758
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005759 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type value_type) {
5760 return (value_type == DataType::Type::kReference) ? SideEffects::CanTriggerGC()
5761 : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005762 }
5763
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005764 DECLARE_INSTRUCTION(ArraySet);
5765
Artem Serovcced8ba2017-07-19 18:18:09 +01005766 protected:
5767 DEFAULT_COPY_CONSTRUCTOR(ArraySet);
5768
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005769 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005770 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5771 static constexpr size_t kFieldExpectedComponentTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005772 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00005773 static constexpr size_t kFlagNeedsTypeCheck =
5774 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5775 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005776 // Cached information for the reference_type_info_ so that codegen
5777 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005778 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5779 static constexpr size_t kNumberOfArraySetPackedBits =
5780 kFlagStaticTypeOfArrayIsObjectArray + 1;
5781 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5782 using ExpectedComponentTypeField =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005783 BitField<DataType::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005784};
5785
Vladimir Markofcb503c2016-05-18 12:48:17 +01005786class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005787 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005788 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005789 : HExpression(DataType::Type::kInt32, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005790 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005791 // Note that arrays do not change length, so the instruction does not
5792 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005793 SetRawInputAt(0, array);
5794 }
5795
Artem Serovcced8ba2017-07-19 18:18:09 +01005796 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle77520bc2015-01-12 18:45:46 +00005797 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005798 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005799 return true;
5800 }
Calin Juravle641547a2015-04-21 22:08:51 +01005801 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5802 return obj == InputAt(0);
5803 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005804
Vladimir Markodce016e2016-04-28 13:10:02 +01005805 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5806
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005807 DECLARE_INSTRUCTION(ArrayLength);
5808
Artem Serovcced8ba2017-07-19 18:18:09 +01005809 protected:
5810 DEFAULT_COPY_CONSTRUCTOR(ArrayLength);
5811
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005812 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005813 // We treat a String as an array, creating the HArrayLength from String.length()
5814 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5815 // determine whether a particular HArrayLength is actually a String.length() by
5816 // looking at the type of the input but that requires holding the mutator lock, so
5817 // we prefer to use a flag, so that code generators don't need to do the locking.
5818 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5819 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5820 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5821 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005822};
5823
Vladimir Markofcb503c2016-05-18 12:48:17 +01005824class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005825 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005826 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5827 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005828 HBoundsCheck(HInstruction* index,
5829 HInstruction* length,
5830 uint32_t dex_pc,
Vladimir Marko0259c242017-12-04 11:27:47 +00005831 bool is_string_char_at = false)
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005832 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005833 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(index->GetType()));
Vladimir Marko0259c242017-12-04 11:27:47 +00005834 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005835 SetRawInputAt(0, index);
5836 SetRawInputAt(1, length);
5837 }
5838
Artem Serovcced8ba2017-07-19 18:18:09 +01005839 bool IsClonable() const OVERRIDE { return true; }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005840 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005841 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005842 return true;
5843 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005844
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005845 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005846
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005847 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005848
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005849 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005850
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005851 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005852
5853 DECLARE_INSTRUCTION(BoundsCheck);
5854
Artem Serovcced8ba2017-07-19 18:18:09 +01005855 protected:
5856 DEFAULT_COPY_CONSTRUCTOR(BoundsCheck);
5857
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005858 private:
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005859 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005860};
5861
Vladimir Markofcb503c2016-05-18 12:48:17 +01005862class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005863 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005864 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005865 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005866
Artem Serovcced8ba2017-07-19 18:18:09 +01005867 bool IsClonable() const OVERRIDE { return true; }
5868
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005869 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005870 return true;
5871 }
5872
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005873 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5874 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005875
5876 DECLARE_INSTRUCTION(SuspendCheck);
5877
Artem Serovcced8ba2017-07-19 18:18:09 +01005878 protected:
5879 DEFAULT_COPY_CONSTRUCTOR(SuspendCheck);
5880
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005881 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005882 // Only used for code generation, in order to share the same slow path between back edges
5883 // of a same loop.
5884 SlowPathCode* slow_path_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005885};
5886
David Srbecky0cf44932015-12-09 14:09:59 +00005887// Pseudo-instruction which provides the native debugger with mapping information.
5888// It ensures that we can generate line number and local variables at this point.
5889class HNativeDebugInfo : public HTemplateInstruction<0> {
5890 public:
5891 explicit HNativeDebugInfo(uint32_t dex_pc)
5892 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5893
5894 bool NeedsEnvironment() const OVERRIDE {
5895 return true;
5896 }
5897
5898 DECLARE_INSTRUCTION(NativeDebugInfo);
5899
Artem Serovcced8ba2017-07-19 18:18:09 +01005900 protected:
5901 DEFAULT_COPY_CONSTRUCTOR(NativeDebugInfo);
David Srbecky0cf44932015-12-09 14:09:59 +00005902};
5903
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005904/**
5905 * Instruction to load a Class object.
5906 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005907class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005908 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005909 // Determines how to load the Class.
5910 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005911 // We cannot load this class. See HSharpening::SharpenLoadClass.
5912 kInvalid = -1,
5913
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005914 // Use the Class* from the method's own ArtMethod*.
5915 kReferrersClass,
5916
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005917 // Use PC-relative boot image Class* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01005918 // Used for boot image classes referenced by boot image code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005919 kBootImageLinkTimePcRelative,
5920
5921 // Use a known boot image Class* address, embedded in the code by the codegen.
5922 // Used for boot image classes referenced by apps in AOT- and JIT-compiled code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005923 kBootImageAddress,
5924
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005925 // Use a PC-relative load from a boot image ClassTable mmapped into the .bss
5926 // of the oat file.
5927 kBootImageClassTable,
5928
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005929 // Load from an entry in the .bss section using a PC-relative load.
5930 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
5931 kBssEntry,
5932
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005933 // Load from the root table associated with the JIT compiled method.
5934 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005935
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005936 // Load using a simple runtime call. This is the fall-back load kind when
5937 // the codegen is unable to use another appropriate kind.
5938 kRuntimeCall,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005939
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005940 kLast = kRuntimeCall
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005941 };
5942
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005943 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005944 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005945 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005946 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005947 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005948 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00005949 bool needs_access_check)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005950 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5951 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005952 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005953 dex_file_(dex_file),
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00005954 klass_(klass),
5955 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005956 // Referrers class should not need access check. We never inline unverified
5957 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005958 DCHECK(!is_referrers_class || !needs_access_check);
5959
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005960 SetPackedField<LoadKindField>(
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005961 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kRuntimeCall);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005962 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00005963 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005964 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005965 }
5966
Artem Serovcced8ba2017-07-19 18:18:09 +01005967 bool IsClonable() const OVERRIDE { return true; }
5968
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005969 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005970
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005971 LoadKind GetLoadKind() const {
5972 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005973 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005974
5975 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005976
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005977 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005978
Andreas Gampea5b09a62016-11-17 15:21:22 -08005979 size_t ComputeHashCode() const OVERRIDE { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005980
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005981 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005982
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005983 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005984 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005985 }
5986
Calin Juravle0ba218d2015-05-19 18:46:01 +01005987 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005988 // The entrypoint the code generator is going to call does not do
5989 // clinit of the class.
5990 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005991 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005992 }
5993
5994 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00005995 return NeedsAccessCheck() ||
5996 MustGenerateClinitCheck() ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005997 GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005998 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01005999 }
6000
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006001 bool CanThrow() const OVERRIDE {
Vladimir Marko41559982017-01-06 14:04:23 +00006002 return NeedsAccessCheck() ||
6003 MustGenerateClinitCheck() ||
6004 // If the class is in the boot image, the lookup in the runtime call cannot throw.
6005 // This keeps CanThrow() consistent between non-PIC (using kBootImageAddress) and
6006 // PIC and subsequently avoids a DCE behavior dependency on the PIC option.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006007 ((GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006008 GetLoadKind() == LoadKind::kBssEntry) &&
6009 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006010 }
6011
Calin Juravleacf735c2015-02-12 15:25:22 +00006012 ReferenceTypeInfo GetLoadedClassRTI() {
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006013 return loaded_class_rti_;
Calin Juravleacf735c2015-02-12 15:25:22 +00006014 }
6015
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006016 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
6017 // Make sure we only set exact types (the loaded class should never be merged).
6018 DCHECK(rti.IsExact());
6019 loaded_class_rti_ = rti;
Calin Juravleacf735c2015-02-12 15:25:22 +00006020 }
6021
Andreas Gampea5b09a62016-11-17 15:21:22 -08006022 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006023 const DexFile& GetDexFile() const { return dex_file_; }
6024
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006025 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006026 return GetLoadKind() == LoadKind::kRuntimeCall;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006027 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00006028
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006029 static SideEffects SideEffectsForArchRuntimeCalls() {
6030 return SideEffects::CanTriggerGC();
6031 }
6032
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006033 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006034 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006035 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006036 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006037
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006038 void MarkInBootImage() {
6039 SetPackedFlag<kFlagIsInBootImage>(true);
6040 }
6041
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006042 void AddSpecialInput(HInstruction* special_input);
6043
6044 using HInstruction::GetInputRecords; // Keep the const version visible.
6045 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
6046 return ArrayRef<HUserRecord<HInstruction*>>(
6047 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6048 }
6049
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006050 DataType::Type GetType() const OVERRIDE {
6051 return DataType::Type::kReference;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006052 }
6053
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006054 Handle<mirror::Class> GetClass() const {
6055 return klass_;
6056 }
6057
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006058 DECLARE_INSTRUCTION(LoadClass);
6059
Artem Serovcced8ba2017-07-19 18:18:09 +01006060 protected:
6061 DEFAULT_COPY_CONSTRUCTOR(LoadClass);
6062
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006063 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006064 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00006065 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006066 // Whether this instruction must generate the initialization check.
6067 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006068 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006069 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
6070 static constexpr size_t kFieldLoadKindSize =
6071 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006072 static constexpr size_t kNumberOfLoadClassPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006073 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006074 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
6075
6076 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006077 return load_kind == LoadKind::kReferrersClass ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006078 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006079 load_kind == LoadKind::kBootImageClassTable ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006080 load_kind == LoadKind::kBssEntry ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006081 load_kind == LoadKind::kRuntimeCall;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006082 }
6083
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006084 void SetLoadKindInternal(LoadKind load_kind);
6085
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006086 // The special input is the HCurrentMethod for kRuntimeCall or kReferrersClass.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006087 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006088 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006089 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006090
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006091 // A type index and dex file where the class can be accessed. The dex file can be:
6092 // - The compiling method's dex file if the class is defined there too.
6093 // - The compiling method's dex file if the class is referenced there.
6094 // - The dex file where the class is defined. When the load kind can only be
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006095 // kBssEntry or kRuntimeCall, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08006096 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006097 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006098
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006099 Handle<mirror::Class> klass_;
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006100
6101 ReferenceTypeInfo loaded_class_rti_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006102};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006103std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
6104
6105// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006106inline void HLoadClass::SetLoadKind(LoadKind load_kind) {
6107 // The load kind should be determined before inserting the instruction to the graph.
6108 DCHECK(GetBlock() == nullptr);
6109 DCHECK(GetEnvironment() == nullptr);
6110 SetPackedField<LoadKindField>(load_kind);
6111 if (load_kind != LoadKind::kRuntimeCall && load_kind != LoadKind::kReferrersClass) {
6112 special_input_ = HUserRecord<HInstruction*>(nullptr);
6113 }
6114 if (!NeedsEnvironment()) {
6115 SetSideEffects(SideEffects::None());
6116 }
6117}
6118
6119// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006120inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006121 // The special input is used for PC-relative loads on some architectures,
6122 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006123 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006124 GetLoadKind() == LoadKind::kBootImageAddress ||
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006125 GetLoadKind() == LoadKind::kBootImageClassTable ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006126 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006127 DCHECK(special_input_.GetInstruction() == nullptr);
6128 special_input_ = HUserRecord<HInstruction*>(special_input);
6129 special_input->AddUseAt(this, 0);
6130}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006131
Vladimir Marko372f10e2016-05-17 16:30:10 +01006132class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006133 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006134 // Determines how to load the String.
6135 enum class LoadKind {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006136 // Use PC-relative boot image String* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006137 // Used for boot image strings referenced by boot image code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006138 kBootImageLinkTimePcRelative,
6139
6140 // Use a known boot image String* address, embedded in the code by the codegen.
6141 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006142 kBootImageAddress,
6143
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006144 // Use a PC-relative load from a boot image InternTable mmapped into the .bss
6145 // of the oat file.
6146 kBootImageInternTable,
6147
Vladimir Markoaad75c62016-10-03 08:46:48 +00006148 // Load from an entry in the .bss section using a PC-relative load.
6149 // Used for strings outside boot image when .bss is accessible with a PC-relative load.
6150 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006151
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006152 // Load from the root table associated with the JIT compiled method.
6153 kJitTableAddress,
6154
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006155 // Load using a simple runtime call. This is the fall-back load kind when
6156 // the codegen is unable to use another appropriate kind.
6157 kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006158
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006159 kLast = kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006160 };
6161
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006162 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006163 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006164 const DexFile& dex_file,
6165 uint32_t dex_pc)
Vladimir Marko372f10e2016-05-17 16:30:10 +01006166 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
6167 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006168 string_index_(string_index),
6169 dex_file_(dex_file) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006170 SetPackedField<LoadKindField>(LoadKind::kRuntimeCall);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006171 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006172
Artem Serovcced8ba2017-07-19 18:18:09 +01006173 bool IsClonable() const OVERRIDE { return true; }
6174
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006175 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006176
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006177 LoadKind GetLoadKind() const {
6178 return GetPackedField<LoadKindField>();
6179 }
6180
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006181 const DexFile& GetDexFile() const {
6182 return dex_file_;
6183 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006184
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006185 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006186 return string_index_;
6187 }
6188
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006189 Handle<mirror::String> GetString() const {
6190 return string_;
6191 }
6192
6193 void SetString(Handle<mirror::String> str) {
6194 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006195 }
6196
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006197 bool CanBeMoved() const OVERRIDE { return true; }
6198
Vladimir Marko372f10e2016-05-17 16:30:10 +01006199 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006200
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006201 size_t ComputeHashCode() const OVERRIDE { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006202
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006203 // Will call the runtime if we need to load the string through
6204 // the dex cache and the string is not guaranteed to be there yet.
6205 bool NeedsEnvironment() const OVERRIDE {
6206 LoadKind load_kind = GetLoadKind();
Vladimir Marko764d4542017-05-16 10:31:41 +01006207 if (load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006208 load_kind == LoadKind::kBootImageAddress ||
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006209 load_kind == LoadKind::kBootImageInternTable ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006210 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006211 return false;
6212 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006213 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006214 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006215
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006216 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006217 return GetLoadKind() == LoadKind::kRuntimeCall;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006218 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006219
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07006220 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006221 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006222
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006223 static SideEffects SideEffectsForArchRuntimeCalls() {
6224 return SideEffects::CanTriggerGC();
6225 }
6226
Vladimir Marko372f10e2016-05-17 16:30:10 +01006227 void AddSpecialInput(HInstruction* special_input);
6228
6229 using HInstruction::GetInputRecords; // Keep the const version visible.
6230 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
6231 return ArrayRef<HUserRecord<HInstruction*>>(
6232 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006233 }
6234
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006235 DataType::Type GetType() const OVERRIDE {
6236 return DataType::Type::kReference;
Vladimir Marko372f10e2016-05-17 16:30:10 +01006237 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006238
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006239 DECLARE_INSTRUCTION(LoadString);
6240
Artem Serovcced8ba2017-07-19 18:18:09 +01006241 protected:
6242 DEFAULT_COPY_CONSTRUCTOR(LoadString);
6243
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006244 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006245 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006246 static constexpr size_t kFieldLoadKindSize =
6247 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
6248 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006249 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006250 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006251
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006252 void SetLoadKindInternal(LoadKind load_kind);
6253
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006254 // The special input is the HCurrentMethod for kRuntimeCall.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006255 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00006256 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01006257 HUserRecord<HInstruction*> special_input_;
6258
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006259 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006260 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006261
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006262 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006263};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006264std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
6265
6266// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006267inline void HLoadString::SetLoadKind(LoadKind load_kind) {
6268 // The load kind should be determined before inserting the instruction to the graph.
6269 DCHECK(GetBlock() == nullptr);
6270 DCHECK(GetEnvironment() == nullptr);
6271 DCHECK_EQ(GetLoadKind(), LoadKind::kRuntimeCall);
6272 SetPackedField<LoadKindField>(load_kind);
6273 if (load_kind != LoadKind::kRuntimeCall) {
6274 special_input_ = HUserRecord<HInstruction*>(nullptr);
6275 }
6276 if (!NeedsEnvironment()) {
6277 SetSideEffects(SideEffects::None());
6278 }
6279}
6280
6281// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006282inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006283 // The special input is used for PC-relative loads on some architectures,
6284 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006285 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006286 GetLoadKind() == LoadKind::kBootImageAddress ||
6287 GetLoadKind() == LoadKind::kBootImageInternTable ||
6288 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01006289 // HLoadString::GetInputRecords() returns an empty array at this point,
6290 // so use the GetInputRecords() from the base class to set the input record.
6291 DCHECK(special_input_.GetInstruction() == nullptr);
6292 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006293 special_input->AddUseAt(this, 0);
6294}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006295
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006296/**
6297 * Performs an initialization check on its Class object input.
6298 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01006299class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006300 public:
Roland Levillain3887c462015-08-12 18:15:42 +01006301 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07006302 : HExpression(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006303 DataType::Type::kReference,
Mingyao Yang217eb062017-12-11 15:20:07 -08006304 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006305 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006306 SetRawInputAt(0, constant);
6307 }
6308
Artem Serovcced8ba2017-07-19 18:18:09 +01006309 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006310 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006311 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006312 return true;
6313 }
6314
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006315 bool NeedsEnvironment() const OVERRIDE {
6316 // May call runtime to initialize the class.
6317 return true;
6318 }
6319
Nicolas Geoffray729645a2015-11-19 13:29:02 +00006320 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006321
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006322 HLoadClass* GetLoadClass() const {
6323 DCHECK(InputAt(0)->IsLoadClass());
6324 return InputAt(0)->AsLoadClass();
6325 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006326
6327 DECLARE_INSTRUCTION(ClinitCheck);
6328
Artem Serovcced8ba2017-07-19 18:18:09 +01006329
6330 protected:
6331 DEFAULT_COPY_CONSTRUCTOR(ClinitCheck);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006332};
6333
Vladimir Markofcb503c2016-05-18 12:48:17 +01006334class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006335 public:
6336 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006337 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006338 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006339 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006340 bool is_volatile,
6341 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006342 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006343 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006344 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01006345 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006346 field_info_(field,
6347 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006348 field_type,
6349 is_volatile,
6350 field_idx,
6351 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006352 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006353 SetRawInputAt(0, cls);
6354 }
6355
Calin Juravle52c48962014-12-16 17:02:57 +00006356
Artem Serovcced8ba2017-07-19 18:18:09 +01006357 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle10c9cbe2014-12-19 10:50:19 +00006358 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006359
Vladimir Marko372f10e2016-05-17 16:30:10 +01006360 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
6361 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006362 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006363 }
6364
6365 size_t ComputeHashCode() const OVERRIDE {
6366 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6367 }
6368
Calin Juravle52c48962014-12-16 17:02:57 +00006369 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006370 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006371 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006372 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006373
Vladimir Marko61b92282017-10-11 13:23:17 +01006374 void SetType(DataType::Type new_type) {
6375 DCHECK(DataType::IsIntegralType(GetType()));
6376 DCHECK(DataType::IsIntegralType(new_type));
6377 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6378 SetPackedField<TypeField>(new_type);
6379 }
6380
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006381 DECLARE_INSTRUCTION(StaticFieldGet);
6382
Artem Serovcced8ba2017-07-19 18:18:09 +01006383 protected:
6384 DEFAULT_COPY_CONSTRUCTOR(StaticFieldGet);
6385
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006386 private:
6387 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006388};
6389
Vladimir Markofcb503c2016-05-18 12:48:17 +01006390class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006391 public:
6392 HStaticFieldSet(HInstruction* cls,
6393 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006394 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006395 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006396 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006397 bool is_volatile,
6398 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006399 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006400 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006401 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01006402 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006403 field_info_(field,
6404 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006405 field_type,
6406 is_volatile,
6407 field_idx,
6408 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006409 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006410 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006411 SetRawInputAt(0, cls);
6412 SetRawInputAt(1, value);
6413 }
6414
Artem Serovcced8ba2017-07-19 18:18:09 +01006415 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle52c48962014-12-16 17:02:57 +00006416 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006417 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006418 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006419 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006420
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006421 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006422 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6423 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006424
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006425 DECLARE_INSTRUCTION(StaticFieldSet);
6426
Artem Serovcced8ba2017-07-19 18:18:09 +01006427 protected:
6428 DEFAULT_COPY_CONSTRUCTOR(StaticFieldSet);
6429
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006430 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006431 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6432 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6433 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6434 "Too many packed fields.");
6435
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006436 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006437};
6438
Vladimir Markofcb503c2016-05-18 12:48:17 +01006439class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006440 public:
6441 HUnresolvedInstanceFieldGet(HInstruction* obj,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006442 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006443 uint32_t field_index,
6444 uint32_t dex_pc)
6445 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6446 field_index_(field_index) {
6447 SetRawInputAt(0, obj);
6448 }
6449
Artem Serovcced8ba2017-07-19 18:18:09 +01006450 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006451 bool NeedsEnvironment() const OVERRIDE { return true; }
6452 bool CanThrow() const OVERRIDE { return true; }
6453
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006454 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006455 uint32_t GetFieldIndex() const { return field_index_; }
6456
6457 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6458
Artem Serovcced8ba2017-07-19 18:18:09 +01006459 protected:
6460 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldGet);
6461
Calin Juravlee460d1d2015-09-29 04:52:17 +01006462 private:
6463 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006464};
6465
Vladimir Markofcb503c2016-05-18 12:48:17 +01006466class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006467 public:
6468 HUnresolvedInstanceFieldSet(HInstruction* obj,
6469 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006470 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006471 uint32_t field_index,
6472 uint32_t dex_pc)
6473 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006474 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006475 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006476 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006477 SetRawInputAt(0, obj);
6478 SetRawInputAt(1, value);
6479 }
6480
Artem Serovcced8ba2017-07-19 18:18:09 +01006481 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006482 bool NeedsEnvironment() const OVERRIDE { return true; }
6483 bool CanThrow() const OVERRIDE { return true; }
6484
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006485 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006486 uint32_t GetFieldIndex() const { return field_index_; }
6487
6488 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6489
Artem Serovcced8ba2017-07-19 18:18:09 +01006490 protected:
6491 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldSet);
6492
Calin Juravlee460d1d2015-09-29 04:52:17 +01006493 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006494 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6495 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006496 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006497 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6498 kFieldFieldType + kFieldFieldTypeSize;
6499 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6500 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006501 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006502
Calin Juravlee460d1d2015-09-29 04:52:17 +01006503 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006504};
6505
Vladimir Markofcb503c2016-05-18 12:48:17 +01006506class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006507 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006508 HUnresolvedStaticFieldGet(DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006509 uint32_t field_index,
6510 uint32_t dex_pc)
6511 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6512 field_index_(field_index) {
6513 }
6514
Artem Serovcced8ba2017-07-19 18:18:09 +01006515 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006516 bool NeedsEnvironment() const OVERRIDE { return true; }
6517 bool CanThrow() const OVERRIDE { return true; }
6518
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006519 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006520 uint32_t GetFieldIndex() const { return field_index_; }
6521
6522 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6523
Artem Serovcced8ba2017-07-19 18:18:09 +01006524 protected:
6525 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldGet);
6526
Calin Juravlee460d1d2015-09-29 04:52:17 +01006527 private:
6528 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006529};
6530
Vladimir Markofcb503c2016-05-18 12:48:17 +01006531class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006532 public:
6533 HUnresolvedStaticFieldSet(HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006534 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006535 uint32_t field_index,
6536 uint32_t dex_pc)
6537 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006538 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006539 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006540 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006541 SetRawInputAt(0, value);
6542 }
6543
Artem Serovcced8ba2017-07-19 18:18:09 +01006544 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006545 bool NeedsEnvironment() const OVERRIDE { return true; }
6546 bool CanThrow() const OVERRIDE { return true; }
6547
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006548 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006549 uint32_t GetFieldIndex() const { return field_index_; }
6550
6551 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6552
Artem Serovcced8ba2017-07-19 18:18:09 +01006553 protected:
6554 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldSet);
6555
Calin Juravlee460d1d2015-09-29 04:52:17 +01006556 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006557 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6558 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006559 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006560 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6561 kFieldFieldType + kFieldFieldTypeSize;
6562 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6563 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006564 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006565
Calin Juravlee460d1d2015-09-29 04:52:17 +01006566 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006567};
6568
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006569// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006570class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006571 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006572 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006573 : HExpression(DataType::Type::kReference, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006574
David Brazdilbbd733e2015-08-18 17:48:17 +01006575 bool CanBeNull() const OVERRIDE { return false; }
6576
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006577 DECLARE_INSTRUCTION(LoadException);
6578
Artem Serovcced8ba2017-07-19 18:18:09 +01006579 protected:
6580 DEFAULT_COPY_CONSTRUCTOR(LoadException);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006581};
6582
David Brazdilcb1c0552015-08-04 16:22:25 +01006583// Implicit part of move-exception which clears thread-local exception storage.
6584// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006585class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006586 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006587 explicit HClearException(uint32_t dex_pc = kNoDexPc)
6588 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01006589
6590 DECLARE_INSTRUCTION(ClearException);
6591
Artem Serovcced8ba2017-07-19 18:18:09 +01006592 protected:
6593 DEFAULT_COPY_CONSTRUCTOR(ClearException);
David Brazdilcb1c0552015-08-04 16:22:25 +01006594};
6595
Vladimir Markofcb503c2016-05-18 12:48:17 +01006596class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006597 public:
6598 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006599 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006600 SetRawInputAt(0, exception);
6601 }
6602
6603 bool IsControlFlow() const OVERRIDE { return true; }
6604
6605 bool NeedsEnvironment() const OVERRIDE { return true; }
6606
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006607 bool CanThrow() const OVERRIDE { return true; }
6608
Aart Bika8b8e9b2018-01-09 11:01:02 -08006609 bool AlwaysThrows() const OVERRIDE { return true; }
6610
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006611 DECLARE_INSTRUCTION(Throw);
6612
Artem Serovcced8ba2017-07-19 18:18:09 +01006613 protected:
6614 DEFAULT_COPY_CONSTRUCTOR(Throw);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006615};
6616
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006617/**
6618 * Implementation strategies for the code generator of a HInstanceOf
6619 * or `HCheckCast`.
6620 */
6621enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006622 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006623 kExactCheck, // Can do a single class compare.
6624 kClassHierarchyCheck, // Can just walk the super class chain.
6625 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6626 kInterfaceCheck, // No optimization yet when checking against an interface.
6627 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006628 kArrayCheck, // No optimization yet when checking against a generic array.
6629 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006630};
6631
Roland Levillain86503782016-02-11 19:07:30 +00006632std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6633
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006634class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006635 public:
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006636 HInstanceOf(HInstruction* object,
6637 HLoadClass* target_class,
6638 TypeCheckKind check_kind,
6639 uint32_t dex_pc)
6640 : HExpression(DataType::Type::kBool,
6641 SideEffectsForArchRuntimeCalls(check_kind),
6642 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006643 SetPackedField<TypeCheckKindField>(check_kind);
6644 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006645 SetRawInputAt(0, object);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006646 SetRawInputAt(1, target_class);
Vladimir Marko87584542017-12-12 17:47:52 +00006647 }
6648
6649 HLoadClass* GetTargetClass() const {
6650 HInstruction* load_class = InputAt(1);
6651 DCHECK(load_class->IsLoadClass());
6652 return load_class->AsLoadClass();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006653 }
6654
Artem Serovcced8ba2017-07-19 18:18:09 +01006655 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006656 bool CanBeMoved() const OVERRIDE { return true; }
6657
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006658 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6659 return true;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006660 }
6661
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00006662 bool NeedsEnvironment() const OVERRIDE {
6663 return CanCallRuntime(GetTypeCheckKind());
6664 }
6665
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006666 // Used only in code generation.
6667 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6668 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6669 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6670 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
6671
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006672 static bool CanCallRuntime(TypeCheckKind check_kind) {
6673 // Mips currently does runtime calls for any other checks.
6674 return check_kind != TypeCheckKind::kExactCheck;
6675 }
6676
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006677 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006678 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006679 }
6680
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006681 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006682
Artem Serovcced8ba2017-07-19 18:18:09 +01006683 protected:
6684 DEFAULT_COPY_CONSTRUCTOR(InstanceOf);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006685
6686 private:
6687 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6688 static constexpr size_t kFieldTypeCheckKindSize =
6689 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6690 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6691 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6692 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6693 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006694};
6695
Vladimir Markofcb503c2016-05-18 12:48:17 +01006696class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006697 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07006698 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006699 : HExpression(DataType::Type::kReference, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006700 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6701 SetPackedFlag<kFlagUpperCanBeNull>(true);
6702 SetPackedFlag<kFlagCanBeNull>(true);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006703 DCHECK_EQ(input->GetType(), DataType::Type::kReference);
Calin Juravleb1498f62015-02-16 13:13:29 +00006704 SetRawInputAt(0, input);
6705 }
6706
Artem Serovcced8ba2017-07-19 18:18:09 +01006707 bool IsClonable() const OVERRIDE { return true; }
6708
David Brazdilf5552582015-12-27 13:36:12 +00006709 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006710 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006711 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006712 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006713
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006714 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006715 DCHECK(GetUpperCanBeNull() || !can_be_null);
6716 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006717 }
6718
Vladimir Markoa1de9182016-02-25 11:37:38 +00006719 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006720
Calin Juravleb1498f62015-02-16 13:13:29 +00006721 DECLARE_INSTRUCTION(BoundType);
6722
Artem Serovcced8ba2017-07-19 18:18:09 +01006723 protected:
6724 DEFAULT_COPY_CONSTRUCTOR(BoundType);
6725
Calin Juravleb1498f62015-02-16 13:13:29 +00006726 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006727 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6728 // is false then CanBeNull() cannot be true).
6729 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6730 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6731 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6732 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6733
Calin Juravleb1498f62015-02-16 13:13:29 +00006734 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006735 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6736 // It is used to bound the type in cases like:
6737 // if (x instanceof ClassX) {
6738 // // uper_bound_ will be ClassX
6739 // }
David Brazdilf5552582015-12-27 13:36:12 +00006740 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006741};
6742
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006743class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006744 public:
6745 HCheckCast(HInstruction* object,
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006746 HLoadClass* target_class,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006747 TypeCheckKind check_kind,
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006748 uint32_t dex_pc)
6749 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6750 SetPackedField<TypeCheckKindField>(check_kind);
6751 SetPackedFlag<kFlagMustDoNullCheck>(true);
6752 SetRawInputAt(0, object);
6753 SetRawInputAt(1, target_class);
6754 }
6755
6756 HLoadClass* GetTargetClass() const {
6757 HInstruction* load_class = InputAt(1);
6758 DCHECK(load_class->IsLoadClass());
6759 return load_class->AsLoadClass();
6760 }
6761
6762 bool IsClonable() const OVERRIDE { return true; }
6763 bool CanBeMoved() const OVERRIDE { return true; }
6764
6765 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6766 return true;
6767 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006768
6769 bool NeedsEnvironment() const OVERRIDE {
6770 // Instruction may throw a CheckCastError.
6771 return true;
6772 }
6773
6774 bool CanThrow() const OVERRIDE { return true; }
6775
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006776 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6777 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6778 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6779 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
6780
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006781 DECLARE_INSTRUCTION(CheckCast);
6782
Artem Serovcced8ba2017-07-19 18:18:09 +01006783 protected:
6784 DEFAULT_COPY_CONSTRUCTOR(CheckCast);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006785
6786 private:
6787 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6788 static constexpr size_t kFieldTypeCheckKindSize =
6789 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6790 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6791 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6792 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6793 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006794};
6795
Andreas Gampe26de38b2016-07-27 17:53:11 -07006796/**
6797 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
6798 * @details We define the combined barrier types that are actually required
6799 * by the Java Memory Model, rather than using exactly the terminology from
6800 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
6801 * primitives. Note that the JSR-133 cookbook generally does not deal with
6802 * store atomicity issues, and the recipes there are not always entirely sufficient.
6803 * The current recipe is as follows:
6804 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
6805 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
6806 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
6807 * -# Use StoreStore barrier after all stores but before return from any constructor whose
6808 * class has final fields.
6809 * -# Use NTStoreStore to order non-temporal stores with respect to all later
6810 * store-to-memory instructions. Only generated together with non-temporal stores.
6811 */
6812enum MemBarrierKind {
6813 kAnyStore,
6814 kLoadAny,
6815 kStoreStore,
6816 kAnyAny,
6817 kNTStoreStore,
6818 kLastBarrierKind = kNTStoreStore
6819};
6820std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
6821
Vladimir Markofcb503c2016-05-18 12:48:17 +01006822class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006823 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006824 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006825 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006826 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6827 SetPackedField<BarrierKindField>(barrier_kind);
6828 }
Calin Juravle27df7582015-04-17 19:12:31 +01006829
Artem Serovcced8ba2017-07-19 18:18:09 +01006830 bool IsClonable() const OVERRIDE { return true; }
6831
Vladimir Markoa1de9182016-02-25 11:37:38 +00006832 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006833
6834 DECLARE_INSTRUCTION(MemoryBarrier);
6835
Artem Serovcced8ba2017-07-19 18:18:09 +01006836 protected:
6837 DEFAULT_COPY_CONSTRUCTOR(MemoryBarrier);
6838
Calin Juravle27df7582015-04-17 19:12:31 +01006839 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006840 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6841 static constexpr size_t kFieldBarrierKindSize =
6842 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6843 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6844 kFieldBarrierKind + kFieldBarrierKindSize;
6845 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6846 "Too many packed fields.");
6847 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006848};
6849
Igor Murashkind01745e2017-04-05 16:40:31 -07006850// A constructor fence orders all prior stores to fields that could be accessed via a final field of
6851// the specified object(s), with respect to any subsequent store that might "publish"
6852// (i.e. make visible) the specified object to another thread.
6853//
6854// JLS 17.5.1 "Semantics of final fields" states that a freeze action happens
6855// for all final fields (that were set) at the end of the invoked constructor.
6856//
6857// The constructor fence models the freeze actions for the final fields of an object
6858// being constructed (semantically at the end of the constructor). Constructor fences
6859// have a per-object affinity; two separate objects being constructed get two separate
6860// constructor fences.
6861//
6862// (Note: that if calling a super-constructor or forwarding to another constructor,
6863// the freezes would happen at the end of *that* constructor being invoked).
6864//
6865// The memory model guarantees that when the object being constructed is "published" after
6866// constructor completion (i.e. escapes the current thread via a store), then any final field
6867// writes must be observable on other threads (once they observe that publication).
6868//
6869// Further, anything written before the freeze, and read by dereferencing through the final field,
6870// must also be visible (so final object field could itself have an object with non-final fields;
6871// yet the freeze must also extend to them).
6872//
6873// Constructor example:
6874//
6875// class HasFinal {
6876// final int field; Optimizing IR for <init>()V:
6877// HasFinal() {
6878// field = 123; HInstanceFieldSet(this, HasFinal.field, 123)
6879// // freeze(this.field); HConstructorFence(this)
6880// } HReturn
6881// }
6882//
6883// HConstructorFence can serve double duty as a fence for new-instance/new-array allocations of
6884// already-initialized classes; in that case the allocation must act as a "default-initializer"
6885// of the object which effectively writes the class pointer "final field".
6886//
6887// For example, we can model default-initialiation as roughly the equivalent of the following:
6888//
6889// class Object {
6890// private final Class header;
6891// }
6892//
6893// Java code: Optimizing IR:
6894//
6895// T new_instance<T>() {
6896// Object obj = allocate_memory(T.class.size); obj = HInvoke(art_quick_alloc_object, T)
6897// obj.header = T.class; // header write is done by above call.
6898// // freeze(obj.header) HConstructorFence(obj)
6899// return (T)obj;
6900// }
6901//
6902// See also:
6903// * CompilerDriver::RequiresConstructorBarrier
6904// * QuasiAtomic::ThreadFenceForConstructor
6905//
6906class HConstructorFence FINAL : public HVariableInputSizeInstruction {
6907 // A fence has variable inputs because the inputs can be removed
6908 // after prepare_for_register_allocation phase.
6909 // (TODO: In the future a fence could freeze multiple objects
6910 // after merging two fences together.)
6911 public:
6912 // `fence_object` is the reference that needs to be protected for correct publication.
6913 //
6914 // It makes sense in the following situations:
6915 // * <init> constructors, it's the "this" parameter (i.e. HParameterValue, s.t. IsThis() == true).
6916 // * new-instance-like instructions, it's the return value (i.e. HNewInstance).
6917 //
6918 // After construction the `fence_object` becomes the 0th input.
6919 // This is not an input in a real sense, but just a convenient place to stash the information
6920 // about the associated object.
6921 HConstructorFence(HInstruction* fence_object,
6922 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01006923 ArenaAllocator* allocator)
Igor Murashkind01745e2017-04-05 16:40:31 -07006924 // We strongly suspect there is not a more accurate way to describe the fine-grained reordering
6925 // constraints described in the class header. We claim that these SideEffects constraints
6926 // enforce a superset of the real constraints.
6927 //
6928 // The ordering described above is conservatively modeled with SideEffects as follows:
6929 //
6930 // * To prevent reordering of the publication stores:
6931 // ----> "Reads of objects" is the initial SideEffect.
6932 // * For every primitive final field store in the constructor:
6933 // ----> Union that field's type as a read (e.g. "Read of T") into the SideEffect.
6934 // * If there are any stores to reference final fields in the constructor:
6935 // ----> Use a more conservative "AllReads" SideEffect because any stores to any references
6936 // that are reachable from `fence_object` also need to be prevented for reordering
6937 // (and we do not want to do alias analysis to figure out what those stores are).
6938 //
6939 // In the implementation, this initially starts out as an "all reads" side effect; this is an
6940 // even more conservative approach than the one described above, and prevents all of the
6941 // above reordering without analyzing any of the instructions in the constructor.
6942 //
6943 // If in a later phase we discover that there are no writes to reference final fields,
6944 // we can refine the side effect to a smaller set of type reads (see above constraints).
6945 : HVariableInputSizeInstruction(SideEffects::AllReads(),
6946 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01006947 allocator,
Igor Murashkind01745e2017-04-05 16:40:31 -07006948 /* number_of_inputs */ 1,
6949 kArenaAllocConstructorFenceInputs) {
6950 DCHECK(fence_object != nullptr);
6951 SetRawInputAt(0, fence_object);
6952 }
6953
6954 // The object associated with this constructor fence.
6955 //
6956 // (Note: This will be null after the prepare_for_register_allocation phase,
6957 // as all constructor fence inputs are removed there).
6958 HInstruction* GetFenceObject() const {
6959 return InputAt(0);
6960 }
6961
6962 // Find all the HConstructorFence uses (`fence_use`) for `this` and:
6963 // - Delete `fence_use` from `this`'s use list.
6964 // - Delete `this` from `fence_use`'s inputs list.
6965 // - If the `fence_use` is dead, remove it from the graph.
6966 //
6967 // A fence is considered dead once it no longer has any uses
6968 // and all of the inputs are dead.
6969 //
6970 // This must *not* be called during/after prepare_for_register_allocation,
6971 // because that removes all the inputs to the fences but the fence is actually
6972 // still considered live.
Igor Murashkin6ef45672017-08-08 13:59:55 -07006973 //
6974 // Returns how many HConstructorFence instructions were removed from graph.
6975 static size_t RemoveConstructorFences(HInstruction* instruction);
Igor Murashkind01745e2017-04-05 16:40:31 -07006976
Igor Murashkindd018df2017-08-09 10:38:31 -07006977 // Combine all inputs of `this` and `other` instruction and remove
6978 // `other` from the graph.
6979 //
6980 // Inputs are unique after the merge.
6981 //
6982 // Requirement: `this` must not be the same as `other.
6983 void Merge(HConstructorFence* other);
6984
Igor Murashkin79d8fa72017-04-18 09:37:23 -07006985 // Check if this constructor fence is protecting
6986 // an HNewInstance or HNewArray that is also the immediate
6987 // predecessor of `this`.
6988 //
Igor Murashkindd018df2017-08-09 10:38:31 -07006989 // If `ignore_inputs` is true, then the immediate predecessor doesn't need
6990 // to be one of the inputs of `this`.
6991 //
Igor Murashkin79d8fa72017-04-18 09:37:23 -07006992 // Returns the associated HNewArray or HNewInstance,
6993 // or null otherwise.
Igor Murashkindd018df2017-08-09 10:38:31 -07006994 HInstruction* GetAssociatedAllocation(bool ignore_inputs = false);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07006995
Igor Murashkind01745e2017-04-05 16:40:31 -07006996 DECLARE_INSTRUCTION(ConstructorFence);
6997
Artem Serovcced8ba2017-07-19 18:18:09 +01006998 protected:
6999 DEFAULT_COPY_CONSTRUCTOR(ConstructorFence);
Igor Murashkind01745e2017-04-05 16:40:31 -07007000};
7001
Vladimir Markofcb503c2016-05-18 12:48:17 +01007002class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007003 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007004 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007005 kEnter,
7006 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00007007 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007008 };
7009
7010 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01007011 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00007012 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
7013 dex_pc) {
7014 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007015 SetRawInputAt(0, object);
7016 }
7017
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007018 // Instruction may go into runtime, so we need an environment.
7019 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00007020
7021 bool CanThrow() const OVERRIDE {
7022 // Verifier guarantees that monitor-exit cannot throw.
7023 // This is important because it allows the HGraphBuilder to remove
7024 // a dead throw-catch loop generated for `synchronized` blocks/methods.
7025 return IsEnter();
7026 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007027
Vladimir Markoa1de9182016-02-25 11:37:38 +00007028 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
7029 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007030
7031 DECLARE_INSTRUCTION(MonitorOperation);
7032
Artem Serovcced8ba2017-07-19 18:18:09 +01007033 protected:
7034 DEFAULT_COPY_CONSTRUCTOR(MonitorOperation);
7035
Calin Juravle52c48962014-12-16 17:02:57 +00007036 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007037 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
7038 static constexpr size_t kFieldOperationKindSize =
7039 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
7040 static constexpr size_t kNumberOfMonitorOperationPackedBits =
7041 kFieldOperationKind + kFieldOperationKindSize;
7042 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7043 "Too many packed fields.");
7044 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007045};
7046
Vladimir Markofcb503c2016-05-18 12:48:17 +01007047class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00007048 public:
7049 HSelect(HInstruction* condition,
7050 HInstruction* true_value,
7051 HInstruction* false_value,
7052 uint32_t dex_pc)
7053 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
7054 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
7055
7056 // First input must be `true_value` or `false_value` to allow codegens to
7057 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
7058 // that architectures which implement HSelect as a conditional move also
7059 // will not need to invert the condition.
7060 SetRawInputAt(0, false_value);
7061 SetRawInputAt(1, true_value);
7062 SetRawInputAt(2, condition);
7063 }
7064
Artem Serovcced8ba2017-07-19 18:18:09 +01007065 bool IsClonable() const OVERRIDE { return true; }
David Brazdil74eb1b22015-12-14 11:44:01 +00007066 HInstruction* GetFalseValue() const { return InputAt(0); }
7067 HInstruction* GetTrueValue() const { return InputAt(1); }
7068 HInstruction* GetCondition() const { return InputAt(2); }
7069
7070 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01007071 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
7072 return true;
7073 }
David Brazdil74eb1b22015-12-14 11:44:01 +00007074
7075 bool CanBeNull() const OVERRIDE {
7076 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
7077 }
7078
7079 DECLARE_INSTRUCTION(Select);
7080
Artem Serovcced8ba2017-07-19 18:18:09 +01007081 protected:
7082 DEFAULT_COPY_CONSTRUCTOR(Select);
David Brazdil74eb1b22015-12-14 11:44:01 +00007083};
7084
Vladimir Markof9f64412015-09-02 14:05:49 +01007085class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007086 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01007087 MoveOperands(Location source,
7088 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007089 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007090 HInstruction* instruction)
7091 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007092
7093 Location GetSource() const { return source_; }
7094 Location GetDestination() const { return destination_; }
7095
7096 void SetSource(Location value) { source_ = value; }
7097 void SetDestination(Location value) { destination_ = value; }
7098
7099 // The parallel move resolver marks moves as "in-progress" by clearing the
7100 // destination (but not the source).
7101 Location MarkPending() {
7102 DCHECK(!IsPending());
7103 Location dest = destination_;
7104 destination_ = Location::NoLocation();
7105 return dest;
7106 }
7107
7108 void ClearPending(Location dest) {
7109 DCHECK(IsPending());
7110 destination_ = dest;
7111 }
7112
7113 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00007114 DCHECK(source_.IsValid() || destination_.IsInvalid());
7115 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007116 }
7117
7118 // True if this blocks a move from the given location.
7119 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08007120 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007121 }
7122
7123 // A move is redundant if it's been eliminated, if its source and
7124 // destination are the same, or if its destination is unneeded.
7125 bool IsRedundant() const {
7126 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
7127 }
7128
7129 // We clear both operands to indicate move that's been eliminated.
7130 void Eliminate() {
7131 source_ = destination_ = Location::NoLocation();
7132 }
7133
7134 bool IsEliminated() const {
7135 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
7136 return source_.IsInvalid();
7137 }
7138
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007139 DataType::Type GetType() const { return type_; }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007140
Nicolas Geoffray90218252015-04-15 11:56:51 +01007141 bool Is64BitMove() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007142 return DataType::Is64BitType(type_);
Nicolas Geoffray90218252015-04-15 11:56:51 +01007143 }
7144
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007145 HInstruction* GetInstruction() const { return instruction_; }
7146
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007147 private:
7148 Location source_;
7149 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01007150 // The type this move is for.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007151 DataType::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007152 // The instruction this move is assocatied with. Null when this move is
7153 // for moving an input in the expected locations of user (including a phi user).
7154 // This is only used in debug mode, to ensure we do not connect interval siblings
7155 // in the same parallel move.
7156 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007157};
7158
Roland Levillainc9285912015-12-18 10:38:42 +00007159std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
7160
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007161static constexpr size_t kDefaultNumberOfMoves = 4;
7162
Vladimir Markofcb503c2016-05-18 12:48:17 +01007163class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007164 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007165 explicit HParallelMove(ArenaAllocator* allocator, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01007166 : HTemplateInstruction(SideEffects::None(), dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007167 moves_(allocator->Adapter(kArenaAllocMoveOperands)) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007168 moves_.reserve(kDefaultNumberOfMoves);
7169 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007170
Nicolas Geoffray90218252015-04-15 11:56:51 +01007171 void AddMove(Location source,
7172 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007173 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007174 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00007175 DCHECK(source.IsValid());
7176 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007177 if (kIsDebugBuild) {
7178 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007179 for (const MoveOperands& move : moves_) {
7180 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007181 // Special case the situation where the move is for the spill slot
7182 // of the instruction.
7183 if ((GetPrevious() == instruction)
7184 || ((GetPrevious() == nullptr)
7185 && instruction->IsPhi()
7186 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007187 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007188 << "Doing parallel moves for the same instruction.";
7189 } else {
7190 DCHECK(false) << "Doing parallel moves for the same instruction.";
7191 }
7192 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00007193 }
7194 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007195 for (const MoveOperands& move : moves_) {
7196 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007197 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01007198 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007199 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007200 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007201 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007202 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007203 }
7204
Vladimir Marko225b6462015-09-28 12:17:40 +01007205 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007206 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007207 }
7208
Vladimir Marko225b6462015-09-28 12:17:40 +01007209 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007210
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01007211 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007212
Artem Serovcced8ba2017-07-19 18:18:09 +01007213 protected:
7214 DEFAULT_COPY_CONSTRUCTOR(ParallelMove);
7215
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007216 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01007217 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007218};
7219
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007220// This instruction computes an intermediate address pointing in the 'middle' of an object. The
7221// result pointer cannot be handled by GC, so extra care is taken to make sure that this value is
7222// never used across anything that can trigger GC.
7223// The result of this instruction is not a pointer in the sense of `DataType::Type::kreference`.
7224// So we represent it by the type `DataType::Type::kInt`.
7225class HIntermediateAddress FINAL : public HExpression<2> {
7226 public:
7227 HIntermediateAddress(HInstruction* base_address, HInstruction* offset, uint32_t dex_pc)
7228 : HExpression(DataType::Type::kInt32, SideEffects::DependsOnGC(), dex_pc) {
7229 DCHECK_EQ(DataType::Size(DataType::Type::kInt32),
7230 DataType::Size(DataType::Type::kReference))
7231 << "kPrimInt and kPrimNot have different sizes.";
7232 SetRawInputAt(0, base_address);
7233 SetRawInputAt(1, offset);
7234 }
7235
Artem Serovcced8ba2017-07-19 18:18:09 +01007236 bool IsClonable() const OVERRIDE { return true; }
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007237 bool CanBeMoved() const OVERRIDE { return true; }
7238 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
7239 return true;
7240 }
7241 bool IsActualObject() const OVERRIDE { return false; }
7242
7243 HInstruction* GetBaseAddress() const { return InputAt(0); }
7244 HInstruction* GetOffset() const { return InputAt(1); }
7245
7246 DECLARE_INSTRUCTION(IntermediateAddress);
7247
Artem Serovcced8ba2017-07-19 18:18:09 +01007248 protected:
7249 DEFAULT_COPY_CONSTRUCTOR(IntermediateAddress);
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007250};
7251
7252
Mark Mendell0616ae02015-04-17 12:49:27 -04007253} // namespace art
7254
Aart Bikf8f5a162017-02-06 15:35:29 -08007255#include "nodes_vector.h"
7256
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007257#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
7258#include "nodes_shared.h"
7259#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007260#ifdef ART_ENABLE_CODEGEN_mips
7261#include "nodes_mips.h"
7262#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04007263#ifdef ART_ENABLE_CODEGEN_x86
7264#include "nodes_x86.h"
7265#endif
7266
7267namespace art {
7268
Igor Murashkin6ef45672017-08-08 13:59:55 -07007269class OptimizingCompilerStats;
7270
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007271class HGraphVisitor : public ValueObject {
7272 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007273 explicit HGraphVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7274 : stats_(stats),
7275 graph_(graph) {}
Dave Allison20dfc792014-06-16 20:44:29 -07007276 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007277
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007278 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007279 virtual void VisitBasicBlock(HBasicBlock* block);
7280
Roland Levillain633021e2014-10-01 14:12:25 +01007281 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007282 void VisitInsertionOrder();
7283
Roland Levillain633021e2014-10-01 14:12:25 +01007284 // Visit the graph following dominator tree reverse post-order.
7285 void VisitReversePostOrder();
7286
Nicolas Geoffray787c3072014-03-17 10:20:19 +00007287 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007288
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007289 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007290#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007291 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
7292
7293 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7294
7295#undef DECLARE_VISIT_INSTRUCTION
7296
Igor Murashkin6ef45672017-08-08 13:59:55 -07007297 protected:
7298 OptimizingCompilerStats* stats_;
7299
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007300 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07007301 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007302
7303 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
7304};
7305
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007306class HGraphDelegateVisitor : public HGraphVisitor {
7307 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007308 explicit HGraphDelegateVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7309 : HGraphVisitor(graph, stats) {}
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007310 virtual ~HGraphDelegateVisitor() {}
7311
7312 // Visit functions that delegate to to super class.
7313#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00007314 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007315
7316 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7317
7318#undef DECLARE_VISIT_INSTRUCTION
7319
7320 private:
7321 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
7322};
7323
Artem Serovcced8ba2017-07-19 18:18:09 +01007324// Create a clone of the instruction, insert it into the graph; replace the old one with a new
7325// and remove the old instruction.
7326HInstruction* ReplaceInstrOrPhiByClone(HInstruction* instr);
7327
7328// Create a clone for each clonable instructions/phis and replace the original with the clone.
7329//
7330// Used for testing individual instruction cloner.
7331class CloneAndReplaceInstructionVisitor : public HGraphDelegateVisitor {
7332 public:
7333 explicit CloneAndReplaceInstructionVisitor(HGraph* graph)
Artem Serov7f4aff62017-06-21 17:02:18 +01007334 : HGraphDelegateVisitor(graph), instr_replaced_by_clones_count_(0) {}
Artem Serovcced8ba2017-07-19 18:18:09 +01007335
7336 void VisitInstruction(HInstruction* instruction) OVERRIDE {
7337 if (instruction->IsClonable()) {
7338 ReplaceInstrOrPhiByClone(instruction);
Artem Serov7f4aff62017-06-21 17:02:18 +01007339 instr_replaced_by_clones_count_++;
Artem Serovcced8ba2017-07-19 18:18:09 +01007340 }
7341 }
7342
Artem Serov7f4aff62017-06-21 17:02:18 +01007343 size_t GetInstrReplacedByClonesCount() const { return instr_replaced_by_clones_count_; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007344
7345 private:
Artem Serov7f4aff62017-06-21 17:02:18 +01007346 size_t instr_replaced_by_clones_count_;
Artem Serovcced8ba2017-07-19 18:18:09 +01007347
7348 DISALLOW_COPY_AND_ASSIGN(CloneAndReplaceInstructionVisitor);
7349};
7350
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007351// Iterator over the blocks that art part of the loop. Includes blocks part
7352// of an inner loop. The order in which the blocks are iterated is on their
7353// block id.
7354class HBlocksInLoopIterator : public ValueObject {
7355 public:
7356 explicit HBlocksInLoopIterator(const HLoopInformation& info)
7357 : blocks_in_loop_(info.GetBlocks()),
7358 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
7359 index_(0) {
7360 if (!blocks_in_loop_.IsBitSet(index_)) {
7361 Advance();
7362 }
7363 }
7364
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007365 bool Done() const { return index_ == blocks_.size(); }
7366 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007367 void Advance() {
7368 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007369 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007370 if (blocks_in_loop_.IsBitSet(index_)) {
7371 break;
7372 }
7373 }
7374 }
7375
7376 private:
7377 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007378 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007379 size_t index_;
7380
7381 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
7382};
7383
Mingyao Yang3584bce2015-05-19 16:01:59 -07007384// Iterator over the blocks that art part of the loop. Includes blocks part
7385// of an inner loop. The order in which the blocks are iterated is reverse
7386// post order.
7387class HBlocksInLoopReversePostOrderIterator : public ValueObject {
7388 public:
7389 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
7390 : blocks_in_loop_(info.GetBlocks()),
7391 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
7392 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007393 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007394 Advance();
7395 }
7396 }
7397
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007398 bool Done() const { return index_ == blocks_.size(); }
7399 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07007400 void Advance() {
7401 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007402 for (size_t e = blocks_.size(); index_ < e; ++index_) {
7403 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007404 break;
7405 }
7406 }
7407 }
7408
7409 private:
7410 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007411 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07007412 size_t index_;
7413
7414 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
7415};
7416
Aart Bikf3e61ee2017-04-12 17:09:20 -07007417// Returns int64_t value of a properly typed constant.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007418inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00007419 if (constant->IsIntConstant()) {
7420 return constant->AsIntConstant()->GetValue();
7421 } else if (constant->IsLongConstant()) {
7422 return constant->AsLongConstant()->GetValue();
7423 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00007424 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00007425 return 0;
7426 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007427}
7428
Aart Bikf3e61ee2017-04-12 17:09:20 -07007429// Returns true iff instruction is an integral constant (and sets value on success).
7430inline bool IsInt64AndGet(HInstruction* instruction, /*out*/ int64_t* value) {
7431 if (instruction->IsIntConstant()) {
7432 *value = instruction->AsIntConstant()->GetValue();
7433 return true;
7434 } else if (instruction->IsLongConstant()) {
7435 *value = instruction->AsLongConstant()->GetValue();
7436 return true;
7437 } else if (instruction->IsNullConstant()) {
7438 *value = 0;
7439 return true;
7440 }
7441 return false;
7442}
7443
Aart Bik0148de42017-09-05 09:25:01 -07007444// Returns true iff instruction is the given integral constant.
7445inline bool IsInt64Value(HInstruction* instruction, int64_t value) {
7446 int64_t val = 0;
7447 return IsInt64AndGet(instruction, &val) && val == value;
7448}
7449
7450// Returns true iff instruction is a zero bit pattern.
7451inline bool IsZeroBitPattern(HInstruction* instruction) {
7452 return instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern();
7453}
7454
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007455#define INSTRUCTION_TYPE_CHECK(type, super) \
7456 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
7457 inline const H##type* HInstruction::As##type() const { \
7458 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
7459 } \
7460 inline H##type* HInstruction::As##type() { \
7461 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
7462 }
7463
7464 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
7465#undef INSTRUCTION_TYPE_CHECK
7466
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00007467// Create space in `blocks` for adding `number_of_new_blocks` entries
7468// starting at location `at`. Blocks after `at` are moved accordingly.
7469inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
7470 size_t number_of_new_blocks,
7471 size_t after) {
7472 DCHECK_LT(after, blocks->size());
7473 size_t old_size = blocks->size();
7474 size_t new_size = old_size + number_of_new_blocks;
7475 blocks->resize(new_size);
7476 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
7477}
7478
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00007479/*
7480 * Hunt "under the hood" of array lengths (leading to array references),
7481 * null checks (also leading to array references), and new arrays
7482 * (leading to the actual length). This makes it more likely related
7483 * instructions become actually comparable.
7484 */
7485inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
7486 while (instruction->IsArrayLength() ||
7487 instruction->IsNullCheck() ||
7488 instruction->IsNewArray()) {
7489 instruction = instruction->IsNewArray()
7490 ? instruction->AsNewArray()->GetLength()
7491 : instruction->InputAt(0);
7492 }
7493 return instruction;
7494}
7495
Artem Serov21c7e6f2017-07-27 16:04:42 +01007496void RemoveEnvironmentUses(HInstruction* instruction);
7497bool HasEnvironmentUsedByOthers(HInstruction* instruction);
7498void ResetEnvironmentInputRecords(HInstruction* instruction);
7499
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007500} // namespace art
7501
7502#endif // ART_COMPILER_OPTIMIZING_NODES_H_