blob: 3c584bd67d907937ace279632dec56a4fcd85830 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
Vladimir Marko60584552015-09-03 13:35:12 +000020#include <algorithm>
Vladimir Markof9f64412015-09-02 14:05:49 +010021#include <array>
Roland Levillain9867bc72015-08-05 10:21:34 +010022#include <type_traits>
23
Mathieu Chartiere5d80f82015-10-15 17:47:48 -070024#include "base/arena_bit_vector.h"
David Brazdil8d5b8b22015-03-24 10:51:52 +000025#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080026#include "base/arena_object.h"
David Brazdild9c90372016-09-14 16:53:55 +010027#include "base/array_ref.h"
Vladimir Marko2c45bc92016-10-25 16:54:12 +010028#include "base/iteration_range.h"
Vladimir Marko60584552015-09-03 13:35:12 +000029#include "base/stl_util.h"
David Brazdild9c90372016-09-14 16:53:55 +010030#include "base/transform_array_ref.h"
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010031#include "data_type.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070032#include "deoptimization_kind.h"
Vladimir Marko87f3fcb2016-04-28 15:52:11 +010033#include "dex_file.h"
Andreas Gampea5b09a62016-11-17 15:21:22 -080034#include "dex_file_types.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000035#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000036#include "handle.h"
37#include "handle_scope.h"
Nicolas Geoffray762869d2016-07-15 15:28:35 +010038#include "intrinsics_enum.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070039#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010040#include "locations.h"
Vladimir Marko58155012015-08-19 12:49:41 +000041#include "method_reference.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000042#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010043#include "offsets.h"
Vladimir Marko46817b82016-03-29 12:21:58 +010044#include "utils/intrusive_forward_list.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000045
46namespace art {
47
Vladimir Markoca6fff82017-10-03 14:49:14 +010048class ArenaStack;
David Brazdil1abb4192015-02-17 18:33:36 +000049class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000050class HBasicBlock;
Igor Murashkind01745e2017-04-05 16:40:31 -070051class HConstructorFence;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010052class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000053class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010054class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000055class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000056class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000057class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000058class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000059class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000060class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000061class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000062class HNullConstant;
Igor Murashkind01745e2017-04-05 16:40:31 -070063class HParameterValue;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010064class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010065class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010066class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010067class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000068class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010069class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000070class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000071
Mathieu Chartier736b5602015-09-02 14:54:11 -070072namespace mirror {
73class DexCache;
74} // namespace mirror
75
Nicolas Geoffray818f2102014-02-18 16:43:35 +000076static const int kDefaultNumberOfBlocks = 8;
77static const int kDefaultNumberOfSuccessors = 2;
78static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010079static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010080static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000081static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000082
Roland Levillain5b5b9312016-03-22 14:57:31 +000083// The maximum (meaningful) distance (31) that can be used in an integer shift/rotate operation.
84static constexpr int32_t kMaxIntShiftDistance = 0x1f;
85// The maximum (meaningful) distance (63) that can be used in a long shift/rotate operation.
86static constexpr int32_t kMaxLongShiftDistance = 0x3f;
Calin Juravle9aec02f2014-11-18 23:06:35 +000087
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010088static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070089static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010090
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010091static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
92
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060093static constexpr uint32_t kNoDexPc = -1;
94
Vladimir Markodbb7f5b2016-03-30 13:23:58 +010095inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
96 // For the purposes of the compiler, the dex files must actually be the same object
97 // if we want to safely treat them as the same. This is especially important for JIT
98 // as custom class loaders can open the same underlying file (or memory) multiple
99 // times and provide different class resolution but no two class loaders should ever
100 // use the same DexFile object - doing so is an unsupported hack that can lead to
101 // all sorts of weird failures.
102 return &lhs == &rhs;
103}
104
Dave Allison20dfc792014-06-16 20:44:29 -0700105enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -0700106 // All types.
107 kCondEQ, // ==
108 kCondNE, // !=
109 // Signed integers and floating-point numbers.
110 kCondLT, // <
111 kCondLE, // <=
112 kCondGT, // >
113 kCondGE, // >=
114 // Unsigned integers.
115 kCondB, // <
116 kCondBE, // <=
117 kCondA, // >
118 kCondAE, // >=
Scott Wakeling2c76e062016-08-31 09:48:54 +0100119 // First and last aliases.
120 kCondFirst = kCondEQ,
121 kCondLast = kCondAE,
Dave Allison20dfc792014-06-16 20:44:29 -0700122};
123
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000124enum GraphAnalysisResult {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000125 kAnalysisSkipped,
David Brazdilbadd8262016-02-02 16:28:56 +0000126 kAnalysisInvalidBytecode,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000127 kAnalysisFailThrowCatchLoop,
128 kAnalysisFailAmbiguousArrayOp,
129 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000130};
131
Andreas Gampe9186ced2016-12-12 14:28:21 -0800132template <typename T>
133static inline typename std::make_unsigned<T>::type MakeUnsigned(T x) {
134 return static_cast<typename std::make_unsigned<T>::type>(x);
135}
136
Vladimir Markof9f64412015-09-02 14:05:49 +0100137class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100138 public:
139 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
140
141 void AddInstruction(HInstruction* instruction);
142 void RemoveInstruction(HInstruction* instruction);
143
David Brazdilc3d743f2015-04-22 13:40:50 +0100144 // Insert `instruction` before/after an existing instruction `cursor`.
145 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
146 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
147
Roland Levillain6b469232014-09-25 10:10:38 +0100148 // Return true if this list contains `instruction`.
149 bool Contains(HInstruction* instruction) const;
150
Roland Levillainccc07a92014-09-16 14:48:16 +0100151 // Return true if `instruction1` is found before `instruction2` in
152 // this instruction list and false otherwise. Abort if none
153 // of these instructions is found.
154 bool FoundBefore(const HInstruction* instruction1,
155 const HInstruction* instruction2) const;
156
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000157 bool IsEmpty() const { return first_instruction_ == nullptr; }
158 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
159
160 // Update the block of all instructions to be `block`.
161 void SetBlockOfInstructions(HBasicBlock* block) const;
162
163 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000164 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000165 void Add(const HInstructionList& instruction_list);
166
David Brazdil2d7352b2015-04-20 14:52:42 +0100167 // Return the number of instructions in the list. This is an expensive operation.
168 size_t CountSize() const;
169
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100170 private:
171 HInstruction* first_instruction_;
172 HInstruction* last_instruction_;
173
174 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000175 friend class HGraph;
176 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100177 friend class HInstructionIterator;
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +0000178 friend class HInstructionIteratorHandleChanges;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100179 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100180
181 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
182};
183
David Brazdil4833f5a2015-12-16 10:37:39 +0000184class ReferenceTypeInfo : ValueObject {
185 public:
186 typedef Handle<mirror::Class> TypeHandle;
187
Vladimir Markoa1de9182016-02-25 11:37:38 +0000188 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
189
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700190 static ReferenceTypeInfo Create(TypeHandle type_handle) REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil94ab38f2016-06-21 17:48:19 +0100191 return Create(type_handle, type_handle->CannotBeAssignedFromOtherTypes());
192 }
193
Vladimir Markoa1de9182016-02-25 11:37:38 +0000194 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000195 return ReferenceTypeInfo(type_handle, is_exact);
196 }
197
198 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
199
Vladimir Markof39745e2016-01-26 12:16:55 +0000200 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000201 return handle.GetReference() != nullptr;
202 }
203
Vladimir Marko456307a2016-04-19 14:12:13 +0000204 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000205 return IsValidHandle(type_handle_);
206 }
207
208 bool IsExact() const { return is_exact_; }
209
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700210 bool IsObjectClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000211 DCHECK(IsValid());
212 return GetTypeHandle()->IsObjectClass();
213 }
214
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700215 bool IsStringClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000216 DCHECK(IsValid());
217 return GetTypeHandle()->IsStringClass();
218 }
219
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700220 bool IsObjectArray() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000221 DCHECK(IsValid());
222 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
223 }
224
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700225 bool IsInterface() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000226 DCHECK(IsValid());
227 return GetTypeHandle()->IsInterface();
228 }
229
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700230 bool IsArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000231 DCHECK(IsValid());
232 return GetTypeHandle()->IsArrayClass();
233 }
234
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700235 bool IsPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000236 DCHECK(IsValid());
237 return GetTypeHandle()->IsPrimitiveArray();
238 }
239
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700240 bool IsNonPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000241 DCHECK(IsValid());
242 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
243 }
244
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700245 bool CanArrayHold(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000246 DCHECK(IsValid());
247 if (!IsExact()) return false;
248 if (!IsArrayClass()) return false;
249 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
250 }
251
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700252 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000253 DCHECK(IsValid());
254 if (!IsExact()) return false;
255 if (!IsArrayClass()) return false;
256 if (!rti.IsArrayClass()) return false;
257 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
258 rti.GetTypeHandle()->GetComponentType());
259 }
260
261 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
262
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700263 bool IsSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000264 DCHECK(IsValid());
265 DCHECK(rti.IsValid());
266 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
267 }
268
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700269 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000270 DCHECK(IsValid());
271 DCHECK(rti.IsValid());
272 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
273 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
274 }
275
276 // Returns true if the type information provide the same amount of details.
277 // Note that it does not mean that the instructions have the same actual type
278 // (because the type can be the result of a merge).
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700279 bool IsEqual(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000280 if (!IsValid() && !rti.IsValid()) {
281 // Invalid types are equal.
282 return true;
283 }
284 if (!IsValid() || !rti.IsValid()) {
285 // One is valid, the other not.
286 return false;
287 }
288 return IsExact() == rti.IsExact()
289 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
290 }
291
292 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000293 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
294 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
295 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000296
297 // The class of the object.
298 TypeHandle type_handle_;
299 // Whether or not the type is exact or a superclass of the actual type.
300 // Whether or not we have any information about this type.
301 bool is_exact_;
302};
303
304std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
305
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000306// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100307class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000308 public:
Vladimir Markoca6fff82017-10-03 14:49:14 +0100309 HGraph(ArenaAllocator* allocator,
310 ArenaStack* arena_stack,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100311 const DexFile& dex_file,
312 uint32_t method_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700313 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100314 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100315 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000316 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100317 int start_instruction_id = 0)
Vladimir Markoca6fff82017-10-03 14:49:14 +0100318 : allocator_(allocator),
319 arena_stack_(arena_stack),
320 blocks_(allocator->Adapter(kArenaAllocBlockList)),
321 reverse_post_order_(allocator->Adapter(kArenaAllocReversePostOrder)),
322 linear_order_(allocator->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700323 entry_block_(nullptr),
324 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100325 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100326 number_of_vregs_(0),
327 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000328 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400329 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000330 has_try_catch_(false),
Aart Bikb13c65b2017-03-21 20:14:07 -0700331 has_simd_(false),
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800332 has_loops_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000333 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000334 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000335 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100336 dex_file_(dex_file),
337 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100338 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100339 in_ssa_form_(false),
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800340 number_of_cha_guards_(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700341 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000342 cached_null_constant_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100343 cached_int_constants_(std::less<int32_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
344 cached_float_constants_(std::less<int32_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
345 cached_long_constants_(std::less<int64_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
346 cached_double_constants_(std::less<int64_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000347 cached_current_method_(nullptr),
Nicolas Geoffray53fec082017-03-27 12:56:16 +0100348 art_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000349 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
Mingyao Yang063fc772016-08-02 11:02:54 -0700350 osr_(osr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100351 cha_single_implementation_list_(allocator->Adapter(kArenaAllocCHA)) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100352 blocks_.reserve(kDefaultNumberOfBlocks);
353 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000354
David Brazdilbadd8262016-02-02 16:28:56 +0000355 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700356 void InitializeInexactObjectRTI(VariableSizedHandleScope* handles);
David Brazdilbadd8262016-02-02 16:28:56 +0000357
Vladimir Markoca6fff82017-10-03 14:49:14 +0100358 ArenaAllocator* GetAllocator() const { return allocator_; }
359 ArenaStack* GetArenaStack() const { return arena_stack_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100360 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
361
David Brazdil69ba7b72015-06-23 18:27:30 +0100362 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000363 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100364
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000365 HBasicBlock* GetEntryBlock() const { return entry_block_; }
366 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100367 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000368
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000369 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
370 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000371
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000372 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100373
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100374 void ComputeDominanceInformation();
375 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000376 void ClearLoopInformation();
377 void FindBackEdges(ArenaBitVector* visited);
378 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100379 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100380 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000381
David Brazdil4833f5a2015-12-16 10:37:39 +0000382 // Analyze all natural loops in this graph. Returns a code specifying that it
383 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000384 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000385 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100386
David Brazdilffee3d32015-07-06 11:48:53 +0100387 // Iterate over blocks to compute try block membership. Needs reverse post
388 // order and loop information.
389 void ComputeTryBlockInformation();
390
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000391 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000392 // Returns the instruction to replace the invoke expression or null if the
393 // invoke is for a void method. Note that the caller is responsible for replacing
394 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000395 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000396
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000397 // Update the loop and try membership of `block`, which was spawned from `reference`.
398 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
399 // should be the new back edge.
400 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
401 HBasicBlock* reference,
402 bool replace_if_back_edge);
403
Mingyao Yang3584bce2015-05-19 16:01:59 -0700404 // Need to add a couple of blocks to test if the loop body is entered and
405 // put deoptimization instructions, etc.
406 void TransformLoopHeaderForBCE(HBasicBlock* header);
407
Aart Bikf8f5a162017-02-06 15:35:29 -0800408 // Adds a new loop directly after the loop with the given header and exit.
409 // Returns the new preheader.
410 HBasicBlock* TransformLoopForVectorization(HBasicBlock* header,
411 HBasicBlock* body,
412 HBasicBlock* exit);
413
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000414 // Removes `block` from the graph. Assumes `block` has been disconnected from
415 // other blocks and has no instructions or phis.
416 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000417
David Brazdilfc6a86a2015-06-26 10:33:45 +0000418 // Splits the edge between `block` and `successor` while preserving the
419 // indices in the predecessor/successor lists. If there are multiple edges
420 // between the blocks, the lowest indices are used.
421 // Returns the new block which is empty and has the same dex pc as `successor`.
422 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
423
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100424 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
Artem Serovc73ee372017-07-31 15:08:40 +0100425 void OrderLoopHeaderPredecessors(HBasicBlock* header);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100426 void SimplifyLoop(HBasicBlock* header);
427
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000428 int32_t GetNextInstructionId() {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100429 CHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000430 return current_instruction_id_++;
431 }
432
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000433 int32_t GetCurrentInstructionId() const {
434 return current_instruction_id_;
435 }
436
437 void SetCurrentInstructionId(int32_t id) {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100438 CHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000439 current_instruction_id_ = id;
440 }
441
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100442 uint16_t GetMaximumNumberOfOutVRegs() const {
443 return maximum_number_of_out_vregs_;
444 }
445
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000446 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
447 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100448 }
449
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100450 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
451 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
452 }
453
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000454 void UpdateTemporariesVRegSlots(size_t slots) {
455 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100456 }
457
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000458 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100459 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000460 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100461 }
462
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100463 void SetNumberOfVRegs(uint16_t number_of_vregs) {
464 number_of_vregs_ = number_of_vregs;
465 }
466
467 uint16_t GetNumberOfVRegs() const {
468 return number_of_vregs_;
469 }
470
471 void SetNumberOfInVRegs(uint16_t value) {
472 number_of_in_vregs_ = value;
473 }
474
David Brazdildee58d62016-04-07 09:54:26 +0000475 uint16_t GetNumberOfInVRegs() const {
476 return number_of_in_vregs_;
477 }
478
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100479 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100480 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100481 return number_of_vregs_ - number_of_in_vregs_;
482 }
483
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100484 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100485 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100486 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100487
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100488 ArrayRef<HBasicBlock* const> GetReversePostOrderSkipEntryBlock() {
489 DCHECK(GetReversePostOrder()[0] == entry_block_);
490 return ArrayRef<HBasicBlock* const>(GetReversePostOrder()).SubArray(1);
491 }
492
493 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetPostOrder() const {
494 return ReverseRange(GetReversePostOrder());
495 }
496
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100497 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100498 return linear_order_;
499 }
500
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100501 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetLinearPostOrder() const {
502 return ReverseRange(GetLinearOrder());
503 }
504
Mark Mendell1152c922015-04-24 17:06:35 -0400505 bool HasBoundsChecks() const {
506 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800507 }
508
Mark Mendell1152c922015-04-24 17:06:35 -0400509 void SetHasBoundsChecks(bool value) {
510 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800511 }
512
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000513 bool IsDebuggable() const { return debuggable_; }
514
David Brazdil8d5b8b22015-03-24 10:51:52 +0000515 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000516 // already, it is created and inserted into the graph. This method is only for
517 // integral types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100518 HConstant* GetConstant(DataType::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000519
520 // TODO: This is problematic for the consistency of reference type propagation
521 // because it can be created anytime after the pass and thus it will be left
522 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600523 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000524
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600525 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
526 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000527 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600528 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
529 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000530 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600531 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
532 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000533 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600534 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
535 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000536 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000537
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100538 HCurrentMethod* GetCurrentMethod();
539
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100540 const DexFile& GetDexFile() const {
541 return dex_file_;
542 }
543
544 uint32_t GetMethodIdx() const {
545 return method_idx_;
546 }
547
Igor Murashkind01745e2017-04-05 16:40:31 -0700548 // Get the method name (without the signature), e.g. "<init>"
549 const char* GetMethodName() const;
550
551 // Get the pretty method name (class + name + optionally signature).
552 std::string PrettyMethod(bool with_signature = true) const;
553
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100554 InvokeType GetInvokeType() const {
555 return invoke_type_;
556 }
557
Mark Mendellc4701932015-04-10 13:18:51 -0400558 InstructionSet GetInstructionSet() const {
559 return instruction_set_;
560 }
561
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000562 bool IsCompilingOsr() const { return osr_; }
563
Mingyao Yang063fc772016-08-02 11:02:54 -0700564 ArenaSet<ArtMethod*>& GetCHASingleImplementationList() {
565 return cha_single_implementation_list_;
566 }
567
568 void AddCHASingleImplementationDependency(ArtMethod* method) {
569 cha_single_implementation_list_.insert(method);
570 }
571
572 bool HasShouldDeoptimizeFlag() const {
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800573 return number_of_cha_guards_ != 0;
Mingyao Yang063fc772016-08-02 11:02:54 -0700574 }
575
David Brazdil77a48ae2015-09-15 12:34:04 +0000576 bool HasTryCatch() const { return has_try_catch_; }
577 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100578
Aart Bikb13c65b2017-03-21 20:14:07 -0700579 bool HasSIMD() const { return has_simd_; }
580 void SetHasSIMD(bool value) { has_simd_ = value; }
581
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800582 bool HasLoops() const { return has_loops_; }
583 void SetHasLoops(bool value) { has_loops_ = value; }
584
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000585 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
586 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
587
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100588 ArtMethod* GetArtMethod() const { return art_method_; }
589 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
590
Roland Levillain5e8d5f02016-10-18 18:03:43 +0100591 // Returns an instruction with the opposite Boolean value from 'cond'.
Mark Mendellf6529172015-11-17 11:16:56 -0500592 // The instruction has been inserted into the graph, either as a constant, or
593 // before cursor.
594 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
595
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000596 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
597
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800598 uint32_t GetNumberOfCHAGuards() { return number_of_cha_guards_; }
599 void SetNumberOfCHAGuards(uint32_t num) { number_of_cha_guards_ = num; }
600 void IncrementNumberOfCHAGuards() { number_of_cha_guards_++; }
601
David Brazdil2d7352b2015-04-20 14:52:42 +0100602 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000603 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100604 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000605
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000606 template <class InstructionType, typename ValueType>
607 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600608 ArenaSafeMap<ValueType, InstructionType*>* cache,
609 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000610 // Try to find an existing constant of the given value.
611 InstructionType* constant = nullptr;
612 auto cached_constant = cache->find(value);
613 if (cached_constant != cache->end()) {
614 constant = cached_constant->second;
615 }
616
617 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100618 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000619 if (constant == nullptr || constant->GetBlock() == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100620 constant = new (allocator_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000621 cache->Overwrite(value, constant);
622 InsertConstant(constant);
623 }
624 return constant;
625 }
626
David Brazdil8d5b8b22015-03-24 10:51:52 +0000627 void InsertConstant(HConstant* instruction);
628
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000629 // Cache a float constant into the graph. This method should only be
630 // called by the SsaBuilder when creating "equivalent" instructions.
631 void CacheFloatConstant(HFloatConstant* constant);
632
633 // See CacheFloatConstant comment.
634 void CacheDoubleConstant(HDoubleConstant* constant);
635
Vladimir Markoca6fff82017-10-03 14:49:14 +0100636 ArenaAllocator* const allocator_;
637 ArenaStack* const arena_stack_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000638
639 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100640 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000641
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100642 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100643 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000644
Aart Bik281c6812016-08-26 11:31:48 -0700645 // List of blocks to perform a linear order tree traversal. Unlike the reverse
646 // post order, this order is not incrementally kept up-to-date.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100647 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100648
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000649 HBasicBlock* entry_block_;
650 HBasicBlock* exit_block_;
651
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100652 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100653 uint16_t maximum_number_of_out_vregs_;
654
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100655 // The number of virtual registers in this method. Contains the parameters.
656 uint16_t number_of_vregs_;
657
658 // The number of virtual registers used by parameters of this method.
659 uint16_t number_of_in_vregs_;
660
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000661 // Number of vreg size slots that the temporaries use (used in baseline compiler).
662 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100663
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800664 // Flag whether there are bounds checks in the graph. We can skip
665 // BCE if it's false. It's only best effort to keep it up to date in
666 // the presence of code elimination so there might be false positives.
Mark Mendell1152c922015-04-24 17:06:35 -0400667 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800668
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800669 // Flag whether there are try/catch blocks in the graph. We will skip
670 // try/catch-related passes if it's false. It's only best effort to keep
671 // it up to date in the presence of code elimination so there might be
672 // false positives.
David Brazdil77a48ae2015-09-15 12:34:04 +0000673 bool has_try_catch_;
674
Aart Bikb13c65b2017-03-21 20:14:07 -0700675 // Flag whether SIMD instructions appear in the graph. If true, the
676 // code generators may have to be more careful spilling the wider
677 // contents of SIMD registers.
678 bool has_simd_;
679
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800680 // Flag whether there are any loops in the graph. We can skip loop
681 // optimization if it's false. It's only best effort to keep it up
682 // to date in the presence of code elimination so there might be false
683 // positives.
684 bool has_loops_;
685
686 // Flag whether there are any irreducible loops in the graph. It's only
687 // best effort to keep it up to date in the presence of code elimination
688 // so there might be false positives.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000689 bool has_irreducible_loops_;
690
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000691 // Indicates whether the graph should be compiled in a way that
692 // ensures full debuggability. If false, we can apply more
693 // aggressive optimizations that may limit the level of debugging.
694 const bool debuggable_;
695
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000696 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000697 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000698
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100699 // The dex file from which the method is from.
700 const DexFile& dex_file_;
701
702 // The method index in the dex file.
703 const uint32_t method_idx_;
704
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100705 // If inlined, this encodes how the callee is being invoked.
706 const InvokeType invoke_type_;
707
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100708 // Whether the graph has been transformed to SSA form. Only used
709 // in debug mode to ensure we are not using properties only valid
710 // for non-SSA form (like the number of temporaries).
711 bool in_ssa_form_;
712
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800713 // Number of CHA guards in the graph. Used to short-circuit the
714 // CHA guard optimization pass when there is no CHA guard left.
715 uint32_t number_of_cha_guards_;
716
Mathieu Chartiere401d142015-04-22 13:56:20 -0700717 const InstructionSet instruction_set_;
718
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000719 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000720 HNullConstant* cached_null_constant_;
721 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000722 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000723 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000724 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000725
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100726 HCurrentMethod* cached_current_method_;
727
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100728 // The ArtMethod this graph is for. Note that for AOT, it may be null,
729 // for example for methods whose declaring class could not be resolved
730 // (such as when the superclass could not be found).
731 ArtMethod* art_method_;
732
David Brazdil4833f5a2015-12-16 10:37:39 +0000733 // Keep the RTI of inexact Object to avoid having to pass stack handle
734 // collection pointer to passes which may create NullConstant.
735 ReferenceTypeInfo inexact_object_rti_;
736
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000737 // Whether we are compiling this graph for on stack replacement: this will
738 // make all loops seen as irreducible and emit special stack maps to mark
739 // compiled code entries which the interpreter can directly jump to.
740 const bool osr_;
741
Mingyao Yang063fc772016-08-02 11:02:54 -0700742 // List of methods that are assumed to have single implementation.
743 ArenaSet<ArtMethod*> cha_single_implementation_list_;
744
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000745 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100746 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000747 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000748 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000749 DISALLOW_COPY_AND_ASSIGN(HGraph);
750};
751
Vladimir Markof9f64412015-09-02 14:05:49 +0100752class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000753 public:
754 HLoopInformation(HBasicBlock* header, HGraph* graph)
755 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100756 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000757 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100758 contains_irreducible_loop_(false),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100759 back_edges_(graph->GetAllocator()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100760 // Make bit vector growable, as the number of blocks may change.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100761 blocks_(graph->GetAllocator(),
762 graph->GetBlocks().size(),
763 true,
764 kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100765 back_edges_.reserve(kDefaultNumberOfBackEdges);
766 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100767
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000768 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100769 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000770
771 void Dump(std::ostream& os);
772
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100773 HBasicBlock* GetHeader() const {
774 return header_;
775 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000776
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000777 void SetHeader(HBasicBlock* block) {
778 header_ = block;
779 }
780
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100781 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
782 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
783 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
784
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000785 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100786 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000787 }
788
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100789 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100790 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100791 }
792
David Brazdil46e2a392015-03-16 17:31:52 +0000793 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100794 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100795 }
796
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000797 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100798 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000799 }
800
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100801 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100802
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100803 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100804 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100805 }
806
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100807 // Returns the lifetime position of the back edge that has the
808 // greatest lifetime position.
809 size_t GetLifetimeEnd() const;
810
811 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100812 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100813 }
814
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000815 // Finds blocks that are part of this loop.
816 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100817
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100818 // Returns whether this loop information contains `block`.
819 // Note that this loop information *must* be populated before entering this function.
820 bool Contains(const HBasicBlock& block) const;
821
822 // Returns whether this loop information is an inner loop of `other`.
823 // Note that `other` *must* be populated before entering this function.
824 bool IsIn(const HLoopInformation& other) const;
825
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800826 // Returns true if instruction is not defined within this loop.
827 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700828
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100829 const ArenaBitVector& GetBlocks() const { return blocks_; }
830
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000831 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000832 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000833
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000834 void ClearAllBlocks() {
835 blocks_.ClearAllBits();
836 }
837
David Brazdil3f4a5222016-05-06 12:46:21 +0100838 bool HasBackEdgeNotDominatedByHeader() const;
839
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100840 bool IsPopulated() const {
841 return blocks_.GetHighestBitSet() != -1;
842 }
843
Anton Shaminf89381f2016-05-16 16:44:13 +0600844 bool DominatesAllBackEdges(HBasicBlock* block);
845
David Sehrc757dec2016-11-04 15:48:34 -0700846 bool HasExitEdge() const;
847
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000848 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100849 // Internal recursive implementation of `Populate`.
850 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100851 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100852
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000853 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100854 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000855 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100856 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100857 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100858 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000859
860 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
861};
862
David Brazdilec16f792015-08-19 15:04:01 +0100863// Stores try/catch information for basic blocks.
864// Note that HGraph is constructed so that catch blocks cannot simultaneously
865// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100866class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100867 public:
868 // Try block information constructor.
869 explicit TryCatchInformation(const HTryBoundary& try_entry)
870 : try_entry_(&try_entry),
871 catch_dex_file_(nullptr),
872 catch_type_index_(DexFile::kDexNoIndex16) {
873 DCHECK(try_entry_ != nullptr);
874 }
875
876 // Catch block information constructor.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800877 TryCatchInformation(dex::TypeIndex catch_type_index, const DexFile& dex_file)
David Brazdilec16f792015-08-19 15:04:01 +0100878 : try_entry_(nullptr),
879 catch_dex_file_(&dex_file),
880 catch_type_index_(catch_type_index) {}
881
882 bool IsTryBlock() const { return try_entry_ != nullptr; }
883
884 const HTryBoundary& GetTryEntry() const {
885 DCHECK(IsTryBlock());
886 return *try_entry_;
887 }
888
889 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
890
891 bool IsCatchAllTypeIndex() const {
892 DCHECK(IsCatchBlock());
Andreas Gampea5b09a62016-11-17 15:21:22 -0800893 return !catch_type_index_.IsValid();
David Brazdilec16f792015-08-19 15:04:01 +0100894 }
895
Andreas Gampea5b09a62016-11-17 15:21:22 -0800896 dex::TypeIndex GetCatchTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +0100897 DCHECK(IsCatchBlock());
898 return catch_type_index_;
899 }
900
901 const DexFile& GetCatchDexFile() const {
902 DCHECK(IsCatchBlock());
903 return *catch_dex_file_;
904 }
905
906 private:
907 // One of possibly several TryBoundary instructions entering the block's try.
908 // Only set for try blocks.
909 const HTryBoundary* try_entry_;
910
911 // Exception type information. Only set for catch blocks.
912 const DexFile* catch_dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800913 const dex::TypeIndex catch_type_index_;
David Brazdilec16f792015-08-19 15:04:01 +0100914};
915
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100916static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100917static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100918
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000919// A block in a method. Contains the list of instructions represented
920// as a double linked list. Each block knows its predecessors and
921// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100922
Vladimir Markof9f64412015-09-02 14:05:49 +0100923class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000924 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700925 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000926 : graph_(graph),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100927 predecessors_(graph->GetAllocator()->Adapter(kArenaAllocPredecessors)),
928 successors_(graph->GetAllocator()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000929 loop_information_(nullptr),
930 dominator_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100931 dominated_blocks_(graph->GetAllocator()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100932 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100933 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100934 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000935 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000936 try_catch_information_(nullptr) {
937 predecessors_.reserve(kDefaultNumberOfPredecessors);
938 successors_.reserve(kDefaultNumberOfSuccessors);
939 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
940 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000941
Vladimir Marko60584552015-09-03 13:35:12 +0000942 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100943 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000944 }
945
Vladimir Marko60584552015-09-03 13:35:12 +0000946 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100947 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000948 }
949
David Brazdild26a4112015-11-10 11:07:31 +0000950 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
951 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
952
Vladimir Marko60584552015-09-03 13:35:12 +0000953 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
954 return ContainsElement(successors_, block, start_from);
955 }
956
957 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100958 return dominated_blocks_;
959 }
960
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100961 bool IsEntryBlock() const {
962 return graph_->GetEntryBlock() == this;
963 }
964
965 bool IsExitBlock() const {
966 return graph_->GetExitBlock() == this;
967 }
968
David Brazdil46e2a392015-03-16 17:31:52 +0000969 bool IsSingleGoto() const;
Mads Ager16e52892017-07-14 13:11:37 +0200970 bool IsSingleReturn() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000971 bool IsSingleTryBoundary() const;
972
973 // Returns true if this block emits nothing but a jump.
974 bool IsSingleJump() const {
975 HLoopInformation* loop_info = GetLoopInformation();
976 return (IsSingleGoto() || IsSingleTryBoundary())
977 // Back edges generate a suspend check.
978 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
979 }
David Brazdil46e2a392015-03-16 17:31:52 +0000980
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000981 void AddBackEdge(HBasicBlock* back_edge) {
982 if (loop_information_ == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100983 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000984 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100985 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000986 loop_information_->AddBackEdge(back_edge);
987 }
988
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000989 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000990 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000991
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100992 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000993 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600994 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000995
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000996 HBasicBlock* GetDominator() const { return dominator_; }
997 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000998 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
999
1000 void RemoveDominatedBlock(HBasicBlock* block) {
1001 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +01001002 }
Vladimir Marko60584552015-09-03 13:35:12 +00001003
1004 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
1005 ReplaceElement(dominated_blocks_, existing, new_block);
1006 }
1007
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001008 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001009
1010 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001011 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001012 }
1013
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001014 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
1015 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +01001016 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001017 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +01001018 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
1019 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001020
Nicolas Geoffray09aa1472016-01-19 10:52:54 +00001021 HInstruction* GetFirstInstructionDisregardMoves() const;
1022
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001023 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001024 successors_.push_back(block);
1025 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001026 }
1027
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001028 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
1029 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001030 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001031 new_block->predecessors_.push_back(this);
1032 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001033 }
1034
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001035 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
1036 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001037 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001038 new_block->successors_.push_back(this);
1039 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001040 }
1041
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001042 // Insert `this` between `predecessor` and `successor. This method
1043 // preserves the indicies, and will update the first edge found between
1044 // `predecessor` and `successor`.
1045 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
1046 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001047 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +00001048 successor->predecessors_[predecessor_index] = this;
1049 predecessor->successors_[successor_index] = this;
1050 successors_.push_back(successor);
1051 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001052 }
1053
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001054 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001055 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001056 }
1057
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001058 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001059 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001060 }
1061
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001062 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001063 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001064 }
1065
1066 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001067 predecessors_.push_back(block);
1068 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001069 }
1070
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001071 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001072 DCHECK_EQ(predecessors_.size(), 2u);
1073 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001074 }
1075
David Brazdil769c9e52015-04-27 13:54:09 +01001076 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001077 DCHECK_EQ(successors_.size(), 2u);
1078 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +01001079 }
1080
David Brazdilfc6a86a2015-06-26 10:33:45 +00001081 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001082 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001083 }
1084
David Brazdilfc6a86a2015-06-26 10:33:45 +00001085 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001086 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001087 }
1088
David Brazdilfc6a86a2015-06-26 10:33:45 +00001089 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001090 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001091 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001092 }
1093
1094 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001095 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001096 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001097 }
1098
1099 // Returns whether the first occurrence of `predecessor` in the list of
1100 // predecessors is at index `idx`.
1101 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001102 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001103 return GetPredecessorIndexOf(predecessor) == idx;
1104 }
1105
David Brazdild7558da2015-09-22 13:04:14 +01001106 // Create a new block between this block and its predecessors. The new block
1107 // is added to the graph, all predecessor edges are relinked to it and an edge
1108 // is created to `this`. Returns the new empty block. Reverse post order or
1109 // loop and try/catch information are not updated.
1110 HBasicBlock* CreateImmediateDominator();
1111
David Brazdilfc6a86a2015-06-26 10:33:45 +00001112 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001113 // created, latter block. Note that this method will add the block to the
1114 // graph, create a Goto at the end of the former block and will create an edge
1115 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001116 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001117 HBasicBlock* SplitBefore(HInstruction* cursor);
1118
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001119 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001120 // created block. Note that this method just updates raw block information,
1121 // like predecessors, successors, dominators, and instruction list. It does not
1122 // update the graph, reverse post order, loop information, nor make sure the
1123 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001124 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1125
1126 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1127 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001128
1129 // Merge `other` at the end of `this`. Successors and dominated blocks of
1130 // `other` are changed to be successors and dominated blocks of `this`. Note
1131 // that this method does not update the graph, reverse post order, loop
1132 // information, nor make sure the blocks are consistent (for example ending
1133 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001134 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001135
1136 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1137 // of `this` are moved to `other`.
1138 // Note that this method does not update the graph, reverse post order, loop
1139 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001140 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001141 void ReplaceWith(HBasicBlock* other);
1142
Aart Bik6b69e0a2017-01-11 10:20:43 -08001143 // Merges the instructions of `other` at the end of `this`.
1144 void MergeInstructionsWith(HBasicBlock* other);
1145
David Brazdil2d7352b2015-04-20 14:52:42 +01001146 // Merge `other` at the end of `this`. This method updates loops, reverse post
1147 // order, links to predecessors, successors, dominators and deletes the block
1148 // from the graph. The two blocks must be successive, i.e. `this` the only
1149 // predecessor of `other` and vice versa.
1150 void MergeWith(HBasicBlock* other);
1151
1152 // Disconnects `this` from all its predecessors, successors and dominator,
1153 // removes it from all loops it is included in and eventually from the graph.
1154 // The block must not dominate any other block. Predecessors and successors
1155 // are safely updated.
1156 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001157
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001158 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001159 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001160 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001161 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001162 // Replace instruction `initial` with `replacement` within this block.
1163 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1164 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001165 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001166 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001167 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1168 // instruction list. With 'ensure_safety' set to true, it verifies that the
1169 // instruction is not in use and removes it from the use lists of its inputs.
1170 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1171 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001172 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001173
1174 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001175 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001176 }
1177
Roland Levillain6b879dd2014-09-22 17:13:44 +01001178 bool IsLoopPreHeaderFirstPredecessor() const {
1179 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001180 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001181 }
1182
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001183 bool IsFirstPredecessorBackEdge() const {
1184 DCHECK(IsLoopHeader());
1185 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1186 }
1187
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001188 HLoopInformation* GetLoopInformation() const {
1189 return loop_information_;
1190 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001191
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001192 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001193 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001194 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001195 void SetInLoop(HLoopInformation* info) {
1196 if (IsLoopHeader()) {
1197 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001198 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001199 loop_information_ = info;
1200 } else if (loop_information_->Contains(*info->GetHeader())) {
1201 // Block is currently part of an outer loop. Make it part of this inner loop.
1202 // Note that a non loop header having a loop information means this loop information
1203 // has already been populated
1204 loop_information_ = info;
1205 } else {
1206 // Block is part of an inner loop. Do not update the loop information.
1207 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1208 // at this point, because this method is being called while populating `info`.
1209 }
1210 }
1211
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001212 // Raw update of the loop information.
1213 void SetLoopInformation(HLoopInformation* info) {
1214 loop_information_ = info;
1215 }
1216
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001217 bool IsInLoop() const { return loop_information_ != nullptr; }
1218
David Brazdilec16f792015-08-19 15:04:01 +01001219 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1220
1221 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1222 try_catch_information_ = try_catch_information;
1223 }
1224
1225 bool IsTryBlock() const {
1226 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1227 }
1228
1229 bool IsCatchBlock() const {
1230 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1231 }
David Brazdilffee3d32015-07-06 11:48:53 +01001232
1233 // Returns the try entry that this block's successors should have. They will
1234 // be in the same try, unless the block ends in a try boundary. In that case,
1235 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001236 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001237
David Brazdild7558da2015-09-22 13:04:14 +01001238 bool HasThrowingInstructions() const;
1239
David Brazdila4b8c212015-05-07 09:59:30 +01001240 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001241 bool Dominates(HBasicBlock* block) const;
1242
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001243 size_t GetLifetimeStart() const { return lifetime_start_; }
1244 size_t GetLifetimeEnd() const { return lifetime_end_; }
1245
1246 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1247 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1248
David Brazdil8d5b8b22015-03-24 10:51:52 +00001249 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001250 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001251 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001252 bool HasSinglePhi() const;
1253
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001254 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001255 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001256 ArenaVector<HBasicBlock*> predecessors_;
1257 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001258 HInstructionList instructions_;
1259 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001260 HLoopInformation* loop_information_;
1261 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001262 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001263 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001264 // The dex program counter of the first instruction of this block.
1265 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001266 size_t lifetime_start_;
1267 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001268 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001269
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001270 friend class HGraph;
1271 friend class HInstruction;
1272
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001273 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1274};
1275
David Brazdilb2bd1c52015-03-25 11:17:37 +00001276// Iterates over the LoopInformation of all loops which contain 'block'
1277// from the innermost to the outermost.
1278class HLoopInformationOutwardIterator : public ValueObject {
1279 public:
1280 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1281 : current_(block.GetLoopInformation()) {}
1282
1283 bool Done() const { return current_ == nullptr; }
1284
1285 void Advance() {
1286 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001287 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001288 }
1289
1290 HLoopInformation* Current() const {
1291 DCHECK(!Done());
1292 return current_;
1293 }
1294
1295 private:
1296 HLoopInformation* current_;
1297
1298 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1299};
1300
Alexandre Ramesef20f712015-06-09 10:29:30 +01001301#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001302 M(Above, Condition) \
1303 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001304 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001305 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001306 M(ArrayGet, Instruction) \
1307 M(ArrayLength, Instruction) \
1308 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001309 M(Below, Condition) \
1310 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001311 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001312 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001313 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001314 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001315 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001316 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001317 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001318 M(Compare, BinaryOperation) \
Igor Murashkind01745e2017-04-05 16:40:31 -07001319 M(ConstructorFence, Instruction) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001320 M(CurrentMethod, Instruction) \
Mingyao Yang063fc772016-08-02 11:02:54 -07001321 M(ShouldDeoptimizeFlag, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001322 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001323 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001324 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001325 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001326 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001327 M(Exit, Instruction) \
1328 M(FloatConstant, Constant) \
1329 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001330 M(GreaterThan, Condition) \
1331 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001332 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001333 M(InstanceFieldGet, Instruction) \
1334 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001335 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001336 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001337 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001338 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001339 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001340 M(InvokeVirtual, Invoke) \
Orion Hodsonac141392017-01-13 11:53:47 +00001341 M(InvokePolymorphic, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001342 M(LessThan, Condition) \
1343 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001344 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001345 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001346 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001347 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001348 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001349 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001350 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001351 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001352 M(Neg, UnaryOperation) \
1353 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001354 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001355 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001356 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001357 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001358 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001359 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001360 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001361 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001362 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001363 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001364 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001365 M(Return, Instruction) \
1366 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001367 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001368 M(Shl, BinaryOperation) \
1369 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001370 M(StaticFieldGet, Instruction) \
1371 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001372 M(UnresolvedInstanceFieldGet, Instruction) \
1373 M(UnresolvedInstanceFieldSet, Instruction) \
1374 M(UnresolvedStaticFieldGet, Instruction) \
1375 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001376 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001377 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001378 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001379 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001380 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001381 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001382 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001383 M(Xor, BinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001384 M(VecReplicateScalar, VecUnaryOperation) \
Aart Bik0148de42017-09-05 09:25:01 -07001385 M(VecExtractScalar, VecUnaryOperation) \
1386 M(VecReduce, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001387 M(VecCnv, VecUnaryOperation) \
1388 M(VecNeg, VecUnaryOperation) \
Aart Bik6daebeb2017-04-03 14:35:41 -07001389 M(VecAbs, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001390 M(VecNot, VecUnaryOperation) \
1391 M(VecAdd, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001392 M(VecHalvingAdd, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001393 M(VecSub, VecBinaryOperation) \
1394 M(VecMul, VecBinaryOperation) \
1395 M(VecDiv, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001396 M(VecMin, VecBinaryOperation) \
1397 M(VecMax, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001398 M(VecAnd, VecBinaryOperation) \
1399 M(VecAndNot, VecBinaryOperation) \
1400 M(VecOr, VecBinaryOperation) \
1401 M(VecXor, VecBinaryOperation) \
1402 M(VecShl, VecBinaryOperation) \
1403 M(VecShr, VecBinaryOperation) \
1404 M(VecUShr, VecBinaryOperation) \
Aart Bik8de59162017-04-21 09:42:01 -07001405 M(VecSetScalars, VecOperation) \
Artem Serovf34dd202017-04-10 17:41:46 +01001406 M(VecMultiplyAccumulate, VecOperation) \
Aart Bikdbbac8f2017-09-01 13:06:08 -07001407 M(VecSADAccumulate, VecOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001408 M(VecLoad, VecMemoryOperation) \
1409 M(VecStore, VecMemoryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001410
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001411/*
1412 * Instructions, shared across several (not all) architectures.
1413 */
1414#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1415#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1416#else
1417#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001418 M(BitwiseNegatedRight, Instruction) \
Anton Kirilov74234da2017-01-13 14:42:47 +00001419 M(DataProcWithShifterOp, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001420 M(MultiplyAccumulate, Instruction) \
Artem Serove1811ed2017-04-27 16:50:47 +01001421 M(IntermediateAddress, Instruction) \
1422 M(IntermediateAddressIndex, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001423#endif
1424
Alexandre Ramesef20f712015-06-09 10:29:30 +01001425#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1426
1427#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1428
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001429#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001430#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001431#else
1432#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1433 M(MipsComputeBaseMethodAddress, Instruction) \
Lena Djokica2901602017-09-21 13:50:52 +02001434 M(MipsPackedSwitch, Instruction) \
1435 M(IntermediateArrayAddressIndex, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001436#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001437
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001438#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1439
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001440#ifndef ART_ENABLE_CODEGEN_x86
1441#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1442#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001443#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1444 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001445 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001446 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001447 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001448#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001449
1450#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1451
1452#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1453 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001454 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001455 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1456 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001457 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001458 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001459 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1460 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1461
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001462#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1463 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001464 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001465 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001466 M(BinaryOperation, Instruction) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001467 M(Invoke, Instruction) \
1468 M(VecOperation, Instruction) \
1469 M(VecUnaryOperation, VecOperation) \
1470 M(VecBinaryOperation, VecOperation) \
1471 M(VecMemoryOperation, VecOperation)
Dave Allison20dfc792014-06-16 20:44:29 -07001472
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001473#define FOR_EACH_INSTRUCTION(M) \
1474 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1475 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1476
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001477#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001478FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1479#undef FORWARD_DECLARATION
1480
Vladimir Marko372f10e2016-05-17 16:30:10 +01001481#define DECLARE_INSTRUCTION(type) \
1482 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
1483 const char* DebugName() const OVERRIDE { return #type; } \
1484 bool InstructionTypeEquals(const HInstruction* other) const OVERRIDE { \
1485 return other->Is##type(); \
1486 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001487 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001488
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001489#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1490 bool Is##type() const { return As##type() != nullptr; } \
1491 const H##type* As##type() const { return this; } \
1492 H##type* As##type() { return this; }
1493
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001494template <typename T>
Vladimir Marko82b07402017-03-01 19:02:04 +00001495class HUseListNode : public ArenaObject<kArenaAllocUseListNode>,
1496 public IntrusiveForwardListNode<HUseListNode<T>> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001497 public:
Igor Murashkind01745e2017-04-05 16:40:31 -07001498 // Get the instruction which has this use as one of the inputs.
David Brazdiled596192015-01-23 10:39:45 +00001499 T GetUser() const { return user_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001500 // Get the position of the input record that this use corresponds to.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001501 size_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001502 // Set the position of the input record that this use corresponds to.
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001503 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001504
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001505 private:
David Brazdiled596192015-01-23 10:39:45 +00001506 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001507 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001508
1509 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001510 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001511
Vladimir Marko46817b82016-03-29 12:21:58 +01001512 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001513
1514 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1515};
1516
David Brazdiled596192015-01-23 10:39:45 +00001517template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001518using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001519
David Brazdil1abb4192015-02-17 18:33:36 +00001520// This class is used by HEnvironment and HInstruction classes to record the
1521// instructions they use and pointers to the corresponding HUseListNodes kept
1522// by the used instructions.
1523template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001524class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001525 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001526 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1527 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001528
Vladimir Marko46817b82016-03-29 12:21:58 +01001529 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1530 : HUserRecord(old_record.instruction_, before_use_node) {}
1531 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1532 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001533 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001534 }
1535
1536 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001537 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1538 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001539
1540 private:
1541 // Instruction used by the user.
1542 HInstruction* instruction_;
1543
Vladimir Marko46817b82016-03-29 12:21:58 +01001544 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1545 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001546};
1547
Vladimir Markoe9004912016-06-16 16:50:52 +01001548// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1549// This is used for HInstruction::GetInputs() to return a container wrapper providing
1550// HInstruction* values even though the underlying container has HUserRecord<>s.
1551struct HInputExtractor {
1552 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1553 return record.GetInstruction();
1554 }
1555 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1556 return record.GetInstruction();
1557 }
1558};
1559
1560using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1561using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1562
Aart Bik854a02b2015-07-14 16:07:00 -07001563/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001564 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001565 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001566 * For write/read dependences on fields/arrays, the dependence analysis uses
1567 * type disambiguation (e.g. a float field write cannot modify the value of an
1568 * integer field read) and the access type (e.g. a reference array write cannot
1569 * modify the value of a reference field read [although it may modify the
1570 * reference fetch prior to reading the field, which is represented by its own
1571 * write/read dependence]). The analysis makes conservative points-to
1572 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1573 * the same, and any reference read depends on any reference read without
1574 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001575 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001576 * The internal representation uses 38-bit and is described in the table below.
1577 * The first line indicates the side effect, and for field/array accesses the
1578 * second line indicates the type of the access (in the order of the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001579 * DataType::Type enum).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001580 * The two numbered lines below indicate the bit position in the bitfield (read
1581 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001582 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001583 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1584 * +-------------+---------+---------+--------------+---------+---------+
1585 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1586 * | 3 |333333322|222222221| 1 |111111110|000000000|
1587 * | 7 |654321098|765432109| 8 |765432109|876543210|
1588 *
1589 * Note that, to ease the implementation, 'changes' bits are least significant
1590 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001591 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001592class SideEffects : public ValueObject {
1593 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001594 SideEffects() : flags_(0) {}
1595
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001596 static SideEffects None() {
1597 return SideEffects(0);
1598 }
1599
1600 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001601 return SideEffects(kAllChangeBits | kAllDependOnBits);
1602 }
1603
1604 static SideEffects AllChanges() {
1605 return SideEffects(kAllChangeBits);
1606 }
1607
1608 static SideEffects AllDependencies() {
1609 return SideEffects(kAllDependOnBits);
1610 }
1611
1612 static SideEffects AllExceptGCDependency() {
1613 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1614 }
1615
1616 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001617 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001618 }
1619
Aart Bik34c3ba92015-07-20 14:08:59 -07001620 static SideEffects AllWrites() {
1621 return SideEffects(kAllWrites);
1622 }
1623
1624 static SideEffects AllReads() {
1625 return SideEffects(kAllReads);
1626 }
1627
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001628 static SideEffects FieldWriteOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001629 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001630 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001631 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001632 }
1633
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001634 static SideEffects ArrayWriteOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001635 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001636 }
1637
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001638 static SideEffects FieldReadOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001639 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001640 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001641 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001642 }
1643
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001644 static SideEffects ArrayReadOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001645 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001646 }
1647
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001648 static SideEffects CanTriggerGC() {
1649 return SideEffects(1ULL << kCanTriggerGCBit);
1650 }
1651
1652 static SideEffects DependsOnGC() {
1653 return SideEffects(1ULL << kDependsOnGCBit);
1654 }
1655
Aart Bik854a02b2015-07-14 16:07:00 -07001656 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001657 SideEffects Union(SideEffects other) const {
1658 return SideEffects(flags_ | other.flags_);
1659 }
1660
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001661 SideEffects Exclusion(SideEffects other) const {
1662 return SideEffects(flags_ & ~other.flags_);
1663 }
1664
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001665 void Add(SideEffects other) {
1666 flags_ |= other.flags_;
1667 }
1668
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001669 bool Includes(SideEffects other) const {
1670 return (other.flags_ & flags_) == other.flags_;
1671 }
1672
1673 bool HasSideEffects() const {
1674 return (flags_ & kAllChangeBits);
1675 }
1676
1677 bool HasDependencies() const {
1678 return (flags_ & kAllDependOnBits);
1679 }
1680
1681 // Returns true if there are no side effects or dependencies.
1682 bool DoesNothing() const {
1683 return flags_ == 0;
1684 }
1685
Aart Bik854a02b2015-07-14 16:07:00 -07001686 // Returns true if something is written.
1687 bool DoesAnyWrite() const {
1688 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001689 }
1690
Aart Bik854a02b2015-07-14 16:07:00 -07001691 // Returns true if something is read.
1692 bool DoesAnyRead() const {
1693 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001694 }
1695
Aart Bik854a02b2015-07-14 16:07:00 -07001696 // Returns true if potentially everything is written and read
1697 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001698 bool DoesAllReadWrite() const {
1699 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1700 }
1701
Aart Bik854a02b2015-07-14 16:07:00 -07001702 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001703 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001704 }
1705
Roland Levillain0d5a2812015-11-13 10:07:31 +00001706 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001707 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001708 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1709 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001710 }
1711
1712 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001713 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001714 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001715 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001716 for (int s = kLastBit; s >= 0; s--) {
1717 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1718 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1719 // This is a bit for the GC side effect.
1720 if (current_bit_is_set) {
1721 flags += "GC";
1722 }
Aart Bik854a02b2015-07-14 16:07:00 -07001723 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001724 } else {
1725 // This is a bit for the array/field analysis.
1726 // The underscore character stands for the 'can trigger GC' bit.
1727 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1728 if (current_bit_is_set) {
1729 flags += kDebug[s];
1730 }
1731 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1732 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1733 flags += "|";
1734 }
1735 }
Aart Bik854a02b2015-07-14 16:07:00 -07001736 }
1737 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001738 }
1739
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001740 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001741
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001742 private:
1743 static constexpr int kFieldArrayAnalysisBits = 9;
1744
1745 static constexpr int kFieldWriteOffset = 0;
1746 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1747 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1748 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1749
1750 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1751
1752 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1753 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1754 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1755 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1756
1757 static constexpr int kLastBit = kDependsOnGCBit;
1758 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1759
1760 // Aliases.
1761
1762 static_assert(kChangeBits == kDependOnBits,
1763 "the 'change' bits should match the 'depend on' bits.");
1764
1765 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1766 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1767 static constexpr uint64_t kAllWrites =
1768 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1769 static constexpr uint64_t kAllReads =
1770 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001771
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001772 // Translates type to bit flag. The type must correspond to a Java type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001773 static uint64_t TypeFlag(DataType::Type type, int offset) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001774 int shift;
1775 switch (type) {
1776 case DataType::Type::kReference: shift = 0; break;
1777 case DataType::Type::kBool: shift = 1; break;
1778 case DataType::Type::kInt8: shift = 2; break;
1779 case DataType::Type::kUint16: shift = 3; break;
1780 case DataType::Type::kInt16: shift = 4; break;
1781 case DataType::Type::kInt32: shift = 5; break;
1782 case DataType::Type::kInt64: shift = 6; break;
1783 case DataType::Type::kFloat32: shift = 7; break;
1784 case DataType::Type::kFloat64: shift = 8; break;
1785 default:
1786 LOG(FATAL) << "Unexpected data type " << type;
1787 UNREACHABLE();
1788 }
Aart Bik854a02b2015-07-14 16:07:00 -07001789 DCHECK_LE(kFieldWriteOffset, shift);
1790 DCHECK_LT(shift, kArrayWriteOffset);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001791 return UINT64_C(1) << (shift + offset);
Aart Bik854a02b2015-07-14 16:07:00 -07001792 }
1793
1794 // Private constructor on direct flags value.
1795 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1796
1797 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001798};
1799
David Brazdiled596192015-01-23 10:39:45 +00001800// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001801class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001802 public:
Mingyao Yang01b47b02017-02-03 12:09:57 -08001803 ALWAYS_INLINE HEnvironment(ArenaAllocator* arena,
1804 size_t number_of_vregs,
1805 ArtMethod* method,
1806 uint32_t dex_pc,
1807 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001808 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
Vladimir Markoec32f642017-06-02 10:51:55 +01001809 locations_(arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001810 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001811 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001812 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001813 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001814 }
1815
Mingyao Yang01b47b02017-02-03 12:09:57 -08001816 ALWAYS_INLINE HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001817 : HEnvironment(arena,
1818 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001819 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001820 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001821 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001822
Vladimir Markoec32f642017-06-02 10:51:55 +01001823 void AllocateLocations() {
1824 DCHECK(locations_.empty());
1825 locations_.resize(vregs_.size());
1826 }
1827
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001828 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001829 if (parent_ != nullptr) {
1830 parent_->SetAndCopyParentChain(allocator, parent);
1831 } else {
1832 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1833 parent_->CopyFrom(parent);
1834 if (parent->GetParent() != nullptr) {
1835 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1836 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001837 }
David Brazdiled596192015-01-23 10:39:45 +00001838 }
1839
Vladimir Marko71bf8092015-09-15 15:33:14 +01001840 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001841 void CopyFrom(HEnvironment* environment);
1842
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001843 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1844 // input to the loop phi instead. This is for inserting instructions that
1845 // require an environment (like HDeoptimization) in the loop pre-header.
1846 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001847
1848 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001849 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001850 }
1851
1852 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001853 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001854 }
1855
David Brazdil1abb4192015-02-17 18:33:36 +00001856 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001857
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001858 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001859
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001860 HEnvironment* GetParent() const { return parent_; }
1861
1862 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001863 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001864 }
1865
1866 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001867 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001868 }
1869
1870 uint32_t GetDexPc() const {
1871 return dex_pc_;
1872 }
1873
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001874 ArtMethod* GetMethod() const {
1875 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001876 }
1877
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001878 HInstruction* GetHolder() const {
1879 return holder_;
1880 }
1881
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001882
1883 bool IsFromInlinedInvoke() const {
1884 return GetParent() != nullptr;
1885 }
1886
David Brazdiled596192015-01-23 10:39:45 +00001887 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001888 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1889 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001890 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001891 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001892 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00001893
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001894 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001895 HInstruction* const holder_;
1896
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001897 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001898
1899 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1900};
1901
Vladimir Markof9f64412015-09-02 14:05:49 +01001902class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001903 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001904 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001905 : previous_(nullptr),
1906 next_(nullptr),
1907 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001908 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001909 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001910 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001911 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001912 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001913 locations_(nullptr),
1914 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001915 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001916 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001917 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1918 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1919 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001920
Dave Allison20dfc792014-06-16 20:44:29 -07001921 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001922
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001923#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001924 enum InstructionKind {
1925 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1926 };
1927#undef DECLARE_KIND
1928
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001929 HInstruction* GetNext() const { return next_; }
1930 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001931
Calin Juravle77520bc2015-01-12 18:45:46 +00001932 HInstruction* GetNextDisregardingMoves() const;
1933 HInstruction* GetPreviousDisregardingMoves() const;
1934
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001935 HBasicBlock* GetBlock() const { return block_; }
Vladimir Markoca6fff82017-10-03 14:49:14 +01001936 ArenaAllocator* GetAllocator() const { return block_->GetGraph()->GetAllocator(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001937 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001938 bool IsInBlock() const { return block_ != nullptr; }
1939 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001940 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1941 bool IsIrreducibleLoopHeaderPhi() const {
1942 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1943 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001944
Vladimir Marko372f10e2016-05-17 16:30:10 +01001945 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
1946
1947 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
1948 // One virtual method is enough, just const_cast<> and then re-add the const.
1949 return ArrayRef<const HUserRecord<HInstruction*>>(
1950 const_cast<HInstruction*>(this)->GetInputRecords());
1951 }
1952
Vladimir Markoe9004912016-06-16 16:50:52 +01001953 HInputsRef GetInputs() {
1954 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001955 }
1956
Vladimir Markoe9004912016-06-16 16:50:52 +01001957 HConstInputsRef GetInputs() const {
1958 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001959 }
1960
1961 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001962 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001963
Aart Bik2767f4b2016-10-28 15:03:53 -07001964 bool HasInput(HInstruction* input) const {
1965 for (const HInstruction* i : GetInputs()) {
1966 if (i == input) {
1967 return true;
1968 }
1969 }
1970 return false;
1971 }
1972
Vladimir Marko372f10e2016-05-17 16:30:10 +01001973 void SetRawInputAt(size_t index, HInstruction* input) {
1974 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1975 }
1976
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001977 virtual void Accept(HGraphVisitor* visitor) = 0;
1978 virtual const char* DebugName() const = 0;
1979
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001980 virtual DataType::Type GetType() const { return DataType::Type::kVoid; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001981
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001982 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001983
1984 uint32_t GetDexPc() const { return dex_pc_; }
1985
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001986 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001987
Nicolas Geoffray331605a2017-03-01 11:01:41 +00001988 // Can the instruction throw?
1989 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
1990 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01001991 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001992 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001993
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001994 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001995 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001996
Calin Juravle10e244f2015-01-26 18:54:32 +00001997 // Does not apply for all instructions, but having this at top level greatly
1998 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001999 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002000 virtual bool CanBeNull() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002001 DCHECK_EQ(GetType(), DataType::Type::kReference) << "CanBeNull only applies to reference types";
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002002 return true;
2003 }
Calin Juravle10e244f2015-01-26 18:54:32 +00002004
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002005 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01002006 return false;
2007 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002008
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002009 virtual bool IsActualObject() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002010 return GetType() == DataType::Type::kReference;
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002011 }
2012
Calin Juravle2e768302015-07-28 14:41:11 +00002013 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00002014
Calin Juravle61d544b2015-02-23 16:46:57 +00002015 ReferenceTypeInfo GetReferenceTypeInfo() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002016 DCHECK_EQ(GetType(), DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002017 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00002018 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00002019 }
Calin Juravleacf735c2015-02-12 15:25:22 +00002020
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002021 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00002022 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002023 // Note: fixup_end remains valid across push_front().
2024 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
2025 HUseListNode<HInstruction*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002026 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HInstruction*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002027 uses_.push_front(*new_node);
2028 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002029 }
2030
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002031 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01002032 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002033 // Note: env_fixup_end remains valid across push_front().
2034 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
2035 HUseListNode<HEnvironment*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002036 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HEnvironment*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002037 env_uses_.push_front(*new_node);
2038 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002039 }
2040
David Brazdil1abb4192015-02-17 18:33:36 +00002041 void RemoveAsUserOfInput(size_t input) {
2042 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002043 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2044 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2045 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00002046 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002047
Vladimir Marko372f10e2016-05-17 16:30:10 +01002048 void RemoveAsUserOfAllInputs() {
2049 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2050 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2051 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2052 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
2053 }
2054 }
2055
David Brazdil1abb4192015-02-17 18:33:36 +00002056 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2057 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002058
Vladimir Marko46817b82016-03-29 12:21:58 +01002059 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2060 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2061 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002062 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002063 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002064 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002065
Aart Bikcc42be02016-10-20 16:14:16 -07002066 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002067 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002068 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002069 !CanThrow() &&
2070 !IsSuspendCheck() &&
2071 !IsControlFlow() &&
2072 !IsNativeDebugInfo() &&
2073 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002074 // If we added an explicit barrier then we should keep it.
Igor Murashkind01745e2017-04-05 16:40:31 -07002075 !IsMemoryBarrier() &&
2076 !IsConstructorFence();
Aart Bik482095d2016-10-10 15:39:10 -07002077 }
2078
Aart Bikcc42be02016-10-20 16:14:16 -07002079 bool IsDeadAndRemovable() const {
2080 return IsRemovable() && !HasUses();
2081 }
2082
Roland Levillain6c82d402014-10-13 16:10:27 +01002083 // Does this instruction strictly dominate `other_instruction`?
2084 // Returns false if this instruction and `other_instruction` are the same.
2085 // Aborts if this instruction and `other_instruction` are both phis.
2086 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002087
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002088 int GetId() const { return id_; }
2089 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002090
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002091 int GetSsaIndex() const { return ssa_index_; }
2092 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2093 bool HasSsaIndex() const { return ssa_index_ != -1; }
2094
2095 bool HasEnvironment() const { return environment_ != nullptr; }
2096 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002097 // Set the `environment_` field. Raw because this method does not
2098 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002099 void SetRawEnvironment(HEnvironment* environment) {
2100 DCHECK(environment_ == nullptr);
2101 DCHECK_EQ(environment->GetHolder(), this);
2102 environment_ = environment;
2103 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002104
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002105 void InsertRawEnvironment(HEnvironment* environment) {
2106 DCHECK(environment_ != nullptr);
2107 DCHECK_EQ(environment->GetHolder(), this);
2108 DCHECK(environment->GetParent() == nullptr);
2109 environment->parent_ = environment_;
2110 environment_ = environment;
2111 }
2112
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002113 void RemoveEnvironment();
2114
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002115 // Set the environment of this instruction, copying it from `environment`. While
2116 // copying, the uses lists are being updated.
2117 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002118 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002119 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002120 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002121 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002122 if (environment->GetParent() != nullptr) {
2123 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2124 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002125 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002126
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002127 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2128 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002129 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002130 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002131 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002132 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002133 if (environment->GetParent() != nullptr) {
2134 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2135 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002136 }
2137
Nicolas Geoffray39468442014-09-02 15:17:15 +01002138 // Returns the number of entries in the environment. Typically, that is the
2139 // number of dex registers in a method. It could be more in case of inlining.
2140 size_t EnvironmentSize() const;
2141
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002142 LocationSummary* GetLocations() const { return locations_; }
2143 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002144
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002145 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002146 void ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002147 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002148
Alexandre Rames188d4312015-04-09 18:30:21 +01002149 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2150 // uses of this instruction by `other` are *not* updated.
2151 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2152 ReplaceWith(other);
2153 other->ReplaceInput(this, use_index);
2154 }
2155
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002156 // Move `this` instruction before `cursor`
2157 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002158
Vladimir Markofb337ea2015-11-25 15:25:10 +00002159 // Move `this` before its first user and out of any loops. If there is no
2160 // out-of-loop user that dominates all other users, move the instruction
2161 // to the end of the out-of-loop common dominator of the user's blocks.
2162 //
2163 // This can be used only on non-throwing instructions with no side effects that
2164 // have at least one use but no environment uses.
2165 void MoveBeforeFirstUserAndOutOfLoops();
2166
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002167#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002168 bool Is##type() const; \
2169 const H##type* As##type() const; \
2170 H##type* As##type();
2171
2172 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2173#undef INSTRUCTION_TYPE_CHECK
2174
2175#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002176 bool Is##type() const { return (As##type() != nullptr); } \
2177 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002178 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002179 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002180#undef INSTRUCTION_TYPE_CHECK
2181
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002182 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002183 // TODO: this method is used by LICM and GVN with possibly different
2184 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002185 virtual bool CanBeMoved() const { return false; }
2186
2187 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002188 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002189 return false;
2190 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002191
2192 // Returns whether any data encoded in the two instructions is equal.
2193 // This method does not look at the inputs. Both instructions must be
2194 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002195 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002196 return false;
2197 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002198
2199 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002200 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002201 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002202 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002203
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002204 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2205 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2206 // the virtual function because the __attribute__((__pure__)) doesn't really
2207 // apply the strong requirement for virtual functions, preventing optimizations.
2208 InstructionKind GetKind() const PURE;
2209 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002210
2211 virtual size_t ComputeHashCode() const {
2212 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002213 for (const HInstruction* input : GetInputs()) {
2214 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002215 }
2216 return result;
2217 }
2218
2219 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002220 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002221 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002222
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002223 size_t GetLifetimePosition() const { return lifetime_position_; }
2224 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2225 LiveInterval* GetLiveInterval() const { return live_interval_; }
2226 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2227 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2228
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002229 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2230
2231 // Returns whether the code generation of the instruction will require to have access
2232 // to the current method. Such instructions are:
2233 // (1): Instructions that require an environment, as calling the runtime requires
2234 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002235 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2236 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002237 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002238 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002239 }
2240
Vladimir Markodc151b22015-10-15 18:02:30 +01002241 // Returns whether the code generation of the instruction will require to have access
2242 // to the dex cache of the current method's declaring class via the current method.
2243 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002244
Mark Mendellc4701932015-04-10 13:18:51 -04002245 // Does this instruction have any use in an environment before
2246 // control flow hits 'other'?
2247 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2248
2249 // Remove all references to environment uses of this instruction.
2250 // The caller must ensure that this is safe to do.
2251 void RemoveEnvironmentUsers();
2252
Vladimir Markoa1de9182016-02-25 11:37:38 +00002253 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2254 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002255
David Brazdil1abb4192015-02-17 18:33:36 +00002256 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002257 // If set, the machine code for this instruction is assumed to be generated by
2258 // its users. Used by liveness analysis to compute use positions accordingly.
2259 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2260 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2261 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2262 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2263
Vladimir Marko372f10e2016-05-17 16:30:10 +01002264 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2265 return GetInputRecords()[i];
2266 }
2267
2268 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2269 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2270 input_records[index] = input;
2271 }
David Brazdil1abb4192015-02-17 18:33:36 +00002272
Vladimir Markoa1de9182016-02-25 11:37:38 +00002273 uint32_t GetPackedFields() const {
2274 return packed_fields_;
2275 }
2276
2277 template <size_t flag>
2278 bool GetPackedFlag() const {
2279 return (packed_fields_ & (1u << flag)) != 0u;
2280 }
2281
2282 template <size_t flag>
2283 void SetPackedFlag(bool value = true) {
2284 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2285 }
2286
2287 template <typename BitFieldType>
2288 typename BitFieldType::value_type GetPackedField() const {
2289 return BitFieldType::Decode(packed_fields_);
2290 }
2291
2292 template <typename BitFieldType>
2293 void SetPackedField(typename BitFieldType::value_type value) {
2294 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2295 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2296 }
2297
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002298 private:
Vladimir Marko46817b82016-03-29 12:21:58 +01002299 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2300 auto before_use_node = uses_.before_begin();
2301 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2302 HInstruction* user = use_node->GetUser();
2303 size_t input_index = use_node->GetIndex();
2304 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2305 before_use_node = use_node;
2306 }
2307 }
2308
2309 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2310 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2311 if (next != uses_.end()) {
2312 HInstruction* next_user = next->GetUser();
2313 size_t next_index = next->GetIndex();
2314 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2315 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2316 }
2317 }
2318
2319 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2320 auto before_env_use_node = env_uses_.before_begin();
2321 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2322 HEnvironment* user = env_use_node->GetUser();
2323 size_t input_index = env_use_node->GetIndex();
2324 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2325 before_env_use_node = env_use_node;
2326 }
2327 }
2328
2329 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2330 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2331 if (next != env_uses_.end()) {
2332 HEnvironment* next_user = next->GetUser();
2333 size_t next_index = next->GetIndex();
2334 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2335 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2336 }
2337 }
David Brazdil1abb4192015-02-17 18:33:36 +00002338
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002339 HInstruction* previous_;
2340 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002341 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002342 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002343
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002344 // An instruction gets an id when it is added to the graph.
2345 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002346 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002347 int id_;
2348
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002349 // When doing liveness analysis, instructions that have uses get an SSA index.
2350 int ssa_index_;
2351
Vladimir Markoa1de9182016-02-25 11:37:38 +00002352 // Packed fields.
2353 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002354
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002355 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002356 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002357
2358 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002359 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002360
Nicolas Geoffray39468442014-09-02 15:17:15 +01002361 // The environment associated with this instruction. Not null if the instruction
2362 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002363 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002364
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002365 // Set by the code generator.
2366 LocationSummary* locations_;
2367
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002368 // Set by the liveness analysis.
2369 LiveInterval* live_interval_;
2370
2371 // Set by the liveness analysis, this is the position in a linear
2372 // order of blocks where this instruction's live interval start.
2373 size_t lifetime_position_;
2374
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002375 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002376
Vladimir Markoa1de9182016-02-25 11:37:38 +00002377 // The reference handle part of the reference type info.
2378 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002379 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002380 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002381
David Brazdil1abb4192015-02-17 18:33:36 +00002382 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002383 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002384 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002385 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002386 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002387
2388 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2389};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002390std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002391
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002392// Iterates over the instructions, while preserving the next instruction
2393// in case the current instruction gets removed from the list by the user
2394// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002395class HInstructionIterator : public ValueObject {
2396 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002397 explicit HInstructionIterator(const HInstructionList& instructions)
2398 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002399 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002400 }
2401
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002402 bool Done() const { return instruction_ == nullptr; }
2403 HInstruction* Current() const { return instruction_; }
2404 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002405 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002406 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002407 }
2408
2409 private:
2410 HInstruction* instruction_;
2411 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002412
2413 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002414};
2415
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002416// Iterates over the instructions without saving the next instruction,
2417// therefore handling changes in the graph potentially made by the user
2418// of this iterator.
2419class HInstructionIteratorHandleChanges : public ValueObject {
2420 public:
2421 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2422 : instruction_(instructions.first_instruction_) {
2423 }
2424
2425 bool Done() const { return instruction_ == nullptr; }
2426 HInstruction* Current() const { return instruction_; }
2427 void Advance() {
2428 instruction_ = instruction_->GetNext();
2429 }
2430
2431 private:
2432 HInstruction* instruction_;
2433
2434 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2435};
2436
2437
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002438class HBackwardInstructionIterator : public ValueObject {
2439 public:
2440 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2441 : instruction_(instructions.last_instruction_) {
2442 next_ = Done() ? nullptr : instruction_->GetPrevious();
2443 }
2444
2445 bool Done() const { return instruction_ == nullptr; }
2446 HInstruction* Current() const { return instruction_; }
2447 void Advance() {
2448 instruction_ = next_;
2449 next_ = Done() ? nullptr : instruction_->GetPrevious();
2450 }
2451
2452 private:
2453 HInstruction* instruction_;
2454 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002455
2456 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002457};
2458
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002459class HVariableInputSizeInstruction : public HInstruction {
2460 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002461 using HInstruction::GetInputRecords; // Keep the const version visible.
2462 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
2463 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2464 }
2465
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002466 void AddInput(HInstruction* input);
2467 void InsertInputAt(size_t index, HInstruction* input);
2468 void RemoveInputAt(size_t index);
2469
Igor Murashkind01745e2017-04-05 16:40:31 -07002470 // Removes all the inputs.
2471 // Also removes this instructions from each input's use list
2472 // (for non-environment uses only).
2473 void RemoveAllInputs();
2474
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002475 protected:
2476 HVariableInputSizeInstruction(SideEffects side_effects,
2477 uint32_t dex_pc,
2478 ArenaAllocator* arena,
2479 size_t number_of_inputs,
2480 ArenaAllocKind kind)
2481 : HInstruction(side_effects, dex_pc),
2482 inputs_(number_of_inputs, arena->Adapter(kind)) {}
2483
2484 ArenaVector<HUserRecord<HInstruction*>> inputs_;
2485
2486 private:
2487 DISALLOW_COPY_AND_ASSIGN(HVariableInputSizeInstruction);
2488};
2489
Vladimir Markof9f64412015-09-02 14:05:49 +01002490template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002491class HTemplateInstruction: public HInstruction {
2492 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002493 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002494 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002495 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002496
Vladimir Marko372f10e2016-05-17 16:30:10 +01002497 using HInstruction::GetInputRecords; // Keep the const version visible.
2498 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2499 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002500 }
2501
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002502 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002503 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002504
2505 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002506};
2507
Vladimir Markof9f64412015-09-02 14:05:49 +01002508// HTemplateInstruction specialization for N=0.
2509template<>
2510class HTemplateInstruction<0>: public HInstruction {
2511 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002512 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002513 : HInstruction(side_effects, dex_pc) {}
2514
Vladimir Markof9f64412015-09-02 14:05:49 +01002515 virtual ~HTemplateInstruction() {}
2516
Vladimir Marko372f10e2016-05-17 16:30:10 +01002517 using HInstruction::GetInputRecords; // Keep the const version visible.
2518 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2519 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002520 }
2521
2522 private:
2523 friend class SsaBuilder;
2524};
2525
Dave Allison20dfc792014-06-16 20:44:29 -07002526template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002527class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002528 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002529 HExpression<N>(DataType::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002530 : HTemplateInstruction<N>(side_effects, dex_pc) {
2531 this->template SetPackedField<TypeField>(type);
2532 }
Dave Allison20dfc792014-06-16 20:44:29 -07002533 virtual ~HExpression() {}
2534
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002535 DataType::Type GetType() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00002536 return TypeField::Decode(this->GetPackedFields());
2537 }
Dave Allison20dfc792014-06-16 20:44:29 -07002538
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002539 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002540 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2541 static constexpr size_t kFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002542 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00002543 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2544 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2545 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002546 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002547};
2548
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002549// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2550// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002551class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002552 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002553 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2554 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002555
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002556 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002557
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002558 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002559
2560 private:
2561 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2562};
2563
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002564// Represents dex's RETURN opcodes. A HReturn is a control flow
2565// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002566class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002567 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002568 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2569 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002570 SetRawInputAt(0, value);
2571 }
2572
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002573 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002574
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002575 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002576
2577 private:
2578 DISALLOW_COPY_AND_ASSIGN(HReturn);
2579};
2580
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002581class HPhi FINAL : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002582 public:
2583 HPhi(ArenaAllocator* arena,
2584 uint32_t reg_number,
2585 size_t number_of_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002586 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +00002587 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002588 : HVariableInputSizeInstruction(
2589 SideEffects::None(),
2590 dex_pc,
2591 arena,
2592 number_of_inputs,
2593 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002594 reg_number_(reg_number) {
2595 SetPackedField<TypeField>(ToPhiType(type));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002596 DCHECK_NE(GetType(), DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +00002597 // Phis are constructed live and marked dead if conflicting or unused.
2598 // Individual steps of SsaBuilder should assume that if a phi has been
2599 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2600 SetPackedFlag<kFlagIsLive>(true);
2601 SetPackedFlag<kFlagCanBeNull>(true);
2602 }
2603
2604 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002605 static DataType::Type ToPhiType(DataType::Type type) {
2606 return DataType::Kind(type);
David Brazdildee58d62016-04-07 09:54:26 +00002607 }
2608
2609 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2610
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002611 DataType::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2612 void SetType(DataType::Type new_type) {
David Brazdildee58d62016-04-07 09:54:26 +00002613 // Make sure that only valid type changes occur. The following are allowed:
2614 // (1) int -> float/ref (primitive type propagation),
2615 // (2) long -> double (primitive type propagation).
2616 DCHECK(GetType() == new_type ||
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002617 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kFloat32) ||
2618 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kReference) ||
2619 (GetType() == DataType::Type::kInt64 && new_type == DataType::Type::kFloat64));
David Brazdildee58d62016-04-07 09:54:26 +00002620 SetPackedField<TypeField>(new_type);
2621 }
2622
2623 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2624 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2625
2626 uint32_t GetRegNumber() const { return reg_number_; }
2627
2628 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2629 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2630 bool IsDead() const { return !IsLive(); }
2631 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2632
Vladimir Markoe9004912016-06-16 16:50:52 +01002633 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002634 return other != nullptr
2635 && other->IsPhi()
2636 && other->AsPhi()->GetBlock() == GetBlock()
2637 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2638 }
2639
Nicolas Geoffrayf57c1ae2017-06-28 17:40:18 +01002640 bool HasEquivalentPhi() const {
2641 if (GetPrevious() != nullptr && GetPrevious()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2642 return true;
2643 }
2644 if (GetNext() != nullptr && GetNext()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2645 return true;
2646 }
2647 return false;
2648 }
2649
David Brazdildee58d62016-04-07 09:54:26 +00002650 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2651 // An equivalent phi is a phi having the same dex register and type.
2652 // It assumes that phis with the same dex register are adjacent.
2653 HPhi* GetNextEquivalentPhiWithSameType() {
2654 HInstruction* next = GetNext();
2655 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2656 if (next->GetType() == GetType()) {
2657 return next->AsPhi();
2658 }
2659 next = next->GetNext();
2660 }
2661 return nullptr;
2662 }
2663
2664 DECLARE_INSTRUCTION(Phi);
2665
David Brazdildee58d62016-04-07 09:54:26 +00002666 private:
2667 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2668 static constexpr size_t kFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002669 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
David Brazdildee58d62016-04-07 09:54:26 +00002670 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2671 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2672 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2673 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002674 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
David Brazdildee58d62016-04-07 09:54:26 +00002675
David Brazdildee58d62016-04-07 09:54:26 +00002676 const uint32_t reg_number_;
2677
2678 DISALLOW_COPY_AND_ASSIGN(HPhi);
2679};
2680
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002681// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002682// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002683// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002684class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002685 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002686 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002687
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002688 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002689
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002690 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002691
2692 private:
2693 DISALLOW_COPY_AND_ASSIGN(HExit);
2694};
2695
2696// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002697class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002698 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002699 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002700
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002701 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002702
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002703 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002704 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002705 }
2706
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002707 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002708
2709 private:
2710 DISALLOW_COPY_AND_ASSIGN(HGoto);
2711};
2712
Roland Levillain9867bc72015-08-05 10:21:34 +01002713class HConstant : public HExpression<0> {
2714 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002715 explicit HConstant(DataType::Type type, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002716 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002717
2718 bool CanBeMoved() const OVERRIDE { return true; }
2719
Roland Levillain1a653882016-03-18 18:05:57 +00002720 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002721 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002722 // Is this constant 0 in the arithmetic sense?
2723 virtual bool IsArithmeticZero() const { return false; }
2724 // Is this constant a 0-bit pattern?
2725 virtual bool IsZeroBitPattern() const { return false; }
2726 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002727 virtual bool IsOne() const { return false; }
2728
David Brazdil77a48ae2015-09-15 12:34:04 +00002729 virtual uint64_t GetValueAsUint64() const = 0;
2730
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002731 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002732
2733 private:
2734 DISALLOW_COPY_AND_ASSIGN(HConstant);
2735};
2736
Vladimir Markofcb503c2016-05-18 12:48:17 +01002737class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002738 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002739 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002740 return true;
2741 }
2742
David Brazdil77a48ae2015-09-15 12:34:04 +00002743 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2744
Roland Levillain9867bc72015-08-05 10:21:34 +01002745 size_t ComputeHashCode() const OVERRIDE { return 0; }
2746
Roland Levillain1a653882016-03-18 18:05:57 +00002747 // The null constant representation is a 0-bit pattern.
2748 virtual bool IsZeroBitPattern() const { return true; }
2749
Roland Levillain9867bc72015-08-05 10:21:34 +01002750 DECLARE_INSTRUCTION(NullConstant);
2751
2752 private:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002753 explicit HNullConstant(uint32_t dex_pc = kNoDexPc)
2754 : HConstant(DataType::Type::kReference, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002755
2756 friend class HGraph;
2757 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2758};
2759
2760// Constants of the type int. Those can be from Dex instructions, or
2761// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002762class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002763 public:
2764 int32_t GetValue() const { return value_; }
2765
David Brazdil9f389d42015-10-01 14:32:56 +01002766 uint64_t GetValueAsUint64() const OVERRIDE {
2767 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2768 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002769
Vladimir Marko372f10e2016-05-17 16:30:10 +01002770 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002771 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002772 return other->AsIntConstant()->value_ == value_;
2773 }
2774
2775 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2776
2777 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002778 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2779 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002780 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2781
Roland Levillain1a653882016-03-18 18:05:57 +00002782 // Integer constants are used to encode Boolean values as well,
2783 // where 1 means true and 0 means false.
2784 bool IsTrue() const { return GetValue() == 1; }
2785 bool IsFalse() const { return GetValue() == 0; }
2786
Roland Levillain9867bc72015-08-05 10:21:34 +01002787 DECLARE_INSTRUCTION(IntConstant);
2788
2789 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002790 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002791 : HConstant(DataType::Type::kInt32, dex_pc), value_(value) {}
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002792 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002793 : HConstant(DataType::Type::kInt32, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002794
2795 const int32_t value_;
2796
2797 friend class HGraph;
2798 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2799 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2800 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2801};
2802
Vladimir Markofcb503c2016-05-18 12:48:17 +01002803class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002804 public:
2805 int64_t GetValue() const { return value_; }
2806
David Brazdil77a48ae2015-09-15 12:34:04 +00002807 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2808
Vladimir Marko372f10e2016-05-17 16:30:10 +01002809 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002810 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002811 return other->AsLongConstant()->value_ == value_;
2812 }
2813
2814 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2815
2816 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002817 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2818 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002819 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2820
2821 DECLARE_INSTRUCTION(LongConstant);
2822
2823 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002824 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002825 : HConstant(DataType::Type::kInt64, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002826
2827 const int64_t value_;
2828
2829 friend class HGraph;
2830 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2831};
Dave Allison20dfc792014-06-16 20:44:29 -07002832
Vladimir Markofcb503c2016-05-18 12:48:17 +01002833class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002834 public:
2835 float GetValue() const { return value_; }
2836
2837 uint64_t GetValueAsUint64() const OVERRIDE {
2838 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2839 }
2840
Vladimir Marko372f10e2016-05-17 16:30:10 +01002841 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002842 DCHECK(other->IsFloatConstant()) << other->DebugName();
2843 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2844 }
2845
2846 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2847
2848 bool IsMinusOne() const OVERRIDE {
2849 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2850 }
Roland Levillain1a653882016-03-18 18:05:57 +00002851 bool IsArithmeticZero() const OVERRIDE {
2852 return std::fpclassify(value_) == FP_ZERO;
2853 }
2854 bool IsArithmeticPositiveZero() const {
2855 return IsArithmeticZero() && !std::signbit(value_);
2856 }
2857 bool IsArithmeticNegativeZero() const {
2858 return IsArithmeticZero() && std::signbit(value_);
2859 }
2860 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002861 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002862 }
2863 bool IsOne() const OVERRIDE {
2864 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2865 }
2866 bool IsNaN() const {
2867 return std::isnan(value_);
2868 }
2869
2870 DECLARE_INSTRUCTION(FloatConstant);
2871
2872 private:
2873 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002874 : HConstant(DataType::Type::kFloat32, dex_pc), value_(value) {}
Roland Levillain31dd3d62016-02-16 12:21:02 +00002875 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002876 : HConstant(DataType::Type::kFloat32, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
Roland Levillain31dd3d62016-02-16 12:21:02 +00002877
2878 const float value_;
2879
2880 // Only the SsaBuilder and HGraph can create floating-point constants.
2881 friend class SsaBuilder;
2882 friend class HGraph;
2883 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2884};
2885
Vladimir Markofcb503c2016-05-18 12:48:17 +01002886class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002887 public:
2888 double GetValue() const { return value_; }
2889
2890 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2891
Vladimir Marko372f10e2016-05-17 16:30:10 +01002892 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002893 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2894 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2895 }
2896
2897 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2898
2899 bool IsMinusOne() const OVERRIDE {
2900 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2901 }
Roland Levillain1a653882016-03-18 18:05:57 +00002902 bool IsArithmeticZero() const OVERRIDE {
2903 return std::fpclassify(value_) == FP_ZERO;
2904 }
2905 bool IsArithmeticPositiveZero() const {
2906 return IsArithmeticZero() && !std::signbit(value_);
2907 }
2908 bool IsArithmeticNegativeZero() const {
2909 return IsArithmeticZero() && std::signbit(value_);
2910 }
2911 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002912 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002913 }
2914 bool IsOne() const OVERRIDE {
2915 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2916 }
2917 bool IsNaN() const {
2918 return std::isnan(value_);
2919 }
2920
2921 DECLARE_INSTRUCTION(DoubleConstant);
2922
2923 private:
2924 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002925 : HConstant(DataType::Type::kFloat64, dex_pc), value_(value) {}
Roland Levillain31dd3d62016-02-16 12:21:02 +00002926 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002927 : HConstant(DataType::Type::kFloat64, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
Roland Levillain31dd3d62016-02-16 12:21:02 +00002928
2929 const double value_;
2930
2931 // Only the SsaBuilder and HGraph can create floating-point constants.
2932 friend class SsaBuilder;
2933 friend class HGraph;
2934 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2935};
2936
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002937// Conditional branch. A block ending with an HIf instruction must have
2938// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002939class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002940 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002941 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2942 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002943 SetRawInputAt(0, input);
2944 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002945
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002946 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002947
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002948 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002949 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002950 }
2951
2952 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002953 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002954 }
2955
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002956 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002957
2958 private:
2959 DISALLOW_COPY_AND_ASSIGN(HIf);
2960};
2961
David Brazdilfc6a86a2015-06-26 10:33:45 +00002962
2963// Abstract instruction which marks the beginning and/or end of a try block and
2964// links it to the respective exception handlers. Behaves the same as a Goto in
2965// non-exceptional control flow.
2966// Normal-flow successor is stored at index zero, exception handlers under
2967// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002968class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002969 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002970 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002971 kEntry,
2972 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002973 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002974 };
2975
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002976 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002977 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2978 SetPackedField<BoundaryKindField>(kind);
2979 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002980
2981 bool IsControlFlow() const OVERRIDE { return true; }
2982
2983 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002984 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002985
David Brazdild26a4112015-11-10 11:07:31 +00002986 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2987 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2988 }
2989
David Brazdilfc6a86a2015-06-26 10:33:45 +00002990 // Returns whether `handler` is among its exception handlers (non-zero index
2991 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002992 bool HasExceptionHandler(const HBasicBlock& handler) const {
2993 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002994 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002995 }
2996
2997 // If not present already, adds `handler` to its block's list of exception
2998 // handlers.
2999 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01003000 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003001 GetBlock()->AddSuccessor(handler);
3002 }
3003 }
3004
Vladimir Markoa1de9182016-02-25 11:37:38 +00003005 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
3006 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003007
David Brazdilffee3d32015-07-06 11:48:53 +01003008 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
3009
David Brazdilfc6a86a2015-06-26 10:33:45 +00003010 DECLARE_INSTRUCTION(TryBoundary);
3011
3012 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003013 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
3014 static constexpr size_t kFieldBoundaryKindSize =
3015 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
3016 static constexpr size_t kNumberOfTryBoundaryPackedBits =
3017 kFieldBoundaryKind + kFieldBoundaryKindSize;
3018 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
3019 "Too many packed fields.");
3020 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00003021
3022 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
3023};
3024
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003025// Deoptimize to interpreter, upon checking a condition.
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003026class HDeoptimize FINAL : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003027 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003028 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
3029 // across.
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003030 HDeoptimize(ArenaAllocator* arena, HInstruction* cond, DeoptimizationKind kind, uint32_t dex_pc)
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003031 : HVariableInputSizeInstruction(
3032 SideEffects::All(),
3033 dex_pc,
3034 arena,
3035 /* number_of_inputs */ 1,
3036 kArenaAllocMisc) {
3037 SetPackedFlag<kFieldCanBeMoved>(false);
3038 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003039 SetRawInputAt(0, cond);
3040 }
3041
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003042 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
3043 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
3044 // instead of `guard`.
3045 // We set CanTriggerGC to prevent any intermediate address to be live
3046 // at the point of the `HDeoptimize`.
3047 HDeoptimize(ArenaAllocator* arena,
3048 HInstruction* cond,
3049 HInstruction* guard,
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003050 DeoptimizationKind kind,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003051 uint32_t dex_pc)
3052 : HVariableInputSizeInstruction(
3053 SideEffects::CanTriggerGC(),
3054 dex_pc,
3055 arena,
3056 /* number_of_inputs */ 2,
3057 kArenaAllocMisc) {
3058 SetPackedFlag<kFieldCanBeMoved>(true);
3059 SetPackedField<DeoptimizeKindField>(kind);
3060 SetRawInputAt(0, cond);
3061 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003062 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003063
3064 bool CanBeMoved() const OVERRIDE { return GetPackedFlag<kFieldCanBeMoved>(); }
3065
3066 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
3067 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
3068 }
3069
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003070 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003071
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003072 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003073
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003074 DeoptimizationKind GetDeoptimizationKind() const { return GetPackedField<DeoptimizeKindField>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003075
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003076 DataType::Type GetType() const OVERRIDE {
3077 return GuardsAnInput() ? GuardedInput()->GetType() : DataType::Type::kVoid;
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003078 }
3079
3080 bool GuardsAnInput() const {
3081 return InputCount() == 2;
3082 }
3083
3084 HInstruction* GuardedInput() const {
3085 DCHECK(GuardsAnInput());
3086 return InputAt(1);
3087 }
3088
3089 void RemoveGuard() {
3090 RemoveInputAt(1);
3091 }
3092
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003093 DECLARE_INSTRUCTION(Deoptimize);
3094
3095 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003096 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3097 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3098 static constexpr size_t kFieldDeoptimizeKindSize =
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003099 MinimumBitsToStore(static_cast<size_t>(DeoptimizationKind::kLast));
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003100 static constexpr size_t kNumberOfDeoptimizePackedBits =
3101 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3102 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3103 "Too many packed fields.");
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003104 using DeoptimizeKindField =
3105 BitField<DeoptimizationKind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003106
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003107 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
3108};
3109
Mingyao Yang063fc772016-08-02 11:02:54 -07003110// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3111// The compiled code checks this flag value in a guard before devirtualized call and
3112// if it's true, starts to do deoptimization.
3113// It has a 4-byte slot on stack.
3114// TODO: allocate a register for this flag.
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003115class HShouldDeoptimizeFlag FINAL : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003116 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003117 // CHA guards are only optimized in a separate pass and it has no side effects
3118 // with regard to other passes.
3119 HShouldDeoptimizeFlag(ArenaAllocator* arena, uint32_t dex_pc)
3120 : HVariableInputSizeInstruction(SideEffects::None(), dex_pc, arena, 0, kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003121 }
3122
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003123 DataType::Type GetType() const OVERRIDE { return DataType::Type::kInt32; }
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003124
3125 // We do all CHA guard elimination/motion in a single pass, after which there is no
3126 // further guard elimination/motion since a guard might have been used for justification
3127 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3128 // to avoid other optimizations trying to move it.
Mingyao Yang063fc772016-08-02 11:02:54 -07003129 bool CanBeMoved() const OVERRIDE { return false; }
3130
3131 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3132
3133 private:
3134 DISALLOW_COPY_AND_ASSIGN(HShouldDeoptimizeFlag);
3135};
3136
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003137// Represents the ArtMethod that was passed as a first argument to
3138// the method. It is used by instructions that depend on it, like
3139// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003140class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003141 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003142 explicit HCurrentMethod(DataType::Type type, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003143 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003144
3145 DECLARE_INSTRUCTION(CurrentMethod);
3146
3147 private:
3148 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
3149};
3150
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003151// Fetches an ArtMethod from the virtual table or the interface method table
3152// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003153class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003154 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003155 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003156 kVTable,
3157 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003158 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003159 };
3160 HClassTableGet(HInstruction* cls,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003161 DataType::Type type,
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003162 TableKind kind,
3163 size_t index,
3164 uint32_t dex_pc)
3165 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003166 index_(index) {
3167 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003168 SetRawInputAt(0, cls);
3169 }
3170
3171 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003172 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003173 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003174 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003175 }
3176
Vladimir Markoa1de9182016-02-25 11:37:38 +00003177 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003178 size_t GetIndex() const { return index_; }
3179
3180 DECLARE_INSTRUCTION(ClassTableGet);
3181
3182 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003183 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
3184 static constexpr size_t kFieldTableKindSize =
3185 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3186 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3187 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3188 "Too many packed fields.");
3189 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3190
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003191 // The index of the ArtMethod in the table.
3192 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003193
3194 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
3195};
3196
Mark Mendellfe57faa2015-09-18 09:26:15 -04003197// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3198// have one successor for each entry in the switch table, and the final successor
3199// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003200class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003201 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003202 HPackedSwitch(int32_t start_value,
3203 uint32_t num_entries,
3204 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003205 uint32_t dex_pc = kNoDexPc)
3206 : HTemplateInstruction(SideEffects::None(), dex_pc),
3207 start_value_(start_value),
3208 num_entries_(num_entries) {
3209 SetRawInputAt(0, input);
3210 }
3211
3212 bool IsControlFlow() const OVERRIDE { return true; }
3213
3214 int32_t GetStartValue() const { return start_value_; }
3215
Vladimir Marko211c2112015-09-24 16:52:33 +01003216 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003217
3218 HBasicBlock* GetDefaultBlock() const {
3219 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003220 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003221 }
3222 DECLARE_INSTRUCTION(PackedSwitch);
3223
3224 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003225 const int32_t start_value_;
3226 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003227
3228 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
3229};
3230
Roland Levillain88cb1752014-10-20 16:36:47 +01003231class HUnaryOperation : public HExpression<1> {
3232 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003233 HUnaryOperation(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003234 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003235 SetRawInputAt(0, input);
3236 }
3237
3238 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003239 DataType::Type GetResultType() const { return GetType(); }
Roland Levillain88cb1752014-10-20 16:36:47 +01003240
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003241 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003242 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003243 return true;
3244 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003245
Roland Levillain31dd3d62016-02-16 12:21:02 +00003246 // Try to statically evaluate `this` and return a HConstant
3247 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003248 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003249 HConstant* TryStaticEvaluation() const;
3250
3251 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003252 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3253 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003254 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3255 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003256
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003257 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003258
3259 private:
3260 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
3261};
3262
Dave Allison20dfc792014-06-16 20:44:29 -07003263class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003264 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003265 HBinaryOperation(DataType::Type result_type,
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003266 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003267 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003268 SideEffects side_effects = SideEffects::None(),
3269 uint32_t dex_pc = kNoDexPc)
3270 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003271 SetRawInputAt(0, left);
3272 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003273 }
3274
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003275 HInstruction* GetLeft() const { return InputAt(0); }
3276 HInstruction* GetRight() const { return InputAt(1); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003277 DataType::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003278
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003279 virtual bool IsCommutative() const { return false; }
3280
3281 // Put constant on the right.
3282 // Returns whether order is changed.
3283 bool OrderInputsWithConstantOnTheRight() {
3284 HInstruction* left = InputAt(0);
3285 HInstruction* right = InputAt(1);
3286 if (left->IsConstant() && !right->IsConstant()) {
3287 ReplaceInput(right, 0);
3288 ReplaceInput(left, 1);
3289 return true;
3290 }
3291 return false;
3292 }
3293
3294 // Order inputs by instruction id, but favor constant on the right side.
3295 // This helps GVN for commutative ops.
3296 void OrderInputs() {
3297 DCHECK(IsCommutative());
3298 HInstruction* left = InputAt(0);
3299 HInstruction* right = InputAt(1);
3300 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3301 return;
3302 }
3303 if (OrderInputsWithConstantOnTheRight()) {
3304 return;
3305 }
3306 // Order according to instruction id.
3307 if (left->GetId() > right->GetId()) {
3308 ReplaceInput(right, 0);
3309 ReplaceInput(left, 1);
3310 }
3311 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003312
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003313 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003314 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003315 return true;
3316 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003317
Roland Levillain31dd3d62016-02-16 12:21:02 +00003318 // Try to statically evaluate `this` and return a HConstant
3319 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003320 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003321 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003322
3323 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003324 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3325 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003326 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3327 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003328 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003329 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3330 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003331 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3332 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003333 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3334 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003335 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003336 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3337 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003338
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003339 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003340 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003341 HConstant* GetConstantRight() const;
3342
3343 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003344 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003345 HInstruction* GetLeastConstantLeft() const;
3346
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003347 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003348
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003349 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003350 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
3351};
3352
Mark Mendellc4701932015-04-10 13:18:51 -04003353// The comparison bias applies for floating point operations and indicates how NaN
3354// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003355enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003356 kNoBias, // bias is not applicable (i.e. for long operation)
3357 kGtBias, // return 1 for NaN comparisons
3358 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003359 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003360};
3361
Roland Levillain31dd3d62016-02-16 12:21:02 +00003362std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3363
Dave Allison20dfc792014-06-16 20:44:29 -07003364class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003365 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003366 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003367 : HBinaryOperation(DataType::Type::kBool, first, second, SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003368 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3369 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003370
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003371 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003372 // `instruction`, and disregard moves in between.
3373 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003374
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003375 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003376
3377 virtual IfCondition GetCondition() const = 0;
3378
Mark Mendellc4701932015-04-10 13:18:51 -04003379 virtual IfCondition GetOppositeCondition() const = 0;
3380
Vladimir Markoa1de9182016-02-25 11:37:38 +00003381 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003382 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3383
Vladimir Markoa1de9182016-02-25 11:37:38 +00003384 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3385 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003386
Vladimir Marko372f10e2016-05-17 16:30:10 +01003387 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003388 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003389 }
3390
Roland Levillain4fa13f62015-07-06 18:11:54 +01003391 bool IsFPConditionTrueIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003392 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003393 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003394 if (if_cond == kCondNE) {
3395 return true;
3396 } else if (if_cond == kCondEQ) {
3397 return false;
3398 }
3399 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003400 }
3401
3402 bool IsFPConditionFalseIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003403 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003404 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003405 if (if_cond == kCondEQ) {
3406 return true;
3407 } else if (if_cond == kCondNE) {
3408 return false;
3409 }
3410 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003411 }
3412
Roland Levillain31dd3d62016-02-16 12:21:02 +00003413 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003414 // Needed if we merge a HCompare into a HCondition.
3415 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3416 static constexpr size_t kFieldComparisonBiasSize =
3417 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3418 static constexpr size_t kNumberOfConditionPackedBits =
3419 kFieldComparisonBias + kFieldComparisonBiasSize;
3420 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3421 using ComparisonBiasField =
3422 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3423
Roland Levillain31dd3d62016-02-16 12:21:02 +00003424 template <typename T>
3425 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3426
3427 template <typename T>
3428 int32_t CompareFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003429 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003430 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3431 // Handle the bias.
3432 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3433 }
3434
3435 // Return an integer constant containing the result of a condition evaluated at compile time.
3436 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3437 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3438 }
3439
Dave Allison20dfc792014-06-16 20:44:29 -07003440 private:
3441 DISALLOW_COPY_AND_ASSIGN(HCondition);
3442};
3443
3444// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003445class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003446 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003447 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3448 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003449
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003450 bool IsCommutative() const OVERRIDE { return true; }
3451
Vladimir Marko9e23df52015-11-10 17:14:35 +00003452 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3453 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003454 return MakeConstantCondition(true, GetDexPc());
3455 }
3456 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3457 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3458 }
3459 // In the following Evaluate methods, a HCompare instruction has
3460 // been merged into this HEqual instruction; evaluate it as
3461 // `Compare(x, y) == 0`.
3462 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3463 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3464 GetDexPc());
3465 }
3466 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3467 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3468 }
3469 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3470 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003471 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003472
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003473 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003474
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003475 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003476 return kCondEQ;
3477 }
3478
Mark Mendellc4701932015-04-10 13:18:51 -04003479 IfCondition GetOppositeCondition() const OVERRIDE {
3480 return kCondNE;
3481 }
3482
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003483 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003484 template <typename T> static bool Compute(T x, T y) { return x == y; }
Aart Bike9f37602015-10-09 11:15:55 -07003485
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003486 DISALLOW_COPY_AND_ASSIGN(HEqual);
3487};
3488
Vladimir Markofcb503c2016-05-18 12:48:17 +01003489class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003490 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003491 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3492 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003493
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003494 bool IsCommutative() const OVERRIDE { return true; }
3495
Vladimir Marko9e23df52015-11-10 17:14:35 +00003496 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3497 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003498 return MakeConstantCondition(false, GetDexPc());
3499 }
3500 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3501 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3502 }
3503 // In the following Evaluate methods, a HCompare instruction has
3504 // been merged into this HNotEqual instruction; evaluate it as
3505 // `Compare(x, y) != 0`.
3506 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3507 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3508 }
3509 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3510 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3511 }
3512 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3513 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003514 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003515
Dave Allison20dfc792014-06-16 20:44:29 -07003516 DECLARE_INSTRUCTION(NotEqual);
3517
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003518 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003519 return kCondNE;
3520 }
3521
Mark Mendellc4701932015-04-10 13:18:51 -04003522 IfCondition GetOppositeCondition() const OVERRIDE {
3523 return kCondEQ;
3524 }
3525
Dave Allison20dfc792014-06-16 20:44:29 -07003526 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003527 template <typename T> static bool Compute(T x, T y) { return x != y; }
Aart Bike9f37602015-10-09 11:15:55 -07003528
Dave Allison20dfc792014-06-16 20:44:29 -07003529 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3530};
3531
Vladimir Markofcb503c2016-05-18 12:48:17 +01003532class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003533 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003534 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3535 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003536
Roland Levillain9867bc72015-08-05 10:21:34 +01003537 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003538 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003539 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003540 // In the following Evaluate methods, a HCompare instruction has
3541 // been merged into this HLessThan instruction; evaluate it as
3542 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003543 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003544 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3545 }
3546 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3547 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3548 }
3549 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3550 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003551 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003552
Dave Allison20dfc792014-06-16 20:44:29 -07003553 DECLARE_INSTRUCTION(LessThan);
3554
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003555 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003556 return kCondLT;
3557 }
3558
Mark Mendellc4701932015-04-10 13:18:51 -04003559 IfCondition GetOppositeCondition() const OVERRIDE {
3560 return kCondGE;
3561 }
3562
Dave Allison20dfc792014-06-16 20:44:29 -07003563 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003564 template <typename T> static bool Compute(T x, T y) { return x < y; }
Aart Bike9f37602015-10-09 11:15:55 -07003565
Dave Allison20dfc792014-06-16 20:44:29 -07003566 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3567};
3568
Vladimir Markofcb503c2016-05-18 12:48:17 +01003569class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003570 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003571 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3572 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003573
Roland Levillain9867bc72015-08-05 10:21:34 +01003574 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003575 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003576 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003577 // In the following Evaluate methods, a HCompare instruction has
3578 // been merged into this HLessThanOrEqual instruction; evaluate it as
3579 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003580 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003581 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3582 }
3583 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3584 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3585 }
3586 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3587 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003588 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003589
Dave Allison20dfc792014-06-16 20:44:29 -07003590 DECLARE_INSTRUCTION(LessThanOrEqual);
3591
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003592 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003593 return kCondLE;
3594 }
3595
Mark Mendellc4701932015-04-10 13:18:51 -04003596 IfCondition GetOppositeCondition() const OVERRIDE {
3597 return kCondGT;
3598 }
3599
Dave Allison20dfc792014-06-16 20:44:29 -07003600 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003601 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003602
Dave Allison20dfc792014-06-16 20:44:29 -07003603 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3604};
3605
Vladimir Markofcb503c2016-05-18 12:48:17 +01003606class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003607 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003608 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3609 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003610
Roland Levillain9867bc72015-08-05 10:21:34 +01003611 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003612 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003613 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003614 // In the following Evaluate methods, a HCompare instruction has
3615 // been merged into this HGreaterThan instruction; evaluate it as
3616 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003617 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003618 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3619 }
3620 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3621 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3622 }
3623 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3624 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003625 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003626
Dave Allison20dfc792014-06-16 20:44:29 -07003627 DECLARE_INSTRUCTION(GreaterThan);
3628
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003629 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003630 return kCondGT;
3631 }
3632
Mark Mendellc4701932015-04-10 13:18:51 -04003633 IfCondition GetOppositeCondition() const OVERRIDE {
3634 return kCondLE;
3635 }
3636
Dave Allison20dfc792014-06-16 20:44:29 -07003637 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003638 template <typename T> static bool Compute(T x, T y) { return x > y; }
Aart Bike9f37602015-10-09 11:15:55 -07003639
Dave Allison20dfc792014-06-16 20:44:29 -07003640 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3641};
3642
Vladimir Markofcb503c2016-05-18 12:48:17 +01003643class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003644 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003645 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3646 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003647
Roland Levillain9867bc72015-08-05 10:21:34 +01003648 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003649 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003650 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003651 // In the following Evaluate methods, a HCompare instruction has
3652 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3653 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003654 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003655 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3656 }
3657 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3658 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3659 }
3660 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3661 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003662 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003663
Dave Allison20dfc792014-06-16 20:44:29 -07003664 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3665
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003666 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003667 return kCondGE;
3668 }
3669
Mark Mendellc4701932015-04-10 13:18:51 -04003670 IfCondition GetOppositeCondition() const OVERRIDE {
3671 return kCondLT;
3672 }
3673
Dave Allison20dfc792014-06-16 20:44:29 -07003674 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003675 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003676
Dave Allison20dfc792014-06-16 20:44:29 -07003677 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3678};
3679
Vladimir Markofcb503c2016-05-18 12:48:17 +01003680class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003681 public:
3682 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3683 : HCondition(first, second, dex_pc) {}
3684
3685 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003686 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003687 }
3688 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003689 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3690 }
3691 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3692 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3693 LOG(FATAL) << DebugName() << " is not defined for float values";
3694 UNREACHABLE();
3695 }
3696 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3697 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3698 LOG(FATAL) << DebugName() << " is not defined for double values";
3699 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003700 }
3701
3702 DECLARE_INSTRUCTION(Below);
3703
3704 IfCondition GetCondition() const OVERRIDE {
3705 return kCondB;
3706 }
3707
3708 IfCondition GetOppositeCondition() const OVERRIDE {
3709 return kCondAE;
3710 }
3711
3712 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003713 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003714 return MakeUnsigned(x) < MakeUnsigned(y);
3715 }
Aart Bike9f37602015-10-09 11:15:55 -07003716
3717 DISALLOW_COPY_AND_ASSIGN(HBelow);
3718};
3719
Vladimir Markofcb503c2016-05-18 12:48:17 +01003720class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003721 public:
3722 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3723 : HCondition(first, second, dex_pc) {}
3724
3725 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003726 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003727 }
3728 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003729 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3730 }
3731 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3732 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3733 LOG(FATAL) << DebugName() << " is not defined for float values";
3734 UNREACHABLE();
3735 }
3736 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3737 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3738 LOG(FATAL) << DebugName() << " is not defined for double values";
3739 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003740 }
3741
3742 DECLARE_INSTRUCTION(BelowOrEqual);
3743
3744 IfCondition GetCondition() const OVERRIDE {
3745 return kCondBE;
3746 }
3747
3748 IfCondition GetOppositeCondition() const OVERRIDE {
3749 return kCondA;
3750 }
3751
3752 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003753 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003754 return MakeUnsigned(x) <= MakeUnsigned(y);
3755 }
Aart Bike9f37602015-10-09 11:15:55 -07003756
3757 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3758};
3759
Vladimir Markofcb503c2016-05-18 12:48:17 +01003760class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003761 public:
3762 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3763 : HCondition(first, second, dex_pc) {}
3764
3765 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003766 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003767 }
3768 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003769 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3770 }
3771 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3772 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3773 LOG(FATAL) << DebugName() << " is not defined for float values";
3774 UNREACHABLE();
3775 }
3776 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3777 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3778 LOG(FATAL) << DebugName() << " is not defined for double values";
3779 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003780 }
3781
3782 DECLARE_INSTRUCTION(Above);
3783
3784 IfCondition GetCondition() const OVERRIDE {
3785 return kCondA;
3786 }
3787
3788 IfCondition GetOppositeCondition() const OVERRIDE {
3789 return kCondBE;
3790 }
3791
3792 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003793 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003794 return MakeUnsigned(x) > MakeUnsigned(y);
3795 }
Aart Bike9f37602015-10-09 11:15:55 -07003796
3797 DISALLOW_COPY_AND_ASSIGN(HAbove);
3798};
3799
Vladimir Markofcb503c2016-05-18 12:48:17 +01003800class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003801 public:
3802 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3803 : HCondition(first, second, dex_pc) {}
3804
3805 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003806 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003807 }
3808 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003809 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3810 }
3811 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3812 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3813 LOG(FATAL) << DebugName() << " is not defined for float values";
3814 UNREACHABLE();
3815 }
3816 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3817 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3818 LOG(FATAL) << DebugName() << " is not defined for double values";
3819 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003820 }
3821
3822 DECLARE_INSTRUCTION(AboveOrEqual);
3823
3824 IfCondition GetCondition() const OVERRIDE {
3825 return kCondAE;
3826 }
3827
3828 IfCondition GetOppositeCondition() const OVERRIDE {
3829 return kCondB;
3830 }
3831
3832 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003833 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003834 return MakeUnsigned(x) >= MakeUnsigned(y);
3835 }
Aart Bike9f37602015-10-09 11:15:55 -07003836
3837 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3838};
Dave Allison20dfc792014-06-16 20:44:29 -07003839
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003840// Instruction to check how two inputs compare to each other.
3841// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003842class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003843 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003844 // Note that `comparison_type` is the type of comparison performed
3845 // between the comparison's inputs, not the type of the instantiated
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003846 // HCompare instruction (which is always DataType::Type::kInt).
3847 HCompare(DataType::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003848 HInstruction* first,
3849 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003850 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003851 uint32_t dex_pc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003852 : HBinaryOperation(DataType::Type::kInt32,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003853 first,
3854 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003855 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003856 dex_pc) {
3857 SetPackedField<ComparisonBiasField>(bias);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003858 DCHECK_EQ(comparison_type, DataType::Kind(first->GetType()));
3859 DCHECK_EQ(comparison_type, DataType::Kind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003860 }
3861
Roland Levillain9867bc72015-08-05 10:21:34 +01003862 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003863 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3864
3865 template <typename T>
3866 int32_t ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003867 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003868 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3869 // Handle the bias.
3870 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3871 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003872
Roland Levillain9867bc72015-08-05 10:21:34 +01003873 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003874 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3875 // reach this code path when processing a freshly built HIR
3876 // graph. However HCompare integer instructions can be synthesized
3877 // by the instruction simplifier to implement IntegerCompare and
3878 // IntegerSignum intrinsics, so we have to handle this case.
3879 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003880 }
3881 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003882 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3883 }
3884 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3885 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3886 }
3887 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3888 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003889 }
3890
Vladimir Marko372f10e2016-05-17 16:30:10 +01003891 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003892 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003893 }
3894
Vladimir Markoa1de9182016-02-25 11:37:38 +00003895 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003896
Roland Levillain31dd3d62016-02-16 12:21:02 +00003897 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003898 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003899 bool IsGtBias() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003900 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003901 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003902 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003903
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003904 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type type ATTRIBUTE_UNUSED) {
Roland Levillain1693a1f2016-03-15 14:57:31 +00003905 // Comparisons do not require a runtime call in any back end.
3906 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003907 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003908
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003909 DECLARE_INSTRUCTION(Compare);
3910
Roland Levillain31dd3d62016-02-16 12:21:02 +00003911 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003912 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3913 static constexpr size_t kFieldComparisonBiasSize =
3914 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3915 static constexpr size_t kNumberOfComparePackedBits =
3916 kFieldComparisonBias + kFieldComparisonBiasSize;
3917 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3918 using ComparisonBiasField =
3919 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3920
Roland Levillain31dd3d62016-02-16 12:21:02 +00003921 // Return an integer constant containing the result of a comparison evaluated at compile time.
3922 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3923 DCHECK(value == -1 || value == 0 || value == 1) << value;
3924 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3925 }
3926
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003927 private:
3928 DISALLOW_COPY_AND_ASSIGN(HCompare);
3929};
3930
Nicolas Geoffray2b615ba2017-01-06 14:40:07 +00003931class HNewInstance FINAL : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00003932 public:
3933 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00003934 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08003935 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00003936 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00003937 bool finalizable,
3938 QuickEntrypointEnum entrypoint)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003939 : HExpression(DataType::Type::kReference, SideEffects::CanTriggerGC(), dex_pc),
David Brazdil6de19382016-01-08 17:37:10 +00003940 type_index_(type_index),
3941 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003942 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003943 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003944 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00003945 }
3946
Andreas Gampea5b09a62016-11-17 15:21:22 -08003947 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00003948 const DexFile& GetDexFile() const { return dex_file_; }
3949
3950 // Calls runtime so needs an environment.
3951 bool NeedsEnvironment() const OVERRIDE { return true; }
3952
Mingyao Yang062157f2016-03-02 10:15:36 -08003953 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
3954 bool CanThrow() const OVERRIDE { return true; }
3955
Nicolas Geoffray5247c082017-01-13 14:17:29 +00003956 bool NeedsChecks() const {
3957 return entrypoint_ == kQuickAllocObjectWithChecks;
3958 }
David Brazdil6de19382016-01-08 17:37:10 +00003959
Vladimir Markoa1de9182016-02-25 11:37:38 +00003960 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003961
3962 bool CanBeNull() const OVERRIDE { return false; }
3963
3964 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3965
3966 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3967 entrypoint_ = entrypoint;
3968 }
3969
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00003970 HLoadClass* GetLoadClass() const {
3971 HInstruction* input = InputAt(0);
3972 if (input->IsClinitCheck()) {
3973 input = input->InputAt(0);
3974 }
3975 DCHECK(input->IsLoadClass());
3976 return input->AsLoadClass();
3977 }
3978
David Brazdil6de19382016-01-08 17:37:10 +00003979 bool IsStringAlloc() const;
3980
3981 DECLARE_INSTRUCTION(NewInstance);
3982
3983 private:
Nicolas Geoffray5247c082017-01-13 14:17:29 +00003984 static constexpr size_t kFlagFinalizable = kNumberOfExpressionPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003985 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3986 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3987 "Too many packed fields.");
3988
Andreas Gampea5b09a62016-11-17 15:21:22 -08003989 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00003990 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003991 QuickEntrypointEnum entrypoint_;
3992
3993 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3994};
3995
Agi Csaki05f20562015-08-19 14:58:14 -07003996enum IntrinsicNeedsEnvironmentOrCache {
3997 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3998 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003999};
4000
Aart Bik5d75afe2015-12-14 11:57:01 -08004001enum IntrinsicSideEffects {
4002 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
4003 kReadSideEffects, // Intrinsic may read heap memory.
4004 kWriteSideEffects, // Intrinsic may write heap memory.
4005 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
4006};
4007
4008enum IntrinsicExceptions {
4009 kNoThrow, // Intrinsic does not throw any exceptions.
4010 kCanThrow // Intrinsic may throw exceptions.
4011};
4012
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004013class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004014 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004015 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004016
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01004017 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004018 SetRawInputAt(index, argument);
4019 }
4020
Roland Levillain3e3d7332015-04-28 11:00:54 +01004021 // Return the number of arguments. This number can be lower than
4022 // the number of inputs returned by InputCount(), as some invoke
4023 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
4024 // inputs at the end of their list of inputs.
4025 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
4026
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004027 DataType::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004028
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004029 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4030
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004031 InvokeType GetInvokeType() const {
4032 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004033 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004034
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01004035 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004036 return intrinsic_;
4037 }
4038
Aart Bik5d75afe2015-12-14 11:57:01 -08004039 void SetIntrinsic(Intrinsics intrinsic,
4040 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
4041 IntrinsicSideEffects side_effects,
4042 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004043
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004044 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00004045 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004046 }
4047
Aart Bikff7d89c2016-11-07 08:49:28 -08004048 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4049
Vladimir Markoa1de9182016-02-25 11:37:38 +00004050 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004051
Aart Bik71bf7b42016-11-16 10:17:46 -08004052 bool CanBeMoved() const OVERRIDE { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004053
Vladimir Marko372f10e2016-05-17 16:30:10 +01004054 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08004055 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4056 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004057
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004058 uint32_t* GetIntrinsicOptimizations() {
4059 return &intrinsic_optimizations_;
4060 }
4061
4062 const uint32_t* GetIntrinsicOptimizations() const {
4063 return &intrinsic_optimizations_;
4064 }
4065
4066 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4067
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004068 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004069 void SetResolvedMethod(ArtMethod* method) { resolved_method_ = method; }
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004070
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004071 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004072
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004073 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004074 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4075 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004076 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4077 static constexpr size_t kFieldReturnType =
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004078 kFieldInvokeType + kFieldInvokeTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004079 static constexpr size_t kFieldReturnTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004080 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00004081 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
4082 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
4083 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004084 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004085 using ReturnTypeField = BitField<DataType::Type, kFieldReturnType, kFieldReturnTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004086
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004087 HInvoke(ArenaAllocator* arena,
4088 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004089 uint32_t number_of_other_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004090 DataType::Type return_type,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004091 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004092 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004093 ArtMethod* resolved_method,
4094 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004095 : HVariableInputSizeInstruction(
4096 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4097 dex_pc,
4098 arena,
4099 number_of_arguments + number_of_other_inputs,
4100 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004101 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004102 resolved_method_(resolved_method),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004103 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07004104 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004105 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004106 SetPackedField<ReturnTypeField>(return_type);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004107 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004108 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004109 }
4110
Roland Levillain3e3d7332015-04-28 11:00:54 +01004111 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004112 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004113 const uint32_t dex_method_index_;
4114 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004115
4116 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4117 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004118
4119 private:
4120 DISALLOW_COPY_AND_ASSIGN(HInvoke);
4121};
4122
Vladimir Markofcb503c2016-05-18 12:48:17 +01004123class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004124 public:
4125 HInvokeUnresolved(ArenaAllocator* arena,
4126 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004127 DataType::Type return_type,
Calin Juravle175dc732015-08-25 15:42:32 +01004128 uint32_t dex_pc,
4129 uint32_t dex_method_index,
4130 InvokeType invoke_type)
4131 : HInvoke(arena,
4132 number_of_arguments,
4133 0u /* number_of_other_inputs */,
4134 return_type,
4135 dex_pc,
4136 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004137 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01004138 invoke_type) {
4139 }
4140
4141 DECLARE_INSTRUCTION(InvokeUnresolved);
4142
4143 private:
4144 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
4145};
4146
Orion Hodsonac141392017-01-13 11:53:47 +00004147class HInvokePolymorphic FINAL : public HInvoke {
4148 public:
4149 HInvokePolymorphic(ArenaAllocator* arena,
4150 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004151 DataType::Type return_type,
Orion Hodsonac141392017-01-13 11:53:47 +00004152 uint32_t dex_pc,
4153 uint32_t dex_method_index)
4154 : HInvoke(arena,
4155 number_of_arguments,
4156 0u /* number_of_other_inputs */,
4157 return_type,
4158 dex_pc,
4159 dex_method_index,
4160 nullptr,
4161 kVirtual) {}
4162
4163 DECLARE_INSTRUCTION(InvokePolymorphic);
4164
4165 private:
4166 DISALLOW_COPY_AND_ASSIGN(HInvokePolymorphic);
4167};
4168
Vladimir Markofcb503c2016-05-18 12:48:17 +01004169class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004170 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004171 // Requirements of this method call regarding the class
4172 // initialization (clinit) check of its declaring class.
4173 enum class ClinitCheckRequirement {
4174 kNone, // Class already initialized.
4175 kExplicit, // Static call having explicit clinit check as last input.
4176 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004177 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004178 };
4179
Vladimir Marko58155012015-08-19 12:49:41 +00004180 // Determines how to load the target ArtMethod*.
4181 enum class MethodLoadKind {
4182 // Use a String init ArtMethod* loaded from Thread entrypoints.
4183 kStringInit,
4184
4185 // Use the method's own ArtMethod* loaded by the register allocator.
4186 kRecursive,
4187
Vladimir Marko65979462017-05-19 17:25:12 +01004188 // Use PC-relative boot image ArtMethod* address that will be known at link time.
4189 // Used for boot image methods referenced by boot image code.
4190 kBootImageLinkTimePcRelative,
4191
Vladimir Marko58155012015-08-19 12:49:41 +00004192 // Use ArtMethod* at a known address, embed the direct address in the code.
4193 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
4194 kDirectAddress,
4195
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004196 // Load from an entry in the .bss section using a PC-relative load.
4197 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
4198 kBssEntry,
Vladimir Marko58155012015-08-19 12:49:41 +00004199
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004200 // Make a runtime call to resolve and call the method. This is the last-resort-kind
4201 // used when other kinds are unimplemented on a particular architecture.
4202 kRuntimeCall,
Vladimir Marko58155012015-08-19 12:49:41 +00004203 };
4204
4205 // Determines the location of the code pointer.
4206 enum class CodePtrLocation {
4207 // Recursive call, use local PC-relative call instruction.
4208 kCallSelf,
4209
Vladimir Marko58155012015-08-19 12:49:41 +00004210 // Use code pointer from the ArtMethod*.
4211 // Used when we don't know the target code. This is also the last-resort-kind used when
4212 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4213 kCallArtMethod,
4214 };
4215
4216 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004217 MethodLoadKind method_load_kind;
4218 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004219 // The method load data holds
4220 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4221 // Note that there are multiple string init methods, each having its own offset.
4222 // - the method address for kDirectAddress
Vladimir Markodc151b22015-10-15 18:02:30 +01004223 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004224 };
4225
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004226 HInvokeStaticOrDirect(ArenaAllocator* arena,
4227 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004228 DataType::Type return_type,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004229 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004230 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004231 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004232 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004233 InvokeType invoke_type,
4234 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004235 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01004236 : HInvoke(arena,
4237 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004238 // There is potentially one extra argument for the HCurrentMethod node, and
4239 // potentially one other if the clinit check is explicit, and potentially
4240 // one other if the method is a string factory.
4241 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004242 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004243 return_type,
4244 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004245 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004246 resolved_method,
4247 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004248 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004249 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004250 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4251 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004252
Vladimir Markodc151b22015-10-15 18:02:30 +01004253 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004254 bool had_current_method_input = HasCurrentMethodInput();
4255 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4256
4257 // Using the current method is the default and once we find a better
4258 // method load kind, we should not go back to using the current method.
4259 DCHECK(had_current_method_input || !needs_current_method_input);
4260
4261 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004262 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4263 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004264 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004265 dispatch_info_ = dispatch_info;
4266 }
4267
Aart Bik6daebeb2017-04-03 14:35:41 -07004268 DispatchInfo GetDispatchInfo() const {
4269 return dispatch_info_;
4270 }
4271
Vladimir Markoc53c0792015-11-19 15:48:33 +00004272 void AddSpecialInput(HInstruction* input) {
4273 // We allow only one special input.
4274 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4275 DCHECK(InputCount() == GetSpecialInputIndex() ||
4276 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4277 InsertInputAt(GetSpecialInputIndex(), input);
4278 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004279
Vladimir Marko372f10e2016-05-17 16:30:10 +01004280 using HInstruction::GetInputRecords; // Keep the const version visible.
4281 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
4282 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4283 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4284 DCHECK(!input_records.empty());
4285 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4286 HInstruction* last_input = input_records.back().GetInstruction();
4287 // Note: `last_input` may be null during arguments setup.
4288 if (last_input != nullptr) {
4289 // `last_input` is the last input of a static invoke marked as having
4290 // an explicit clinit check. It must either be:
4291 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4292 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4293 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4294 }
4295 }
4296 return input_records;
4297 }
4298
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004299 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004300 // We access the method via the dex cache so we can't do an implicit null check.
4301 // TODO: for intrinsics we can generate implicit null checks.
4302 return false;
4303 }
4304
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004305 bool CanBeNull() const OVERRIDE {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004306 return GetPackedField<ReturnTypeField>() == DataType::Type::kReference && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004307 }
4308
Vladimir Markoc53c0792015-11-19 15:48:33 +00004309 // Get the index of the special input, if any.
4310 //
David Brazdil6de19382016-01-08 17:37:10 +00004311 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4312 // method pointer; otherwise there may be one platform-specific special input,
4313 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004314 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004315 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004316
Vladimir Marko58155012015-08-19 12:49:41 +00004317 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4318 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4319 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004320 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004321 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004322 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko65979462017-05-19 17:25:12 +01004323 bool HasPcRelativeMethodLoadKind() const {
4324 return GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004325 GetMethodLoadKind() == MethodLoadKind::kBssEntry;
Vladimir Marko65979462017-05-19 17:25:12 +01004326 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004327 bool HasCurrentMethodInput() const {
4328 // This function can be called only after the invoke has been fully initialized by the builder.
4329 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004330 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004331 return true;
4332 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004333 DCHECK(InputCount() == GetSpecialInputIndex() ||
4334 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004335 return false;
4336 }
4337 }
Vladimir Marko58155012015-08-19 12:49:41 +00004338
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004339 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004340 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004341 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004342 }
4343
4344 uint64_t GetMethodAddress() const {
4345 DCHECK(HasMethodAddress());
4346 return dispatch_info_.method_load_data;
4347 }
4348
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004349 const DexFile& GetDexFileForPcRelativeDexCache() const;
4350
Vladimir Markoa1de9182016-02-25 11:37:38 +00004351 ClinitCheckRequirement GetClinitCheckRequirement() const {
4352 return GetPackedField<ClinitCheckRequirementField>();
4353 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004354
Roland Levillain4c0eb422015-04-24 16:43:49 +01004355 // Is this instruction a call to a static method?
4356 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004357 return GetInvokeType() == kStatic;
4358 }
4359
4360 MethodReference GetTargetMethod() const {
4361 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004362 }
4363
Vladimir Markofbb184a2015-11-13 14:47:00 +00004364 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4365 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4366 // instruction; only relevant for static calls with explicit clinit check.
4367 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004368 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004369 size_t last_input_index = inputs_.size() - 1u;
4370 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004371 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004372 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004373 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004374 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004375 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004376 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004377 }
4378
4379 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004380 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004381 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004382 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004383 }
4384
4385 // Is this a call to a static method whose declaring class has an
4386 // implicit intialization check requirement?
4387 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004388 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004389 }
4390
Vladimir Markob554b5a2015-11-06 12:57:55 +00004391 // Does this method load kind need the current method as an input?
4392 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004393 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kRuntimeCall;
Vladimir Markob554b5a2015-11-06 12:57:55 +00004394 }
4395
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004396 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004397
4398 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004399 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004400 static constexpr size_t kFieldClinitCheckRequirementSize =
4401 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4402 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4403 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4404 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4405 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004406 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4407 kFieldClinitCheckRequirement,
4408 kFieldClinitCheckRequirementSize>;
4409
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004410 // Cached values of the resolved method, to avoid needing the mutator lock.
Vladimir Marko58155012015-08-19 12:49:41 +00004411 MethodReference target_method_;
4412 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004413
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004414 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004415};
Vladimir Markof64242a2015-12-01 14:58:23 +00004416std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004417std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004418
Vladimir Markofcb503c2016-05-18 12:48:17 +01004419class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004420 public:
4421 HInvokeVirtual(ArenaAllocator* arena,
4422 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004423 DataType::Type return_type,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004424 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004425 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004426 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004427 uint32_t vtable_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004428 : HInvoke(arena,
4429 number_of_arguments,
4430 0u,
4431 return_type,
4432 dex_pc,
4433 dex_method_index,
4434 resolved_method,
4435 kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004436 vtable_index_(vtable_index) {}
4437
Aart Bik71bf7b42016-11-16 10:17:46 -08004438 bool CanBeNull() const OVERRIDE {
4439 switch (GetIntrinsic()) {
4440 case Intrinsics::kThreadCurrentThread:
4441 case Intrinsics::kStringBufferAppend:
4442 case Intrinsics::kStringBufferToString:
4443 case Intrinsics::kStringBuilderAppend:
4444 case Intrinsics::kStringBuilderToString:
4445 return false;
4446 default:
4447 return HInvoke::CanBeNull();
4448 }
4449 }
4450
Calin Juravle641547a2015-04-21 22:08:51 +01004451 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004452 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004453 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004454 }
4455
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004456 uint32_t GetVTableIndex() const { return vtable_index_; }
4457
4458 DECLARE_INSTRUCTION(InvokeVirtual);
4459
4460 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004461 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004462 const uint32_t vtable_index_;
4463
4464 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4465};
4466
Vladimir Markofcb503c2016-05-18 12:48:17 +01004467class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004468 public:
4469 HInvokeInterface(ArenaAllocator* arena,
4470 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004471 DataType::Type return_type,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004472 uint32_t dex_pc,
4473 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004474 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004475 uint32_t imt_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004476 : HInvoke(arena,
4477 number_of_arguments,
4478 0u,
4479 return_type,
4480 dex_pc,
4481 dex_method_index,
4482 resolved_method,
4483 kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004484 imt_index_(imt_index) {}
4485
Calin Juravle641547a2015-04-21 22:08:51 +01004486 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004487 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004488 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004489 }
4490
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004491 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
4492 // The assembly stub currently needs it.
4493 return true;
4494 }
4495
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004496 uint32_t GetImtIndex() const { return imt_index_; }
4497 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4498
4499 DECLARE_INSTRUCTION(InvokeInterface);
4500
4501 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004502 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004503 const uint32_t imt_index_;
4504
4505 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4506};
4507
Vladimir Markofcb503c2016-05-18 12:48:17 +01004508class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004509 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004510 HNeg(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004511 : HUnaryOperation(result_type, input, dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004512 DCHECK_EQ(result_type, DataType::Kind(input->GetType()));
Roland Levillain937e6cd2016-03-22 11:54:37 +00004513 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004514
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004515 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004516
4517 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004518 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004519 }
4520 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004521 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004522 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004523 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4524 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4525 }
4526 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4527 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4528 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004529
Roland Levillain88cb1752014-10-20 16:36:47 +01004530 DECLARE_INSTRUCTION(Neg);
4531
4532 private:
4533 DISALLOW_COPY_AND_ASSIGN(HNeg);
4534};
4535
Vladimir Markofcb503c2016-05-18 12:48:17 +01004536class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004537 public:
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004538 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004539 : HExpression(DataType::Type::kReference, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004540 SetRawInputAt(0, cls);
4541 SetRawInputAt(1, length);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004542 }
4543
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004544 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004545 bool NeedsEnvironment() const OVERRIDE { return true; }
4546
Mingyao Yang0c365e62015-03-31 15:09:29 -07004547 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4548 bool CanThrow() const OVERRIDE { return true; }
4549
Calin Juravle10e244f2015-01-26 18:54:32 +00004550 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004551
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004552 HLoadClass* GetLoadClass() const {
4553 DCHECK(InputAt(0)->IsLoadClass());
4554 return InputAt(0)->AsLoadClass();
4555 }
4556
4557 HInstruction* GetLength() const {
4558 return InputAt(1);
4559 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004560
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004561 DECLARE_INSTRUCTION(NewArray);
4562
4563 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004564 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4565};
4566
Vladimir Markofcb503c2016-05-18 12:48:17 +01004567class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004568 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004569 HAdd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004570 HInstruction* left,
4571 HInstruction* right,
4572 uint32_t dex_pc = kNoDexPc)
4573 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004574
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004575 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004576
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004577 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004578
4579 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004580 return GetBlock()->GetGraph()->GetIntConstant(
4581 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004582 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004583 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004584 return GetBlock()->GetGraph()->GetLongConstant(
4585 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004586 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004587 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4588 return GetBlock()->GetGraph()->GetFloatConstant(
4589 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4590 }
4591 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4592 return GetBlock()->GetGraph()->GetDoubleConstant(
4593 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4594 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004595
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004596 DECLARE_INSTRUCTION(Add);
4597
4598 private:
4599 DISALLOW_COPY_AND_ASSIGN(HAdd);
4600};
4601
Vladimir Markofcb503c2016-05-18 12:48:17 +01004602class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004603 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004604 HSub(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004605 HInstruction* left,
4606 HInstruction* right,
4607 uint32_t dex_pc = kNoDexPc)
4608 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004609
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004610 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004611
4612 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004613 return GetBlock()->GetGraph()->GetIntConstant(
4614 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004615 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004616 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004617 return GetBlock()->GetGraph()->GetLongConstant(
4618 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004619 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004620 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4621 return GetBlock()->GetGraph()->GetFloatConstant(
4622 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4623 }
4624 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4625 return GetBlock()->GetGraph()->GetDoubleConstant(
4626 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4627 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004628
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004629 DECLARE_INSTRUCTION(Sub);
4630
4631 private:
4632 DISALLOW_COPY_AND_ASSIGN(HSub);
4633};
4634
Vladimir Markofcb503c2016-05-18 12:48:17 +01004635class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004636 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004637 HMul(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004638 HInstruction* left,
4639 HInstruction* right,
4640 uint32_t dex_pc = kNoDexPc)
4641 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004642
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004643 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004644
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004645 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004646
4647 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004648 return GetBlock()->GetGraph()->GetIntConstant(
4649 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004650 }
4651 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004652 return GetBlock()->GetGraph()->GetLongConstant(
4653 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004654 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004655 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4656 return GetBlock()->GetGraph()->GetFloatConstant(
4657 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4658 }
4659 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4660 return GetBlock()->GetGraph()->GetDoubleConstant(
4661 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4662 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004663
4664 DECLARE_INSTRUCTION(Mul);
4665
4666 private:
4667 DISALLOW_COPY_AND_ASSIGN(HMul);
4668};
4669
Vladimir Markofcb503c2016-05-18 12:48:17 +01004670class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004671 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004672 HDiv(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004673 HInstruction* left,
4674 HInstruction* right,
4675 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004676 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004677
Roland Levillain9867bc72015-08-05 10:21:34 +01004678 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004679 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004680 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004681 // Our graph structure ensures we never have 0 for `y` during
4682 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004683 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004684 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004685 return (y == -1) ? -x : x / y;
4686 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004687
Roland Levillain31dd3d62016-02-16 12:21:02 +00004688 template <typename T>
4689 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004690 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004691 return x / y;
4692 }
4693
Roland Levillain9867bc72015-08-05 10:21:34 +01004694 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004695 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004696 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004697 }
4698 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004699 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004700 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4701 }
4702 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4703 return GetBlock()->GetGraph()->GetFloatConstant(
4704 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4705 }
4706 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4707 return GetBlock()->GetGraph()->GetDoubleConstant(
4708 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004709 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004710
4711 DECLARE_INSTRUCTION(Div);
4712
4713 private:
4714 DISALLOW_COPY_AND_ASSIGN(HDiv);
4715};
4716
Vladimir Markofcb503c2016-05-18 12:48:17 +01004717class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004718 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004719 HRem(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004720 HInstruction* left,
4721 HInstruction* right,
4722 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004723 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004724
Roland Levillain9867bc72015-08-05 10:21:34 +01004725 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004726 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004727 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004728 // Our graph structure ensures we never have 0 for `y` during
4729 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004730 DCHECK_NE(y, 0);
4731 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4732 return (y == -1) ? 0 : x % y;
4733 }
4734
Roland Levillain31dd3d62016-02-16 12:21:02 +00004735 template <typename T>
4736 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004737 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004738 return std::fmod(x, y);
4739 }
4740
Roland Levillain9867bc72015-08-05 10:21:34 +01004741 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004742 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004743 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004744 }
4745 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004746 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004747 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004748 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004749 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4750 return GetBlock()->GetGraph()->GetFloatConstant(
4751 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4752 }
4753 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4754 return GetBlock()->GetGraph()->GetDoubleConstant(
4755 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4756 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004757
4758 DECLARE_INSTRUCTION(Rem);
4759
4760 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004761 DISALLOW_COPY_AND_ASSIGN(HRem);
4762};
4763
Vladimir Markofcb503c2016-05-18 12:48:17 +01004764class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00004765 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004766 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4767 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004768 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004769 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004770 SetRawInputAt(0, value);
4771 }
4772
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004773 DataType::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004774
Calin Juravled0d48522014-11-04 16:40:20 +00004775 bool CanBeMoved() const OVERRIDE { return true; }
4776
Vladimir Marko372f10e2016-05-17 16:30:10 +01004777 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004778 return true;
4779 }
4780
4781 bool NeedsEnvironment() const OVERRIDE { return true; }
4782 bool CanThrow() const OVERRIDE { return true; }
4783
Calin Juravled0d48522014-11-04 16:40:20 +00004784 DECLARE_INSTRUCTION(DivZeroCheck);
4785
4786 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004787 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4788};
4789
Vladimir Markofcb503c2016-05-18 12:48:17 +01004790class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004791 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004792 HShl(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004793 HInstruction* value,
4794 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004795 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004796 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004797 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
4798 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004799 }
4800
Roland Levillain5b5b9312016-03-22 14:57:31 +00004801 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004802 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004803 return value << (distance & max_shift_distance);
4804 }
4805
4806 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004807 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004808 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004809 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004810 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004811 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004812 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004813 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004814 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4815 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4816 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4817 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004818 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004819 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4820 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004821 LOG(FATAL) << DebugName() << " is not defined for float values";
4822 UNREACHABLE();
4823 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004824 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4825 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004826 LOG(FATAL) << DebugName() << " is not defined for double values";
4827 UNREACHABLE();
4828 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004829
4830 DECLARE_INSTRUCTION(Shl);
4831
4832 private:
4833 DISALLOW_COPY_AND_ASSIGN(HShl);
4834};
4835
Vladimir Markofcb503c2016-05-18 12:48:17 +01004836class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004837 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004838 HShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004839 HInstruction* value,
4840 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004841 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004842 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004843 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
4844 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004845 }
4846
Roland Levillain5b5b9312016-03-22 14:57:31 +00004847 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004848 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004849 return value >> (distance & max_shift_distance);
4850 }
4851
4852 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004853 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004854 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004855 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004856 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004857 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004858 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004859 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004860 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4861 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4862 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4863 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004864 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004865 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4866 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004867 LOG(FATAL) << DebugName() << " is not defined for float values";
4868 UNREACHABLE();
4869 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004870 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4871 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004872 LOG(FATAL) << DebugName() << " is not defined for double values";
4873 UNREACHABLE();
4874 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004875
4876 DECLARE_INSTRUCTION(Shr);
4877
4878 private:
4879 DISALLOW_COPY_AND_ASSIGN(HShr);
4880};
4881
Vladimir Markofcb503c2016-05-18 12:48:17 +01004882class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004883 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004884 HUShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004885 HInstruction* value,
4886 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004887 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004888 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004889 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
4890 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004891 }
4892
Roland Levillain5b5b9312016-03-22 14:57:31 +00004893 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004894 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004895 typedef typename std::make_unsigned<T>::type V;
4896 V ux = static_cast<V>(value);
4897 return static_cast<T>(ux >> (distance & max_shift_distance));
4898 }
4899
4900 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004901 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004902 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004903 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004904 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004905 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004906 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004907 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004908 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4909 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4910 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4911 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004912 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004913 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4914 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004915 LOG(FATAL) << DebugName() << " is not defined for float values";
4916 UNREACHABLE();
4917 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004918 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4919 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004920 LOG(FATAL) << DebugName() << " is not defined for double values";
4921 UNREACHABLE();
4922 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004923
4924 DECLARE_INSTRUCTION(UShr);
4925
4926 private:
4927 DISALLOW_COPY_AND_ASSIGN(HUShr);
4928};
4929
Vladimir Markofcb503c2016-05-18 12:48:17 +01004930class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004931 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004932 HAnd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004933 HInstruction* left,
4934 HInstruction* right,
4935 uint32_t dex_pc = kNoDexPc)
4936 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004937
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004938 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004939
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004940 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004941
4942 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004943 return GetBlock()->GetGraph()->GetIntConstant(
4944 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004945 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004946 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004947 return GetBlock()->GetGraph()->GetLongConstant(
4948 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004949 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004950 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4951 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4952 LOG(FATAL) << DebugName() << " is not defined for float values";
4953 UNREACHABLE();
4954 }
4955 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4956 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4957 LOG(FATAL) << DebugName() << " is not defined for double values";
4958 UNREACHABLE();
4959 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004960
4961 DECLARE_INSTRUCTION(And);
4962
4963 private:
4964 DISALLOW_COPY_AND_ASSIGN(HAnd);
4965};
4966
Vladimir Markofcb503c2016-05-18 12:48:17 +01004967class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004968 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004969 HOr(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004970 HInstruction* left,
4971 HInstruction* right,
4972 uint32_t dex_pc = kNoDexPc)
4973 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004974
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004975 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004976
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004977 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004978
4979 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004980 return GetBlock()->GetGraph()->GetIntConstant(
4981 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004982 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004983 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004984 return GetBlock()->GetGraph()->GetLongConstant(
4985 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004986 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004987 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4988 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4989 LOG(FATAL) << DebugName() << " is not defined for float values";
4990 UNREACHABLE();
4991 }
4992 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4993 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4994 LOG(FATAL) << DebugName() << " is not defined for double values";
4995 UNREACHABLE();
4996 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004997
4998 DECLARE_INSTRUCTION(Or);
4999
5000 private:
5001 DISALLOW_COPY_AND_ASSIGN(HOr);
5002};
5003
Vladimir Markofcb503c2016-05-18 12:48:17 +01005004class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005005 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005006 HXor(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005007 HInstruction* left,
5008 HInstruction* right,
5009 uint32_t dex_pc = kNoDexPc)
5010 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005011
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005012 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005013
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005014 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005015
5016 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005017 return GetBlock()->GetGraph()->GetIntConstant(
5018 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005019 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005020 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005021 return GetBlock()->GetGraph()->GetLongConstant(
5022 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005023 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005024 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5025 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5026 LOG(FATAL) << DebugName() << " is not defined for float values";
5027 UNREACHABLE();
5028 }
5029 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5030 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5031 LOG(FATAL) << DebugName() << " is not defined for double values";
5032 UNREACHABLE();
5033 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005034
5035 DECLARE_INSTRUCTION(Xor);
5036
5037 private:
5038 DISALLOW_COPY_AND_ASSIGN(HXor);
5039};
5040
Vladimir Markofcb503c2016-05-18 12:48:17 +01005041class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005042 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005043 HRor(DataType::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00005044 : HBinaryOperation(result_type, value, distance) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005045 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5046 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00005047 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005048
Roland Levillain5b5b9312016-03-22 14:57:31 +00005049 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005050 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005051 typedef typename std::make_unsigned<T>::type V;
5052 V ux = static_cast<V>(value);
5053 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005054 return static_cast<T>(ux);
5055 } else {
5056 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005057 return static_cast<T>(ux >> (distance & max_shift_value)) |
5058 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005059 }
5060 }
5061
Roland Levillain5b5b9312016-03-22 14:57:31 +00005062 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005063 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005064 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005065 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005066 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005067 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005068 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005069 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005070 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5071 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5072 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5073 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005074 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005075 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5076 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005077 LOG(FATAL) << DebugName() << " is not defined for float values";
5078 UNREACHABLE();
5079 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005080 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5081 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005082 LOG(FATAL) << DebugName() << " is not defined for double values";
5083 UNREACHABLE();
5084 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005085
5086 DECLARE_INSTRUCTION(Ror);
5087
5088 private:
5089 DISALLOW_COPY_AND_ASSIGN(HRor);
5090};
5091
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005092// The value of a parameter in this method. Its location depends on
5093// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01005094class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005095 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005096 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005097 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005098 uint8_t index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005099 DataType::Type parameter_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005100 bool is_this = false)
5101 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005102 dex_file_(dex_file),
5103 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005104 index_(index) {
5105 SetPackedFlag<kFlagIsThis>(is_this);
5106 SetPackedFlag<kFlagCanBeNull>(!is_this);
5107 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005108
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005109 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005110 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005111 uint8_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07005112 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005113
Vladimir Markoa1de9182016-02-25 11:37:38 +00005114 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
5115 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005116
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005117 DECLARE_INSTRUCTION(ParameterValue);
5118
5119 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005120 // Whether or not the parameter value corresponds to 'this' argument.
5121 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
5122 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5123 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5124 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5125 "Too many packed fields.");
5126
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005127 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005128 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005129 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005130 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005131 const uint8_t index_;
5132
5133 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
5134};
5135
Vladimir Markofcb503c2016-05-18 12:48:17 +01005136class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005137 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005138 HNot(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005139 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005140
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005141 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005142 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005143 return true;
5144 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005145
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005146 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005147
5148 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005149 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005150 }
5151 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005152 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005153 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005154 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5155 LOG(FATAL) << DebugName() << " is not defined for float values";
5156 UNREACHABLE();
5157 }
5158 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5159 LOG(FATAL) << DebugName() << " is not defined for double values";
5160 UNREACHABLE();
5161 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005162
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005163 DECLARE_INSTRUCTION(Not);
5164
5165 private:
5166 DISALLOW_COPY_AND_ASSIGN(HNot);
5167};
5168
Vladimir Markofcb503c2016-05-18 12:48:17 +01005169class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005170 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005171 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005172 : HUnaryOperation(DataType::Type::kBool, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01005173
5174 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005175 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01005176 return true;
5177 }
5178
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005179 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005180 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005181 return !x;
5182 }
5183
Roland Levillain9867bc72015-08-05 10:21:34 +01005184 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005185 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005186 }
5187 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5188 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005189 UNREACHABLE();
5190 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005191 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5192 LOG(FATAL) << DebugName() << " is not defined for float values";
5193 UNREACHABLE();
5194 }
5195 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5196 LOG(FATAL) << DebugName() << " is not defined for double values";
5197 UNREACHABLE();
5198 }
David Brazdil66d126e2015-04-03 16:02:44 +01005199
5200 DECLARE_INSTRUCTION(BooleanNot);
5201
5202 private:
5203 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
5204};
5205
Vladimir Markofcb503c2016-05-18 12:48:17 +01005206class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005207 public:
5208 // Instantiate a type conversion of `input` to `result_type`.
Vladimir Markoc8fb2112017-10-03 11:37:52 +01005209 HTypeConversion(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005210 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005211 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005212 // Invariant: We should never generate a conversion to a Boolean value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005213 DCHECK_NE(DataType::Type::kBool, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005214 }
5215
5216 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005217 DataType::Type GetInputType() const { return GetInput()->GetType(); }
5218 DataType::Type GetResultType() const { return GetType(); }
Roland Levillaindff1f282014-11-05 14:15:05 +00005219
5220 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005221 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5222 return true;
5223 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005224
Mark Mendelle82549b2015-05-06 10:55:34 -04005225 // Try to statically evaluate the conversion and return a HConstant
5226 // containing the result. If the input cannot be converted, return nullptr.
5227 HConstant* TryStaticEvaluation() const;
5228
Roland Levillaindff1f282014-11-05 14:15:05 +00005229 DECLARE_INSTRUCTION(TypeConversion);
5230
5231 private:
5232 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
5233};
5234
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005235static constexpr uint32_t kNoRegNumber = -1;
5236
Vladimir Markofcb503c2016-05-18 12:48:17 +01005237class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005238 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005239 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
5240 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005241 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005242 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005243 SetRawInputAt(0, value);
5244 }
5245
Calin Juravle10e244f2015-01-26 18:54:32 +00005246 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005247 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005248 return true;
5249 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005250
Calin Juravle10e244f2015-01-26 18:54:32 +00005251 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005252
Calin Juravle10e244f2015-01-26 18:54:32 +00005253 bool CanThrow() const OVERRIDE { return true; }
5254
5255 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005256
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005257
5258 DECLARE_INSTRUCTION(NullCheck);
5259
5260 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005261 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
5262};
5263
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005264// Embeds an ArtField and all the information required by the compiler. We cache
5265// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005266class FieldInfo : public ValueObject {
5267 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005268 FieldInfo(ArtField* field,
5269 MemberOffset field_offset,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005270 DataType::Type field_type,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005271 bool is_volatile,
5272 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005273 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005274 const DexFile& dex_file)
5275 : field_(field),
5276 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005277 field_type_(field_type),
5278 is_volatile_(is_volatile),
5279 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005280 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005281 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005282
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005283 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005284 MemberOffset GetFieldOffset() const { return field_offset_; }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005285 DataType::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005286 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005287 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005288 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005289 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005290
5291 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005292 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005293 const MemberOffset field_offset_;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005294 const DataType::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005295 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005296 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005297 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005298 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005299};
5300
Vladimir Markofcb503c2016-05-18 12:48:17 +01005301class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005302 public:
5303 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005304 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005305 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005306 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005307 bool is_volatile,
5308 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005309 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005310 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005311 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005312 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005313 field_info_(field,
5314 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005315 field_type,
5316 is_volatile,
5317 field_idx,
5318 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005319 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005320 SetRawInputAt(0, value);
5321 }
5322
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005323 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005324
Vladimir Marko372f10e2016-05-17 16:30:10 +01005325 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5326 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005327 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005328 }
5329
Calin Juravle641547a2015-04-21 22:08:51 +01005330 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005331 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005332 }
5333
5334 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005335 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5336 }
5337
Calin Juravle52c48962014-12-16 17:02:57 +00005338 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005339 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005340 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005341 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005342
5343 DECLARE_INSTRUCTION(InstanceFieldGet);
5344
5345 private:
5346 const FieldInfo field_info_;
5347
5348 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5349};
5350
Vladimir Markofcb503c2016-05-18 12:48:17 +01005351class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005352 public:
5353 HInstanceFieldSet(HInstruction* object,
5354 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005355 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005356 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005357 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005358 bool is_volatile,
5359 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005360 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005361 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005362 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005363 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005364 field_info_(field,
5365 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005366 field_type,
5367 is_volatile,
5368 field_idx,
5369 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005370 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005371 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005372 SetRawInputAt(0, object);
5373 SetRawInputAt(1, value);
5374 }
5375
Calin Juravle641547a2015-04-21 22:08:51 +01005376 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005377 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005378 }
5379
Calin Juravle52c48962014-12-16 17:02:57 +00005380 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005381 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005382 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005383 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005384 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005385 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5386 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005387
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005388 DECLARE_INSTRUCTION(InstanceFieldSet);
5389
5390 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005391 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5392 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5393 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5394 "Too many packed fields.");
5395
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005396 const FieldInfo field_info_;
5397
5398 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5399};
5400
Vladimir Markofcb503c2016-05-18 12:48:17 +01005401class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005402 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005403 HArrayGet(HInstruction* array,
5404 HInstruction* index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005405 DataType::Type type,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005406 uint32_t dex_pc)
5407 : HArrayGet(array,
5408 index,
5409 type,
5410 SideEffects::ArrayReadOfType(type),
5411 dex_pc,
5412 /* is_string_char_at */ false) {}
5413
5414 HArrayGet(HInstruction* array,
5415 HInstruction* index,
5416 DataType::Type type,
5417 SideEffects side_effects,
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005418 uint32_t dex_pc,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005419 bool is_string_char_at)
5420 : HExpression(type, side_effects, dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005421 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005422 SetRawInputAt(0, array);
5423 SetRawInputAt(1, index);
5424 }
5425
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005426 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005427 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005428 return true;
5429 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005430 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005431 // TODO: We can be smarter here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01005432 // Currently, unless the array is the result of NewArray, the array access is always
5433 // preceded by some form of null NullCheck necessary for the bounds check, usually
5434 // implicit null check on the ArrayLength input to BoundsCheck or Deoptimize for
5435 // dynamic BCE. There are cases when these could be removed to produce better code.
5436 // If we ever add optimizations to do so we should allow an implicit check here
5437 // (as long as the address falls in the first page).
5438 //
5439 // As an example of such fancy optimization, we could eliminate BoundsCheck for
5440 // a = cond ? new int[1] : null;
5441 // a[0]; // The Phi does not need bounds check for either input.
Calin Juravle77520bc2015-01-12 18:45:46 +00005442 return false;
5443 }
5444
David Brazdil4833f5a2015-12-16 10:37:39 +00005445 bool IsEquivalentOf(HArrayGet* other) const {
5446 bool result = (GetDexPc() == other->GetDexPc());
5447 if (kIsDebugBuild && result) {
5448 DCHECK_EQ(GetBlock(), other->GetBlock());
5449 DCHECK_EQ(GetArray(), other->GetArray());
5450 DCHECK_EQ(GetIndex(), other->GetIndex());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005451 if (DataType::IsIntOrLongType(GetType())) {
5452 DCHECK(DataType::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005453 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005454 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
5455 DCHECK(DataType::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005456 }
5457 }
5458 return result;
5459 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005460
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005461 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5462
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005463 HInstruction* GetArray() const { return InputAt(0); }
5464 HInstruction* GetIndex() const { return InputAt(1); }
5465
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005466 DECLARE_INSTRUCTION(ArrayGet);
5467
5468 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005469 // We treat a String as an array, creating the HArrayGet from String.charAt()
5470 // intrinsic in the instruction simplifier. We can always determine whether
5471 // a particular HArrayGet is actually a String.charAt() by looking at the type
5472 // of the input but that requires holding the mutator lock, so we prefer to use
5473 // a flag, so that code generators don't need to do the locking.
5474 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
5475 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5476 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5477 "Too many packed fields.");
5478
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005479 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5480};
5481
Vladimir Markofcb503c2016-05-18 12:48:17 +01005482class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005483 public:
5484 HArraySet(HInstruction* array,
5485 HInstruction* index,
5486 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005487 DataType::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005488 uint32_t dex_pc)
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005489 : HArraySet(array,
5490 index,
5491 value,
5492 expected_component_type,
5493 // Make a best guess for side effects now, may be refined during SSA building.
5494 ComputeSideEffects(GetComponentType(value->GetType(), expected_component_type)),
5495 dex_pc) {}
5496
5497 HArraySet(HInstruction* array,
5498 HInstruction* index,
5499 HInstruction* value,
5500 DataType::Type expected_component_type,
5501 SideEffects side_effects,
5502 uint32_t dex_pc)
5503 : HTemplateInstruction(side_effects, dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005504 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005505 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005506 SetPackedFlag<kFlagValueCanBeNull>(true);
5507 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005508 SetRawInputAt(0, array);
5509 SetRawInputAt(1, index);
5510 SetRawInputAt(2, value);
5511 }
5512
Calin Juravle77520bc2015-01-12 18:45:46 +00005513 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005514 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005515 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005516 }
5517
Mingyao Yang81014cb2015-06-02 03:16:27 -07005518 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005519 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005520
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005521 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005522 // TODO: Same as for ArrayGet.
5523 return false;
5524 }
5525
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005526 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005527 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005528 }
5529
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005530 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005531 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005532 }
5533
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005534 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005535 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005536 }
5537
Vladimir Markoa1de9182016-02-25 11:37:38 +00005538 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5539 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5540 bool StaticTypeOfArrayIsObjectArray() const {
5541 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5542 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005543
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005544 HInstruction* GetArray() const { return InputAt(0); }
5545 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005546 HInstruction* GetValue() const { return InputAt(2); }
5547
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005548 DataType::Type GetComponentType() const {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005549 return GetComponentType(GetValue()->GetType(), GetRawExpectedComponentType());
5550 }
5551
5552 static DataType::Type GetComponentType(DataType::Type value_type,
5553 DataType::Type expected_component_type) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005554 // The Dex format does not type floating point index operations. Since the
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005555 // `expected_component_type` comes from SSA building and can therefore not
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005556 // be correct, we also check what is the value type. If it is a floating
5557 // point type, we must use that type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005558 return ((value_type == DataType::Type::kFloat32) || (value_type == DataType::Type::kFloat64))
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005559 ? value_type
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005560 : expected_component_type;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005561 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005562
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005563 DataType::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005564 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005565 }
5566
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005567 static SideEffects ComputeSideEffects(DataType::Type type) {
5568 return SideEffects::ArrayWriteOfType(type).Union(SideEffectsForArchRuntimeCalls(type));
Aart Bik18b36ab2016-04-13 16:41:35 -07005569 }
5570
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005571 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type value_type) {
5572 return (value_type == DataType::Type::kReference) ? SideEffects::CanTriggerGC()
5573 : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005574 }
5575
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005576 DECLARE_INSTRUCTION(ArraySet);
5577
5578 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005579 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5580 static constexpr size_t kFieldExpectedComponentTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005581 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00005582 static constexpr size_t kFlagNeedsTypeCheck =
5583 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5584 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005585 // Cached information for the reference_type_info_ so that codegen
5586 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005587 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5588 static constexpr size_t kNumberOfArraySetPackedBits =
5589 kFlagStaticTypeOfArrayIsObjectArray + 1;
5590 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5591 using ExpectedComponentTypeField =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005592 BitField<DataType::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005593
5594 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5595};
5596
Vladimir Markofcb503c2016-05-18 12:48:17 +01005597class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005598 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005599 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005600 : HExpression(DataType::Type::kInt32, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005601 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005602 // Note that arrays do not change length, so the instruction does not
5603 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005604 SetRawInputAt(0, array);
5605 }
5606
Calin Juravle77520bc2015-01-12 18:45:46 +00005607 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005608 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005609 return true;
5610 }
Calin Juravle641547a2015-04-21 22:08:51 +01005611 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5612 return obj == InputAt(0);
5613 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005614
Vladimir Markodce016e2016-04-28 13:10:02 +01005615 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5616
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005617 DECLARE_INSTRUCTION(ArrayLength);
5618
5619 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005620 // We treat a String as an array, creating the HArrayLength from String.length()
5621 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5622 // determine whether a particular HArrayLength is actually a String.length() by
5623 // looking at the type of the input but that requires holding the mutator lock, so
5624 // we prefer to use a flag, so that code generators don't need to do the locking.
5625 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5626 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5627 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5628 "Too many packed fields.");
5629
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005630 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5631};
5632
Vladimir Markofcb503c2016-05-18 12:48:17 +01005633class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005634 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005635 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5636 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005637 HBoundsCheck(HInstruction* index,
5638 HInstruction* length,
5639 uint32_t dex_pc,
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005640 bool string_char_at = false)
5641 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005642 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(index->GetType()));
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005643 SetPackedFlag<kFlagIsStringCharAt>(string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005644 SetRawInputAt(0, index);
5645 SetRawInputAt(1, length);
5646 }
5647
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005648 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005649 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005650 return true;
5651 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005652
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005653 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005654
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005655 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005656
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005657 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005658
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005659 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005660
5661 DECLARE_INSTRUCTION(BoundsCheck);
5662
5663 private:
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005664 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005665
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005666 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5667};
5668
Vladimir Markofcb503c2016-05-18 12:48:17 +01005669class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005670 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005671 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005672 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005673
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005674 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005675 return true;
5676 }
5677
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005678 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5679 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005680
5681 DECLARE_INSTRUCTION(SuspendCheck);
5682
5683 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005684 // Only used for code generation, in order to share the same slow path between back edges
5685 // of a same loop.
5686 SlowPathCode* slow_path_;
5687
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005688 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5689};
5690
David Srbecky0cf44932015-12-09 14:09:59 +00005691// Pseudo-instruction which provides the native debugger with mapping information.
5692// It ensures that we can generate line number and local variables at this point.
5693class HNativeDebugInfo : public HTemplateInstruction<0> {
5694 public:
5695 explicit HNativeDebugInfo(uint32_t dex_pc)
5696 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5697
5698 bool NeedsEnvironment() const OVERRIDE {
5699 return true;
5700 }
5701
5702 DECLARE_INSTRUCTION(NativeDebugInfo);
5703
5704 private:
5705 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5706};
5707
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005708/**
5709 * Instruction to load a Class object.
5710 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005711class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005712 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005713 // Determines how to load the Class.
5714 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005715 // We cannot load this class. See HSharpening::SharpenLoadClass.
5716 kInvalid = -1,
5717
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005718 // Use the Class* from the method's own ArtMethod*.
5719 kReferrersClass,
5720
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005721 // Use PC-relative boot image Class* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01005722 // Used for boot image classes referenced by boot image code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005723 kBootImageLinkTimePcRelative,
5724
5725 // Use a known boot image Class* address, embedded in the code by the codegen.
5726 // Used for boot image classes referenced by apps in AOT- and JIT-compiled code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005727 kBootImageAddress,
5728
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005729 // Use a PC-relative load from a boot image ClassTable mmapped into the .bss
5730 // of the oat file.
5731 kBootImageClassTable,
5732
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005733 // Load from an entry in the .bss section using a PC-relative load.
5734 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
5735 kBssEntry,
5736
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005737 // Load from the root table associated with the JIT compiled method.
5738 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005739
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005740 // Load using a simple runtime call. This is the fall-back load kind when
5741 // the codegen is unable to use another appropriate kind.
5742 kRuntimeCall,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005743
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005744 kLast = kRuntimeCall
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005745 };
5746
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005747 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005748 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005749 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005750 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005751 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005752 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00005753 bool needs_access_check)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005754 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5755 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005756 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005757 dex_file_(dex_file),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005758 klass_(klass),
Calin Juravle2e768302015-07-28 14:41:11 +00005759 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005760 // Referrers class should not need access check. We never inline unverified
5761 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005762 DCHECK(!is_referrers_class || !needs_access_check);
5763
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005764 SetPackedField<LoadKindField>(
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005765 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kRuntimeCall);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005766 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00005767 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005768 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005769 }
5770
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005771 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005772
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005773 LoadKind GetLoadKind() const {
5774 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005775 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005776
5777 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005778
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005779 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005780
Andreas Gampea5b09a62016-11-17 15:21:22 -08005781 size_t ComputeHashCode() const OVERRIDE { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005782
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005783 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005784
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005785 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005786 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005787 }
5788
Calin Juravle0ba218d2015-05-19 18:46:01 +01005789 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005790 // The entrypoint the code generator is going to call does not do
5791 // clinit of the class.
5792 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005793 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005794 }
5795
5796 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00005797 return NeedsAccessCheck() ||
5798 MustGenerateClinitCheck() ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005799 GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005800 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01005801 }
5802
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005803 bool CanThrow() const OVERRIDE {
Vladimir Marko41559982017-01-06 14:04:23 +00005804 return NeedsAccessCheck() ||
5805 MustGenerateClinitCheck() ||
5806 // If the class is in the boot image, the lookup in the runtime call cannot throw.
5807 // This keeps CanThrow() consistent between non-PIC (using kBootImageAddress) and
5808 // PIC and subsequently avoids a DCE behavior dependency on the PIC option.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005809 ((GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005810 GetLoadKind() == LoadKind::kBssEntry) &&
5811 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005812 }
5813
Calin Juravleacf735c2015-02-12 15:25:22 +00005814 ReferenceTypeInfo GetLoadedClassRTI() {
5815 return loaded_class_rti_;
5816 }
5817
5818 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5819 // Make sure we only set exact types (the loaded class should never be merged).
5820 DCHECK(rti.IsExact());
5821 loaded_class_rti_ = rti;
5822 }
5823
Andreas Gampea5b09a62016-11-17 15:21:22 -08005824 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005825 const DexFile& GetDexFile() const { return dex_file_; }
5826
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005827 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005828 return GetLoadKind() == LoadKind::kRuntimeCall;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005829 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005830
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005831 static SideEffects SideEffectsForArchRuntimeCalls() {
5832 return SideEffects::CanTriggerGC();
5833 }
5834
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005835 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005836 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005837 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005838 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005839
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005840 void MarkInBootImage() {
5841 SetPackedFlag<kFlagIsInBootImage>(true);
5842 }
5843
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005844 void AddSpecialInput(HInstruction* special_input);
5845
5846 using HInstruction::GetInputRecords; // Keep the const version visible.
5847 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5848 return ArrayRef<HUserRecord<HInstruction*>>(
5849 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
5850 }
5851
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005852 DataType::Type GetType() const OVERRIDE {
5853 return DataType::Type::kReference;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005854 }
5855
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005856 Handle<mirror::Class> GetClass() const {
5857 return klass_;
5858 }
5859
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005860 DECLARE_INSTRUCTION(LoadClass);
5861
5862 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005863 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00005864 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005865 // Whether this instruction must generate the initialization check.
5866 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005867 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005868 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
5869 static constexpr size_t kFieldLoadKindSize =
5870 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5871 static constexpr size_t kNumberOfLoadClassPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005872 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005873 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
5874
5875 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005876 return load_kind == LoadKind::kReferrersClass ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005877 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005878 load_kind == LoadKind::kBootImageClassTable ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005879 load_kind == LoadKind::kBssEntry ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005880 load_kind == LoadKind::kRuntimeCall;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005881 }
5882
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005883 void SetLoadKindInternal(LoadKind load_kind);
5884
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005885 // The special input is the HCurrentMethod for kRuntimeCall or kReferrersClass.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005886 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005887 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005888 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005889
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005890 // A type index and dex file where the class can be accessed. The dex file can be:
5891 // - The compiling method's dex file if the class is defined there too.
5892 // - The compiling method's dex file if the class is referenced there.
5893 // - The dex file where the class is defined. When the load kind can only be
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005894 // kBssEntry or kRuntimeCall, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08005895 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005896 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005897
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005898 Handle<mirror::Class> klass_;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005899
Calin Juravleacf735c2015-02-12 15:25:22 +00005900 ReferenceTypeInfo loaded_class_rti_;
5901
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005902 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5903};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005904std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
5905
5906// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005907inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005908 // The special input is used for PC-relative loads on some architectures,
5909 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005910 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005911 GetLoadKind() == LoadKind::kBootImageAddress ||
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005912 GetLoadKind() == LoadKind::kBootImageClassTable ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005913 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005914 DCHECK(special_input_.GetInstruction() == nullptr);
5915 special_input_ = HUserRecord<HInstruction*>(special_input);
5916 special_input->AddUseAt(this, 0);
5917}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005918
Vladimir Marko372f10e2016-05-17 16:30:10 +01005919class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005920 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005921 // Determines how to load the String.
5922 enum class LoadKind {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005923 // Use PC-relative boot image String* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01005924 // Used for boot image strings referenced by boot image code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005925 kBootImageLinkTimePcRelative,
5926
5927 // Use a known boot image String* address, embedded in the code by the codegen.
5928 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005929 kBootImageAddress,
5930
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005931 // Use a PC-relative load from a boot image InternTable mmapped into the .bss
5932 // of the oat file.
5933 kBootImageInternTable,
5934
Vladimir Markoaad75c62016-10-03 08:46:48 +00005935 // Load from an entry in the .bss section using a PC-relative load.
5936 // Used for strings outside boot image when .bss is accessible with a PC-relative load.
5937 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005938
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005939 // Load from the root table associated with the JIT compiled method.
5940 kJitTableAddress,
5941
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005942 // Load using a simple runtime call. This is the fall-back load kind when
5943 // the codegen is unable to use another appropriate kind.
5944 kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005945
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005946 kLast = kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005947 };
5948
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005949 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005950 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005951 const DexFile& dex_file,
5952 uint32_t dex_pc)
Vladimir Marko372f10e2016-05-17 16:30:10 +01005953 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5954 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005955 string_index_(string_index),
5956 dex_file_(dex_file) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005957 SetPackedField<LoadKindField>(LoadKind::kRuntimeCall);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005958 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005959
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005960 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005961
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005962 LoadKind GetLoadKind() const {
5963 return GetPackedField<LoadKindField>();
5964 }
5965
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005966 const DexFile& GetDexFile() const {
5967 return dex_file_;
5968 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005969
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005970 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005971 return string_index_;
5972 }
5973
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005974 Handle<mirror::String> GetString() const {
5975 return string_;
5976 }
5977
5978 void SetString(Handle<mirror::String> str) {
5979 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005980 }
5981
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005982 bool CanBeMoved() const OVERRIDE { return true; }
5983
Vladimir Marko372f10e2016-05-17 16:30:10 +01005984 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005985
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005986 size_t ComputeHashCode() const OVERRIDE { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005987
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005988 // Will call the runtime if we need to load the string through
5989 // the dex cache and the string is not guaranteed to be there yet.
5990 bool NeedsEnvironment() const OVERRIDE {
5991 LoadKind load_kind = GetLoadKind();
Vladimir Marko764d4542017-05-16 10:31:41 +01005992 if (load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005993 load_kind == LoadKind::kBootImageAddress ||
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005994 load_kind == LoadKind::kBootImageInternTable ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005995 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005996 return false;
5997 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00005998 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005999 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006000
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006001 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006002 return GetLoadKind() == LoadKind::kRuntimeCall;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006003 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006004
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07006005 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006006 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006007
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006008 static SideEffects SideEffectsForArchRuntimeCalls() {
6009 return SideEffects::CanTriggerGC();
6010 }
6011
Vladimir Marko372f10e2016-05-17 16:30:10 +01006012 void AddSpecialInput(HInstruction* special_input);
6013
6014 using HInstruction::GetInputRecords; // Keep the const version visible.
6015 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
6016 return ArrayRef<HUserRecord<HInstruction*>>(
6017 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006018 }
6019
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006020 DataType::Type GetType() const OVERRIDE {
6021 return DataType::Type::kReference;
Vladimir Marko372f10e2016-05-17 16:30:10 +01006022 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006023
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006024 DECLARE_INSTRUCTION(LoadString);
6025
6026 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006027 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006028 static constexpr size_t kFieldLoadKindSize =
6029 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
6030 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006031 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006032 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006033
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006034 void SetLoadKindInternal(LoadKind load_kind);
6035
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006036 // The special input is the HCurrentMethod for kRuntimeCall.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006037 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00006038 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01006039 HUserRecord<HInstruction*> special_input_;
6040
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006041 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006042 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006043
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006044 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006045
6046 DISALLOW_COPY_AND_ASSIGN(HLoadString);
6047};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006048std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
6049
6050// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006051inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006052 // The special input is used for PC-relative loads on some architectures,
6053 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006054 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006055 GetLoadKind() == LoadKind::kBootImageAddress ||
6056 GetLoadKind() == LoadKind::kBootImageInternTable ||
6057 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01006058 // HLoadString::GetInputRecords() returns an empty array at this point,
6059 // so use the GetInputRecords() from the base class to set the input record.
6060 DCHECK(special_input_.GetInstruction() == nullptr);
6061 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006062 special_input->AddUseAt(this, 0);
6063}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006064
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006065/**
6066 * Performs an initialization check on its Class object input.
6067 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01006068class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006069 public:
Roland Levillain3887c462015-08-12 18:15:42 +01006070 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07006071 : HExpression(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006072 DataType::Type::kReference,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006073 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
6074 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006075 SetRawInputAt(0, constant);
6076 }
6077
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006078 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006079 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006080 return true;
6081 }
6082
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006083 bool NeedsEnvironment() const OVERRIDE {
6084 // May call runtime to initialize the class.
6085 return true;
6086 }
6087
Nicolas Geoffray729645a2015-11-19 13:29:02 +00006088 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006089
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006090 HLoadClass* GetLoadClass() const {
6091 DCHECK(InputAt(0)->IsLoadClass());
6092 return InputAt(0)->AsLoadClass();
6093 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006094
6095 DECLARE_INSTRUCTION(ClinitCheck);
6096
6097 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006098 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
6099};
6100
Vladimir Markofcb503c2016-05-18 12:48:17 +01006101class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006102 public:
6103 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006104 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006105 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006106 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006107 bool is_volatile,
6108 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006109 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006110 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006111 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01006112 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006113 field_info_(field,
6114 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006115 field_type,
6116 is_volatile,
6117 field_idx,
6118 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006119 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006120 SetRawInputAt(0, cls);
6121 }
6122
Calin Juravle52c48962014-12-16 17:02:57 +00006123
Calin Juravle10c9cbe2014-12-19 10:50:19 +00006124 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006125
Vladimir Marko372f10e2016-05-17 16:30:10 +01006126 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
6127 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006128 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006129 }
6130
6131 size_t ComputeHashCode() const OVERRIDE {
6132 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6133 }
6134
Calin Juravle52c48962014-12-16 17:02:57 +00006135 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006136 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006137 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006138 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006139
6140 DECLARE_INSTRUCTION(StaticFieldGet);
6141
6142 private:
6143 const FieldInfo field_info_;
6144
6145 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
6146};
6147
Vladimir Markofcb503c2016-05-18 12:48:17 +01006148class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006149 public:
6150 HStaticFieldSet(HInstruction* cls,
6151 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006152 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006153 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006154 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006155 bool is_volatile,
6156 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006157 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006158 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006159 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01006160 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006161 field_info_(field,
6162 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006163 field_type,
6164 is_volatile,
6165 field_idx,
6166 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006167 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006168 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006169 SetRawInputAt(0, cls);
6170 SetRawInputAt(1, value);
6171 }
6172
Calin Juravle52c48962014-12-16 17:02:57 +00006173 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006174 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006175 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006176 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006177
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006178 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006179 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6180 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006181
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006182 DECLARE_INSTRUCTION(StaticFieldSet);
6183
6184 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006185 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6186 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6187 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6188 "Too many packed fields.");
6189
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006190 const FieldInfo field_info_;
6191
6192 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
6193};
6194
Vladimir Markofcb503c2016-05-18 12:48:17 +01006195class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006196 public:
6197 HUnresolvedInstanceFieldGet(HInstruction* obj,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006198 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006199 uint32_t field_index,
6200 uint32_t dex_pc)
6201 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6202 field_index_(field_index) {
6203 SetRawInputAt(0, obj);
6204 }
6205
6206 bool NeedsEnvironment() const OVERRIDE { return true; }
6207 bool CanThrow() const OVERRIDE { return true; }
6208
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006209 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006210 uint32_t GetFieldIndex() const { return field_index_; }
6211
6212 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6213
6214 private:
6215 const uint32_t field_index_;
6216
6217 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
6218};
6219
Vladimir Markofcb503c2016-05-18 12:48:17 +01006220class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006221 public:
6222 HUnresolvedInstanceFieldSet(HInstruction* obj,
6223 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006224 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006225 uint32_t field_index,
6226 uint32_t dex_pc)
6227 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006228 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006229 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006230 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006231 SetRawInputAt(0, obj);
6232 SetRawInputAt(1, value);
6233 }
6234
6235 bool NeedsEnvironment() const OVERRIDE { return true; }
6236 bool CanThrow() const OVERRIDE { return true; }
6237
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006238 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006239 uint32_t GetFieldIndex() const { return field_index_; }
6240
6241 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6242
6243 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006244 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6245 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006246 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006247 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6248 kFieldFieldType + kFieldFieldTypeSize;
6249 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6250 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006251 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006252
Calin Juravlee460d1d2015-09-29 04:52:17 +01006253 const uint32_t field_index_;
6254
6255 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
6256};
6257
Vladimir Markofcb503c2016-05-18 12:48:17 +01006258class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006259 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006260 HUnresolvedStaticFieldGet(DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006261 uint32_t field_index,
6262 uint32_t dex_pc)
6263 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6264 field_index_(field_index) {
6265 }
6266
6267 bool NeedsEnvironment() const OVERRIDE { return true; }
6268 bool CanThrow() const OVERRIDE { return true; }
6269
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006270 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006271 uint32_t GetFieldIndex() const { return field_index_; }
6272
6273 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6274
6275 private:
6276 const uint32_t field_index_;
6277
6278 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
6279};
6280
Vladimir Markofcb503c2016-05-18 12:48:17 +01006281class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006282 public:
6283 HUnresolvedStaticFieldSet(HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006284 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006285 uint32_t field_index,
6286 uint32_t dex_pc)
6287 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006288 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006289 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006290 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006291 SetRawInputAt(0, value);
6292 }
6293
6294 bool NeedsEnvironment() const OVERRIDE { return true; }
6295 bool CanThrow() const OVERRIDE { return true; }
6296
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006297 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006298 uint32_t GetFieldIndex() const { return field_index_; }
6299
6300 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6301
6302 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006303 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6304 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006305 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006306 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6307 kFieldFieldType + kFieldFieldTypeSize;
6308 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6309 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006310 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006311
Calin Juravlee460d1d2015-09-29 04:52:17 +01006312 const uint32_t field_index_;
6313
6314 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
6315};
6316
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006317// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006318class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006319 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006320 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006321 : HExpression(DataType::Type::kReference, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006322
David Brazdilbbd733e2015-08-18 17:48:17 +01006323 bool CanBeNull() const OVERRIDE { return false; }
6324
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006325 DECLARE_INSTRUCTION(LoadException);
6326
6327 private:
6328 DISALLOW_COPY_AND_ASSIGN(HLoadException);
6329};
6330
David Brazdilcb1c0552015-08-04 16:22:25 +01006331// Implicit part of move-exception which clears thread-local exception storage.
6332// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006333class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006334 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006335 explicit HClearException(uint32_t dex_pc = kNoDexPc)
6336 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01006337
6338 DECLARE_INSTRUCTION(ClearException);
6339
6340 private:
6341 DISALLOW_COPY_AND_ASSIGN(HClearException);
6342};
6343
Vladimir Markofcb503c2016-05-18 12:48:17 +01006344class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006345 public:
6346 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006347 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006348 SetRawInputAt(0, exception);
6349 }
6350
6351 bool IsControlFlow() const OVERRIDE { return true; }
6352
6353 bool NeedsEnvironment() const OVERRIDE { return true; }
6354
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006355 bool CanThrow() const OVERRIDE { return true; }
6356
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006357
6358 DECLARE_INSTRUCTION(Throw);
6359
6360 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006361 DISALLOW_COPY_AND_ASSIGN(HThrow);
6362};
6363
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006364/**
6365 * Implementation strategies for the code generator of a HInstanceOf
6366 * or `HCheckCast`.
6367 */
6368enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006369 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006370 kExactCheck, // Can do a single class compare.
6371 kClassHierarchyCheck, // Can just walk the super class chain.
6372 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6373 kInterfaceCheck, // No optimization yet when checking against an interface.
6374 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006375 kArrayCheck, // No optimization yet when checking against a generic array.
6376 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006377};
6378
Roland Levillain86503782016-02-11 19:07:30 +00006379std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6380
Vladimir Markofcb503c2016-05-18 12:48:17 +01006381class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006382 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006383 HInstanceOf(HInstruction* object,
6384 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006385 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006386 uint32_t dex_pc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006387 : HExpression(DataType::Type::kBool,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006388 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006389 dex_pc) {
6390 SetPackedField<TypeCheckKindField>(check_kind);
6391 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006392 SetRawInputAt(0, object);
6393 SetRawInputAt(1, constant);
6394 }
6395
6396 bool CanBeMoved() const OVERRIDE { return true; }
6397
Vladimir Marko372f10e2016-05-17 16:30:10 +01006398 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006399 return true;
6400 }
6401
6402 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006403 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006404 }
6405
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006406 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006407 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6408 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6409 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6410 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006411
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006412 static bool CanCallRuntime(TypeCheckKind check_kind) {
6413 // Mips currently does runtime calls for any other checks.
6414 return check_kind != TypeCheckKind::kExactCheck;
6415 }
6416
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006417 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006418 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006419 }
6420
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006421 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006422
6423 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006424 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6425 static constexpr size_t kFieldTypeCheckKindSize =
6426 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6427 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6428 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6429 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6430 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006431
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006432 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6433};
6434
Vladimir Markofcb503c2016-05-18 12:48:17 +01006435class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006436 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07006437 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006438 : HExpression(DataType::Type::kReference, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006439 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6440 SetPackedFlag<kFlagUpperCanBeNull>(true);
6441 SetPackedFlag<kFlagCanBeNull>(true);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006442 DCHECK_EQ(input->GetType(), DataType::Type::kReference);
Calin Juravleb1498f62015-02-16 13:13:29 +00006443 SetRawInputAt(0, input);
6444 }
6445
David Brazdilf5552582015-12-27 13:36:12 +00006446 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006447 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006448 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006449 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006450
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006451 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006452 DCHECK(GetUpperCanBeNull() || !can_be_null);
6453 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006454 }
6455
Vladimir Markoa1de9182016-02-25 11:37:38 +00006456 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006457
Calin Juravleb1498f62015-02-16 13:13:29 +00006458 DECLARE_INSTRUCTION(BoundType);
6459
6460 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006461 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6462 // is false then CanBeNull() cannot be true).
6463 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6464 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6465 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6466 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6467
Calin Juravleb1498f62015-02-16 13:13:29 +00006468 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006469 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6470 // It is used to bound the type in cases like:
6471 // if (x instanceof ClassX) {
6472 // // uper_bound_ will be ClassX
6473 // }
David Brazdilf5552582015-12-27 13:36:12 +00006474 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006475
6476 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6477};
6478
Vladimir Markofcb503c2016-05-18 12:48:17 +01006479class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006480 public:
6481 HCheckCast(HInstruction* object,
6482 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006483 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006484 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006485 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6486 SetPackedField<TypeCheckKindField>(check_kind);
6487 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006488 SetRawInputAt(0, object);
6489 SetRawInputAt(1, constant);
6490 }
6491
6492 bool CanBeMoved() const OVERRIDE { return true; }
6493
Vladimir Marko372f10e2016-05-17 16:30:10 +01006494 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006495 return true;
6496 }
6497
6498 bool NeedsEnvironment() const OVERRIDE {
6499 // Instruction may throw a CheckCastError.
6500 return true;
6501 }
6502
6503 bool CanThrow() const OVERRIDE { return true; }
6504
Vladimir Markoa1de9182016-02-25 11:37:38 +00006505 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6506 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6507 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6508 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006509
6510 DECLARE_INSTRUCTION(CheckCast);
6511
6512 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006513 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6514 static constexpr size_t kFieldTypeCheckKindSize =
6515 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6516 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6517 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6518 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6519 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006520
6521 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006522};
6523
Andreas Gampe26de38b2016-07-27 17:53:11 -07006524/**
6525 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
6526 * @details We define the combined barrier types that are actually required
6527 * by the Java Memory Model, rather than using exactly the terminology from
6528 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
6529 * primitives. Note that the JSR-133 cookbook generally does not deal with
6530 * store atomicity issues, and the recipes there are not always entirely sufficient.
6531 * The current recipe is as follows:
6532 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
6533 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
6534 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
6535 * -# Use StoreStore barrier after all stores but before return from any constructor whose
6536 * class has final fields.
6537 * -# Use NTStoreStore to order non-temporal stores with respect to all later
6538 * store-to-memory instructions. Only generated together with non-temporal stores.
6539 */
6540enum MemBarrierKind {
6541 kAnyStore,
6542 kLoadAny,
6543 kStoreStore,
6544 kAnyAny,
6545 kNTStoreStore,
6546 kLastBarrierKind = kNTStoreStore
6547};
6548std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
6549
Vladimir Markofcb503c2016-05-18 12:48:17 +01006550class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006551 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006552 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006553 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006554 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6555 SetPackedField<BarrierKindField>(barrier_kind);
6556 }
Calin Juravle27df7582015-04-17 19:12:31 +01006557
Vladimir Markoa1de9182016-02-25 11:37:38 +00006558 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006559
6560 DECLARE_INSTRUCTION(MemoryBarrier);
6561
6562 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006563 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6564 static constexpr size_t kFieldBarrierKindSize =
6565 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6566 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6567 kFieldBarrierKind + kFieldBarrierKindSize;
6568 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6569 "Too many packed fields.");
6570 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006571
6572 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6573};
6574
Igor Murashkind01745e2017-04-05 16:40:31 -07006575// A constructor fence orders all prior stores to fields that could be accessed via a final field of
6576// the specified object(s), with respect to any subsequent store that might "publish"
6577// (i.e. make visible) the specified object to another thread.
6578//
6579// JLS 17.5.1 "Semantics of final fields" states that a freeze action happens
6580// for all final fields (that were set) at the end of the invoked constructor.
6581//
6582// The constructor fence models the freeze actions for the final fields of an object
6583// being constructed (semantically at the end of the constructor). Constructor fences
6584// have a per-object affinity; two separate objects being constructed get two separate
6585// constructor fences.
6586//
6587// (Note: that if calling a super-constructor or forwarding to another constructor,
6588// the freezes would happen at the end of *that* constructor being invoked).
6589//
6590// The memory model guarantees that when the object being constructed is "published" after
6591// constructor completion (i.e. escapes the current thread via a store), then any final field
6592// writes must be observable on other threads (once they observe that publication).
6593//
6594// Further, anything written before the freeze, and read by dereferencing through the final field,
6595// must also be visible (so final object field could itself have an object with non-final fields;
6596// yet the freeze must also extend to them).
6597//
6598// Constructor example:
6599//
6600// class HasFinal {
6601// final int field; Optimizing IR for <init>()V:
6602// HasFinal() {
6603// field = 123; HInstanceFieldSet(this, HasFinal.field, 123)
6604// // freeze(this.field); HConstructorFence(this)
6605// } HReturn
6606// }
6607//
6608// HConstructorFence can serve double duty as a fence for new-instance/new-array allocations of
6609// already-initialized classes; in that case the allocation must act as a "default-initializer"
6610// of the object which effectively writes the class pointer "final field".
6611//
6612// For example, we can model default-initialiation as roughly the equivalent of the following:
6613//
6614// class Object {
6615// private final Class header;
6616// }
6617//
6618// Java code: Optimizing IR:
6619//
6620// T new_instance<T>() {
6621// Object obj = allocate_memory(T.class.size); obj = HInvoke(art_quick_alloc_object, T)
6622// obj.header = T.class; // header write is done by above call.
6623// // freeze(obj.header) HConstructorFence(obj)
6624// return (T)obj;
6625// }
6626//
6627// See also:
6628// * CompilerDriver::RequiresConstructorBarrier
6629// * QuasiAtomic::ThreadFenceForConstructor
6630//
6631class HConstructorFence FINAL : public HVariableInputSizeInstruction {
6632 // A fence has variable inputs because the inputs can be removed
6633 // after prepare_for_register_allocation phase.
6634 // (TODO: In the future a fence could freeze multiple objects
6635 // after merging two fences together.)
6636 public:
6637 // `fence_object` is the reference that needs to be protected for correct publication.
6638 //
6639 // It makes sense in the following situations:
6640 // * <init> constructors, it's the "this" parameter (i.e. HParameterValue, s.t. IsThis() == true).
6641 // * new-instance-like instructions, it's the return value (i.e. HNewInstance).
6642 //
6643 // After construction the `fence_object` becomes the 0th input.
6644 // This is not an input in a real sense, but just a convenient place to stash the information
6645 // about the associated object.
6646 HConstructorFence(HInstruction* fence_object,
6647 uint32_t dex_pc,
6648 ArenaAllocator* arena)
6649 // We strongly suspect there is not a more accurate way to describe the fine-grained reordering
6650 // constraints described in the class header. We claim that these SideEffects constraints
6651 // enforce a superset of the real constraints.
6652 //
6653 // The ordering described above is conservatively modeled with SideEffects as follows:
6654 //
6655 // * To prevent reordering of the publication stores:
6656 // ----> "Reads of objects" is the initial SideEffect.
6657 // * For every primitive final field store in the constructor:
6658 // ----> Union that field's type as a read (e.g. "Read of T") into the SideEffect.
6659 // * If there are any stores to reference final fields in the constructor:
6660 // ----> Use a more conservative "AllReads" SideEffect because any stores to any references
6661 // that are reachable from `fence_object` also need to be prevented for reordering
6662 // (and we do not want to do alias analysis to figure out what those stores are).
6663 //
6664 // In the implementation, this initially starts out as an "all reads" side effect; this is an
6665 // even more conservative approach than the one described above, and prevents all of the
6666 // above reordering without analyzing any of the instructions in the constructor.
6667 //
6668 // If in a later phase we discover that there are no writes to reference final fields,
6669 // we can refine the side effect to a smaller set of type reads (see above constraints).
6670 : HVariableInputSizeInstruction(SideEffects::AllReads(),
6671 dex_pc,
6672 arena,
6673 /* number_of_inputs */ 1,
6674 kArenaAllocConstructorFenceInputs) {
6675 DCHECK(fence_object != nullptr);
6676 SetRawInputAt(0, fence_object);
6677 }
6678
6679 // The object associated with this constructor fence.
6680 //
6681 // (Note: This will be null after the prepare_for_register_allocation phase,
6682 // as all constructor fence inputs are removed there).
6683 HInstruction* GetFenceObject() const {
6684 return InputAt(0);
6685 }
6686
6687 // Find all the HConstructorFence uses (`fence_use`) for `this` and:
6688 // - Delete `fence_use` from `this`'s use list.
6689 // - Delete `this` from `fence_use`'s inputs list.
6690 // - If the `fence_use` is dead, remove it from the graph.
6691 //
6692 // A fence is considered dead once it no longer has any uses
6693 // and all of the inputs are dead.
6694 //
6695 // This must *not* be called during/after prepare_for_register_allocation,
6696 // because that removes all the inputs to the fences but the fence is actually
6697 // still considered live.
Igor Murashkin6ef45672017-08-08 13:59:55 -07006698 //
6699 // Returns how many HConstructorFence instructions were removed from graph.
6700 static size_t RemoveConstructorFences(HInstruction* instruction);
Igor Murashkind01745e2017-04-05 16:40:31 -07006701
Igor Murashkindd018df2017-08-09 10:38:31 -07006702 // Combine all inputs of `this` and `other` instruction and remove
6703 // `other` from the graph.
6704 //
6705 // Inputs are unique after the merge.
6706 //
6707 // Requirement: `this` must not be the same as `other.
6708 void Merge(HConstructorFence* other);
6709
Igor Murashkin79d8fa72017-04-18 09:37:23 -07006710 // Check if this constructor fence is protecting
6711 // an HNewInstance or HNewArray that is also the immediate
6712 // predecessor of `this`.
6713 //
Igor Murashkindd018df2017-08-09 10:38:31 -07006714 // If `ignore_inputs` is true, then the immediate predecessor doesn't need
6715 // to be one of the inputs of `this`.
6716 //
Igor Murashkin79d8fa72017-04-18 09:37:23 -07006717 // Returns the associated HNewArray or HNewInstance,
6718 // or null otherwise.
Igor Murashkindd018df2017-08-09 10:38:31 -07006719 HInstruction* GetAssociatedAllocation(bool ignore_inputs = false);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07006720
Igor Murashkind01745e2017-04-05 16:40:31 -07006721 DECLARE_INSTRUCTION(ConstructorFence);
6722
6723 private:
6724 DISALLOW_COPY_AND_ASSIGN(HConstructorFence);
6725};
6726
Vladimir Markofcb503c2016-05-18 12:48:17 +01006727class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006728 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006729 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006730 kEnter,
6731 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006732 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006733 };
6734
6735 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006736 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006737 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6738 dex_pc) {
6739 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006740 SetRawInputAt(0, object);
6741 }
6742
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006743 // Instruction may go into runtime, so we need an environment.
6744 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006745
6746 bool CanThrow() const OVERRIDE {
6747 // Verifier guarantees that monitor-exit cannot throw.
6748 // This is important because it allows the HGraphBuilder to remove
6749 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6750 return IsEnter();
6751 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006752
Vladimir Markoa1de9182016-02-25 11:37:38 +00006753 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6754 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006755
6756 DECLARE_INSTRUCTION(MonitorOperation);
6757
Calin Juravle52c48962014-12-16 17:02:57 +00006758 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006759 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6760 static constexpr size_t kFieldOperationKindSize =
6761 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6762 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6763 kFieldOperationKind + kFieldOperationKindSize;
6764 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6765 "Too many packed fields.");
6766 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006767
6768 private:
6769 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6770};
6771
Vladimir Markofcb503c2016-05-18 12:48:17 +01006772class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00006773 public:
6774 HSelect(HInstruction* condition,
6775 HInstruction* true_value,
6776 HInstruction* false_value,
6777 uint32_t dex_pc)
6778 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6779 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6780
6781 // First input must be `true_value` or `false_value` to allow codegens to
6782 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6783 // that architectures which implement HSelect as a conditional move also
6784 // will not need to invert the condition.
6785 SetRawInputAt(0, false_value);
6786 SetRawInputAt(1, true_value);
6787 SetRawInputAt(2, condition);
6788 }
6789
6790 HInstruction* GetFalseValue() const { return InputAt(0); }
6791 HInstruction* GetTrueValue() const { return InputAt(1); }
6792 HInstruction* GetCondition() const { return InputAt(2); }
6793
6794 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006795 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6796 return true;
6797 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006798
6799 bool CanBeNull() const OVERRIDE {
6800 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6801 }
6802
6803 DECLARE_INSTRUCTION(Select);
6804
6805 private:
6806 DISALLOW_COPY_AND_ASSIGN(HSelect);
6807};
6808
Vladimir Markof9f64412015-09-02 14:05:49 +01006809class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006810 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006811 MoveOperands(Location source,
6812 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006813 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01006814 HInstruction* instruction)
6815 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006816
6817 Location GetSource() const { return source_; }
6818 Location GetDestination() const { return destination_; }
6819
6820 void SetSource(Location value) { source_ = value; }
6821 void SetDestination(Location value) { destination_ = value; }
6822
6823 // The parallel move resolver marks moves as "in-progress" by clearing the
6824 // destination (but not the source).
6825 Location MarkPending() {
6826 DCHECK(!IsPending());
6827 Location dest = destination_;
6828 destination_ = Location::NoLocation();
6829 return dest;
6830 }
6831
6832 void ClearPending(Location dest) {
6833 DCHECK(IsPending());
6834 destination_ = dest;
6835 }
6836
6837 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006838 DCHECK(source_.IsValid() || destination_.IsInvalid());
6839 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006840 }
6841
6842 // True if this blocks a move from the given location.
6843 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006844 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006845 }
6846
6847 // A move is redundant if it's been eliminated, if its source and
6848 // destination are the same, or if its destination is unneeded.
6849 bool IsRedundant() const {
6850 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6851 }
6852
6853 // We clear both operands to indicate move that's been eliminated.
6854 void Eliminate() {
6855 source_ = destination_ = Location::NoLocation();
6856 }
6857
6858 bool IsEliminated() const {
6859 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6860 return source_.IsInvalid();
6861 }
6862
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006863 DataType::Type GetType() const { return type_; }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006864
Nicolas Geoffray90218252015-04-15 11:56:51 +01006865 bool Is64BitMove() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006866 return DataType::Is64BitType(type_);
Nicolas Geoffray90218252015-04-15 11:56:51 +01006867 }
6868
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006869 HInstruction* GetInstruction() const { return instruction_; }
6870
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006871 private:
6872 Location source_;
6873 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006874 // The type this move is for.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006875 DataType::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006876 // The instruction this move is assocatied with. Null when this move is
6877 // for moving an input in the expected locations of user (including a phi user).
6878 // This is only used in debug mode, to ensure we do not connect interval siblings
6879 // in the same parallel move.
6880 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006881};
6882
Roland Levillainc9285912015-12-18 10:38:42 +00006883std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6884
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006885static constexpr size_t kDefaultNumberOfMoves = 4;
6886
Vladimir Markofcb503c2016-05-18 12:48:17 +01006887class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006888 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006889 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006890 : HTemplateInstruction(SideEffects::None(), dex_pc),
6891 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6892 moves_.reserve(kDefaultNumberOfMoves);
6893 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006894
Nicolas Geoffray90218252015-04-15 11:56:51 +01006895 void AddMove(Location source,
6896 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006897 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01006898 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006899 DCHECK(source.IsValid());
6900 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006901 if (kIsDebugBuild) {
6902 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006903 for (const MoveOperands& move : moves_) {
6904 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006905 // Special case the situation where the move is for the spill slot
6906 // of the instruction.
6907 if ((GetPrevious() == instruction)
6908 || ((GetPrevious() == nullptr)
6909 && instruction->IsPhi()
6910 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006911 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006912 << "Doing parallel moves for the same instruction.";
6913 } else {
6914 DCHECK(false) << "Doing parallel moves for the same instruction.";
6915 }
6916 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006917 }
6918 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006919 for (const MoveOperands& move : moves_) {
6920 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006921 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006922 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006923 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006924 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006925 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006926 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006927 }
6928
Vladimir Marko225b6462015-09-28 12:17:40 +01006929 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006930 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006931 }
6932
Vladimir Marko225b6462015-09-28 12:17:40 +01006933 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006934
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006935 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006936
6937 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006938 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006939
6940 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6941};
6942
Mark Mendell0616ae02015-04-17 12:49:27 -04006943} // namespace art
6944
Aart Bikf8f5a162017-02-06 15:35:29 -08006945#include "nodes_vector.h"
6946
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006947#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6948#include "nodes_shared.h"
6949#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006950#ifdef ART_ENABLE_CODEGEN_mips
6951#include "nodes_mips.h"
6952#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006953#ifdef ART_ENABLE_CODEGEN_x86
6954#include "nodes_x86.h"
6955#endif
6956
6957namespace art {
6958
Igor Murashkin6ef45672017-08-08 13:59:55 -07006959class OptimizingCompilerStats;
6960
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006961class HGraphVisitor : public ValueObject {
6962 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07006963 explicit HGraphVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
6964 : stats_(stats),
6965 graph_(graph) {}
Dave Allison20dfc792014-06-16 20:44:29 -07006966 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006967
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006968 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006969 virtual void VisitBasicBlock(HBasicBlock* block);
6970
Roland Levillain633021e2014-10-01 14:12:25 +01006971 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006972 void VisitInsertionOrder();
6973
Roland Levillain633021e2014-10-01 14:12:25 +01006974 // Visit the graph following dominator tree reverse post-order.
6975 void VisitReversePostOrder();
6976
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006977 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006978
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006979 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006980#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006981 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6982
6983 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6984
6985#undef DECLARE_VISIT_INSTRUCTION
6986
Igor Murashkin6ef45672017-08-08 13:59:55 -07006987 protected:
6988 OptimizingCompilerStats* stats_;
6989
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006990 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006991 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006992
6993 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6994};
6995
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006996class HGraphDelegateVisitor : public HGraphVisitor {
6997 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07006998 explicit HGraphDelegateVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
6999 : HGraphVisitor(graph, stats) {}
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007000 virtual ~HGraphDelegateVisitor() {}
7001
7002 // Visit functions that delegate to to super class.
7003#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00007004 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007005
7006 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7007
7008#undef DECLARE_VISIT_INSTRUCTION
7009
7010 private:
7011 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
7012};
7013
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007014// Iterator over the blocks that art part of the loop. Includes blocks part
7015// of an inner loop. The order in which the blocks are iterated is on their
7016// block id.
7017class HBlocksInLoopIterator : public ValueObject {
7018 public:
7019 explicit HBlocksInLoopIterator(const HLoopInformation& info)
7020 : blocks_in_loop_(info.GetBlocks()),
7021 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
7022 index_(0) {
7023 if (!blocks_in_loop_.IsBitSet(index_)) {
7024 Advance();
7025 }
7026 }
7027
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007028 bool Done() const { return index_ == blocks_.size(); }
7029 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007030 void Advance() {
7031 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007032 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007033 if (blocks_in_loop_.IsBitSet(index_)) {
7034 break;
7035 }
7036 }
7037 }
7038
7039 private:
7040 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007041 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007042 size_t index_;
7043
7044 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
7045};
7046
Mingyao Yang3584bce2015-05-19 16:01:59 -07007047// Iterator over the blocks that art part of the loop. Includes blocks part
7048// of an inner loop. The order in which the blocks are iterated is reverse
7049// post order.
7050class HBlocksInLoopReversePostOrderIterator : public ValueObject {
7051 public:
7052 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
7053 : blocks_in_loop_(info.GetBlocks()),
7054 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
7055 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007056 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007057 Advance();
7058 }
7059 }
7060
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007061 bool Done() const { return index_ == blocks_.size(); }
7062 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07007063 void Advance() {
7064 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007065 for (size_t e = blocks_.size(); index_ < e; ++index_) {
7066 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007067 break;
7068 }
7069 }
7070 }
7071
7072 private:
7073 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007074 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07007075 size_t index_;
7076
7077 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
7078};
7079
Aart Bikf3e61ee2017-04-12 17:09:20 -07007080// Returns int64_t value of a properly typed constant.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007081inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00007082 if (constant->IsIntConstant()) {
7083 return constant->AsIntConstant()->GetValue();
7084 } else if (constant->IsLongConstant()) {
7085 return constant->AsLongConstant()->GetValue();
7086 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00007087 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00007088 return 0;
7089 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007090}
7091
Aart Bikf3e61ee2017-04-12 17:09:20 -07007092// Returns true iff instruction is an integral constant (and sets value on success).
7093inline bool IsInt64AndGet(HInstruction* instruction, /*out*/ int64_t* value) {
7094 if (instruction->IsIntConstant()) {
7095 *value = instruction->AsIntConstant()->GetValue();
7096 return true;
7097 } else if (instruction->IsLongConstant()) {
7098 *value = instruction->AsLongConstant()->GetValue();
7099 return true;
7100 } else if (instruction->IsNullConstant()) {
7101 *value = 0;
7102 return true;
7103 }
7104 return false;
7105}
7106
Aart Bik0148de42017-09-05 09:25:01 -07007107// Returns true iff instruction is the given integral constant.
7108inline bool IsInt64Value(HInstruction* instruction, int64_t value) {
7109 int64_t val = 0;
7110 return IsInt64AndGet(instruction, &val) && val == value;
7111}
7112
7113// Returns true iff instruction is a zero bit pattern.
7114inline bool IsZeroBitPattern(HInstruction* instruction) {
7115 return instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern();
7116}
7117
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007118#define INSTRUCTION_TYPE_CHECK(type, super) \
7119 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
7120 inline const H##type* HInstruction::As##type() const { \
7121 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
7122 } \
7123 inline H##type* HInstruction::As##type() { \
7124 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
7125 }
7126
7127 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
7128#undef INSTRUCTION_TYPE_CHECK
7129
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00007130// Create space in `blocks` for adding `number_of_new_blocks` entries
7131// starting at location `at`. Blocks after `at` are moved accordingly.
7132inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
7133 size_t number_of_new_blocks,
7134 size_t after) {
7135 DCHECK_LT(after, blocks->size());
7136 size_t old_size = blocks->size();
7137 size_t new_size = old_size + number_of_new_blocks;
7138 blocks->resize(new_size);
7139 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
7140}
7141
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00007142/*
7143 * Hunt "under the hood" of array lengths (leading to array references),
7144 * null checks (also leading to array references), and new arrays
7145 * (leading to the actual length). This makes it more likely related
7146 * instructions become actually comparable.
7147 */
7148inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
7149 while (instruction->IsArrayLength() ||
7150 instruction->IsNullCheck() ||
7151 instruction->IsNewArray()) {
7152 instruction = instruction->IsNewArray()
7153 ? instruction->AsNewArray()->GetLength()
7154 : instruction->InputAt(0);
7155 }
7156 return instruction;
7157}
7158
Artem Serov21c7e6f2017-07-27 16:04:42 +01007159void RemoveEnvironmentUses(HInstruction* instruction);
7160bool HasEnvironmentUsedByOthers(HInstruction* instruction);
7161void ResetEnvironmentInputRecords(HInstruction* instruction);
7162
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007163} // namespace art
7164
7165#endif // ART_COMPILER_OPTIMIZING_NODES_H_