blob: 6672901781408a10b7a7db61c1e5579c5315947f [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
Vladimir Marko60584552015-09-03 13:35:12 +000020#include <algorithm>
Vladimir Markof9f64412015-09-02 14:05:49 +010021#include <array>
Roland Levillain9867bc72015-08-05 10:21:34 +010022#include <type_traits>
23
Mathieu Chartiere5d80f82015-10-15 17:47:48 -070024#include "base/arena_bit_vector.h"
David Brazdil8d5b8b22015-03-24 10:51:52 +000025#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080026#include "base/arena_object.h"
David Brazdild9c90372016-09-14 16:53:55 +010027#include "base/array_ref.h"
Vladimir Marko2c45bc92016-10-25 16:54:12 +010028#include "base/iteration_range.h"
Vladimir Marko60584552015-09-03 13:35:12 +000029#include "base/stl_util.h"
David Brazdild9c90372016-09-14 16:53:55 +010030#include "base/transform_array_ref.h"
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010031#include "data_type.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070032#include "deoptimization_kind.h"
Vladimir Marko87f3fcb2016-04-28 15:52:11 +010033#include "dex_file.h"
Andreas Gampea5b09a62016-11-17 15:21:22 -080034#include "dex_file_types.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000035#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000036#include "handle.h"
37#include "handle_scope.h"
Nicolas Geoffray762869d2016-07-15 15:28:35 +010038#include "intrinsics_enum.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070039#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010040#include "locations.h"
Vladimir Marko58155012015-08-19 12:49:41 +000041#include "method_reference.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000042#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010043#include "offsets.h"
Vladimir Marko46817b82016-03-29 12:21:58 +010044#include "utils/intrusive_forward_list.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000045
46namespace art {
47
Vladimir Markoca6fff82017-10-03 14:49:14 +010048class ArenaStack;
David Brazdil1abb4192015-02-17 18:33:36 +000049class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000050class HBasicBlock;
Igor Murashkind01745e2017-04-05 16:40:31 -070051class HConstructorFence;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010052class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000053class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010054class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000055class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000056class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000057class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000058class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000059class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000060class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000061class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000062class HNullConstant;
Igor Murashkind01745e2017-04-05 16:40:31 -070063class HParameterValue;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010064class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010065class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010066class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010067class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000068class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010069class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000070class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000071
Mathieu Chartier736b5602015-09-02 14:54:11 -070072namespace mirror {
73class DexCache;
74} // namespace mirror
75
Nicolas Geoffray818f2102014-02-18 16:43:35 +000076static const int kDefaultNumberOfBlocks = 8;
77static const int kDefaultNumberOfSuccessors = 2;
78static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010079static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010080static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000081static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000082
Roland Levillain5b5b9312016-03-22 14:57:31 +000083// The maximum (meaningful) distance (31) that can be used in an integer shift/rotate operation.
84static constexpr int32_t kMaxIntShiftDistance = 0x1f;
85// The maximum (meaningful) distance (63) that can be used in a long shift/rotate operation.
86static constexpr int32_t kMaxLongShiftDistance = 0x3f;
Calin Juravle9aec02f2014-11-18 23:06:35 +000087
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010088static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070089static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010090
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010091static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
92
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060093static constexpr uint32_t kNoDexPc = -1;
94
Vladimir Markodbb7f5b2016-03-30 13:23:58 +010095inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
96 // For the purposes of the compiler, the dex files must actually be the same object
97 // if we want to safely treat them as the same. This is especially important for JIT
98 // as custom class loaders can open the same underlying file (or memory) multiple
99 // times and provide different class resolution but no two class loaders should ever
100 // use the same DexFile object - doing so is an unsupported hack that can lead to
101 // all sorts of weird failures.
102 return &lhs == &rhs;
103}
104
Dave Allison20dfc792014-06-16 20:44:29 -0700105enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -0700106 // All types.
107 kCondEQ, // ==
108 kCondNE, // !=
109 // Signed integers and floating-point numbers.
110 kCondLT, // <
111 kCondLE, // <=
112 kCondGT, // >
113 kCondGE, // >=
114 // Unsigned integers.
115 kCondB, // <
116 kCondBE, // <=
117 kCondA, // >
118 kCondAE, // >=
Scott Wakeling2c76e062016-08-31 09:48:54 +0100119 // First and last aliases.
120 kCondFirst = kCondEQ,
121 kCondLast = kCondAE,
Dave Allison20dfc792014-06-16 20:44:29 -0700122};
123
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000124enum GraphAnalysisResult {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000125 kAnalysisSkipped,
David Brazdilbadd8262016-02-02 16:28:56 +0000126 kAnalysisInvalidBytecode,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000127 kAnalysisFailThrowCatchLoop,
128 kAnalysisFailAmbiguousArrayOp,
129 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000130};
131
Andreas Gampe9186ced2016-12-12 14:28:21 -0800132template <typename T>
133static inline typename std::make_unsigned<T>::type MakeUnsigned(T x) {
134 return static_cast<typename std::make_unsigned<T>::type>(x);
135}
136
Vladimir Markof9f64412015-09-02 14:05:49 +0100137class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100138 public:
139 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
140
141 void AddInstruction(HInstruction* instruction);
142 void RemoveInstruction(HInstruction* instruction);
143
David Brazdilc3d743f2015-04-22 13:40:50 +0100144 // Insert `instruction` before/after an existing instruction `cursor`.
145 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
146 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
147
Roland Levillain6b469232014-09-25 10:10:38 +0100148 // Return true if this list contains `instruction`.
149 bool Contains(HInstruction* instruction) const;
150
Roland Levillainccc07a92014-09-16 14:48:16 +0100151 // Return true if `instruction1` is found before `instruction2` in
152 // this instruction list and false otherwise. Abort if none
153 // of these instructions is found.
154 bool FoundBefore(const HInstruction* instruction1,
155 const HInstruction* instruction2) const;
156
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000157 bool IsEmpty() const { return first_instruction_ == nullptr; }
158 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
159
160 // Update the block of all instructions to be `block`.
161 void SetBlockOfInstructions(HBasicBlock* block) const;
162
163 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000164 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000165 void Add(const HInstructionList& instruction_list);
166
David Brazdil2d7352b2015-04-20 14:52:42 +0100167 // Return the number of instructions in the list. This is an expensive operation.
168 size_t CountSize() const;
169
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100170 private:
171 HInstruction* first_instruction_;
172 HInstruction* last_instruction_;
173
174 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000175 friend class HGraph;
176 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100177 friend class HInstructionIterator;
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +0000178 friend class HInstructionIteratorHandleChanges;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100179 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100180
181 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
182};
183
David Brazdil4833f5a2015-12-16 10:37:39 +0000184class ReferenceTypeInfo : ValueObject {
185 public:
186 typedef Handle<mirror::Class> TypeHandle;
187
Vladimir Markoa1de9182016-02-25 11:37:38 +0000188 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
189
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700190 static ReferenceTypeInfo Create(TypeHandle type_handle) REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil94ab38f2016-06-21 17:48:19 +0100191 return Create(type_handle, type_handle->CannotBeAssignedFromOtherTypes());
192 }
193
Vladimir Markoa1de9182016-02-25 11:37:38 +0000194 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000195 return ReferenceTypeInfo(type_handle, is_exact);
196 }
197
198 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
199
Vladimir Markof39745e2016-01-26 12:16:55 +0000200 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000201 return handle.GetReference() != nullptr;
202 }
203
Vladimir Marko456307a2016-04-19 14:12:13 +0000204 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000205 return IsValidHandle(type_handle_);
206 }
207
208 bool IsExact() const { return is_exact_; }
209
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700210 bool IsObjectClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000211 DCHECK(IsValid());
212 return GetTypeHandle()->IsObjectClass();
213 }
214
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700215 bool IsStringClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000216 DCHECK(IsValid());
217 return GetTypeHandle()->IsStringClass();
218 }
219
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700220 bool IsObjectArray() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000221 DCHECK(IsValid());
222 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
223 }
224
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700225 bool IsInterface() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000226 DCHECK(IsValid());
227 return GetTypeHandle()->IsInterface();
228 }
229
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700230 bool IsArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000231 DCHECK(IsValid());
232 return GetTypeHandle()->IsArrayClass();
233 }
234
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700235 bool IsPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000236 DCHECK(IsValid());
237 return GetTypeHandle()->IsPrimitiveArray();
238 }
239
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700240 bool IsNonPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000241 DCHECK(IsValid());
242 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
243 }
244
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700245 bool CanArrayHold(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000246 DCHECK(IsValid());
247 if (!IsExact()) return false;
248 if (!IsArrayClass()) return false;
249 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
250 }
251
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700252 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000253 DCHECK(IsValid());
254 if (!IsExact()) return false;
255 if (!IsArrayClass()) return false;
256 if (!rti.IsArrayClass()) return false;
257 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
258 rti.GetTypeHandle()->GetComponentType());
259 }
260
261 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
262
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700263 bool IsSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000264 DCHECK(IsValid());
265 DCHECK(rti.IsValid());
266 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
267 }
268
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700269 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000270 DCHECK(IsValid());
271 DCHECK(rti.IsValid());
272 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
273 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
274 }
275
276 // Returns true if the type information provide the same amount of details.
277 // Note that it does not mean that the instructions have the same actual type
278 // (because the type can be the result of a merge).
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700279 bool IsEqual(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000280 if (!IsValid() && !rti.IsValid()) {
281 // Invalid types are equal.
282 return true;
283 }
284 if (!IsValid() || !rti.IsValid()) {
285 // One is valid, the other not.
286 return false;
287 }
288 return IsExact() == rti.IsExact()
289 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
290 }
291
292 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000293 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
294 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
295 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000296
297 // The class of the object.
298 TypeHandle type_handle_;
299 // Whether or not the type is exact or a superclass of the actual type.
300 // Whether or not we have any information about this type.
301 bool is_exact_;
302};
303
304std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
305
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000306// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100307class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000308 public:
Vladimir Markoca6fff82017-10-03 14:49:14 +0100309 HGraph(ArenaAllocator* allocator,
310 ArenaStack* arena_stack,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100311 const DexFile& dex_file,
312 uint32_t method_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700313 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100314 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100315 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000316 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100317 int start_instruction_id = 0)
Vladimir Markoca6fff82017-10-03 14:49:14 +0100318 : allocator_(allocator),
319 arena_stack_(arena_stack),
320 blocks_(allocator->Adapter(kArenaAllocBlockList)),
321 reverse_post_order_(allocator->Adapter(kArenaAllocReversePostOrder)),
322 linear_order_(allocator->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700323 entry_block_(nullptr),
324 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100325 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100326 number_of_vregs_(0),
327 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000328 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400329 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000330 has_try_catch_(false),
Aart Bikb13c65b2017-03-21 20:14:07 -0700331 has_simd_(false),
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800332 has_loops_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000333 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000334 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000335 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100336 dex_file_(dex_file),
337 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100338 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100339 in_ssa_form_(false),
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800340 number_of_cha_guards_(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700341 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000342 cached_null_constant_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100343 cached_int_constants_(std::less<int32_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
344 cached_float_constants_(std::less<int32_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
345 cached_long_constants_(std::less<int64_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
346 cached_double_constants_(std::less<int64_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000347 cached_current_method_(nullptr),
Nicolas Geoffray53fec082017-03-27 12:56:16 +0100348 art_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000349 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
Mingyao Yang063fc772016-08-02 11:02:54 -0700350 osr_(osr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100351 cha_single_implementation_list_(allocator->Adapter(kArenaAllocCHA)) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100352 blocks_.reserve(kDefaultNumberOfBlocks);
353 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000354
David Brazdilbadd8262016-02-02 16:28:56 +0000355 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700356 void InitializeInexactObjectRTI(VariableSizedHandleScope* handles);
David Brazdilbadd8262016-02-02 16:28:56 +0000357
Vladimir Markoca6fff82017-10-03 14:49:14 +0100358 ArenaAllocator* GetAllocator() const { return allocator_; }
359 ArenaStack* GetArenaStack() const { return arena_stack_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100360 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
361
David Brazdil69ba7b72015-06-23 18:27:30 +0100362 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000363 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100364
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000365 HBasicBlock* GetEntryBlock() const { return entry_block_; }
366 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100367 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000368
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000369 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
370 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000371
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000372 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100373
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100374 void ComputeDominanceInformation();
375 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000376 void ClearLoopInformation();
377 void FindBackEdges(ArenaBitVector* visited);
378 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100379 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100380 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000381
David Brazdil4833f5a2015-12-16 10:37:39 +0000382 // Analyze all natural loops in this graph. Returns a code specifying that it
383 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000384 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000385 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100386
David Brazdilffee3d32015-07-06 11:48:53 +0100387 // Iterate over blocks to compute try block membership. Needs reverse post
388 // order and loop information.
389 void ComputeTryBlockInformation();
390
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000391 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000392 // Returns the instruction to replace the invoke expression or null if the
393 // invoke is for a void method. Note that the caller is responsible for replacing
394 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000395 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000396
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000397 // Update the loop and try membership of `block`, which was spawned from `reference`.
398 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
399 // should be the new back edge.
400 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
401 HBasicBlock* reference,
402 bool replace_if_back_edge);
403
Mingyao Yang3584bce2015-05-19 16:01:59 -0700404 // Need to add a couple of blocks to test if the loop body is entered and
405 // put deoptimization instructions, etc.
406 void TransformLoopHeaderForBCE(HBasicBlock* header);
407
Aart Bikf8f5a162017-02-06 15:35:29 -0800408 // Adds a new loop directly after the loop with the given header and exit.
409 // Returns the new preheader.
410 HBasicBlock* TransformLoopForVectorization(HBasicBlock* header,
411 HBasicBlock* body,
412 HBasicBlock* exit);
413
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000414 // Removes `block` from the graph. Assumes `block` has been disconnected from
415 // other blocks and has no instructions or phis.
416 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000417
David Brazdilfc6a86a2015-06-26 10:33:45 +0000418 // Splits the edge between `block` and `successor` while preserving the
419 // indices in the predecessor/successor lists. If there are multiple edges
420 // between the blocks, the lowest indices are used.
421 // Returns the new block which is empty and has the same dex pc as `successor`.
422 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
423
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100424 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
Artem Serovc73ee372017-07-31 15:08:40 +0100425 void OrderLoopHeaderPredecessors(HBasicBlock* header);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100426 void SimplifyLoop(HBasicBlock* header);
427
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000428 int32_t GetNextInstructionId() {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100429 CHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000430 return current_instruction_id_++;
431 }
432
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000433 int32_t GetCurrentInstructionId() const {
434 return current_instruction_id_;
435 }
436
437 void SetCurrentInstructionId(int32_t id) {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100438 CHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000439 current_instruction_id_ = id;
440 }
441
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100442 uint16_t GetMaximumNumberOfOutVRegs() const {
443 return maximum_number_of_out_vregs_;
444 }
445
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000446 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
447 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100448 }
449
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100450 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
451 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
452 }
453
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000454 void UpdateTemporariesVRegSlots(size_t slots) {
455 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100456 }
457
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000458 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100459 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000460 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100461 }
462
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100463 void SetNumberOfVRegs(uint16_t number_of_vregs) {
464 number_of_vregs_ = number_of_vregs;
465 }
466
467 uint16_t GetNumberOfVRegs() const {
468 return number_of_vregs_;
469 }
470
471 void SetNumberOfInVRegs(uint16_t value) {
472 number_of_in_vregs_ = value;
473 }
474
David Brazdildee58d62016-04-07 09:54:26 +0000475 uint16_t GetNumberOfInVRegs() const {
476 return number_of_in_vregs_;
477 }
478
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100479 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100480 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100481 return number_of_vregs_ - number_of_in_vregs_;
482 }
483
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100484 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100485 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100486 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100487
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100488 ArrayRef<HBasicBlock* const> GetReversePostOrderSkipEntryBlock() {
489 DCHECK(GetReversePostOrder()[0] == entry_block_);
490 return ArrayRef<HBasicBlock* const>(GetReversePostOrder()).SubArray(1);
491 }
492
493 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetPostOrder() const {
494 return ReverseRange(GetReversePostOrder());
495 }
496
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100497 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100498 return linear_order_;
499 }
500
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100501 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetLinearPostOrder() const {
502 return ReverseRange(GetLinearOrder());
503 }
504
Mark Mendell1152c922015-04-24 17:06:35 -0400505 bool HasBoundsChecks() const {
506 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800507 }
508
Mark Mendell1152c922015-04-24 17:06:35 -0400509 void SetHasBoundsChecks(bool value) {
510 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800511 }
512
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000513 bool IsDebuggable() const { return debuggable_; }
514
David Brazdil8d5b8b22015-03-24 10:51:52 +0000515 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000516 // already, it is created and inserted into the graph. This method is only for
517 // integral types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100518 HConstant* GetConstant(DataType::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000519
520 // TODO: This is problematic for the consistency of reference type propagation
521 // because it can be created anytime after the pass and thus it will be left
522 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600523 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000524
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600525 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
526 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000527 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600528 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
529 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000530 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600531 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
532 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000533 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600534 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
535 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000536 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000537
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100538 HCurrentMethod* GetCurrentMethod();
539
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100540 const DexFile& GetDexFile() const {
541 return dex_file_;
542 }
543
544 uint32_t GetMethodIdx() const {
545 return method_idx_;
546 }
547
Igor Murashkind01745e2017-04-05 16:40:31 -0700548 // Get the method name (without the signature), e.g. "<init>"
549 const char* GetMethodName() const;
550
551 // Get the pretty method name (class + name + optionally signature).
552 std::string PrettyMethod(bool with_signature = true) const;
553
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100554 InvokeType GetInvokeType() const {
555 return invoke_type_;
556 }
557
Mark Mendellc4701932015-04-10 13:18:51 -0400558 InstructionSet GetInstructionSet() const {
559 return instruction_set_;
560 }
561
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000562 bool IsCompilingOsr() const { return osr_; }
563
Mingyao Yang063fc772016-08-02 11:02:54 -0700564 ArenaSet<ArtMethod*>& GetCHASingleImplementationList() {
565 return cha_single_implementation_list_;
566 }
567
568 void AddCHASingleImplementationDependency(ArtMethod* method) {
569 cha_single_implementation_list_.insert(method);
570 }
571
572 bool HasShouldDeoptimizeFlag() const {
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800573 return number_of_cha_guards_ != 0;
Mingyao Yang063fc772016-08-02 11:02:54 -0700574 }
575
David Brazdil77a48ae2015-09-15 12:34:04 +0000576 bool HasTryCatch() const { return has_try_catch_; }
577 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100578
Aart Bikb13c65b2017-03-21 20:14:07 -0700579 bool HasSIMD() const { return has_simd_; }
580 void SetHasSIMD(bool value) { has_simd_ = value; }
581
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800582 bool HasLoops() const { return has_loops_; }
583 void SetHasLoops(bool value) { has_loops_ = value; }
584
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000585 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
586 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
587
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100588 ArtMethod* GetArtMethod() const { return art_method_; }
589 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
590
Roland Levillain5e8d5f02016-10-18 18:03:43 +0100591 // Returns an instruction with the opposite Boolean value from 'cond'.
Mark Mendellf6529172015-11-17 11:16:56 -0500592 // The instruction has been inserted into the graph, either as a constant, or
593 // before cursor.
594 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
595
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000596 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
597
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800598 uint32_t GetNumberOfCHAGuards() { return number_of_cha_guards_; }
599 void SetNumberOfCHAGuards(uint32_t num) { number_of_cha_guards_ = num; }
600 void IncrementNumberOfCHAGuards() { number_of_cha_guards_++; }
601
David Brazdil2d7352b2015-04-20 14:52:42 +0100602 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000603 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100604 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000605
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000606 template <class InstructionType, typename ValueType>
607 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600608 ArenaSafeMap<ValueType, InstructionType*>* cache,
609 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000610 // Try to find an existing constant of the given value.
611 InstructionType* constant = nullptr;
612 auto cached_constant = cache->find(value);
613 if (cached_constant != cache->end()) {
614 constant = cached_constant->second;
615 }
616
617 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100618 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000619 if (constant == nullptr || constant->GetBlock() == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100620 constant = new (allocator_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000621 cache->Overwrite(value, constant);
622 InsertConstant(constant);
623 }
624 return constant;
625 }
626
David Brazdil8d5b8b22015-03-24 10:51:52 +0000627 void InsertConstant(HConstant* instruction);
628
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000629 // Cache a float constant into the graph. This method should only be
630 // called by the SsaBuilder when creating "equivalent" instructions.
631 void CacheFloatConstant(HFloatConstant* constant);
632
633 // See CacheFloatConstant comment.
634 void CacheDoubleConstant(HDoubleConstant* constant);
635
Vladimir Markoca6fff82017-10-03 14:49:14 +0100636 ArenaAllocator* const allocator_;
637 ArenaStack* const arena_stack_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000638
639 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100640 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000641
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100642 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100643 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000644
Aart Bik281c6812016-08-26 11:31:48 -0700645 // List of blocks to perform a linear order tree traversal. Unlike the reverse
646 // post order, this order is not incrementally kept up-to-date.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100647 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100648
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000649 HBasicBlock* entry_block_;
650 HBasicBlock* exit_block_;
651
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100652 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100653 uint16_t maximum_number_of_out_vregs_;
654
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100655 // The number of virtual registers in this method. Contains the parameters.
656 uint16_t number_of_vregs_;
657
658 // The number of virtual registers used by parameters of this method.
659 uint16_t number_of_in_vregs_;
660
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000661 // Number of vreg size slots that the temporaries use (used in baseline compiler).
662 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100663
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800664 // Flag whether there are bounds checks in the graph. We can skip
665 // BCE if it's false. It's only best effort to keep it up to date in
666 // the presence of code elimination so there might be false positives.
Mark Mendell1152c922015-04-24 17:06:35 -0400667 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800668
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800669 // Flag whether there are try/catch blocks in the graph. We will skip
670 // try/catch-related passes if it's false. It's only best effort to keep
671 // it up to date in the presence of code elimination so there might be
672 // false positives.
David Brazdil77a48ae2015-09-15 12:34:04 +0000673 bool has_try_catch_;
674
Aart Bikb13c65b2017-03-21 20:14:07 -0700675 // Flag whether SIMD instructions appear in the graph. If true, the
676 // code generators may have to be more careful spilling the wider
677 // contents of SIMD registers.
678 bool has_simd_;
679
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800680 // Flag whether there are any loops in the graph. We can skip loop
681 // optimization if it's false. It's only best effort to keep it up
682 // to date in the presence of code elimination so there might be false
683 // positives.
684 bool has_loops_;
685
686 // Flag whether there are any irreducible loops in the graph. It's only
687 // best effort to keep it up to date in the presence of code elimination
688 // so there might be false positives.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000689 bool has_irreducible_loops_;
690
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000691 // Indicates whether the graph should be compiled in a way that
692 // ensures full debuggability. If false, we can apply more
693 // aggressive optimizations that may limit the level of debugging.
694 const bool debuggable_;
695
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000696 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000697 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000698
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100699 // The dex file from which the method is from.
700 const DexFile& dex_file_;
701
702 // The method index in the dex file.
703 const uint32_t method_idx_;
704
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100705 // If inlined, this encodes how the callee is being invoked.
706 const InvokeType invoke_type_;
707
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100708 // Whether the graph has been transformed to SSA form. Only used
709 // in debug mode to ensure we are not using properties only valid
710 // for non-SSA form (like the number of temporaries).
711 bool in_ssa_form_;
712
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800713 // Number of CHA guards in the graph. Used to short-circuit the
714 // CHA guard optimization pass when there is no CHA guard left.
715 uint32_t number_of_cha_guards_;
716
Mathieu Chartiere401d142015-04-22 13:56:20 -0700717 const InstructionSet instruction_set_;
718
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000719 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000720 HNullConstant* cached_null_constant_;
721 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000722 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000723 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000724 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000725
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100726 HCurrentMethod* cached_current_method_;
727
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100728 // The ArtMethod this graph is for. Note that for AOT, it may be null,
729 // for example for methods whose declaring class could not be resolved
730 // (such as when the superclass could not be found).
731 ArtMethod* art_method_;
732
David Brazdil4833f5a2015-12-16 10:37:39 +0000733 // Keep the RTI of inexact Object to avoid having to pass stack handle
734 // collection pointer to passes which may create NullConstant.
735 ReferenceTypeInfo inexact_object_rti_;
736
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000737 // Whether we are compiling this graph for on stack replacement: this will
738 // make all loops seen as irreducible and emit special stack maps to mark
739 // compiled code entries which the interpreter can directly jump to.
740 const bool osr_;
741
Mingyao Yang063fc772016-08-02 11:02:54 -0700742 // List of methods that are assumed to have single implementation.
743 ArenaSet<ArtMethod*> cha_single_implementation_list_;
744
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000745 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100746 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000747 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000748 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000749 DISALLOW_COPY_AND_ASSIGN(HGraph);
750};
751
Vladimir Markof9f64412015-09-02 14:05:49 +0100752class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000753 public:
754 HLoopInformation(HBasicBlock* header, HGraph* graph)
755 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100756 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000757 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100758 contains_irreducible_loop_(false),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100759 back_edges_(graph->GetAllocator()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100760 // Make bit vector growable, as the number of blocks may change.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100761 blocks_(graph->GetAllocator(),
762 graph->GetBlocks().size(),
763 true,
764 kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100765 back_edges_.reserve(kDefaultNumberOfBackEdges);
766 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100767
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000768 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100769 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000770
771 void Dump(std::ostream& os);
772
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100773 HBasicBlock* GetHeader() const {
774 return header_;
775 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000776
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000777 void SetHeader(HBasicBlock* block) {
778 header_ = block;
779 }
780
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100781 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
782 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
783 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
784
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000785 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100786 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000787 }
788
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100789 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100790 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100791 }
792
David Brazdil46e2a392015-03-16 17:31:52 +0000793 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100794 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100795 }
796
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000797 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100798 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000799 }
800
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100801 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100802
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100803 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100804 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100805 }
806
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100807 // Returns the lifetime position of the back edge that has the
808 // greatest lifetime position.
809 size_t GetLifetimeEnd() const;
810
811 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100812 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100813 }
814
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000815 // Finds blocks that are part of this loop.
816 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100817
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100818 // Returns whether this loop information contains `block`.
819 // Note that this loop information *must* be populated before entering this function.
820 bool Contains(const HBasicBlock& block) const;
821
822 // Returns whether this loop information is an inner loop of `other`.
823 // Note that `other` *must* be populated before entering this function.
824 bool IsIn(const HLoopInformation& other) const;
825
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800826 // Returns true if instruction is not defined within this loop.
827 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700828
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100829 const ArenaBitVector& GetBlocks() const { return blocks_; }
830
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000831 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000832 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000833
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000834 void ClearAllBlocks() {
835 blocks_.ClearAllBits();
836 }
837
David Brazdil3f4a5222016-05-06 12:46:21 +0100838 bool HasBackEdgeNotDominatedByHeader() const;
839
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100840 bool IsPopulated() const {
841 return blocks_.GetHighestBitSet() != -1;
842 }
843
Anton Shaminf89381f2016-05-16 16:44:13 +0600844 bool DominatesAllBackEdges(HBasicBlock* block);
845
David Sehrc757dec2016-11-04 15:48:34 -0700846 bool HasExitEdge() const;
847
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000848 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100849 // Internal recursive implementation of `Populate`.
850 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100851 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100852
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000853 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100854 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000855 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100856 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100857 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100858 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000859
860 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
861};
862
David Brazdilec16f792015-08-19 15:04:01 +0100863// Stores try/catch information for basic blocks.
864// Note that HGraph is constructed so that catch blocks cannot simultaneously
865// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100866class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100867 public:
868 // Try block information constructor.
869 explicit TryCatchInformation(const HTryBoundary& try_entry)
870 : try_entry_(&try_entry),
871 catch_dex_file_(nullptr),
872 catch_type_index_(DexFile::kDexNoIndex16) {
873 DCHECK(try_entry_ != nullptr);
874 }
875
876 // Catch block information constructor.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800877 TryCatchInformation(dex::TypeIndex catch_type_index, const DexFile& dex_file)
David Brazdilec16f792015-08-19 15:04:01 +0100878 : try_entry_(nullptr),
879 catch_dex_file_(&dex_file),
880 catch_type_index_(catch_type_index) {}
881
882 bool IsTryBlock() const { return try_entry_ != nullptr; }
883
884 const HTryBoundary& GetTryEntry() const {
885 DCHECK(IsTryBlock());
886 return *try_entry_;
887 }
888
889 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
890
891 bool IsCatchAllTypeIndex() const {
892 DCHECK(IsCatchBlock());
Andreas Gampea5b09a62016-11-17 15:21:22 -0800893 return !catch_type_index_.IsValid();
David Brazdilec16f792015-08-19 15:04:01 +0100894 }
895
Andreas Gampea5b09a62016-11-17 15:21:22 -0800896 dex::TypeIndex GetCatchTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +0100897 DCHECK(IsCatchBlock());
898 return catch_type_index_;
899 }
900
901 const DexFile& GetCatchDexFile() const {
902 DCHECK(IsCatchBlock());
903 return *catch_dex_file_;
904 }
905
906 private:
907 // One of possibly several TryBoundary instructions entering the block's try.
908 // Only set for try blocks.
909 const HTryBoundary* try_entry_;
910
911 // Exception type information. Only set for catch blocks.
912 const DexFile* catch_dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800913 const dex::TypeIndex catch_type_index_;
David Brazdilec16f792015-08-19 15:04:01 +0100914};
915
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100916static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100917static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100918
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000919// A block in a method. Contains the list of instructions represented
920// as a double linked list. Each block knows its predecessors and
921// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100922
Vladimir Markof9f64412015-09-02 14:05:49 +0100923class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000924 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700925 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000926 : graph_(graph),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100927 predecessors_(graph->GetAllocator()->Adapter(kArenaAllocPredecessors)),
928 successors_(graph->GetAllocator()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000929 loop_information_(nullptr),
930 dominator_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100931 dominated_blocks_(graph->GetAllocator()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100932 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100933 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100934 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000935 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000936 try_catch_information_(nullptr) {
937 predecessors_.reserve(kDefaultNumberOfPredecessors);
938 successors_.reserve(kDefaultNumberOfSuccessors);
939 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
940 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000941
Vladimir Marko60584552015-09-03 13:35:12 +0000942 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100943 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000944 }
945
Vladimir Marko60584552015-09-03 13:35:12 +0000946 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100947 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000948 }
949
David Brazdild26a4112015-11-10 11:07:31 +0000950 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
951 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
952
Vladimir Marko60584552015-09-03 13:35:12 +0000953 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
954 return ContainsElement(successors_, block, start_from);
955 }
956
957 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100958 return dominated_blocks_;
959 }
960
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100961 bool IsEntryBlock() const {
962 return graph_->GetEntryBlock() == this;
963 }
964
965 bool IsExitBlock() const {
966 return graph_->GetExitBlock() == this;
967 }
968
David Brazdil46e2a392015-03-16 17:31:52 +0000969 bool IsSingleGoto() const;
Mads Ager16e52892017-07-14 13:11:37 +0200970 bool IsSingleReturn() const;
Mingyao Yang46721ef2017-10-05 14:45:17 -0700971 bool IsSingleReturnOrReturnVoidAllowingPhis() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000972 bool IsSingleTryBoundary() const;
973
974 // Returns true if this block emits nothing but a jump.
975 bool IsSingleJump() const {
976 HLoopInformation* loop_info = GetLoopInformation();
977 return (IsSingleGoto() || IsSingleTryBoundary())
978 // Back edges generate a suspend check.
979 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
980 }
David Brazdil46e2a392015-03-16 17:31:52 +0000981
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000982 void AddBackEdge(HBasicBlock* back_edge) {
983 if (loop_information_ == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100984 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000985 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100986 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000987 loop_information_->AddBackEdge(back_edge);
988 }
989
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000990 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000991 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000992
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100993 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000994 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600995 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000996
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000997 HBasicBlock* GetDominator() const { return dominator_; }
998 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000999 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
1000
1001 void RemoveDominatedBlock(HBasicBlock* block) {
1002 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +01001003 }
Vladimir Marko60584552015-09-03 13:35:12 +00001004
1005 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
1006 ReplaceElement(dominated_blocks_, existing, new_block);
1007 }
1008
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001009 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001010
1011 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001012 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001013 }
1014
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001015 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
1016 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +01001017 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001018 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +01001019 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
1020 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001021
Nicolas Geoffray09aa1472016-01-19 10:52:54 +00001022 HInstruction* GetFirstInstructionDisregardMoves() const;
1023
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001024 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001025 successors_.push_back(block);
1026 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001027 }
1028
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001029 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
1030 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001031 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001032 new_block->predecessors_.push_back(this);
1033 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001034 }
1035
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001036 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
1037 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001038 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001039 new_block->successors_.push_back(this);
1040 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001041 }
1042
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001043 // Insert `this` between `predecessor` and `successor. This method
1044 // preserves the indicies, and will update the first edge found between
1045 // `predecessor` and `successor`.
1046 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
1047 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001048 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +00001049 successor->predecessors_[predecessor_index] = this;
1050 predecessor->successors_[successor_index] = this;
1051 successors_.push_back(successor);
1052 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001053 }
1054
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001055 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001056 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001057 }
1058
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001059 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001060 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001061 }
1062
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001063 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001064 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001065 }
1066
1067 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001068 predecessors_.push_back(block);
1069 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001070 }
1071
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001072 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001073 DCHECK_EQ(predecessors_.size(), 2u);
1074 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001075 }
1076
David Brazdil769c9e52015-04-27 13:54:09 +01001077 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001078 DCHECK_EQ(successors_.size(), 2u);
1079 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +01001080 }
1081
David Brazdilfc6a86a2015-06-26 10:33:45 +00001082 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001083 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001084 }
1085
David Brazdilfc6a86a2015-06-26 10:33:45 +00001086 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001087 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001088 }
1089
David Brazdilfc6a86a2015-06-26 10:33:45 +00001090 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001091 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001092 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001093 }
1094
1095 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001096 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001097 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001098 }
1099
1100 // Returns whether the first occurrence of `predecessor` in the list of
1101 // predecessors is at index `idx`.
1102 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001103 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001104 return GetPredecessorIndexOf(predecessor) == idx;
1105 }
1106
David Brazdild7558da2015-09-22 13:04:14 +01001107 // Create a new block between this block and its predecessors. The new block
1108 // is added to the graph, all predecessor edges are relinked to it and an edge
1109 // is created to `this`. Returns the new empty block. Reverse post order or
1110 // loop and try/catch information are not updated.
1111 HBasicBlock* CreateImmediateDominator();
1112
David Brazdilfc6a86a2015-06-26 10:33:45 +00001113 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001114 // created, latter block. Note that this method will add the block to the
1115 // graph, create a Goto at the end of the former block and will create an edge
1116 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001117 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001118 HBasicBlock* SplitBefore(HInstruction* cursor);
1119
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001120 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001121 // created block. Note that this method just updates raw block information,
1122 // like predecessors, successors, dominators, and instruction list. It does not
1123 // update the graph, reverse post order, loop information, nor make sure the
1124 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001125 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1126
1127 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1128 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001129
1130 // Merge `other` at the end of `this`. Successors and dominated blocks of
1131 // `other` are changed to be successors and dominated blocks of `this`. Note
1132 // that this method does not update the graph, reverse post order, loop
1133 // information, nor make sure the blocks are consistent (for example ending
1134 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001135 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001136
1137 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1138 // of `this` are moved to `other`.
1139 // Note that this method does not update the graph, reverse post order, loop
1140 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001141 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001142 void ReplaceWith(HBasicBlock* other);
1143
Aart Bik6b69e0a2017-01-11 10:20:43 -08001144 // Merges the instructions of `other` at the end of `this`.
1145 void MergeInstructionsWith(HBasicBlock* other);
1146
David Brazdil2d7352b2015-04-20 14:52:42 +01001147 // Merge `other` at the end of `this`. This method updates loops, reverse post
1148 // order, links to predecessors, successors, dominators and deletes the block
1149 // from the graph. The two blocks must be successive, i.e. `this` the only
1150 // predecessor of `other` and vice versa.
1151 void MergeWith(HBasicBlock* other);
1152
1153 // Disconnects `this` from all its predecessors, successors and dominator,
1154 // removes it from all loops it is included in and eventually from the graph.
1155 // The block must not dominate any other block. Predecessors and successors
1156 // are safely updated.
1157 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001158
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001159 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001160 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001161 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001162 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001163 // Replace instruction `initial` with `replacement` within this block.
1164 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1165 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001166 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001167 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001168 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1169 // instruction list. With 'ensure_safety' set to true, it verifies that the
1170 // instruction is not in use and removes it from the use lists of its inputs.
1171 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1172 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001173 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001174
1175 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001176 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001177 }
1178
Roland Levillain6b879dd2014-09-22 17:13:44 +01001179 bool IsLoopPreHeaderFirstPredecessor() const {
1180 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001181 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001182 }
1183
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001184 bool IsFirstPredecessorBackEdge() const {
1185 DCHECK(IsLoopHeader());
1186 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1187 }
1188
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001189 HLoopInformation* GetLoopInformation() const {
1190 return loop_information_;
1191 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001192
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001193 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001194 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001195 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001196 void SetInLoop(HLoopInformation* info) {
1197 if (IsLoopHeader()) {
1198 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001199 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001200 loop_information_ = info;
1201 } else if (loop_information_->Contains(*info->GetHeader())) {
1202 // Block is currently part of an outer loop. Make it part of this inner loop.
1203 // Note that a non loop header having a loop information means this loop information
1204 // has already been populated
1205 loop_information_ = info;
1206 } else {
1207 // Block is part of an inner loop. Do not update the loop information.
1208 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1209 // at this point, because this method is being called while populating `info`.
1210 }
1211 }
1212
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001213 // Raw update of the loop information.
1214 void SetLoopInformation(HLoopInformation* info) {
1215 loop_information_ = info;
1216 }
1217
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001218 bool IsInLoop() const { return loop_information_ != nullptr; }
1219
David Brazdilec16f792015-08-19 15:04:01 +01001220 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1221
1222 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1223 try_catch_information_ = try_catch_information;
1224 }
1225
1226 bool IsTryBlock() const {
1227 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1228 }
1229
1230 bool IsCatchBlock() const {
1231 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1232 }
David Brazdilffee3d32015-07-06 11:48:53 +01001233
1234 // Returns the try entry that this block's successors should have. They will
1235 // be in the same try, unless the block ends in a try boundary. In that case,
1236 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001237 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001238
David Brazdild7558da2015-09-22 13:04:14 +01001239 bool HasThrowingInstructions() const;
1240
David Brazdila4b8c212015-05-07 09:59:30 +01001241 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001242 bool Dominates(HBasicBlock* block) const;
1243
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001244 size_t GetLifetimeStart() const { return lifetime_start_; }
1245 size_t GetLifetimeEnd() const { return lifetime_end_; }
1246
1247 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1248 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1249
David Brazdil8d5b8b22015-03-24 10:51:52 +00001250 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001251 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001252 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001253 bool HasSinglePhi() const;
1254
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001255 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001256 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001257 ArenaVector<HBasicBlock*> predecessors_;
1258 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001259 HInstructionList instructions_;
1260 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001261 HLoopInformation* loop_information_;
1262 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001263 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001264 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001265 // The dex program counter of the first instruction of this block.
1266 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001267 size_t lifetime_start_;
1268 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001269 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001270
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001271 friend class HGraph;
1272 friend class HInstruction;
1273
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001274 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1275};
1276
David Brazdilb2bd1c52015-03-25 11:17:37 +00001277// Iterates over the LoopInformation of all loops which contain 'block'
1278// from the innermost to the outermost.
1279class HLoopInformationOutwardIterator : public ValueObject {
1280 public:
1281 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1282 : current_(block.GetLoopInformation()) {}
1283
1284 bool Done() const { return current_ == nullptr; }
1285
1286 void Advance() {
1287 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001288 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001289 }
1290
1291 HLoopInformation* Current() const {
1292 DCHECK(!Done());
1293 return current_;
1294 }
1295
1296 private:
1297 HLoopInformation* current_;
1298
1299 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1300};
1301
Alexandre Ramesef20f712015-06-09 10:29:30 +01001302#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001303 M(Above, Condition) \
1304 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001305 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001306 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001307 M(ArrayGet, Instruction) \
1308 M(ArrayLength, Instruction) \
1309 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001310 M(Below, Condition) \
1311 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001312 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001313 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001314 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001315 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001316 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001317 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001318 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001319 M(Compare, BinaryOperation) \
Igor Murashkind01745e2017-04-05 16:40:31 -07001320 M(ConstructorFence, Instruction) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001321 M(CurrentMethod, Instruction) \
Mingyao Yang063fc772016-08-02 11:02:54 -07001322 M(ShouldDeoptimizeFlag, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001323 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001324 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001325 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001326 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001327 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001328 M(Exit, Instruction) \
1329 M(FloatConstant, Constant) \
1330 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001331 M(GreaterThan, Condition) \
1332 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001333 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001334 M(InstanceFieldGet, Instruction) \
1335 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001336 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001337 M(IntConstant, Constant) \
xueliang.zhonge0eb4832017-10-30 13:43:14 +00001338 M(IntermediateAddress, Instruction) \
Calin Juravle175dc732015-08-25 15:42:32 +01001339 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001340 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001341 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001342 M(InvokeVirtual, Invoke) \
Orion Hodsonac141392017-01-13 11:53:47 +00001343 M(InvokePolymorphic, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001344 M(LessThan, Condition) \
1345 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001346 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001347 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001348 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001349 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001350 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001351 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001352 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001353 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001354 M(Neg, UnaryOperation) \
1355 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001356 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001357 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001358 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001359 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001360 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001361 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001362 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001363 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001364 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001365 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001366 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001367 M(Return, Instruction) \
1368 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001369 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001370 M(Shl, BinaryOperation) \
1371 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001372 M(StaticFieldGet, Instruction) \
1373 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001374 M(UnresolvedInstanceFieldGet, Instruction) \
1375 M(UnresolvedInstanceFieldSet, Instruction) \
1376 M(UnresolvedStaticFieldGet, Instruction) \
1377 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001378 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001379 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001380 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001381 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001382 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001383 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001384 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001385 M(Xor, BinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001386 M(VecReplicateScalar, VecUnaryOperation) \
Aart Bik0148de42017-09-05 09:25:01 -07001387 M(VecExtractScalar, VecUnaryOperation) \
1388 M(VecReduce, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001389 M(VecCnv, VecUnaryOperation) \
1390 M(VecNeg, VecUnaryOperation) \
Aart Bik6daebeb2017-04-03 14:35:41 -07001391 M(VecAbs, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001392 M(VecNot, VecUnaryOperation) \
1393 M(VecAdd, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001394 M(VecHalvingAdd, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001395 M(VecSub, VecBinaryOperation) \
1396 M(VecMul, VecBinaryOperation) \
1397 M(VecDiv, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001398 M(VecMin, VecBinaryOperation) \
1399 M(VecMax, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001400 M(VecAnd, VecBinaryOperation) \
1401 M(VecAndNot, VecBinaryOperation) \
1402 M(VecOr, VecBinaryOperation) \
1403 M(VecXor, VecBinaryOperation) \
1404 M(VecShl, VecBinaryOperation) \
1405 M(VecShr, VecBinaryOperation) \
1406 M(VecUShr, VecBinaryOperation) \
Aart Bik8de59162017-04-21 09:42:01 -07001407 M(VecSetScalars, VecOperation) \
Artem Serovf34dd202017-04-10 17:41:46 +01001408 M(VecMultiplyAccumulate, VecOperation) \
Aart Bikdbbac8f2017-09-01 13:06:08 -07001409 M(VecSADAccumulate, VecOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001410 M(VecLoad, VecMemoryOperation) \
1411 M(VecStore, VecMemoryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001412
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001413/*
1414 * Instructions, shared across several (not all) architectures.
1415 */
1416#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1417#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1418#else
1419#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001420 M(BitwiseNegatedRight, Instruction) \
Anton Kirilov74234da2017-01-13 14:42:47 +00001421 M(DataProcWithShifterOp, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001422 M(MultiplyAccumulate, Instruction) \
Artem Serove1811ed2017-04-27 16:50:47 +01001423 M(IntermediateAddressIndex, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001424#endif
1425
Alexandre Ramesef20f712015-06-09 10:29:30 +01001426#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1427
1428#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1429
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001430#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001431#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001432#else
1433#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1434 M(MipsComputeBaseMethodAddress, Instruction) \
Lena Djokica2901602017-09-21 13:50:52 +02001435 M(MipsPackedSwitch, Instruction) \
1436 M(IntermediateArrayAddressIndex, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001437#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001438
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001439#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1440
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001441#ifndef ART_ENABLE_CODEGEN_x86
1442#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1443#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001444#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1445 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001446 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001447 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001448 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001449#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001450
1451#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1452
1453#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1454 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001455 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001456 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1457 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001458 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001459 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001460 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1461 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1462
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001463#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1464 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001465 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001466 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001467 M(BinaryOperation, Instruction) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001468 M(Invoke, Instruction) \
1469 M(VecOperation, Instruction) \
1470 M(VecUnaryOperation, VecOperation) \
1471 M(VecBinaryOperation, VecOperation) \
1472 M(VecMemoryOperation, VecOperation)
Dave Allison20dfc792014-06-16 20:44:29 -07001473
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001474#define FOR_EACH_INSTRUCTION(M) \
1475 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1476 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1477
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001478#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001479FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1480#undef FORWARD_DECLARATION
1481
Vladimir Marko372f10e2016-05-17 16:30:10 +01001482#define DECLARE_INSTRUCTION(type) \
1483 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
1484 const char* DebugName() const OVERRIDE { return #type; } \
1485 bool InstructionTypeEquals(const HInstruction* other) const OVERRIDE { \
1486 return other->Is##type(); \
1487 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001488 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001489
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001490#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1491 bool Is##type() const { return As##type() != nullptr; } \
1492 const H##type* As##type() const { return this; } \
1493 H##type* As##type() { return this; }
1494
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001495template <typename T>
Vladimir Marko82b07402017-03-01 19:02:04 +00001496class HUseListNode : public ArenaObject<kArenaAllocUseListNode>,
1497 public IntrusiveForwardListNode<HUseListNode<T>> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001498 public:
Igor Murashkind01745e2017-04-05 16:40:31 -07001499 // Get the instruction which has this use as one of the inputs.
David Brazdiled596192015-01-23 10:39:45 +00001500 T GetUser() const { return user_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001501 // Get the position of the input record that this use corresponds to.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001502 size_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001503 // Set the position of the input record that this use corresponds to.
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001504 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001505
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001506 private:
David Brazdiled596192015-01-23 10:39:45 +00001507 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001508 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001509
1510 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001511 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001512
Vladimir Marko46817b82016-03-29 12:21:58 +01001513 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001514
1515 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1516};
1517
David Brazdiled596192015-01-23 10:39:45 +00001518template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001519using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001520
David Brazdil1abb4192015-02-17 18:33:36 +00001521// This class is used by HEnvironment and HInstruction classes to record the
1522// instructions they use and pointers to the corresponding HUseListNodes kept
1523// by the used instructions.
1524template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001525class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001526 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001527 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1528 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001529
Vladimir Marko46817b82016-03-29 12:21:58 +01001530 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1531 : HUserRecord(old_record.instruction_, before_use_node) {}
1532 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1533 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001534 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001535 }
1536
1537 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001538 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1539 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001540
1541 private:
1542 // Instruction used by the user.
1543 HInstruction* instruction_;
1544
Vladimir Marko46817b82016-03-29 12:21:58 +01001545 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1546 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001547};
1548
Vladimir Markoe9004912016-06-16 16:50:52 +01001549// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1550// This is used for HInstruction::GetInputs() to return a container wrapper providing
1551// HInstruction* values even though the underlying container has HUserRecord<>s.
1552struct HInputExtractor {
1553 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1554 return record.GetInstruction();
1555 }
1556 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1557 return record.GetInstruction();
1558 }
1559};
1560
1561using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1562using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1563
Aart Bik854a02b2015-07-14 16:07:00 -07001564/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001565 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001566 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001567 * For write/read dependences on fields/arrays, the dependence analysis uses
1568 * type disambiguation (e.g. a float field write cannot modify the value of an
1569 * integer field read) and the access type (e.g. a reference array write cannot
1570 * modify the value of a reference field read [although it may modify the
1571 * reference fetch prior to reading the field, which is represented by its own
1572 * write/read dependence]). The analysis makes conservative points-to
1573 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1574 * the same, and any reference read depends on any reference read without
1575 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001576 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001577 * The internal representation uses 38-bit and is described in the table below.
1578 * The first line indicates the side effect, and for field/array accesses the
1579 * second line indicates the type of the access (in the order of the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001580 * DataType::Type enum).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001581 * The two numbered lines below indicate the bit position in the bitfield (read
1582 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001583 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001584 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1585 * +-------------+---------+---------+--------------+---------+---------+
1586 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1587 * | 3 |333333322|222222221| 1 |111111110|000000000|
1588 * | 7 |654321098|765432109| 8 |765432109|876543210|
1589 *
1590 * Note that, to ease the implementation, 'changes' bits are least significant
1591 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001592 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001593class SideEffects : public ValueObject {
1594 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001595 SideEffects() : flags_(0) {}
1596
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001597 static SideEffects None() {
1598 return SideEffects(0);
1599 }
1600
1601 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001602 return SideEffects(kAllChangeBits | kAllDependOnBits);
1603 }
1604
1605 static SideEffects AllChanges() {
1606 return SideEffects(kAllChangeBits);
1607 }
1608
1609 static SideEffects AllDependencies() {
1610 return SideEffects(kAllDependOnBits);
1611 }
1612
1613 static SideEffects AllExceptGCDependency() {
1614 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1615 }
1616
1617 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001618 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001619 }
1620
Aart Bik34c3ba92015-07-20 14:08:59 -07001621 static SideEffects AllWrites() {
1622 return SideEffects(kAllWrites);
1623 }
1624
1625 static SideEffects AllReads() {
1626 return SideEffects(kAllReads);
1627 }
1628
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001629 static SideEffects FieldWriteOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001630 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001631 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001632 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001633 }
1634
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001635 static SideEffects ArrayWriteOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001636 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001637 }
1638
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001639 static SideEffects FieldReadOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001640 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001641 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001642 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001643 }
1644
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001645 static SideEffects ArrayReadOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001646 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001647 }
1648
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001649 static SideEffects CanTriggerGC() {
1650 return SideEffects(1ULL << kCanTriggerGCBit);
1651 }
1652
1653 static SideEffects DependsOnGC() {
1654 return SideEffects(1ULL << kDependsOnGCBit);
1655 }
1656
Aart Bik854a02b2015-07-14 16:07:00 -07001657 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001658 SideEffects Union(SideEffects other) const {
1659 return SideEffects(flags_ | other.flags_);
1660 }
1661
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001662 SideEffects Exclusion(SideEffects other) const {
1663 return SideEffects(flags_ & ~other.flags_);
1664 }
1665
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001666 void Add(SideEffects other) {
1667 flags_ |= other.flags_;
1668 }
1669
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001670 bool Includes(SideEffects other) const {
1671 return (other.flags_ & flags_) == other.flags_;
1672 }
1673
1674 bool HasSideEffects() const {
1675 return (flags_ & kAllChangeBits);
1676 }
1677
1678 bool HasDependencies() const {
1679 return (flags_ & kAllDependOnBits);
1680 }
1681
1682 // Returns true if there are no side effects or dependencies.
1683 bool DoesNothing() const {
1684 return flags_ == 0;
1685 }
1686
Aart Bik854a02b2015-07-14 16:07:00 -07001687 // Returns true if something is written.
1688 bool DoesAnyWrite() const {
1689 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001690 }
1691
Aart Bik854a02b2015-07-14 16:07:00 -07001692 // Returns true if something is read.
1693 bool DoesAnyRead() const {
1694 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001695 }
1696
Aart Bik854a02b2015-07-14 16:07:00 -07001697 // Returns true if potentially everything is written and read
1698 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001699 bool DoesAllReadWrite() const {
1700 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1701 }
1702
Aart Bik854a02b2015-07-14 16:07:00 -07001703 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001704 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001705 }
1706
Roland Levillain0d5a2812015-11-13 10:07:31 +00001707 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001708 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001709 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1710 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001711 }
1712
1713 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001714 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001715 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001716 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001717 for (int s = kLastBit; s >= 0; s--) {
1718 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1719 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1720 // This is a bit for the GC side effect.
1721 if (current_bit_is_set) {
1722 flags += "GC";
1723 }
Aart Bik854a02b2015-07-14 16:07:00 -07001724 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001725 } else {
1726 // This is a bit for the array/field analysis.
1727 // The underscore character stands for the 'can trigger GC' bit.
1728 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1729 if (current_bit_is_set) {
1730 flags += kDebug[s];
1731 }
1732 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1733 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1734 flags += "|";
1735 }
1736 }
Aart Bik854a02b2015-07-14 16:07:00 -07001737 }
1738 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001739 }
1740
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001741 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001742
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001743 private:
1744 static constexpr int kFieldArrayAnalysisBits = 9;
1745
1746 static constexpr int kFieldWriteOffset = 0;
1747 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1748 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1749 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1750
1751 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1752
1753 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1754 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1755 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1756 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1757
1758 static constexpr int kLastBit = kDependsOnGCBit;
1759 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1760
1761 // Aliases.
1762
1763 static_assert(kChangeBits == kDependOnBits,
1764 "the 'change' bits should match the 'depend on' bits.");
1765
1766 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1767 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1768 static constexpr uint64_t kAllWrites =
1769 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1770 static constexpr uint64_t kAllReads =
1771 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001772
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001773 // Translates type to bit flag. The type must correspond to a Java type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001774 static uint64_t TypeFlag(DataType::Type type, int offset) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001775 int shift;
1776 switch (type) {
1777 case DataType::Type::kReference: shift = 0; break;
1778 case DataType::Type::kBool: shift = 1; break;
1779 case DataType::Type::kInt8: shift = 2; break;
1780 case DataType::Type::kUint16: shift = 3; break;
1781 case DataType::Type::kInt16: shift = 4; break;
1782 case DataType::Type::kInt32: shift = 5; break;
1783 case DataType::Type::kInt64: shift = 6; break;
1784 case DataType::Type::kFloat32: shift = 7; break;
1785 case DataType::Type::kFloat64: shift = 8; break;
1786 default:
1787 LOG(FATAL) << "Unexpected data type " << type;
1788 UNREACHABLE();
1789 }
Aart Bik854a02b2015-07-14 16:07:00 -07001790 DCHECK_LE(kFieldWriteOffset, shift);
1791 DCHECK_LT(shift, kArrayWriteOffset);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001792 return UINT64_C(1) << (shift + offset);
Aart Bik854a02b2015-07-14 16:07:00 -07001793 }
1794
1795 // Private constructor on direct flags value.
1796 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1797
1798 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001799};
1800
David Brazdiled596192015-01-23 10:39:45 +00001801// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001802class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001803 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001804 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
Mingyao Yang01b47b02017-02-03 12:09:57 -08001805 size_t number_of_vregs,
1806 ArtMethod* method,
1807 uint32_t dex_pc,
1808 HInstruction* holder)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001809 : vregs_(number_of_vregs, allocator->Adapter(kArenaAllocEnvironmentVRegs)),
1810 locations_(allocator->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001811 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001812 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001813 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001814 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001815 }
1816
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001817 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
1818 const HEnvironment& to_copy,
1819 HInstruction* holder)
1820 : HEnvironment(allocator,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001821 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001822 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001823 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001824 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001825
Vladimir Markoec32f642017-06-02 10:51:55 +01001826 void AllocateLocations() {
1827 DCHECK(locations_.empty());
1828 locations_.resize(vregs_.size());
1829 }
1830
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001831 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001832 if (parent_ != nullptr) {
1833 parent_->SetAndCopyParentChain(allocator, parent);
1834 } else {
1835 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1836 parent_->CopyFrom(parent);
1837 if (parent->GetParent() != nullptr) {
1838 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1839 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001840 }
David Brazdiled596192015-01-23 10:39:45 +00001841 }
1842
Vladimir Marko69d310e2017-10-09 14:12:23 +01001843 void CopyFrom(ArrayRef<HInstruction* const> locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001844 void CopyFrom(HEnvironment* environment);
1845
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001846 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1847 // input to the loop phi instead. This is for inserting instructions that
1848 // require an environment (like HDeoptimization) in the loop pre-header.
1849 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001850
1851 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001852 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001853 }
1854
1855 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001856 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001857 }
1858
David Brazdil1abb4192015-02-17 18:33:36 +00001859 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001860
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001861 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001862
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001863 HEnvironment* GetParent() const { return parent_; }
1864
1865 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001866 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001867 }
1868
1869 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001870 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001871 }
1872
1873 uint32_t GetDexPc() const {
1874 return dex_pc_;
1875 }
1876
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001877 ArtMethod* GetMethod() const {
1878 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001879 }
1880
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001881 HInstruction* GetHolder() const {
1882 return holder_;
1883 }
1884
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001885
1886 bool IsFromInlinedInvoke() const {
1887 return GetParent() != nullptr;
1888 }
1889
David Brazdiled596192015-01-23 10:39:45 +00001890 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001891 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1892 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001893 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001894 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001895 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00001896
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001897 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001898 HInstruction* const holder_;
1899
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001900 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001901
1902 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1903};
1904
Vladimir Markof9f64412015-09-02 14:05:49 +01001905class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001906 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001907 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001908 : previous_(nullptr),
1909 next_(nullptr),
1910 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001911 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001912 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001913 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001914 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001915 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001916 locations_(nullptr),
1917 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001918 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001919 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001920 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1921 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1922 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001923
Dave Allison20dfc792014-06-16 20:44:29 -07001924 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001925
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001926#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001927 enum InstructionKind {
1928 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1929 };
1930#undef DECLARE_KIND
1931
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001932 HInstruction* GetNext() const { return next_; }
1933 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001934
Calin Juravle77520bc2015-01-12 18:45:46 +00001935 HInstruction* GetNextDisregardingMoves() const;
1936 HInstruction* GetPreviousDisregardingMoves() const;
1937
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001938 HBasicBlock* GetBlock() const { return block_; }
Vladimir Markoca6fff82017-10-03 14:49:14 +01001939 ArenaAllocator* GetAllocator() const { return block_->GetGraph()->GetAllocator(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001940 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001941 bool IsInBlock() const { return block_ != nullptr; }
1942 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001943 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1944 bool IsIrreducibleLoopHeaderPhi() const {
1945 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1946 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001947
Vladimir Marko372f10e2016-05-17 16:30:10 +01001948 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
1949
1950 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
1951 // One virtual method is enough, just const_cast<> and then re-add the const.
1952 return ArrayRef<const HUserRecord<HInstruction*>>(
1953 const_cast<HInstruction*>(this)->GetInputRecords());
1954 }
1955
Vladimir Markoe9004912016-06-16 16:50:52 +01001956 HInputsRef GetInputs() {
1957 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001958 }
1959
Vladimir Markoe9004912016-06-16 16:50:52 +01001960 HConstInputsRef GetInputs() const {
1961 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001962 }
1963
1964 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001965 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001966
Aart Bik2767f4b2016-10-28 15:03:53 -07001967 bool HasInput(HInstruction* input) const {
1968 for (const HInstruction* i : GetInputs()) {
1969 if (i == input) {
1970 return true;
1971 }
1972 }
1973 return false;
1974 }
1975
Vladimir Marko372f10e2016-05-17 16:30:10 +01001976 void SetRawInputAt(size_t index, HInstruction* input) {
1977 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1978 }
1979
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001980 virtual void Accept(HGraphVisitor* visitor) = 0;
1981 virtual const char* DebugName() const = 0;
1982
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001983 virtual DataType::Type GetType() const { return DataType::Type::kVoid; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001984
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001985 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001986
1987 uint32_t GetDexPc() const { return dex_pc_; }
1988
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001989 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001990
Nicolas Geoffray331605a2017-03-01 11:01:41 +00001991 // Can the instruction throw?
1992 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
1993 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01001994 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001995 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001996
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001997 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001998 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001999
Calin Juravle10e244f2015-01-26 18:54:32 +00002000 // Does not apply for all instructions, but having this at top level greatly
2001 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00002002 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002003 virtual bool CanBeNull() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002004 DCHECK_EQ(GetType(), DataType::Type::kReference) << "CanBeNull only applies to reference types";
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002005 return true;
2006 }
Calin Juravle10e244f2015-01-26 18:54:32 +00002007
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002008 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01002009 return false;
2010 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002011
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002012 virtual bool IsActualObject() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002013 return GetType() == DataType::Type::kReference;
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002014 }
2015
Calin Juravle2e768302015-07-28 14:41:11 +00002016 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00002017
Calin Juravle61d544b2015-02-23 16:46:57 +00002018 ReferenceTypeInfo GetReferenceTypeInfo() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002019 DCHECK_EQ(GetType(), DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002020 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00002021 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00002022 }
Calin Juravleacf735c2015-02-12 15:25:22 +00002023
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002024 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00002025 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002026 // Note: fixup_end remains valid across push_front().
2027 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
2028 HUseListNode<HInstruction*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002029 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HInstruction*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002030 uses_.push_front(*new_node);
2031 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002032 }
2033
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002034 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01002035 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002036 // Note: env_fixup_end remains valid across push_front().
2037 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
2038 HUseListNode<HEnvironment*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002039 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HEnvironment*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002040 env_uses_.push_front(*new_node);
2041 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002042 }
2043
David Brazdil1abb4192015-02-17 18:33:36 +00002044 void RemoveAsUserOfInput(size_t input) {
2045 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002046 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2047 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2048 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00002049 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002050
Vladimir Marko372f10e2016-05-17 16:30:10 +01002051 void RemoveAsUserOfAllInputs() {
2052 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2053 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2054 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2055 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
2056 }
2057 }
2058
David Brazdil1abb4192015-02-17 18:33:36 +00002059 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2060 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002061
Vladimir Marko46817b82016-03-29 12:21:58 +01002062 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2063 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2064 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002065 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002066 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002067 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002068
Aart Bikcc42be02016-10-20 16:14:16 -07002069 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002070 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002071 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002072 !CanThrow() &&
2073 !IsSuspendCheck() &&
2074 !IsControlFlow() &&
2075 !IsNativeDebugInfo() &&
2076 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002077 // If we added an explicit barrier then we should keep it.
Igor Murashkind01745e2017-04-05 16:40:31 -07002078 !IsMemoryBarrier() &&
2079 !IsConstructorFence();
Aart Bik482095d2016-10-10 15:39:10 -07002080 }
2081
Aart Bikcc42be02016-10-20 16:14:16 -07002082 bool IsDeadAndRemovable() const {
2083 return IsRemovable() && !HasUses();
2084 }
2085
Roland Levillain6c82d402014-10-13 16:10:27 +01002086 // Does this instruction strictly dominate `other_instruction`?
2087 // Returns false if this instruction and `other_instruction` are the same.
2088 // Aborts if this instruction and `other_instruction` are both phis.
2089 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002090
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002091 int GetId() const { return id_; }
2092 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002093
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002094 int GetSsaIndex() const { return ssa_index_; }
2095 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2096 bool HasSsaIndex() const { return ssa_index_ != -1; }
2097
2098 bool HasEnvironment() const { return environment_ != nullptr; }
2099 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002100 // Set the `environment_` field. Raw because this method does not
2101 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002102 void SetRawEnvironment(HEnvironment* environment) {
2103 DCHECK(environment_ == nullptr);
2104 DCHECK_EQ(environment->GetHolder(), this);
2105 environment_ = environment;
2106 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002107
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002108 void InsertRawEnvironment(HEnvironment* environment) {
2109 DCHECK(environment_ != nullptr);
2110 DCHECK_EQ(environment->GetHolder(), this);
2111 DCHECK(environment->GetParent() == nullptr);
2112 environment->parent_ = environment_;
2113 environment_ = environment;
2114 }
2115
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002116 void RemoveEnvironment();
2117
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002118 // Set the environment of this instruction, copying it from `environment`. While
2119 // copying, the uses lists are being updated.
2120 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002121 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002122 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002123 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002124 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002125 if (environment->GetParent() != nullptr) {
2126 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2127 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002128 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002129
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002130 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2131 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002132 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002133 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002134 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002135 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002136 if (environment->GetParent() != nullptr) {
2137 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2138 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002139 }
2140
Nicolas Geoffray39468442014-09-02 15:17:15 +01002141 // Returns the number of entries in the environment. Typically, that is the
2142 // number of dex registers in a method. It could be more in case of inlining.
2143 size_t EnvironmentSize() const;
2144
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002145 LocationSummary* GetLocations() const { return locations_; }
2146 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002147
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002148 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002149 void ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002150 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002151
Alexandre Rames188d4312015-04-09 18:30:21 +01002152 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2153 // uses of this instruction by `other` are *not* updated.
2154 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2155 ReplaceWith(other);
2156 other->ReplaceInput(this, use_index);
2157 }
2158
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002159 // Move `this` instruction before `cursor`
2160 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002161
Vladimir Markofb337ea2015-11-25 15:25:10 +00002162 // Move `this` before its first user and out of any loops. If there is no
2163 // out-of-loop user that dominates all other users, move the instruction
2164 // to the end of the out-of-loop common dominator of the user's blocks.
2165 //
2166 // This can be used only on non-throwing instructions with no side effects that
2167 // have at least one use but no environment uses.
2168 void MoveBeforeFirstUserAndOutOfLoops();
2169
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002170#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002171 bool Is##type() const; \
2172 const H##type* As##type() const; \
2173 H##type* As##type();
2174
2175 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2176#undef INSTRUCTION_TYPE_CHECK
2177
2178#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002179 bool Is##type() const { return (As##type() != nullptr); } \
2180 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002181 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002182 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002183#undef INSTRUCTION_TYPE_CHECK
2184
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002185 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002186 // TODO: this method is used by LICM and GVN with possibly different
2187 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002188 virtual bool CanBeMoved() const { return false; }
2189
2190 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002191 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002192 return false;
2193 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002194
2195 // Returns whether any data encoded in the two instructions is equal.
2196 // This method does not look at the inputs. Both instructions must be
2197 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002198 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002199 return false;
2200 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002201
2202 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002203 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002204 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002205 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002206
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002207 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2208 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2209 // the virtual function because the __attribute__((__pure__)) doesn't really
2210 // apply the strong requirement for virtual functions, preventing optimizations.
2211 InstructionKind GetKind() const PURE;
2212 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002213
2214 virtual size_t ComputeHashCode() const {
2215 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002216 for (const HInstruction* input : GetInputs()) {
2217 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002218 }
2219 return result;
2220 }
2221
2222 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002223 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002224 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002225
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002226 size_t GetLifetimePosition() const { return lifetime_position_; }
2227 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2228 LiveInterval* GetLiveInterval() const { return live_interval_; }
2229 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2230 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2231
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002232 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2233
2234 // Returns whether the code generation of the instruction will require to have access
2235 // to the current method. Such instructions are:
2236 // (1): Instructions that require an environment, as calling the runtime requires
2237 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002238 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2239 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002240 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002241 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002242 }
2243
Vladimir Markodc151b22015-10-15 18:02:30 +01002244 // Returns whether the code generation of the instruction will require to have access
2245 // to the dex cache of the current method's declaring class via the current method.
2246 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002247
Mark Mendellc4701932015-04-10 13:18:51 -04002248 // Does this instruction have any use in an environment before
2249 // control flow hits 'other'?
2250 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2251
2252 // Remove all references to environment uses of this instruction.
2253 // The caller must ensure that this is safe to do.
2254 void RemoveEnvironmentUsers();
2255
Vladimir Markoa1de9182016-02-25 11:37:38 +00002256 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2257 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002258
David Brazdil1abb4192015-02-17 18:33:36 +00002259 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002260 // If set, the machine code for this instruction is assumed to be generated by
2261 // its users. Used by liveness analysis to compute use positions accordingly.
2262 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2263 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2264 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2265 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2266
Vladimir Marko372f10e2016-05-17 16:30:10 +01002267 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2268 return GetInputRecords()[i];
2269 }
2270
2271 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2272 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2273 input_records[index] = input;
2274 }
David Brazdil1abb4192015-02-17 18:33:36 +00002275
Vladimir Markoa1de9182016-02-25 11:37:38 +00002276 uint32_t GetPackedFields() const {
2277 return packed_fields_;
2278 }
2279
2280 template <size_t flag>
2281 bool GetPackedFlag() const {
2282 return (packed_fields_ & (1u << flag)) != 0u;
2283 }
2284
2285 template <size_t flag>
2286 void SetPackedFlag(bool value = true) {
2287 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2288 }
2289
2290 template <typename BitFieldType>
2291 typename BitFieldType::value_type GetPackedField() const {
2292 return BitFieldType::Decode(packed_fields_);
2293 }
2294
2295 template <typename BitFieldType>
2296 void SetPackedField(typename BitFieldType::value_type value) {
2297 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2298 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2299 }
2300
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002301 private:
Vladimir Marko46817b82016-03-29 12:21:58 +01002302 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2303 auto before_use_node = uses_.before_begin();
2304 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2305 HInstruction* user = use_node->GetUser();
2306 size_t input_index = use_node->GetIndex();
2307 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2308 before_use_node = use_node;
2309 }
2310 }
2311
2312 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2313 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2314 if (next != uses_.end()) {
2315 HInstruction* next_user = next->GetUser();
2316 size_t next_index = next->GetIndex();
2317 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2318 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2319 }
2320 }
2321
2322 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2323 auto before_env_use_node = env_uses_.before_begin();
2324 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2325 HEnvironment* user = env_use_node->GetUser();
2326 size_t input_index = env_use_node->GetIndex();
2327 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2328 before_env_use_node = env_use_node;
2329 }
2330 }
2331
2332 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2333 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2334 if (next != env_uses_.end()) {
2335 HEnvironment* next_user = next->GetUser();
2336 size_t next_index = next->GetIndex();
2337 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2338 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2339 }
2340 }
David Brazdil1abb4192015-02-17 18:33:36 +00002341
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002342 HInstruction* previous_;
2343 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002344 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002345 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002346
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002347 // An instruction gets an id when it is added to the graph.
2348 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002349 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002350 int id_;
2351
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002352 // When doing liveness analysis, instructions that have uses get an SSA index.
2353 int ssa_index_;
2354
Vladimir Markoa1de9182016-02-25 11:37:38 +00002355 // Packed fields.
2356 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002357
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002358 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002359 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002360
2361 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002362 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002363
Nicolas Geoffray39468442014-09-02 15:17:15 +01002364 // The environment associated with this instruction. Not null if the instruction
2365 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002366 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002367
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002368 // Set by the code generator.
2369 LocationSummary* locations_;
2370
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002371 // Set by the liveness analysis.
2372 LiveInterval* live_interval_;
2373
2374 // Set by the liveness analysis, this is the position in a linear
2375 // order of blocks where this instruction's live interval start.
2376 size_t lifetime_position_;
2377
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002378 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002379
Vladimir Markoa1de9182016-02-25 11:37:38 +00002380 // The reference handle part of the reference type info.
2381 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002382 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002383 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002384
David Brazdil1abb4192015-02-17 18:33:36 +00002385 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002386 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002387 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002388 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002389 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002390
2391 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2392};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002393std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002394
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002395// Iterates over the instructions, while preserving the next instruction
2396// in case the current instruction gets removed from the list by the user
2397// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002398class HInstructionIterator : public ValueObject {
2399 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002400 explicit HInstructionIterator(const HInstructionList& instructions)
2401 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002402 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002403 }
2404
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002405 bool Done() const { return instruction_ == nullptr; }
2406 HInstruction* Current() const { return instruction_; }
2407 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002408 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002409 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002410 }
2411
2412 private:
2413 HInstruction* instruction_;
2414 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002415
2416 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002417};
2418
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002419// Iterates over the instructions without saving the next instruction,
2420// therefore handling changes in the graph potentially made by the user
2421// of this iterator.
2422class HInstructionIteratorHandleChanges : public ValueObject {
2423 public:
2424 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2425 : instruction_(instructions.first_instruction_) {
2426 }
2427
2428 bool Done() const { return instruction_ == nullptr; }
2429 HInstruction* Current() const { return instruction_; }
2430 void Advance() {
2431 instruction_ = instruction_->GetNext();
2432 }
2433
2434 private:
2435 HInstruction* instruction_;
2436
2437 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2438};
2439
2440
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002441class HBackwardInstructionIterator : public ValueObject {
2442 public:
2443 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2444 : instruction_(instructions.last_instruction_) {
2445 next_ = Done() ? nullptr : instruction_->GetPrevious();
2446 }
2447
2448 bool Done() const { return instruction_ == nullptr; }
2449 HInstruction* Current() const { return instruction_; }
2450 void Advance() {
2451 instruction_ = next_;
2452 next_ = Done() ? nullptr : instruction_->GetPrevious();
2453 }
2454
2455 private:
2456 HInstruction* instruction_;
2457 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002458
2459 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002460};
2461
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002462class HVariableInputSizeInstruction : public HInstruction {
2463 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002464 using HInstruction::GetInputRecords; // Keep the const version visible.
2465 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
2466 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2467 }
2468
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002469 void AddInput(HInstruction* input);
2470 void InsertInputAt(size_t index, HInstruction* input);
2471 void RemoveInputAt(size_t index);
2472
Igor Murashkind01745e2017-04-05 16:40:31 -07002473 // Removes all the inputs.
2474 // Also removes this instructions from each input's use list
2475 // (for non-environment uses only).
2476 void RemoveAllInputs();
2477
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002478 protected:
2479 HVariableInputSizeInstruction(SideEffects side_effects,
2480 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002481 ArenaAllocator* allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002482 size_t number_of_inputs,
2483 ArenaAllocKind kind)
2484 : HInstruction(side_effects, dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002485 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002486
2487 ArenaVector<HUserRecord<HInstruction*>> inputs_;
2488
2489 private:
2490 DISALLOW_COPY_AND_ASSIGN(HVariableInputSizeInstruction);
2491};
2492
Vladimir Markof9f64412015-09-02 14:05:49 +01002493template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002494class HTemplateInstruction: public HInstruction {
2495 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002496 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002497 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002498 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002499
Vladimir Marko372f10e2016-05-17 16:30:10 +01002500 using HInstruction::GetInputRecords; // Keep the const version visible.
2501 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2502 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002503 }
2504
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002505 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002506 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002507
2508 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002509};
2510
Vladimir Markof9f64412015-09-02 14:05:49 +01002511// HTemplateInstruction specialization for N=0.
2512template<>
2513class HTemplateInstruction<0>: public HInstruction {
2514 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002515 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002516 : HInstruction(side_effects, dex_pc) {}
2517
Vladimir Markof9f64412015-09-02 14:05:49 +01002518 virtual ~HTemplateInstruction() {}
2519
Vladimir Marko372f10e2016-05-17 16:30:10 +01002520 using HInstruction::GetInputRecords; // Keep the const version visible.
2521 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2522 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002523 }
2524
2525 private:
2526 friend class SsaBuilder;
2527};
2528
Dave Allison20dfc792014-06-16 20:44:29 -07002529template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002530class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002531 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002532 HExpression<N>(DataType::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002533 : HTemplateInstruction<N>(side_effects, dex_pc) {
2534 this->template SetPackedField<TypeField>(type);
2535 }
Dave Allison20dfc792014-06-16 20:44:29 -07002536 virtual ~HExpression() {}
2537
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002538 DataType::Type GetType() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00002539 return TypeField::Decode(this->GetPackedFields());
2540 }
Dave Allison20dfc792014-06-16 20:44:29 -07002541
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002542 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002543 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2544 static constexpr size_t kFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002545 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00002546 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2547 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2548 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002549 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002550};
2551
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002552// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2553// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002554class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002555 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002556 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2557 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002558
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002559 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002560
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002561 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002562
2563 private:
2564 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2565};
2566
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002567// Represents dex's RETURN opcodes. A HReturn is a control flow
2568// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002569class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002570 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002571 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2572 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002573 SetRawInputAt(0, value);
2574 }
2575
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002576 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002577
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002578 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002579
2580 private:
2581 DISALLOW_COPY_AND_ASSIGN(HReturn);
2582};
2583
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002584class HPhi FINAL : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002585 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002586 HPhi(ArenaAllocator* allocator,
David Brazdildee58d62016-04-07 09:54:26 +00002587 uint32_t reg_number,
2588 size_t number_of_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002589 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +00002590 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002591 : HVariableInputSizeInstruction(
2592 SideEffects::None(),
2593 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002594 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002595 number_of_inputs,
2596 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002597 reg_number_(reg_number) {
2598 SetPackedField<TypeField>(ToPhiType(type));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002599 DCHECK_NE(GetType(), DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +00002600 // Phis are constructed live and marked dead if conflicting or unused.
2601 // Individual steps of SsaBuilder should assume that if a phi has been
2602 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2603 SetPackedFlag<kFlagIsLive>(true);
2604 SetPackedFlag<kFlagCanBeNull>(true);
2605 }
2606
2607 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002608 static DataType::Type ToPhiType(DataType::Type type) {
2609 return DataType::Kind(type);
David Brazdildee58d62016-04-07 09:54:26 +00002610 }
2611
2612 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2613
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002614 DataType::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2615 void SetType(DataType::Type new_type) {
David Brazdildee58d62016-04-07 09:54:26 +00002616 // Make sure that only valid type changes occur. The following are allowed:
2617 // (1) int -> float/ref (primitive type propagation),
2618 // (2) long -> double (primitive type propagation).
2619 DCHECK(GetType() == new_type ||
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002620 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kFloat32) ||
2621 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kReference) ||
2622 (GetType() == DataType::Type::kInt64 && new_type == DataType::Type::kFloat64));
David Brazdildee58d62016-04-07 09:54:26 +00002623 SetPackedField<TypeField>(new_type);
2624 }
2625
2626 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2627 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2628
2629 uint32_t GetRegNumber() const { return reg_number_; }
2630
2631 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2632 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2633 bool IsDead() const { return !IsLive(); }
2634 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2635
Vladimir Markoe9004912016-06-16 16:50:52 +01002636 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002637 return other != nullptr
2638 && other->IsPhi()
2639 && other->AsPhi()->GetBlock() == GetBlock()
2640 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2641 }
2642
Nicolas Geoffrayf57c1ae2017-06-28 17:40:18 +01002643 bool HasEquivalentPhi() const {
2644 if (GetPrevious() != nullptr && GetPrevious()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2645 return true;
2646 }
2647 if (GetNext() != nullptr && GetNext()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2648 return true;
2649 }
2650 return false;
2651 }
2652
David Brazdildee58d62016-04-07 09:54:26 +00002653 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2654 // An equivalent phi is a phi having the same dex register and type.
2655 // It assumes that phis with the same dex register are adjacent.
2656 HPhi* GetNextEquivalentPhiWithSameType() {
2657 HInstruction* next = GetNext();
2658 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2659 if (next->GetType() == GetType()) {
2660 return next->AsPhi();
2661 }
2662 next = next->GetNext();
2663 }
2664 return nullptr;
2665 }
2666
2667 DECLARE_INSTRUCTION(Phi);
2668
David Brazdildee58d62016-04-07 09:54:26 +00002669 private:
2670 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2671 static constexpr size_t kFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002672 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
David Brazdildee58d62016-04-07 09:54:26 +00002673 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2674 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2675 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2676 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002677 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
David Brazdildee58d62016-04-07 09:54:26 +00002678
David Brazdildee58d62016-04-07 09:54:26 +00002679 const uint32_t reg_number_;
2680
2681 DISALLOW_COPY_AND_ASSIGN(HPhi);
2682};
2683
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002684// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002685// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002686// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002687class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002688 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002689 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002690
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002691 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002692
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002693 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002694
2695 private:
2696 DISALLOW_COPY_AND_ASSIGN(HExit);
2697};
2698
2699// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002700class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002701 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002702 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002703
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002704 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002705
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002706 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002707 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002708 }
2709
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002710 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002711
2712 private:
2713 DISALLOW_COPY_AND_ASSIGN(HGoto);
2714};
2715
Roland Levillain9867bc72015-08-05 10:21:34 +01002716class HConstant : public HExpression<0> {
2717 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002718 explicit HConstant(DataType::Type type, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002719 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002720
2721 bool CanBeMoved() const OVERRIDE { return true; }
2722
Roland Levillain1a653882016-03-18 18:05:57 +00002723 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002724 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002725 // Is this constant 0 in the arithmetic sense?
2726 virtual bool IsArithmeticZero() const { return false; }
2727 // Is this constant a 0-bit pattern?
2728 virtual bool IsZeroBitPattern() const { return false; }
2729 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002730 virtual bool IsOne() const { return false; }
2731
David Brazdil77a48ae2015-09-15 12:34:04 +00002732 virtual uint64_t GetValueAsUint64() const = 0;
2733
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002734 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002735
2736 private:
2737 DISALLOW_COPY_AND_ASSIGN(HConstant);
2738};
2739
Vladimir Markofcb503c2016-05-18 12:48:17 +01002740class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002741 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002742 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002743 return true;
2744 }
2745
David Brazdil77a48ae2015-09-15 12:34:04 +00002746 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2747
Roland Levillain9867bc72015-08-05 10:21:34 +01002748 size_t ComputeHashCode() const OVERRIDE { return 0; }
2749
Roland Levillain1a653882016-03-18 18:05:57 +00002750 // The null constant representation is a 0-bit pattern.
2751 virtual bool IsZeroBitPattern() const { return true; }
2752
Roland Levillain9867bc72015-08-05 10:21:34 +01002753 DECLARE_INSTRUCTION(NullConstant);
2754
2755 private:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002756 explicit HNullConstant(uint32_t dex_pc = kNoDexPc)
2757 : HConstant(DataType::Type::kReference, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002758
2759 friend class HGraph;
2760 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2761};
2762
2763// Constants of the type int. Those can be from Dex instructions, or
2764// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002765class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002766 public:
2767 int32_t GetValue() const { return value_; }
2768
David Brazdil9f389d42015-10-01 14:32:56 +01002769 uint64_t GetValueAsUint64() const OVERRIDE {
2770 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2771 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002772
Vladimir Marko372f10e2016-05-17 16:30:10 +01002773 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002774 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002775 return other->AsIntConstant()->value_ == value_;
2776 }
2777
2778 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2779
2780 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002781 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2782 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002783 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2784
Roland Levillain1a653882016-03-18 18:05:57 +00002785 // Integer constants are used to encode Boolean values as well,
2786 // where 1 means true and 0 means false.
2787 bool IsTrue() const { return GetValue() == 1; }
2788 bool IsFalse() const { return GetValue() == 0; }
2789
Roland Levillain9867bc72015-08-05 10:21:34 +01002790 DECLARE_INSTRUCTION(IntConstant);
2791
2792 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002793 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002794 : HConstant(DataType::Type::kInt32, dex_pc), value_(value) {}
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002795 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002796 : HConstant(DataType::Type::kInt32, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002797
2798 const int32_t value_;
2799
2800 friend class HGraph;
2801 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2802 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2803 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2804};
2805
Vladimir Markofcb503c2016-05-18 12:48:17 +01002806class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002807 public:
2808 int64_t GetValue() const { return value_; }
2809
David Brazdil77a48ae2015-09-15 12:34:04 +00002810 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2811
Vladimir Marko372f10e2016-05-17 16:30:10 +01002812 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002813 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002814 return other->AsLongConstant()->value_ == value_;
2815 }
2816
2817 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2818
2819 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002820 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2821 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002822 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2823
2824 DECLARE_INSTRUCTION(LongConstant);
2825
2826 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002827 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002828 : HConstant(DataType::Type::kInt64, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002829
2830 const int64_t value_;
2831
2832 friend class HGraph;
2833 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2834};
Dave Allison20dfc792014-06-16 20:44:29 -07002835
Vladimir Markofcb503c2016-05-18 12:48:17 +01002836class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002837 public:
2838 float GetValue() const { return value_; }
2839
2840 uint64_t GetValueAsUint64() const OVERRIDE {
2841 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2842 }
2843
Vladimir Marko372f10e2016-05-17 16:30:10 +01002844 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002845 DCHECK(other->IsFloatConstant()) << other->DebugName();
2846 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2847 }
2848
2849 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2850
2851 bool IsMinusOne() const OVERRIDE {
2852 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2853 }
Roland Levillain1a653882016-03-18 18:05:57 +00002854 bool IsArithmeticZero() const OVERRIDE {
2855 return std::fpclassify(value_) == FP_ZERO;
2856 }
2857 bool IsArithmeticPositiveZero() const {
2858 return IsArithmeticZero() && !std::signbit(value_);
2859 }
2860 bool IsArithmeticNegativeZero() const {
2861 return IsArithmeticZero() && std::signbit(value_);
2862 }
2863 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002864 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002865 }
2866 bool IsOne() const OVERRIDE {
2867 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2868 }
2869 bool IsNaN() const {
2870 return std::isnan(value_);
2871 }
2872
2873 DECLARE_INSTRUCTION(FloatConstant);
2874
2875 private:
2876 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002877 : HConstant(DataType::Type::kFloat32, dex_pc), value_(value) {}
Roland Levillain31dd3d62016-02-16 12:21:02 +00002878 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002879 : HConstant(DataType::Type::kFloat32, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
Roland Levillain31dd3d62016-02-16 12:21:02 +00002880
2881 const float value_;
2882
2883 // Only the SsaBuilder and HGraph can create floating-point constants.
2884 friend class SsaBuilder;
2885 friend class HGraph;
2886 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2887};
2888
Vladimir Markofcb503c2016-05-18 12:48:17 +01002889class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002890 public:
2891 double GetValue() const { return value_; }
2892
2893 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2894
Vladimir Marko372f10e2016-05-17 16:30:10 +01002895 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002896 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2897 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2898 }
2899
2900 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2901
2902 bool IsMinusOne() const OVERRIDE {
2903 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2904 }
Roland Levillain1a653882016-03-18 18:05:57 +00002905 bool IsArithmeticZero() const OVERRIDE {
2906 return std::fpclassify(value_) == FP_ZERO;
2907 }
2908 bool IsArithmeticPositiveZero() const {
2909 return IsArithmeticZero() && !std::signbit(value_);
2910 }
2911 bool IsArithmeticNegativeZero() const {
2912 return IsArithmeticZero() && std::signbit(value_);
2913 }
2914 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002915 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002916 }
2917 bool IsOne() const OVERRIDE {
2918 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2919 }
2920 bool IsNaN() const {
2921 return std::isnan(value_);
2922 }
2923
2924 DECLARE_INSTRUCTION(DoubleConstant);
2925
2926 private:
2927 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002928 : HConstant(DataType::Type::kFloat64, dex_pc), value_(value) {}
Roland Levillain31dd3d62016-02-16 12:21:02 +00002929 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002930 : HConstant(DataType::Type::kFloat64, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
Roland Levillain31dd3d62016-02-16 12:21:02 +00002931
2932 const double value_;
2933
2934 // Only the SsaBuilder and HGraph can create floating-point constants.
2935 friend class SsaBuilder;
2936 friend class HGraph;
2937 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2938};
2939
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002940// Conditional branch. A block ending with an HIf instruction must have
2941// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002942class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002943 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002944 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2945 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002946 SetRawInputAt(0, input);
2947 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002948
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002949 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002950
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002951 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002952 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002953 }
2954
2955 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002956 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002957 }
2958
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002959 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002960
2961 private:
2962 DISALLOW_COPY_AND_ASSIGN(HIf);
2963};
2964
David Brazdilfc6a86a2015-06-26 10:33:45 +00002965
2966// Abstract instruction which marks the beginning and/or end of a try block and
2967// links it to the respective exception handlers. Behaves the same as a Goto in
2968// non-exceptional control flow.
2969// Normal-flow successor is stored at index zero, exception handlers under
2970// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002971class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002972 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002973 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002974 kEntry,
2975 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002976 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002977 };
2978
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002979 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002980 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2981 SetPackedField<BoundaryKindField>(kind);
2982 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002983
2984 bool IsControlFlow() const OVERRIDE { return true; }
2985
2986 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002987 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002988
David Brazdild26a4112015-11-10 11:07:31 +00002989 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2990 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2991 }
2992
David Brazdilfc6a86a2015-06-26 10:33:45 +00002993 // Returns whether `handler` is among its exception handlers (non-zero index
2994 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002995 bool HasExceptionHandler(const HBasicBlock& handler) const {
2996 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002997 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002998 }
2999
3000 // If not present already, adds `handler` to its block's list of exception
3001 // handlers.
3002 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01003003 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003004 GetBlock()->AddSuccessor(handler);
3005 }
3006 }
3007
Vladimir Markoa1de9182016-02-25 11:37:38 +00003008 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
3009 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003010
David Brazdilffee3d32015-07-06 11:48:53 +01003011 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
3012
David Brazdilfc6a86a2015-06-26 10:33:45 +00003013 DECLARE_INSTRUCTION(TryBoundary);
3014
3015 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003016 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
3017 static constexpr size_t kFieldBoundaryKindSize =
3018 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
3019 static constexpr size_t kNumberOfTryBoundaryPackedBits =
3020 kFieldBoundaryKind + kFieldBoundaryKindSize;
3021 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
3022 "Too many packed fields.");
3023 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00003024
3025 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
3026};
3027
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003028// Deoptimize to interpreter, upon checking a condition.
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003029class HDeoptimize FINAL : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003030 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003031 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
3032 // across.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003033 HDeoptimize(ArenaAllocator* allocator,
3034 HInstruction* cond,
3035 DeoptimizationKind kind,
3036 uint32_t dex_pc)
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003037 : HVariableInputSizeInstruction(
3038 SideEffects::All(),
3039 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003040 allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003041 /* number_of_inputs */ 1,
3042 kArenaAllocMisc) {
3043 SetPackedFlag<kFieldCanBeMoved>(false);
3044 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003045 SetRawInputAt(0, cond);
3046 }
3047
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003048 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
3049 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
3050 // instead of `guard`.
3051 // We set CanTriggerGC to prevent any intermediate address to be live
3052 // at the point of the `HDeoptimize`.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003053 HDeoptimize(ArenaAllocator* allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003054 HInstruction* cond,
3055 HInstruction* guard,
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003056 DeoptimizationKind kind,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003057 uint32_t dex_pc)
3058 : HVariableInputSizeInstruction(
3059 SideEffects::CanTriggerGC(),
3060 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003061 allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003062 /* number_of_inputs */ 2,
3063 kArenaAllocMisc) {
3064 SetPackedFlag<kFieldCanBeMoved>(true);
3065 SetPackedField<DeoptimizeKindField>(kind);
3066 SetRawInputAt(0, cond);
3067 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003068 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003069
3070 bool CanBeMoved() const OVERRIDE { return GetPackedFlag<kFieldCanBeMoved>(); }
3071
3072 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
3073 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
3074 }
3075
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003076 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003077
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003078 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003079
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003080 DeoptimizationKind GetDeoptimizationKind() const { return GetPackedField<DeoptimizeKindField>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003081
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003082 DataType::Type GetType() const OVERRIDE {
3083 return GuardsAnInput() ? GuardedInput()->GetType() : DataType::Type::kVoid;
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003084 }
3085
3086 bool GuardsAnInput() const {
3087 return InputCount() == 2;
3088 }
3089
3090 HInstruction* GuardedInput() const {
3091 DCHECK(GuardsAnInput());
3092 return InputAt(1);
3093 }
3094
3095 void RemoveGuard() {
3096 RemoveInputAt(1);
3097 }
3098
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003099 DECLARE_INSTRUCTION(Deoptimize);
3100
3101 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003102 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3103 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3104 static constexpr size_t kFieldDeoptimizeKindSize =
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003105 MinimumBitsToStore(static_cast<size_t>(DeoptimizationKind::kLast));
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003106 static constexpr size_t kNumberOfDeoptimizePackedBits =
3107 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3108 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3109 "Too many packed fields.");
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003110 using DeoptimizeKindField =
3111 BitField<DeoptimizationKind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003112
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003113 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
3114};
3115
Mingyao Yang063fc772016-08-02 11:02:54 -07003116// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3117// The compiled code checks this flag value in a guard before devirtualized call and
3118// if it's true, starts to do deoptimization.
3119// It has a 4-byte slot on stack.
3120// TODO: allocate a register for this flag.
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003121class HShouldDeoptimizeFlag FINAL : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003122 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003123 // CHA guards are only optimized in a separate pass and it has no side effects
3124 // with regard to other passes.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003125 HShouldDeoptimizeFlag(ArenaAllocator* allocator, uint32_t dex_pc)
3126 : HVariableInputSizeInstruction(SideEffects::None(), dex_pc, allocator, 0, kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003127 }
3128
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003129 DataType::Type GetType() const OVERRIDE { return DataType::Type::kInt32; }
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003130
3131 // We do all CHA guard elimination/motion in a single pass, after which there is no
3132 // further guard elimination/motion since a guard might have been used for justification
3133 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3134 // to avoid other optimizations trying to move it.
Mingyao Yang063fc772016-08-02 11:02:54 -07003135 bool CanBeMoved() const OVERRIDE { return false; }
3136
3137 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3138
3139 private:
3140 DISALLOW_COPY_AND_ASSIGN(HShouldDeoptimizeFlag);
3141};
3142
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003143// Represents the ArtMethod that was passed as a first argument to
3144// the method. It is used by instructions that depend on it, like
3145// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003146class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003147 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003148 explicit HCurrentMethod(DataType::Type type, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003149 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003150
3151 DECLARE_INSTRUCTION(CurrentMethod);
3152
3153 private:
3154 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
3155};
3156
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003157// Fetches an ArtMethod from the virtual table or the interface method table
3158// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003159class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003160 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003161 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003162 kVTable,
3163 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003164 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003165 };
3166 HClassTableGet(HInstruction* cls,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003167 DataType::Type type,
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003168 TableKind kind,
3169 size_t index,
3170 uint32_t dex_pc)
3171 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003172 index_(index) {
3173 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003174 SetRawInputAt(0, cls);
3175 }
3176
3177 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003178 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003179 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003180 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003181 }
3182
Vladimir Markoa1de9182016-02-25 11:37:38 +00003183 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003184 size_t GetIndex() const { return index_; }
3185
3186 DECLARE_INSTRUCTION(ClassTableGet);
3187
3188 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003189 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
3190 static constexpr size_t kFieldTableKindSize =
3191 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3192 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3193 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3194 "Too many packed fields.");
3195 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3196
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003197 // The index of the ArtMethod in the table.
3198 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003199
3200 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
3201};
3202
Mark Mendellfe57faa2015-09-18 09:26:15 -04003203// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3204// have one successor for each entry in the switch table, and the final successor
3205// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003206class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003207 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003208 HPackedSwitch(int32_t start_value,
3209 uint32_t num_entries,
3210 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003211 uint32_t dex_pc = kNoDexPc)
3212 : HTemplateInstruction(SideEffects::None(), dex_pc),
3213 start_value_(start_value),
3214 num_entries_(num_entries) {
3215 SetRawInputAt(0, input);
3216 }
3217
3218 bool IsControlFlow() const OVERRIDE { return true; }
3219
3220 int32_t GetStartValue() const { return start_value_; }
3221
Vladimir Marko211c2112015-09-24 16:52:33 +01003222 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003223
3224 HBasicBlock* GetDefaultBlock() const {
3225 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003226 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003227 }
3228 DECLARE_INSTRUCTION(PackedSwitch);
3229
3230 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003231 const int32_t start_value_;
3232 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003233
3234 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
3235};
3236
Roland Levillain88cb1752014-10-20 16:36:47 +01003237class HUnaryOperation : public HExpression<1> {
3238 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003239 HUnaryOperation(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003240 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003241 SetRawInputAt(0, input);
3242 }
3243
3244 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003245 DataType::Type GetResultType() const { return GetType(); }
Roland Levillain88cb1752014-10-20 16:36:47 +01003246
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003247 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003248 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003249 return true;
3250 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003251
Roland Levillain31dd3d62016-02-16 12:21:02 +00003252 // Try to statically evaluate `this` and return a HConstant
3253 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003254 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003255 HConstant* TryStaticEvaluation() const;
3256
3257 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003258 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3259 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003260 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3261 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003262
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003263 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003264
3265 private:
3266 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
3267};
3268
Dave Allison20dfc792014-06-16 20:44:29 -07003269class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003270 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003271 HBinaryOperation(DataType::Type result_type,
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003272 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003273 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003274 SideEffects side_effects = SideEffects::None(),
3275 uint32_t dex_pc = kNoDexPc)
3276 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003277 SetRawInputAt(0, left);
3278 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003279 }
3280
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003281 HInstruction* GetLeft() const { return InputAt(0); }
3282 HInstruction* GetRight() const { return InputAt(1); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003283 DataType::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003284
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003285 virtual bool IsCommutative() const { return false; }
3286
3287 // Put constant on the right.
3288 // Returns whether order is changed.
3289 bool OrderInputsWithConstantOnTheRight() {
3290 HInstruction* left = InputAt(0);
3291 HInstruction* right = InputAt(1);
3292 if (left->IsConstant() && !right->IsConstant()) {
3293 ReplaceInput(right, 0);
3294 ReplaceInput(left, 1);
3295 return true;
3296 }
3297 return false;
3298 }
3299
3300 // Order inputs by instruction id, but favor constant on the right side.
3301 // This helps GVN for commutative ops.
3302 void OrderInputs() {
3303 DCHECK(IsCommutative());
3304 HInstruction* left = InputAt(0);
3305 HInstruction* right = InputAt(1);
3306 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3307 return;
3308 }
3309 if (OrderInputsWithConstantOnTheRight()) {
3310 return;
3311 }
3312 // Order according to instruction id.
3313 if (left->GetId() > right->GetId()) {
3314 ReplaceInput(right, 0);
3315 ReplaceInput(left, 1);
3316 }
3317 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003318
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003319 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003320 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003321 return true;
3322 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003323
Roland Levillain31dd3d62016-02-16 12:21:02 +00003324 // Try to statically evaluate `this` and return a HConstant
3325 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003326 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003327 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003328
3329 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003330 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3331 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003332 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3333 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003334 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003335 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3336 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003337 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3338 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003339 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3340 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003341 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003342 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3343 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003344
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003345 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003346 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003347 HConstant* GetConstantRight() const;
3348
3349 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003350 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003351 HInstruction* GetLeastConstantLeft() const;
3352
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003353 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003354
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003355 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003356 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
3357};
3358
Mark Mendellc4701932015-04-10 13:18:51 -04003359// The comparison bias applies for floating point operations and indicates how NaN
3360// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003361enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003362 kNoBias, // bias is not applicable (i.e. for long operation)
3363 kGtBias, // return 1 for NaN comparisons
3364 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003365 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003366};
3367
Roland Levillain31dd3d62016-02-16 12:21:02 +00003368std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3369
Dave Allison20dfc792014-06-16 20:44:29 -07003370class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003371 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003372 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003373 : HBinaryOperation(DataType::Type::kBool, first, second, SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003374 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3375 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003376
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003377 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003378 // `instruction`, and disregard moves in between.
3379 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003380
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003381 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003382
3383 virtual IfCondition GetCondition() const = 0;
3384
Mark Mendellc4701932015-04-10 13:18:51 -04003385 virtual IfCondition GetOppositeCondition() const = 0;
3386
Vladimir Markoa1de9182016-02-25 11:37:38 +00003387 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003388 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3389
Vladimir Markoa1de9182016-02-25 11:37:38 +00003390 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3391 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003392
Vladimir Marko372f10e2016-05-17 16:30:10 +01003393 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003394 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003395 }
3396
Roland Levillain4fa13f62015-07-06 18:11:54 +01003397 bool IsFPConditionTrueIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003398 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003399 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003400 if (if_cond == kCondNE) {
3401 return true;
3402 } else if (if_cond == kCondEQ) {
3403 return false;
3404 }
3405 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003406 }
3407
3408 bool IsFPConditionFalseIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003409 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003410 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003411 if (if_cond == kCondEQ) {
3412 return true;
3413 } else if (if_cond == kCondNE) {
3414 return false;
3415 }
3416 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003417 }
3418
Roland Levillain31dd3d62016-02-16 12:21:02 +00003419 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003420 // Needed if we merge a HCompare into a HCondition.
3421 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3422 static constexpr size_t kFieldComparisonBiasSize =
3423 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3424 static constexpr size_t kNumberOfConditionPackedBits =
3425 kFieldComparisonBias + kFieldComparisonBiasSize;
3426 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3427 using ComparisonBiasField =
3428 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3429
Roland Levillain31dd3d62016-02-16 12:21:02 +00003430 template <typename T>
3431 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3432
3433 template <typename T>
3434 int32_t CompareFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003435 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003436 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3437 // Handle the bias.
3438 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3439 }
3440
3441 // Return an integer constant containing the result of a condition evaluated at compile time.
3442 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3443 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3444 }
3445
Dave Allison20dfc792014-06-16 20:44:29 -07003446 private:
3447 DISALLOW_COPY_AND_ASSIGN(HCondition);
3448};
3449
3450// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003451class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003452 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003453 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3454 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003455
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003456 bool IsCommutative() const OVERRIDE { return true; }
3457
Vladimir Marko9e23df52015-11-10 17:14:35 +00003458 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3459 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003460 return MakeConstantCondition(true, GetDexPc());
3461 }
3462 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3463 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3464 }
3465 // In the following Evaluate methods, a HCompare instruction has
3466 // been merged into this HEqual instruction; evaluate it as
3467 // `Compare(x, y) == 0`.
3468 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3469 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3470 GetDexPc());
3471 }
3472 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3473 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3474 }
3475 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3476 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003477 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003478
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003479 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003480
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003481 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003482 return kCondEQ;
3483 }
3484
Mark Mendellc4701932015-04-10 13:18:51 -04003485 IfCondition GetOppositeCondition() const OVERRIDE {
3486 return kCondNE;
3487 }
3488
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003489 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003490 template <typename T> static bool Compute(T x, T y) { return x == y; }
Aart Bike9f37602015-10-09 11:15:55 -07003491
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003492 DISALLOW_COPY_AND_ASSIGN(HEqual);
3493};
3494
Vladimir Markofcb503c2016-05-18 12:48:17 +01003495class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003496 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003497 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3498 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003499
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003500 bool IsCommutative() const OVERRIDE { return true; }
3501
Vladimir Marko9e23df52015-11-10 17:14:35 +00003502 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3503 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003504 return MakeConstantCondition(false, GetDexPc());
3505 }
3506 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3507 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3508 }
3509 // In the following Evaluate methods, a HCompare instruction has
3510 // been merged into this HNotEqual instruction; evaluate it as
3511 // `Compare(x, y) != 0`.
3512 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3513 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3514 }
3515 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3516 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3517 }
3518 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3519 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003520 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003521
Dave Allison20dfc792014-06-16 20:44:29 -07003522 DECLARE_INSTRUCTION(NotEqual);
3523
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003524 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003525 return kCondNE;
3526 }
3527
Mark Mendellc4701932015-04-10 13:18:51 -04003528 IfCondition GetOppositeCondition() const OVERRIDE {
3529 return kCondEQ;
3530 }
3531
Dave Allison20dfc792014-06-16 20:44:29 -07003532 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003533 template <typename T> static bool Compute(T x, T y) { return x != y; }
Aart Bike9f37602015-10-09 11:15:55 -07003534
Dave Allison20dfc792014-06-16 20:44:29 -07003535 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3536};
3537
Vladimir Markofcb503c2016-05-18 12:48:17 +01003538class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003539 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003540 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3541 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003542
Roland Levillain9867bc72015-08-05 10:21:34 +01003543 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003544 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003545 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003546 // In the following Evaluate methods, a HCompare instruction has
3547 // been merged into this HLessThan instruction; evaluate it as
3548 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003549 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003550 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3551 }
3552 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3553 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3554 }
3555 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3556 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003557 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003558
Dave Allison20dfc792014-06-16 20:44:29 -07003559 DECLARE_INSTRUCTION(LessThan);
3560
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003561 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003562 return kCondLT;
3563 }
3564
Mark Mendellc4701932015-04-10 13:18:51 -04003565 IfCondition GetOppositeCondition() const OVERRIDE {
3566 return kCondGE;
3567 }
3568
Dave Allison20dfc792014-06-16 20:44:29 -07003569 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003570 template <typename T> static bool Compute(T x, T y) { return x < y; }
Aart Bike9f37602015-10-09 11:15:55 -07003571
Dave Allison20dfc792014-06-16 20:44:29 -07003572 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3573};
3574
Vladimir Markofcb503c2016-05-18 12:48:17 +01003575class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003576 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003577 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3578 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003579
Roland Levillain9867bc72015-08-05 10:21:34 +01003580 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003581 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003582 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003583 // In the following Evaluate methods, a HCompare instruction has
3584 // been merged into this HLessThanOrEqual instruction; evaluate it as
3585 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003586 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003587 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3588 }
3589 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3590 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3591 }
3592 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3593 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003594 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003595
Dave Allison20dfc792014-06-16 20:44:29 -07003596 DECLARE_INSTRUCTION(LessThanOrEqual);
3597
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003598 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003599 return kCondLE;
3600 }
3601
Mark Mendellc4701932015-04-10 13:18:51 -04003602 IfCondition GetOppositeCondition() const OVERRIDE {
3603 return kCondGT;
3604 }
3605
Dave Allison20dfc792014-06-16 20:44:29 -07003606 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003607 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003608
Dave Allison20dfc792014-06-16 20:44:29 -07003609 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3610};
3611
Vladimir Markofcb503c2016-05-18 12:48:17 +01003612class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003613 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003614 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3615 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003616
Roland Levillain9867bc72015-08-05 10:21:34 +01003617 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003618 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003619 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003620 // In the following Evaluate methods, a HCompare instruction has
3621 // been merged into this HGreaterThan instruction; evaluate it as
3622 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003623 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003624 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3625 }
3626 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3627 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3628 }
3629 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3630 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003631 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003632
Dave Allison20dfc792014-06-16 20:44:29 -07003633 DECLARE_INSTRUCTION(GreaterThan);
3634
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003635 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003636 return kCondGT;
3637 }
3638
Mark Mendellc4701932015-04-10 13:18:51 -04003639 IfCondition GetOppositeCondition() const OVERRIDE {
3640 return kCondLE;
3641 }
3642
Dave Allison20dfc792014-06-16 20:44:29 -07003643 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003644 template <typename T> static bool Compute(T x, T y) { return x > y; }
Aart Bike9f37602015-10-09 11:15:55 -07003645
Dave Allison20dfc792014-06-16 20:44:29 -07003646 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3647};
3648
Vladimir Markofcb503c2016-05-18 12:48:17 +01003649class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003650 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003651 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3652 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003653
Roland Levillain9867bc72015-08-05 10:21:34 +01003654 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003655 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003656 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003657 // In the following Evaluate methods, a HCompare instruction has
3658 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3659 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003660 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003661 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3662 }
3663 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3664 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3665 }
3666 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3667 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003668 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003669
Dave Allison20dfc792014-06-16 20:44:29 -07003670 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3671
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003672 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003673 return kCondGE;
3674 }
3675
Mark Mendellc4701932015-04-10 13:18:51 -04003676 IfCondition GetOppositeCondition() const OVERRIDE {
3677 return kCondLT;
3678 }
3679
Dave Allison20dfc792014-06-16 20:44:29 -07003680 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003681 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003682
Dave Allison20dfc792014-06-16 20:44:29 -07003683 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3684};
3685
Vladimir Markofcb503c2016-05-18 12:48:17 +01003686class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003687 public:
3688 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3689 : HCondition(first, second, dex_pc) {}
3690
3691 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003692 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003693 }
3694 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003695 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3696 }
3697 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3698 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3699 LOG(FATAL) << DebugName() << " is not defined for float values";
3700 UNREACHABLE();
3701 }
3702 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3703 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3704 LOG(FATAL) << DebugName() << " is not defined for double values";
3705 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003706 }
3707
3708 DECLARE_INSTRUCTION(Below);
3709
3710 IfCondition GetCondition() const OVERRIDE {
3711 return kCondB;
3712 }
3713
3714 IfCondition GetOppositeCondition() const OVERRIDE {
3715 return kCondAE;
3716 }
3717
3718 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003719 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003720 return MakeUnsigned(x) < MakeUnsigned(y);
3721 }
Aart Bike9f37602015-10-09 11:15:55 -07003722
3723 DISALLOW_COPY_AND_ASSIGN(HBelow);
3724};
3725
Vladimir Markofcb503c2016-05-18 12:48:17 +01003726class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003727 public:
3728 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3729 : HCondition(first, second, dex_pc) {}
3730
3731 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003732 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003733 }
3734 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003735 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3736 }
3737 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3738 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3739 LOG(FATAL) << DebugName() << " is not defined for float values";
3740 UNREACHABLE();
3741 }
3742 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3743 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3744 LOG(FATAL) << DebugName() << " is not defined for double values";
3745 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003746 }
3747
3748 DECLARE_INSTRUCTION(BelowOrEqual);
3749
3750 IfCondition GetCondition() const OVERRIDE {
3751 return kCondBE;
3752 }
3753
3754 IfCondition GetOppositeCondition() const OVERRIDE {
3755 return kCondA;
3756 }
3757
3758 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003759 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003760 return MakeUnsigned(x) <= MakeUnsigned(y);
3761 }
Aart Bike9f37602015-10-09 11:15:55 -07003762
3763 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3764};
3765
Vladimir Markofcb503c2016-05-18 12:48:17 +01003766class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003767 public:
3768 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3769 : HCondition(first, second, dex_pc) {}
3770
3771 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003772 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003773 }
3774 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003775 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3776 }
3777 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3778 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3779 LOG(FATAL) << DebugName() << " is not defined for float values";
3780 UNREACHABLE();
3781 }
3782 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3783 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3784 LOG(FATAL) << DebugName() << " is not defined for double values";
3785 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003786 }
3787
3788 DECLARE_INSTRUCTION(Above);
3789
3790 IfCondition GetCondition() const OVERRIDE {
3791 return kCondA;
3792 }
3793
3794 IfCondition GetOppositeCondition() const OVERRIDE {
3795 return kCondBE;
3796 }
3797
3798 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003799 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003800 return MakeUnsigned(x) > MakeUnsigned(y);
3801 }
Aart Bike9f37602015-10-09 11:15:55 -07003802
3803 DISALLOW_COPY_AND_ASSIGN(HAbove);
3804};
3805
Vladimir Markofcb503c2016-05-18 12:48:17 +01003806class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003807 public:
3808 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3809 : HCondition(first, second, dex_pc) {}
3810
3811 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003812 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003813 }
3814 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003815 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3816 }
3817 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3818 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3819 LOG(FATAL) << DebugName() << " is not defined for float values";
3820 UNREACHABLE();
3821 }
3822 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3823 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3824 LOG(FATAL) << DebugName() << " is not defined for double values";
3825 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003826 }
3827
3828 DECLARE_INSTRUCTION(AboveOrEqual);
3829
3830 IfCondition GetCondition() const OVERRIDE {
3831 return kCondAE;
3832 }
3833
3834 IfCondition GetOppositeCondition() const OVERRIDE {
3835 return kCondB;
3836 }
3837
3838 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003839 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003840 return MakeUnsigned(x) >= MakeUnsigned(y);
3841 }
Aart Bike9f37602015-10-09 11:15:55 -07003842
3843 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3844};
Dave Allison20dfc792014-06-16 20:44:29 -07003845
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003846// Instruction to check how two inputs compare to each other.
3847// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003848class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003849 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003850 // Note that `comparison_type` is the type of comparison performed
3851 // between the comparison's inputs, not the type of the instantiated
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003852 // HCompare instruction (which is always DataType::Type::kInt).
3853 HCompare(DataType::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003854 HInstruction* first,
3855 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003856 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003857 uint32_t dex_pc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003858 : HBinaryOperation(DataType::Type::kInt32,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003859 first,
3860 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003861 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003862 dex_pc) {
3863 SetPackedField<ComparisonBiasField>(bias);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003864 DCHECK_EQ(comparison_type, DataType::Kind(first->GetType()));
3865 DCHECK_EQ(comparison_type, DataType::Kind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003866 }
3867
Roland Levillain9867bc72015-08-05 10:21:34 +01003868 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003869 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3870
3871 template <typename T>
3872 int32_t ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003873 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003874 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3875 // Handle the bias.
3876 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3877 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003878
Roland Levillain9867bc72015-08-05 10:21:34 +01003879 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003880 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3881 // reach this code path when processing a freshly built HIR
3882 // graph. However HCompare integer instructions can be synthesized
3883 // by the instruction simplifier to implement IntegerCompare and
3884 // IntegerSignum intrinsics, so we have to handle this case.
3885 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003886 }
3887 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003888 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3889 }
3890 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3891 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3892 }
3893 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3894 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003895 }
3896
Vladimir Marko372f10e2016-05-17 16:30:10 +01003897 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003898 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003899 }
3900
Vladimir Markoa1de9182016-02-25 11:37:38 +00003901 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003902
Roland Levillain31dd3d62016-02-16 12:21:02 +00003903 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003904 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003905 bool IsGtBias() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003906 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003907 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003908 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003909
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003910 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type type ATTRIBUTE_UNUSED) {
Roland Levillain1693a1f2016-03-15 14:57:31 +00003911 // Comparisons do not require a runtime call in any back end.
3912 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003913 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003914
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003915 DECLARE_INSTRUCTION(Compare);
3916
Roland Levillain31dd3d62016-02-16 12:21:02 +00003917 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003918 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3919 static constexpr size_t kFieldComparisonBiasSize =
3920 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3921 static constexpr size_t kNumberOfComparePackedBits =
3922 kFieldComparisonBias + kFieldComparisonBiasSize;
3923 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3924 using ComparisonBiasField =
3925 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3926
Roland Levillain31dd3d62016-02-16 12:21:02 +00003927 // Return an integer constant containing the result of a comparison evaluated at compile time.
3928 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3929 DCHECK(value == -1 || value == 0 || value == 1) << value;
3930 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3931 }
3932
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003933 private:
3934 DISALLOW_COPY_AND_ASSIGN(HCompare);
3935};
3936
Nicolas Geoffray2b615ba2017-01-06 14:40:07 +00003937class HNewInstance FINAL : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00003938 public:
3939 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00003940 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08003941 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00003942 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00003943 bool finalizable,
3944 QuickEntrypointEnum entrypoint)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003945 : HExpression(DataType::Type::kReference, SideEffects::CanTriggerGC(), dex_pc),
David Brazdil6de19382016-01-08 17:37:10 +00003946 type_index_(type_index),
3947 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003948 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003949 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003950 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00003951 }
3952
Andreas Gampea5b09a62016-11-17 15:21:22 -08003953 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00003954 const DexFile& GetDexFile() const { return dex_file_; }
3955
3956 // Calls runtime so needs an environment.
3957 bool NeedsEnvironment() const OVERRIDE { return true; }
3958
Mingyao Yang062157f2016-03-02 10:15:36 -08003959 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
3960 bool CanThrow() const OVERRIDE { return true; }
3961
Nicolas Geoffray5247c082017-01-13 14:17:29 +00003962 bool NeedsChecks() const {
3963 return entrypoint_ == kQuickAllocObjectWithChecks;
3964 }
David Brazdil6de19382016-01-08 17:37:10 +00003965
Vladimir Markoa1de9182016-02-25 11:37:38 +00003966 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003967
3968 bool CanBeNull() const OVERRIDE { return false; }
3969
3970 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3971
3972 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3973 entrypoint_ = entrypoint;
3974 }
3975
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00003976 HLoadClass* GetLoadClass() const {
3977 HInstruction* input = InputAt(0);
3978 if (input->IsClinitCheck()) {
3979 input = input->InputAt(0);
3980 }
3981 DCHECK(input->IsLoadClass());
3982 return input->AsLoadClass();
3983 }
3984
David Brazdil6de19382016-01-08 17:37:10 +00003985 bool IsStringAlloc() const;
3986
3987 DECLARE_INSTRUCTION(NewInstance);
3988
3989 private:
Nicolas Geoffray5247c082017-01-13 14:17:29 +00003990 static constexpr size_t kFlagFinalizable = kNumberOfExpressionPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003991 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3992 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3993 "Too many packed fields.");
3994
Andreas Gampea5b09a62016-11-17 15:21:22 -08003995 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00003996 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003997 QuickEntrypointEnum entrypoint_;
3998
3999 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
4000};
4001
Agi Csaki05f20562015-08-19 14:58:14 -07004002enum IntrinsicNeedsEnvironmentOrCache {
4003 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
4004 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07004005};
4006
Aart Bik5d75afe2015-12-14 11:57:01 -08004007enum IntrinsicSideEffects {
4008 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
4009 kReadSideEffects, // Intrinsic may read heap memory.
4010 kWriteSideEffects, // Intrinsic may write heap memory.
4011 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
4012};
4013
4014enum IntrinsicExceptions {
4015 kNoThrow, // Intrinsic does not throw any exceptions.
4016 kCanThrow // Intrinsic may throw exceptions.
4017};
4018
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004019class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004020 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004021 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004022
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01004023 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004024 SetRawInputAt(index, argument);
4025 }
4026
Roland Levillain3e3d7332015-04-28 11:00:54 +01004027 // Return the number of arguments. This number can be lower than
4028 // the number of inputs returned by InputCount(), as some invoke
4029 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
4030 // inputs at the end of their list of inputs.
4031 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
4032
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004033 DataType::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004034
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004035 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4036
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004037 InvokeType GetInvokeType() const {
4038 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004039 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004040
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01004041 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004042 return intrinsic_;
4043 }
4044
Aart Bik5d75afe2015-12-14 11:57:01 -08004045 void SetIntrinsic(Intrinsics intrinsic,
4046 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
4047 IntrinsicSideEffects side_effects,
4048 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004049
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004050 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00004051 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004052 }
4053
Aart Bikff7d89c2016-11-07 08:49:28 -08004054 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4055
Vladimir Markoa1de9182016-02-25 11:37:38 +00004056 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004057
Aart Bik71bf7b42016-11-16 10:17:46 -08004058 bool CanBeMoved() const OVERRIDE { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004059
Vladimir Marko372f10e2016-05-17 16:30:10 +01004060 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08004061 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4062 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004063
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004064 uint32_t* GetIntrinsicOptimizations() {
4065 return &intrinsic_optimizations_;
4066 }
4067
4068 const uint32_t* GetIntrinsicOptimizations() const {
4069 return &intrinsic_optimizations_;
4070 }
4071
4072 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4073
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004074 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004075 void SetResolvedMethod(ArtMethod* method) { resolved_method_ = method; }
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004076
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004077 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004078
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004079 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004080 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4081 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004082 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4083 static constexpr size_t kFieldReturnType =
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004084 kFieldInvokeType + kFieldInvokeTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004085 static constexpr size_t kFieldReturnTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004086 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00004087 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
4088 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
4089 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004090 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004091 using ReturnTypeField = BitField<DataType::Type, kFieldReturnType, kFieldReturnTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004092
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004093 HInvoke(ArenaAllocator* allocator,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004094 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004095 uint32_t number_of_other_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004096 DataType::Type return_type,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004097 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004098 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004099 ArtMethod* resolved_method,
4100 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004101 : HVariableInputSizeInstruction(
4102 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4103 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004104 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004105 number_of_arguments + number_of_other_inputs,
4106 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004107 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004108 resolved_method_(resolved_method),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004109 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07004110 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004111 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004112 SetPackedField<ReturnTypeField>(return_type);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004113 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004114 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004115 }
4116
Roland Levillain3e3d7332015-04-28 11:00:54 +01004117 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004118 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004119 const uint32_t dex_method_index_;
4120 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004121
4122 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4123 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004124
4125 private:
4126 DISALLOW_COPY_AND_ASSIGN(HInvoke);
4127};
4128
Vladimir Markofcb503c2016-05-18 12:48:17 +01004129class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004130 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004131 HInvokeUnresolved(ArenaAllocator* allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004132 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004133 DataType::Type return_type,
Calin Juravle175dc732015-08-25 15:42:32 +01004134 uint32_t dex_pc,
4135 uint32_t dex_method_index,
4136 InvokeType invoke_type)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004137 : HInvoke(allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004138 number_of_arguments,
4139 0u /* number_of_other_inputs */,
4140 return_type,
4141 dex_pc,
4142 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004143 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01004144 invoke_type) {
4145 }
4146
4147 DECLARE_INSTRUCTION(InvokeUnresolved);
4148
4149 private:
4150 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
4151};
4152
Orion Hodsonac141392017-01-13 11:53:47 +00004153class HInvokePolymorphic FINAL : public HInvoke {
4154 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004155 HInvokePolymorphic(ArenaAllocator* allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004156 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004157 DataType::Type return_type,
Orion Hodsonac141392017-01-13 11:53:47 +00004158 uint32_t dex_pc,
4159 uint32_t dex_method_index)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004160 : HInvoke(allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004161 number_of_arguments,
4162 0u /* number_of_other_inputs */,
4163 return_type,
4164 dex_pc,
4165 dex_method_index,
4166 nullptr,
4167 kVirtual) {}
4168
4169 DECLARE_INSTRUCTION(InvokePolymorphic);
4170
4171 private:
4172 DISALLOW_COPY_AND_ASSIGN(HInvokePolymorphic);
4173};
4174
Vladimir Markofcb503c2016-05-18 12:48:17 +01004175class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004176 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004177 // Requirements of this method call regarding the class
4178 // initialization (clinit) check of its declaring class.
4179 enum class ClinitCheckRequirement {
4180 kNone, // Class already initialized.
4181 kExplicit, // Static call having explicit clinit check as last input.
4182 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004183 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004184 };
4185
Vladimir Marko58155012015-08-19 12:49:41 +00004186 // Determines how to load the target ArtMethod*.
4187 enum class MethodLoadKind {
4188 // Use a String init ArtMethod* loaded from Thread entrypoints.
4189 kStringInit,
4190
4191 // Use the method's own ArtMethod* loaded by the register allocator.
4192 kRecursive,
4193
Vladimir Marko65979462017-05-19 17:25:12 +01004194 // Use PC-relative boot image ArtMethod* address that will be known at link time.
4195 // Used for boot image methods referenced by boot image code.
4196 kBootImageLinkTimePcRelative,
4197
Vladimir Marko58155012015-08-19 12:49:41 +00004198 // Use ArtMethod* at a known address, embed the direct address in the code.
4199 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
4200 kDirectAddress,
4201
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004202 // Load from an entry in the .bss section using a PC-relative load.
4203 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
4204 kBssEntry,
Vladimir Marko58155012015-08-19 12:49:41 +00004205
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004206 // Make a runtime call to resolve and call the method. This is the last-resort-kind
4207 // used when other kinds are unimplemented on a particular architecture.
4208 kRuntimeCall,
Vladimir Marko58155012015-08-19 12:49:41 +00004209 };
4210
4211 // Determines the location of the code pointer.
4212 enum class CodePtrLocation {
4213 // Recursive call, use local PC-relative call instruction.
4214 kCallSelf,
4215
Vladimir Marko58155012015-08-19 12:49:41 +00004216 // Use code pointer from the ArtMethod*.
4217 // Used when we don't know the target code. This is also the last-resort-kind used when
4218 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4219 kCallArtMethod,
4220 };
4221
4222 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004223 MethodLoadKind method_load_kind;
4224 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004225 // The method load data holds
4226 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4227 // Note that there are multiple string init methods, each having its own offset.
4228 // - the method address for kDirectAddress
Vladimir Markodc151b22015-10-15 18:02:30 +01004229 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004230 };
4231
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004232 HInvokeStaticOrDirect(ArenaAllocator* allocator,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004233 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004234 DataType::Type return_type,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004235 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004236 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004237 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004238 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004239 InvokeType invoke_type,
4240 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004241 ClinitCheckRequirement clinit_check_requirement)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004242 : HInvoke(allocator,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004243 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004244 // There is potentially one extra argument for the HCurrentMethod node, and
4245 // potentially one other if the clinit check is explicit, and potentially
4246 // one other if the method is a string factory.
4247 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004248 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004249 return_type,
4250 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004251 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004252 resolved_method,
4253 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004254 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004255 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004256 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4257 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004258
Vladimir Markodc151b22015-10-15 18:02:30 +01004259 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004260 bool had_current_method_input = HasCurrentMethodInput();
4261 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4262
4263 // Using the current method is the default and once we find a better
4264 // method load kind, we should not go back to using the current method.
4265 DCHECK(had_current_method_input || !needs_current_method_input);
4266
4267 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004268 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4269 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004270 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004271 dispatch_info_ = dispatch_info;
4272 }
4273
Aart Bik6daebeb2017-04-03 14:35:41 -07004274 DispatchInfo GetDispatchInfo() const {
4275 return dispatch_info_;
4276 }
4277
Vladimir Markoc53c0792015-11-19 15:48:33 +00004278 void AddSpecialInput(HInstruction* input) {
4279 // We allow only one special input.
4280 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4281 DCHECK(InputCount() == GetSpecialInputIndex() ||
4282 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4283 InsertInputAt(GetSpecialInputIndex(), input);
4284 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004285
Vladimir Marko372f10e2016-05-17 16:30:10 +01004286 using HInstruction::GetInputRecords; // Keep the const version visible.
4287 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
4288 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4289 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4290 DCHECK(!input_records.empty());
4291 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4292 HInstruction* last_input = input_records.back().GetInstruction();
4293 // Note: `last_input` may be null during arguments setup.
4294 if (last_input != nullptr) {
4295 // `last_input` is the last input of a static invoke marked as having
4296 // an explicit clinit check. It must either be:
4297 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4298 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4299 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4300 }
4301 }
4302 return input_records;
4303 }
4304
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004305 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004306 // We access the method via the dex cache so we can't do an implicit null check.
4307 // TODO: for intrinsics we can generate implicit null checks.
4308 return false;
4309 }
4310
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004311 bool CanBeNull() const OVERRIDE {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004312 return GetPackedField<ReturnTypeField>() == DataType::Type::kReference && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004313 }
4314
Vladimir Markoc53c0792015-11-19 15:48:33 +00004315 // Get the index of the special input, if any.
4316 //
David Brazdil6de19382016-01-08 17:37:10 +00004317 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4318 // method pointer; otherwise there may be one platform-specific special input,
4319 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004320 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004321 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004322
Vladimir Marko58155012015-08-19 12:49:41 +00004323 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4324 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4325 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004326 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004327 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004328 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko65979462017-05-19 17:25:12 +01004329 bool HasPcRelativeMethodLoadKind() const {
4330 return GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004331 GetMethodLoadKind() == MethodLoadKind::kBssEntry;
Vladimir Marko65979462017-05-19 17:25:12 +01004332 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004333 bool HasCurrentMethodInput() const {
4334 // This function can be called only after the invoke has been fully initialized by the builder.
4335 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004336 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004337 return true;
4338 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004339 DCHECK(InputCount() == GetSpecialInputIndex() ||
4340 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004341 return false;
4342 }
4343 }
Vladimir Marko58155012015-08-19 12:49:41 +00004344
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004345 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004346 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004347 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004348 }
4349
4350 uint64_t GetMethodAddress() const {
4351 DCHECK(HasMethodAddress());
4352 return dispatch_info_.method_load_data;
4353 }
4354
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004355 const DexFile& GetDexFileForPcRelativeDexCache() const;
4356
Vladimir Markoa1de9182016-02-25 11:37:38 +00004357 ClinitCheckRequirement GetClinitCheckRequirement() const {
4358 return GetPackedField<ClinitCheckRequirementField>();
4359 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004360
Roland Levillain4c0eb422015-04-24 16:43:49 +01004361 // Is this instruction a call to a static method?
4362 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004363 return GetInvokeType() == kStatic;
4364 }
4365
4366 MethodReference GetTargetMethod() const {
4367 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004368 }
4369
Vladimir Markofbb184a2015-11-13 14:47:00 +00004370 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4371 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4372 // instruction; only relevant for static calls with explicit clinit check.
4373 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004374 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004375 size_t last_input_index = inputs_.size() - 1u;
4376 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004377 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004378 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004379 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004380 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004381 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004382 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004383 }
4384
4385 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004386 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004387 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004388 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004389 }
4390
4391 // Is this a call to a static method whose declaring class has an
4392 // implicit intialization check requirement?
4393 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004394 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004395 }
4396
Vladimir Markob554b5a2015-11-06 12:57:55 +00004397 // Does this method load kind need the current method as an input?
4398 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004399 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kRuntimeCall;
Vladimir Markob554b5a2015-11-06 12:57:55 +00004400 }
4401
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004402 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004403
4404 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004405 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004406 static constexpr size_t kFieldClinitCheckRequirementSize =
4407 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4408 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4409 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4410 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4411 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004412 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4413 kFieldClinitCheckRequirement,
4414 kFieldClinitCheckRequirementSize>;
4415
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004416 // Cached values of the resolved method, to avoid needing the mutator lock.
Vladimir Marko58155012015-08-19 12:49:41 +00004417 MethodReference target_method_;
4418 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004419
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004420 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004421};
Vladimir Markof64242a2015-12-01 14:58:23 +00004422std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004423std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004424
Vladimir Markofcb503c2016-05-18 12:48:17 +01004425class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004426 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004427 HInvokeVirtual(ArenaAllocator* allocator,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004428 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004429 DataType::Type return_type,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004430 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004431 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004432 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004433 uint32_t vtable_index)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004434 : HInvoke(allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004435 number_of_arguments,
4436 0u,
4437 return_type,
4438 dex_pc,
4439 dex_method_index,
4440 resolved_method,
4441 kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004442 vtable_index_(vtable_index) {}
4443
Aart Bik71bf7b42016-11-16 10:17:46 -08004444 bool CanBeNull() const OVERRIDE {
4445 switch (GetIntrinsic()) {
4446 case Intrinsics::kThreadCurrentThread:
4447 case Intrinsics::kStringBufferAppend:
4448 case Intrinsics::kStringBufferToString:
4449 case Intrinsics::kStringBuilderAppend:
4450 case Intrinsics::kStringBuilderToString:
4451 return false;
4452 default:
4453 return HInvoke::CanBeNull();
4454 }
4455 }
4456
Calin Juravle641547a2015-04-21 22:08:51 +01004457 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004458 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004459 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004460 }
4461
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004462 uint32_t GetVTableIndex() const { return vtable_index_; }
4463
4464 DECLARE_INSTRUCTION(InvokeVirtual);
4465
4466 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004467 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004468 const uint32_t vtable_index_;
4469
4470 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4471};
4472
Vladimir Markofcb503c2016-05-18 12:48:17 +01004473class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004474 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004475 HInvokeInterface(ArenaAllocator* allocator,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004476 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004477 DataType::Type return_type,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004478 uint32_t dex_pc,
4479 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004480 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004481 uint32_t imt_index)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004482 : HInvoke(allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004483 number_of_arguments,
4484 0u,
4485 return_type,
4486 dex_pc,
4487 dex_method_index,
4488 resolved_method,
4489 kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004490 imt_index_(imt_index) {}
4491
Calin Juravle641547a2015-04-21 22:08:51 +01004492 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004493 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004494 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004495 }
4496
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004497 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
4498 // The assembly stub currently needs it.
4499 return true;
4500 }
4501
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004502 uint32_t GetImtIndex() const { return imt_index_; }
4503 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4504
4505 DECLARE_INSTRUCTION(InvokeInterface);
4506
4507 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004508 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004509 const uint32_t imt_index_;
4510
4511 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4512};
4513
Vladimir Markofcb503c2016-05-18 12:48:17 +01004514class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004515 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004516 HNeg(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004517 : HUnaryOperation(result_type, input, dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004518 DCHECK_EQ(result_type, DataType::Kind(input->GetType()));
Roland Levillain937e6cd2016-03-22 11:54:37 +00004519 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004520
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004521 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004522
4523 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004524 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004525 }
4526 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004527 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004528 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004529 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4530 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4531 }
4532 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4533 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4534 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004535
Roland Levillain88cb1752014-10-20 16:36:47 +01004536 DECLARE_INSTRUCTION(Neg);
4537
4538 private:
4539 DISALLOW_COPY_AND_ASSIGN(HNeg);
4540};
4541
Vladimir Markofcb503c2016-05-18 12:48:17 +01004542class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004543 public:
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004544 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004545 : HExpression(DataType::Type::kReference, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004546 SetRawInputAt(0, cls);
4547 SetRawInputAt(1, length);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004548 }
4549
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004550 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004551 bool NeedsEnvironment() const OVERRIDE { return true; }
4552
Mingyao Yang0c365e62015-03-31 15:09:29 -07004553 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4554 bool CanThrow() const OVERRIDE { return true; }
4555
Calin Juravle10e244f2015-01-26 18:54:32 +00004556 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004557
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004558 HLoadClass* GetLoadClass() const {
4559 DCHECK(InputAt(0)->IsLoadClass());
4560 return InputAt(0)->AsLoadClass();
4561 }
4562
4563 HInstruction* GetLength() const {
4564 return InputAt(1);
4565 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004566
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004567 DECLARE_INSTRUCTION(NewArray);
4568
4569 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004570 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4571};
4572
Vladimir Markofcb503c2016-05-18 12:48:17 +01004573class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004574 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004575 HAdd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004576 HInstruction* left,
4577 HInstruction* right,
4578 uint32_t dex_pc = kNoDexPc)
4579 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004580
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004581 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004582
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004583 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004584
4585 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004586 return GetBlock()->GetGraph()->GetIntConstant(
4587 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004588 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004589 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004590 return GetBlock()->GetGraph()->GetLongConstant(
4591 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004592 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004593 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4594 return GetBlock()->GetGraph()->GetFloatConstant(
4595 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4596 }
4597 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4598 return GetBlock()->GetGraph()->GetDoubleConstant(
4599 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4600 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004601
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004602 DECLARE_INSTRUCTION(Add);
4603
4604 private:
4605 DISALLOW_COPY_AND_ASSIGN(HAdd);
4606};
4607
Vladimir Markofcb503c2016-05-18 12:48:17 +01004608class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004609 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004610 HSub(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004611 HInstruction* left,
4612 HInstruction* right,
4613 uint32_t dex_pc = kNoDexPc)
4614 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004615
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004616 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004617
4618 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004619 return GetBlock()->GetGraph()->GetIntConstant(
4620 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004621 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004622 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004623 return GetBlock()->GetGraph()->GetLongConstant(
4624 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004625 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004626 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4627 return GetBlock()->GetGraph()->GetFloatConstant(
4628 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4629 }
4630 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4631 return GetBlock()->GetGraph()->GetDoubleConstant(
4632 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4633 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004634
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004635 DECLARE_INSTRUCTION(Sub);
4636
4637 private:
4638 DISALLOW_COPY_AND_ASSIGN(HSub);
4639};
4640
Vladimir Markofcb503c2016-05-18 12:48:17 +01004641class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004642 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004643 HMul(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004644 HInstruction* left,
4645 HInstruction* right,
4646 uint32_t dex_pc = kNoDexPc)
4647 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004648
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004649 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004650
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004651 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004652
4653 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004654 return GetBlock()->GetGraph()->GetIntConstant(
4655 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004656 }
4657 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004658 return GetBlock()->GetGraph()->GetLongConstant(
4659 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004660 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004661 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4662 return GetBlock()->GetGraph()->GetFloatConstant(
4663 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4664 }
4665 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4666 return GetBlock()->GetGraph()->GetDoubleConstant(
4667 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4668 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004669
4670 DECLARE_INSTRUCTION(Mul);
4671
4672 private:
4673 DISALLOW_COPY_AND_ASSIGN(HMul);
4674};
4675
Vladimir Markofcb503c2016-05-18 12:48:17 +01004676class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004677 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004678 HDiv(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004679 HInstruction* left,
4680 HInstruction* right,
4681 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004682 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004683
Roland Levillain9867bc72015-08-05 10:21:34 +01004684 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004685 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004686 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004687 // Our graph structure ensures we never have 0 for `y` during
4688 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004689 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004690 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004691 return (y == -1) ? -x : x / y;
4692 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004693
Roland Levillain31dd3d62016-02-16 12:21:02 +00004694 template <typename T>
4695 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004696 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004697 return x / y;
4698 }
4699
Roland Levillain9867bc72015-08-05 10:21:34 +01004700 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004701 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004702 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004703 }
4704 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004705 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004706 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4707 }
4708 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4709 return GetBlock()->GetGraph()->GetFloatConstant(
4710 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4711 }
4712 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4713 return GetBlock()->GetGraph()->GetDoubleConstant(
4714 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004715 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004716
4717 DECLARE_INSTRUCTION(Div);
4718
4719 private:
4720 DISALLOW_COPY_AND_ASSIGN(HDiv);
4721};
4722
Vladimir Markofcb503c2016-05-18 12:48:17 +01004723class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004724 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004725 HRem(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004726 HInstruction* left,
4727 HInstruction* right,
4728 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004729 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004730
Roland Levillain9867bc72015-08-05 10:21:34 +01004731 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004732 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004733 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004734 // Our graph structure ensures we never have 0 for `y` during
4735 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004736 DCHECK_NE(y, 0);
4737 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4738 return (y == -1) ? 0 : x % y;
4739 }
4740
Roland Levillain31dd3d62016-02-16 12:21:02 +00004741 template <typename T>
4742 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004743 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004744 return std::fmod(x, y);
4745 }
4746
Roland Levillain9867bc72015-08-05 10:21:34 +01004747 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004748 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004749 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004750 }
4751 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004752 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004753 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004754 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004755 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4756 return GetBlock()->GetGraph()->GetFloatConstant(
4757 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4758 }
4759 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4760 return GetBlock()->GetGraph()->GetDoubleConstant(
4761 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4762 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004763
4764 DECLARE_INSTRUCTION(Rem);
4765
4766 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004767 DISALLOW_COPY_AND_ASSIGN(HRem);
4768};
4769
Vladimir Markofcb503c2016-05-18 12:48:17 +01004770class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00004771 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004772 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4773 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004774 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004775 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004776 SetRawInputAt(0, value);
4777 }
4778
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004779 DataType::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004780
Calin Juravled0d48522014-11-04 16:40:20 +00004781 bool CanBeMoved() const OVERRIDE { return true; }
4782
Vladimir Marko372f10e2016-05-17 16:30:10 +01004783 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004784 return true;
4785 }
4786
4787 bool NeedsEnvironment() const OVERRIDE { return true; }
4788 bool CanThrow() const OVERRIDE { return true; }
4789
Calin Juravled0d48522014-11-04 16:40:20 +00004790 DECLARE_INSTRUCTION(DivZeroCheck);
4791
4792 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004793 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4794};
4795
Vladimir Markofcb503c2016-05-18 12:48:17 +01004796class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004797 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004798 HShl(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004799 HInstruction* value,
4800 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004801 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004802 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004803 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
4804 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004805 }
4806
Roland Levillain5b5b9312016-03-22 14:57:31 +00004807 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004808 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004809 return value << (distance & max_shift_distance);
4810 }
4811
4812 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004813 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004814 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004815 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004816 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004817 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004818 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004819 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004820 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4821 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4822 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4823 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004824 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004825 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4826 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004827 LOG(FATAL) << DebugName() << " is not defined for float values";
4828 UNREACHABLE();
4829 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004830 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4831 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004832 LOG(FATAL) << DebugName() << " is not defined for double values";
4833 UNREACHABLE();
4834 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004835
4836 DECLARE_INSTRUCTION(Shl);
4837
4838 private:
4839 DISALLOW_COPY_AND_ASSIGN(HShl);
4840};
4841
Vladimir Markofcb503c2016-05-18 12:48:17 +01004842class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004843 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004844 HShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004845 HInstruction* value,
4846 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004847 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004848 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004849 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
4850 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004851 }
4852
Roland Levillain5b5b9312016-03-22 14:57:31 +00004853 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004854 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004855 return value >> (distance & max_shift_distance);
4856 }
4857
4858 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004859 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004860 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004861 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004862 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004863 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004864 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004865 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004866 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4867 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4868 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4869 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004870 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004871 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4872 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004873 LOG(FATAL) << DebugName() << " is not defined for float values";
4874 UNREACHABLE();
4875 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004876 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4877 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004878 LOG(FATAL) << DebugName() << " is not defined for double values";
4879 UNREACHABLE();
4880 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004881
4882 DECLARE_INSTRUCTION(Shr);
4883
4884 private:
4885 DISALLOW_COPY_AND_ASSIGN(HShr);
4886};
4887
Vladimir Markofcb503c2016-05-18 12:48:17 +01004888class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004889 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004890 HUShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004891 HInstruction* value,
4892 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004893 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004894 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004895 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
4896 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004897 }
4898
Roland Levillain5b5b9312016-03-22 14:57:31 +00004899 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004900 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004901 typedef typename std::make_unsigned<T>::type V;
4902 V ux = static_cast<V>(value);
4903 return static_cast<T>(ux >> (distance & max_shift_distance));
4904 }
4905
4906 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004907 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004908 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004909 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004910 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004911 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004912 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004913 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004914 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4915 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4916 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4917 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004918 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004919 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4920 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004921 LOG(FATAL) << DebugName() << " is not defined for float values";
4922 UNREACHABLE();
4923 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004924 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4925 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004926 LOG(FATAL) << DebugName() << " is not defined for double values";
4927 UNREACHABLE();
4928 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004929
4930 DECLARE_INSTRUCTION(UShr);
4931
4932 private:
4933 DISALLOW_COPY_AND_ASSIGN(HUShr);
4934};
4935
Vladimir Markofcb503c2016-05-18 12:48:17 +01004936class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004937 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004938 HAnd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004939 HInstruction* left,
4940 HInstruction* right,
4941 uint32_t dex_pc = kNoDexPc)
4942 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004943
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004944 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004945
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004946 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004947
4948 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004949 return GetBlock()->GetGraph()->GetIntConstant(
4950 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004951 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004952 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004953 return GetBlock()->GetGraph()->GetLongConstant(
4954 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004955 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004956 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4957 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4958 LOG(FATAL) << DebugName() << " is not defined for float values";
4959 UNREACHABLE();
4960 }
4961 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4962 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4963 LOG(FATAL) << DebugName() << " is not defined for double values";
4964 UNREACHABLE();
4965 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004966
4967 DECLARE_INSTRUCTION(And);
4968
4969 private:
4970 DISALLOW_COPY_AND_ASSIGN(HAnd);
4971};
4972
Vladimir Markofcb503c2016-05-18 12:48:17 +01004973class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004974 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004975 HOr(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004976 HInstruction* left,
4977 HInstruction* right,
4978 uint32_t dex_pc = kNoDexPc)
4979 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004980
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004981 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004982
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004983 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004984
4985 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004986 return GetBlock()->GetGraph()->GetIntConstant(
4987 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004988 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004989 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004990 return GetBlock()->GetGraph()->GetLongConstant(
4991 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004992 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004993 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4994 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4995 LOG(FATAL) << DebugName() << " is not defined for float values";
4996 UNREACHABLE();
4997 }
4998 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4999 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5000 LOG(FATAL) << DebugName() << " is not defined for double values";
5001 UNREACHABLE();
5002 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005003
5004 DECLARE_INSTRUCTION(Or);
5005
5006 private:
5007 DISALLOW_COPY_AND_ASSIGN(HOr);
5008};
5009
Vladimir Markofcb503c2016-05-18 12:48:17 +01005010class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005011 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005012 HXor(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005013 HInstruction* left,
5014 HInstruction* right,
5015 uint32_t dex_pc = kNoDexPc)
5016 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005017
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005018 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005019
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005020 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005021
5022 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005023 return GetBlock()->GetGraph()->GetIntConstant(
5024 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005025 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005026 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005027 return GetBlock()->GetGraph()->GetLongConstant(
5028 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005029 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005030 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5031 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5032 LOG(FATAL) << DebugName() << " is not defined for float values";
5033 UNREACHABLE();
5034 }
5035 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5036 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5037 LOG(FATAL) << DebugName() << " is not defined for double values";
5038 UNREACHABLE();
5039 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005040
5041 DECLARE_INSTRUCTION(Xor);
5042
5043 private:
5044 DISALLOW_COPY_AND_ASSIGN(HXor);
5045};
5046
Vladimir Markofcb503c2016-05-18 12:48:17 +01005047class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005048 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005049 HRor(DataType::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00005050 : HBinaryOperation(result_type, value, distance) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005051 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5052 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00005053 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005054
Roland Levillain5b5b9312016-03-22 14:57:31 +00005055 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005056 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005057 typedef typename std::make_unsigned<T>::type V;
5058 V ux = static_cast<V>(value);
5059 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005060 return static_cast<T>(ux);
5061 } else {
5062 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005063 return static_cast<T>(ux >> (distance & max_shift_value)) |
5064 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005065 }
5066 }
5067
Roland Levillain5b5b9312016-03-22 14:57:31 +00005068 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005069 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005070 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005071 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005072 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005073 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005074 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005075 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005076 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5077 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5078 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5079 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005080 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005081 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5082 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005083 LOG(FATAL) << DebugName() << " is not defined for float values";
5084 UNREACHABLE();
5085 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005086 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5087 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005088 LOG(FATAL) << DebugName() << " is not defined for double values";
5089 UNREACHABLE();
5090 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005091
5092 DECLARE_INSTRUCTION(Ror);
5093
5094 private:
5095 DISALLOW_COPY_AND_ASSIGN(HRor);
5096};
5097
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005098// The value of a parameter in this method. Its location depends on
5099// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01005100class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005101 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005102 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005103 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005104 uint8_t index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005105 DataType::Type parameter_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005106 bool is_this = false)
5107 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005108 dex_file_(dex_file),
5109 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005110 index_(index) {
5111 SetPackedFlag<kFlagIsThis>(is_this);
5112 SetPackedFlag<kFlagCanBeNull>(!is_this);
5113 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005114
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005115 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005116 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005117 uint8_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07005118 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005119
Vladimir Markoa1de9182016-02-25 11:37:38 +00005120 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
5121 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005122
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005123 DECLARE_INSTRUCTION(ParameterValue);
5124
5125 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005126 // Whether or not the parameter value corresponds to 'this' argument.
5127 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
5128 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5129 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5130 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5131 "Too many packed fields.");
5132
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005133 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005134 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005135 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005136 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005137 const uint8_t index_;
5138
5139 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
5140};
5141
Vladimir Markofcb503c2016-05-18 12:48:17 +01005142class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005143 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005144 HNot(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005145 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005146
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005147 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005148 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005149 return true;
5150 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005151
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005152 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005153
5154 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005155 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005156 }
5157 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005158 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005159 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005160 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5161 LOG(FATAL) << DebugName() << " is not defined for float values";
5162 UNREACHABLE();
5163 }
5164 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5165 LOG(FATAL) << DebugName() << " is not defined for double values";
5166 UNREACHABLE();
5167 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005168
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005169 DECLARE_INSTRUCTION(Not);
5170
5171 private:
5172 DISALLOW_COPY_AND_ASSIGN(HNot);
5173};
5174
Vladimir Markofcb503c2016-05-18 12:48:17 +01005175class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005176 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005177 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005178 : HUnaryOperation(DataType::Type::kBool, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01005179
5180 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005181 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01005182 return true;
5183 }
5184
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005185 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005186 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005187 return !x;
5188 }
5189
Roland Levillain9867bc72015-08-05 10:21:34 +01005190 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005191 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005192 }
5193 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5194 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005195 UNREACHABLE();
5196 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005197 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5198 LOG(FATAL) << DebugName() << " is not defined for float values";
5199 UNREACHABLE();
5200 }
5201 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5202 LOG(FATAL) << DebugName() << " is not defined for double values";
5203 UNREACHABLE();
5204 }
David Brazdil66d126e2015-04-03 16:02:44 +01005205
5206 DECLARE_INSTRUCTION(BooleanNot);
5207
5208 private:
5209 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
5210};
5211
Vladimir Markofcb503c2016-05-18 12:48:17 +01005212class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005213 public:
5214 // Instantiate a type conversion of `input` to `result_type`.
Vladimir Markoc8fb2112017-10-03 11:37:52 +01005215 HTypeConversion(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005216 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005217 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005218 // Invariant: We should never generate a conversion to a Boolean value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005219 DCHECK_NE(DataType::Type::kBool, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005220 }
5221
5222 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005223 DataType::Type GetInputType() const { return GetInput()->GetType(); }
5224 DataType::Type GetResultType() const { return GetType(); }
Roland Levillaindff1f282014-11-05 14:15:05 +00005225
5226 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005227 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5228 return true;
5229 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005230
Mark Mendelle82549b2015-05-06 10:55:34 -04005231 // Try to statically evaluate the conversion and return a HConstant
5232 // containing the result. If the input cannot be converted, return nullptr.
5233 HConstant* TryStaticEvaluation() const;
5234
Roland Levillaindff1f282014-11-05 14:15:05 +00005235 DECLARE_INSTRUCTION(TypeConversion);
5236
5237 private:
5238 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
5239};
5240
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005241static constexpr uint32_t kNoRegNumber = -1;
5242
Vladimir Markofcb503c2016-05-18 12:48:17 +01005243class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005244 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005245 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
5246 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005247 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005248 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005249 SetRawInputAt(0, value);
5250 }
5251
Calin Juravle10e244f2015-01-26 18:54:32 +00005252 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005253 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005254 return true;
5255 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005256
Calin Juravle10e244f2015-01-26 18:54:32 +00005257 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005258
Calin Juravle10e244f2015-01-26 18:54:32 +00005259 bool CanThrow() const OVERRIDE { return true; }
5260
5261 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005262
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005263
5264 DECLARE_INSTRUCTION(NullCheck);
5265
5266 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005267 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
5268};
5269
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005270// Embeds an ArtField and all the information required by the compiler. We cache
5271// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005272class FieldInfo : public ValueObject {
5273 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005274 FieldInfo(ArtField* field,
5275 MemberOffset field_offset,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005276 DataType::Type field_type,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005277 bool is_volatile,
5278 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005279 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005280 const DexFile& dex_file)
5281 : field_(field),
5282 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005283 field_type_(field_type),
5284 is_volatile_(is_volatile),
5285 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005286 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005287 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005288
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005289 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005290 MemberOffset GetFieldOffset() const { return field_offset_; }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005291 DataType::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005292 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005293 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005294 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005295 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005296
5297 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005298 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005299 const MemberOffset field_offset_;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005300 const DataType::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005301 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005302 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005303 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005304 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005305};
5306
Vladimir Markofcb503c2016-05-18 12:48:17 +01005307class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005308 public:
5309 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005310 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005311 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005312 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005313 bool is_volatile,
5314 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005315 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005316 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005317 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005318 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005319 field_info_(field,
5320 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005321 field_type,
5322 is_volatile,
5323 field_idx,
5324 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005325 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005326 SetRawInputAt(0, value);
5327 }
5328
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005329 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005330
Vladimir Marko372f10e2016-05-17 16:30:10 +01005331 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5332 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005333 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005334 }
5335
Calin Juravle641547a2015-04-21 22:08:51 +01005336 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005337 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005338 }
5339
5340 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005341 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5342 }
5343
Calin Juravle52c48962014-12-16 17:02:57 +00005344 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005345 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005346 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005347 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005348
Vladimir Marko61b92282017-10-11 13:23:17 +01005349 void SetType(DataType::Type new_type) {
5350 DCHECK(DataType::IsIntegralType(GetType()));
5351 DCHECK(DataType::IsIntegralType(new_type));
5352 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5353 SetPackedField<TypeField>(new_type);
5354 }
5355
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005356 DECLARE_INSTRUCTION(InstanceFieldGet);
5357
5358 private:
5359 const FieldInfo field_info_;
5360
5361 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5362};
5363
Vladimir Markofcb503c2016-05-18 12:48:17 +01005364class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005365 public:
5366 HInstanceFieldSet(HInstruction* object,
5367 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005368 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005369 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005370 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005371 bool is_volatile,
5372 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005373 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005374 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005375 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005376 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005377 field_info_(field,
5378 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005379 field_type,
5380 is_volatile,
5381 field_idx,
5382 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005383 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005384 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005385 SetRawInputAt(0, object);
5386 SetRawInputAt(1, value);
5387 }
5388
Calin Juravle641547a2015-04-21 22:08:51 +01005389 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005390 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005391 }
5392
Calin Juravle52c48962014-12-16 17:02:57 +00005393 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005394 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005395 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005396 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005397 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005398 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5399 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005400
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005401 DECLARE_INSTRUCTION(InstanceFieldSet);
5402
5403 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005404 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5405 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5406 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5407 "Too many packed fields.");
5408
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005409 const FieldInfo field_info_;
5410
5411 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5412};
5413
Vladimir Markofcb503c2016-05-18 12:48:17 +01005414class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005415 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005416 HArrayGet(HInstruction* array,
5417 HInstruction* index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005418 DataType::Type type,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005419 uint32_t dex_pc)
5420 : HArrayGet(array,
5421 index,
5422 type,
5423 SideEffects::ArrayReadOfType(type),
5424 dex_pc,
5425 /* is_string_char_at */ false) {}
5426
5427 HArrayGet(HInstruction* array,
5428 HInstruction* index,
5429 DataType::Type type,
5430 SideEffects side_effects,
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005431 uint32_t dex_pc,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005432 bool is_string_char_at)
5433 : HExpression(type, side_effects, dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005434 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005435 SetRawInputAt(0, array);
5436 SetRawInputAt(1, index);
5437 }
5438
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005439 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005440 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005441 return true;
5442 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005443 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005444 // TODO: We can be smarter here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01005445 // Currently, unless the array is the result of NewArray, the array access is always
5446 // preceded by some form of null NullCheck necessary for the bounds check, usually
5447 // implicit null check on the ArrayLength input to BoundsCheck or Deoptimize for
5448 // dynamic BCE. There are cases when these could be removed to produce better code.
5449 // If we ever add optimizations to do so we should allow an implicit check here
5450 // (as long as the address falls in the first page).
5451 //
5452 // As an example of such fancy optimization, we could eliminate BoundsCheck for
5453 // a = cond ? new int[1] : null;
5454 // a[0]; // The Phi does not need bounds check for either input.
Calin Juravle77520bc2015-01-12 18:45:46 +00005455 return false;
5456 }
5457
David Brazdil4833f5a2015-12-16 10:37:39 +00005458 bool IsEquivalentOf(HArrayGet* other) const {
5459 bool result = (GetDexPc() == other->GetDexPc());
5460 if (kIsDebugBuild && result) {
5461 DCHECK_EQ(GetBlock(), other->GetBlock());
5462 DCHECK_EQ(GetArray(), other->GetArray());
5463 DCHECK_EQ(GetIndex(), other->GetIndex());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005464 if (DataType::IsIntOrLongType(GetType())) {
5465 DCHECK(DataType::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005466 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005467 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
5468 DCHECK(DataType::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005469 }
5470 }
5471 return result;
5472 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005473
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005474 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5475
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005476 HInstruction* GetArray() const { return InputAt(0); }
5477 HInstruction* GetIndex() const { return InputAt(1); }
5478
Vladimir Marko61b92282017-10-11 13:23:17 +01005479 void SetType(DataType::Type new_type) {
5480 DCHECK(DataType::IsIntegralType(GetType()));
5481 DCHECK(DataType::IsIntegralType(new_type));
5482 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5483 SetPackedField<TypeField>(new_type);
5484 }
5485
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005486 DECLARE_INSTRUCTION(ArrayGet);
5487
5488 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005489 // We treat a String as an array, creating the HArrayGet from String.charAt()
5490 // intrinsic in the instruction simplifier. We can always determine whether
5491 // a particular HArrayGet is actually a String.charAt() by looking at the type
5492 // of the input but that requires holding the mutator lock, so we prefer to use
5493 // a flag, so that code generators don't need to do the locking.
5494 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
5495 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5496 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5497 "Too many packed fields.");
5498
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005499 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5500};
5501
Vladimir Markofcb503c2016-05-18 12:48:17 +01005502class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005503 public:
5504 HArraySet(HInstruction* array,
5505 HInstruction* index,
5506 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005507 DataType::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005508 uint32_t dex_pc)
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005509 : HArraySet(array,
5510 index,
5511 value,
5512 expected_component_type,
5513 // Make a best guess for side effects now, may be refined during SSA building.
5514 ComputeSideEffects(GetComponentType(value->GetType(), expected_component_type)),
5515 dex_pc) {}
5516
5517 HArraySet(HInstruction* array,
5518 HInstruction* index,
5519 HInstruction* value,
5520 DataType::Type expected_component_type,
5521 SideEffects side_effects,
5522 uint32_t dex_pc)
5523 : HTemplateInstruction(side_effects, dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005524 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005525 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005526 SetPackedFlag<kFlagValueCanBeNull>(true);
5527 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005528 SetRawInputAt(0, array);
5529 SetRawInputAt(1, index);
5530 SetRawInputAt(2, value);
5531 }
5532
Calin Juravle77520bc2015-01-12 18:45:46 +00005533 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005534 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005535 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005536 }
5537
Mingyao Yang81014cb2015-06-02 03:16:27 -07005538 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005539 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005540
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005541 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005542 // TODO: Same as for ArrayGet.
5543 return false;
5544 }
5545
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005546 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005547 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005548 }
5549
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005550 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005551 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005552 }
5553
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005554 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005555 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005556 }
5557
Vladimir Markoa1de9182016-02-25 11:37:38 +00005558 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5559 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5560 bool StaticTypeOfArrayIsObjectArray() const {
5561 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5562 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005563
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005564 HInstruction* GetArray() const { return InputAt(0); }
5565 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005566 HInstruction* GetValue() const { return InputAt(2); }
5567
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005568 DataType::Type GetComponentType() const {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005569 return GetComponentType(GetValue()->GetType(), GetRawExpectedComponentType());
5570 }
5571
5572 static DataType::Type GetComponentType(DataType::Type value_type,
5573 DataType::Type expected_component_type) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005574 // The Dex format does not type floating point index operations. Since the
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005575 // `expected_component_type` comes from SSA building and can therefore not
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005576 // be correct, we also check what is the value type. If it is a floating
5577 // point type, we must use that type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005578 return ((value_type == DataType::Type::kFloat32) || (value_type == DataType::Type::kFloat64))
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005579 ? value_type
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005580 : expected_component_type;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005581 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005582
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005583 DataType::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005584 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005585 }
5586
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005587 static SideEffects ComputeSideEffects(DataType::Type type) {
5588 return SideEffects::ArrayWriteOfType(type).Union(SideEffectsForArchRuntimeCalls(type));
Aart Bik18b36ab2016-04-13 16:41:35 -07005589 }
5590
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005591 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type value_type) {
5592 return (value_type == DataType::Type::kReference) ? SideEffects::CanTriggerGC()
5593 : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005594 }
5595
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005596 DECLARE_INSTRUCTION(ArraySet);
5597
5598 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005599 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5600 static constexpr size_t kFieldExpectedComponentTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005601 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00005602 static constexpr size_t kFlagNeedsTypeCheck =
5603 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5604 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005605 // Cached information for the reference_type_info_ so that codegen
5606 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005607 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5608 static constexpr size_t kNumberOfArraySetPackedBits =
5609 kFlagStaticTypeOfArrayIsObjectArray + 1;
5610 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5611 using ExpectedComponentTypeField =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005612 BitField<DataType::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005613
5614 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5615};
5616
Vladimir Markofcb503c2016-05-18 12:48:17 +01005617class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005618 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005619 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005620 : HExpression(DataType::Type::kInt32, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005621 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005622 // Note that arrays do not change length, so the instruction does not
5623 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005624 SetRawInputAt(0, array);
5625 }
5626
Calin Juravle77520bc2015-01-12 18:45:46 +00005627 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005628 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005629 return true;
5630 }
Calin Juravle641547a2015-04-21 22:08:51 +01005631 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5632 return obj == InputAt(0);
5633 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005634
Vladimir Markodce016e2016-04-28 13:10:02 +01005635 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5636
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005637 DECLARE_INSTRUCTION(ArrayLength);
5638
5639 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005640 // We treat a String as an array, creating the HArrayLength from String.length()
5641 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5642 // determine whether a particular HArrayLength is actually a String.length() by
5643 // looking at the type of the input but that requires holding the mutator lock, so
5644 // we prefer to use a flag, so that code generators don't need to do the locking.
5645 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5646 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5647 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5648 "Too many packed fields.");
5649
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005650 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5651};
5652
Vladimir Markofcb503c2016-05-18 12:48:17 +01005653class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005654 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005655 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5656 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005657 HBoundsCheck(HInstruction* index,
5658 HInstruction* length,
5659 uint32_t dex_pc,
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005660 bool string_char_at = false)
5661 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005662 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(index->GetType()));
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005663 SetPackedFlag<kFlagIsStringCharAt>(string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005664 SetRawInputAt(0, index);
5665 SetRawInputAt(1, length);
5666 }
5667
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005668 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005669 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005670 return true;
5671 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005672
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005673 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005674
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005675 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005676
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005677 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005678
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005679 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005680
5681 DECLARE_INSTRUCTION(BoundsCheck);
5682
5683 private:
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005684 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005685
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005686 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5687};
5688
Vladimir Markofcb503c2016-05-18 12:48:17 +01005689class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005690 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005691 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005692 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005693
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005694 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005695 return true;
5696 }
5697
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005698 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5699 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005700
5701 DECLARE_INSTRUCTION(SuspendCheck);
5702
5703 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005704 // Only used for code generation, in order to share the same slow path between back edges
5705 // of a same loop.
5706 SlowPathCode* slow_path_;
5707
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005708 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5709};
5710
David Srbecky0cf44932015-12-09 14:09:59 +00005711// Pseudo-instruction which provides the native debugger with mapping information.
5712// It ensures that we can generate line number and local variables at this point.
5713class HNativeDebugInfo : public HTemplateInstruction<0> {
5714 public:
5715 explicit HNativeDebugInfo(uint32_t dex_pc)
5716 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5717
5718 bool NeedsEnvironment() const OVERRIDE {
5719 return true;
5720 }
5721
5722 DECLARE_INSTRUCTION(NativeDebugInfo);
5723
5724 private:
5725 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5726};
5727
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005728/**
5729 * Instruction to load a Class object.
5730 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005731class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005732 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005733 // Determines how to load the Class.
5734 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005735 // We cannot load this class. See HSharpening::SharpenLoadClass.
5736 kInvalid = -1,
5737
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005738 // Use the Class* from the method's own ArtMethod*.
5739 kReferrersClass,
5740
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005741 // Use PC-relative boot image Class* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01005742 // Used for boot image classes referenced by boot image code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005743 kBootImageLinkTimePcRelative,
5744
5745 // Use a known boot image Class* address, embedded in the code by the codegen.
5746 // Used for boot image classes referenced by apps in AOT- and JIT-compiled code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005747 kBootImageAddress,
5748
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005749 // Use a PC-relative load from a boot image ClassTable mmapped into the .bss
5750 // of the oat file.
5751 kBootImageClassTable,
5752
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005753 // Load from an entry in the .bss section using a PC-relative load.
5754 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
5755 kBssEntry,
5756
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005757 // Load from the root table associated with the JIT compiled method.
5758 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005759
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005760 // Load using a simple runtime call. This is the fall-back load kind when
5761 // the codegen is unable to use another appropriate kind.
5762 kRuntimeCall,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005763
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005764 kLast = kRuntimeCall
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005765 };
5766
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005767 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005768 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005769 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005770 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005771 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005772 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00005773 bool needs_access_check)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005774 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5775 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005776 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005777 dex_file_(dex_file),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005778 klass_(klass),
Calin Juravle2e768302015-07-28 14:41:11 +00005779 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005780 // Referrers class should not need access check. We never inline unverified
5781 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005782 DCHECK(!is_referrers_class || !needs_access_check);
5783
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005784 SetPackedField<LoadKindField>(
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005785 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kRuntimeCall);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005786 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00005787 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005788 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005789 }
5790
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005791 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005792
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005793 LoadKind GetLoadKind() const {
5794 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005795 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005796
5797 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005798
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005799 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005800
Andreas Gampea5b09a62016-11-17 15:21:22 -08005801 size_t ComputeHashCode() const OVERRIDE { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005802
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005803 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005804
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005805 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005806 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005807 }
5808
Calin Juravle0ba218d2015-05-19 18:46:01 +01005809 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005810 // The entrypoint the code generator is going to call does not do
5811 // clinit of the class.
5812 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005813 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005814 }
5815
5816 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00005817 return NeedsAccessCheck() ||
5818 MustGenerateClinitCheck() ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005819 GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005820 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01005821 }
5822
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005823 bool CanThrow() const OVERRIDE {
Vladimir Marko41559982017-01-06 14:04:23 +00005824 return NeedsAccessCheck() ||
5825 MustGenerateClinitCheck() ||
5826 // If the class is in the boot image, the lookup in the runtime call cannot throw.
5827 // This keeps CanThrow() consistent between non-PIC (using kBootImageAddress) and
5828 // PIC and subsequently avoids a DCE behavior dependency on the PIC option.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005829 ((GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005830 GetLoadKind() == LoadKind::kBssEntry) &&
5831 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005832 }
5833
Calin Juravleacf735c2015-02-12 15:25:22 +00005834 ReferenceTypeInfo GetLoadedClassRTI() {
5835 return loaded_class_rti_;
5836 }
5837
5838 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5839 // Make sure we only set exact types (the loaded class should never be merged).
5840 DCHECK(rti.IsExact());
5841 loaded_class_rti_ = rti;
5842 }
5843
Andreas Gampea5b09a62016-11-17 15:21:22 -08005844 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005845 const DexFile& GetDexFile() const { return dex_file_; }
5846
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005847 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005848 return GetLoadKind() == LoadKind::kRuntimeCall;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005849 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005850
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005851 static SideEffects SideEffectsForArchRuntimeCalls() {
5852 return SideEffects::CanTriggerGC();
5853 }
5854
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005855 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005856 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005857 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005858 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005859
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005860 void MarkInBootImage() {
5861 SetPackedFlag<kFlagIsInBootImage>(true);
5862 }
5863
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005864 void AddSpecialInput(HInstruction* special_input);
5865
5866 using HInstruction::GetInputRecords; // Keep the const version visible.
5867 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5868 return ArrayRef<HUserRecord<HInstruction*>>(
5869 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
5870 }
5871
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005872 DataType::Type GetType() const OVERRIDE {
5873 return DataType::Type::kReference;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005874 }
5875
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005876 Handle<mirror::Class> GetClass() const {
5877 return klass_;
5878 }
5879
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005880 DECLARE_INSTRUCTION(LoadClass);
5881
5882 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005883 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00005884 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005885 // Whether this instruction must generate the initialization check.
5886 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005887 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005888 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
5889 static constexpr size_t kFieldLoadKindSize =
5890 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5891 static constexpr size_t kNumberOfLoadClassPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005892 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005893 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
5894
5895 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005896 return load_kind == LoadKind::kReferrersClass ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005897 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005898 load_kind == LoadKind::kBootImageClassTable ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005899 load_kind == LoadKind::kBssEntry ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005900 load_kind == LoadKind::kRuntimeCall;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005901 }
5902
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005903 void SetLoadKindInternal(LoadKind load_kind);
5904
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005905 // The special input is the HCurrentMethod for kRuntimeCall or kReferrersClass.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005906 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005907 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005908 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005909
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005910 // A type index and dex file where the class can be accessed. The dex file can be:
5911 // - The compiling method's dex file if the class is defined there too.
5912 // - The compiling method's dex file if the class is referenced there.
5913 // - The dex file where the class is defined. When the load kind can only be
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005914 // kBssEntry or kRuntimeCall, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08005915 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005916 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005917
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005918 Handle<mirror::Class> klass_;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005919
Calin Juravleacf735c2015-02-12 15:25:22 +00005920 ReferenceTypeInfo loaded_class_rti_;
5921
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005922 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5923};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005924std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
5925
5926// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005927inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005928 // The special input is used for PC-relative loads on some architectures,
5929 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005930 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005931 GetLoadKind() == LoadKind::kBootImageAddress ||
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005932 GetLoadKind() == LoadKind::kBootImageClassTable ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005933 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005934 DCHECK(special_input_.GetInstruction() == nullptr);
5935 special_input_ = HUserRecord<HInstruction*>(special_input);
5936 special_input->AddUseAt(this, 0);
5937}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005938
Vladimir Marko372f10e2016-05-17 16:30:10 +01005939class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005940 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005941 // Determines how to load the String.
5942 enum class LoadKind {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005943 // Use PC-relative boot image String* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01005944 // Used for boot image strings referenced by boot image code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005945 kBootImageLinkTimePcRelative,
5946
5947 // Use a known boot image String* address, embedded in the code by the codegen.
5948 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005949 kBootImageAddress,
5950
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005951 // Use a PC-relative load from a boot image InternTable mmapped into the .bss
5952 // of the oat file.
5953 kBootImageInternTable,
5954
Vladimir Markoaad75c62016-10-03 08:46:48 +00005955 // Load from an entry in the .bss section using a PC-relative load.
5956 // Used for strings outside boot image when .bss is accessible with a PC-relative load.
5957 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005958
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005959 // Load from the root table associated with the JIT compiled method.
5960 kJitTableAddress,
5961
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005962 // Load using a simple runtime call. This is the fall-back load kind when
5963 // the codegen is unable to use another appropriate kind.
5964 kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005965
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005966 kLast = kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005967 };
5968
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005969 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005970 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005971 const DexFile& dex_file,
5972 uint32_t dex_pc)
Vladimir Marko372f10e2016-05-17 16:30:10 +01005973 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5974 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005975 string_index_(string_index),
5976 dex_file_(dex_file) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005977 SetPackedField<LoadKindField>(LoadKind::kRuntimeCall);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005978 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005979
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005980 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005981
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005982 LoadKind GetLoadKind() const {
5983 return GetPackedField<LoadKindField>();
5984 }
5985
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005986 const DexFile& GetDexFile() const {
5987 return dex_file_;
5988 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005989
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005990 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005991 return string_index_;
5992 }
5993
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005994 Handle<mirror::String> GetString() const {
5995 return string_;
5996 }
5997
5998 void SetString(Handle<mirror::String> str) {
5999 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006000 }
6001
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006002 bool CanBeMoved() const OVERRIDE { return true; }
6003
Vladimir Marko372f10e2016-05-17 16:30:10 +01006004 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006005
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006006 size_t ComputeHashCode() const OVERRIDE { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006007
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006008 // Will call the runtime if we need to load the string through
6009 // the dex cache and the string is not guaranteed to be there yet.
6010 bool NeedsEnvironment() const OVERRIDE {
6011 LoadKind load_kind = GetLoadKind();
Vladimir Marko764d4542017-05-16 10:31:41 +01006012 if (load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006013 load_kind == LoadKind::kBootImageAddress ||
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006014 load_kind == LoadKind::kBootImageInternTable ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006015 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006016 return false;
6017 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006018 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006019 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006020
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006021 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006022 return GetLoadKind() == LoadKind::kRuntimeCall;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006023 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006024
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07006025 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006026 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006027
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006028 static SideEffects SideEffectsForArchRuntimeCalls() {
6029 return SideEffects::CanTriggerGC();
6030 }
6031
Vladimir Marko372f10e2016-05-17 16:30:10 +01006032 void AddSpecialInput(HInstruction* special_input);
6033
6034 using HInstruction::GetInputRecords; // Keep the const version visible.
6035 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
6036 return ArrayRef<HUserRecord<HInstruction*>>(
6037 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006038 }
6039
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006040 DataType::Type GetType() const OVERRIDE {
6041 return DataType::Type::kReference;
Vladimir Marko372f10e2016-05-17 16:30:10 +01006042 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006043
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006044 DECLARE_INSTRUCTION(LoadString);
6045
6046 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006047 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006048 static constexpr size_t kFieldLoadKindSize =
6049 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
6050 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006051 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006052 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006053
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006054 void SetLoadKindInternal(LoadKind load_kind);
6055
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006056 // The special input is the HCurrentMethod for kRuntimeCall.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006057 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00006058 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01006059 HUserRecord<HInstruction*> special_input_;
6060
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006061 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006062 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006063
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006064 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006065
6066 DISALLOW_COPY_AND_ASSIGN(HLoadString);
6067};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006068std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
6069
6070// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006071inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006072 // The special input is used for PC-relative loads on some architectures,
6073 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006074 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006075 GetLoadKind() == LoadKind::kBootImageAddress ||
6076 GetLoadKind() == LoadKind::kBootImageInternTable ||
6077 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01006078 // HLoadString::GetInputRecords() returns an empty array at this point,
6079 // so use the GetInputRecords() from the base class to set the input record.
6080 DCHECK(special_input_.GetInstruction() == nullptr);
6081 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006082 special_input->AddUseAt(this, 0);
6083}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006084
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006085/**
6086 * Performs an initialization check on its Class object input.
6087 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01006088class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006089 public:
Roland Levillain3887c462015-08-12 18:15:42 +01006090 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07006091 : HExpression(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006092 DataType::Type::kReference,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006093 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
6094 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006095 SetRawInputAt(0, constant);
6096 }
6097
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006098 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006099 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006100 return true;
6101 }
6102
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006103 bool NeedsEnvironment() const OVERRIDE {
6104 // May call runtime to initialize the class.
6105 return true;
6106 }
6107
Nicolas Geoffray729645a2015-11-19 13:29:02 +00006108 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006109
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006110 HLoadClass* GetLoadClass() const {
6111 DCHECK(InputAt(0)->IsLoadClass());
6112 return InputAt(0)->AsLoadClass();
6113 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006114
6115 DECLARE_INSTRUCTION(ClinitCheck);
6116
6117 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006118 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
6119};
6120
Vladimir Markofcb503c2016-05-18 12:48:17 +01006121class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006122 public:
6123 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006124 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006125 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006126 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006127 bool is_volatile,
6128 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006129 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006130 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006131 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01006132 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006133 field_info_(field,
6134 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006135 field_type,
6136 is_volatile,
6137 field_idx,
6138 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006139 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006140 SetRawInputAt(0, cls);
6141 }
6142
Calin Juravle52c48962014-12-16 17:02:57 +00006143
Calin Juravle10c9cbe2014-12-19 10:50:19 +00006144 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006145
Vladimir Marko372f10e2016-05-17 16:30:10 +01006146 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
6147 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006148 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006149 }
6150
6151 size_t ComputeHashCode() const OVERRIDE {
6152 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6153 }
6154
Calin Juravle52c48962014-12-16 17:02:57 +00006155 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006156 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006157 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006158 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006159
Vladimir Marko61b92282017-10-11 13:23:17 +01006160 void SetType(DataType::Type new_type) {
6161 DCHECK(DataType::IsIntegralType(GetType()));
6162 DCHECK(DataType::IsIntegralType(new_type));
6163 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6164 SetPackedField<TypeField>(new_type);
6165 }
6166
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006167 DECLARE_INSTRUCTION(StaticFieldGet);
6168
6169 private:
6170 const FieldInfo field_info_;
6171
6172 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
6173};
6174
Vladimir Markofcb503c2016-05-18 12:48:17 +01006175class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006176 public:
6177 HStaticFieldSet(HInstruction* cls,
6178 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006179 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006180 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006181 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006182 bool is_volatile,
6183 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006184 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006185 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006186 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01006187 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006188 field_info_(field,
6189 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006190 field_type,
6191 is_volatile,
6192 field_idx,
6193 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006194 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006195 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006196 SetRawInputAt(0, cls);
6197 SetRawInputAt(1, value);
6198 }
6199
Calin Juravle52c48962014-12-16 17:02:57 +00006200 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006201 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006202 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006203 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006204
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006205 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006206 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6207 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006208
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006209 DECLARE_INSTRUCTION(StaticFieldSet);
6210
6211 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006212 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6213 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6214 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6215 "Too many packed fields.");
6216
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006217 const FieldInfo field_info_;
6218
6219 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
6220};
6221
Vladimir Markofcb503c2016-05-18 12:48:17 +01006222class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006223 public:
6224 HUnresolvedInstanceFieldGet(HInstruction* obj,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006225 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006226 uint32_t field_index,
6227 uint32_t dex_pc)
6228 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6229 field_index_(field_index) {
6230 SetRawInputAt(0, obj);
6231 }
6232
6233 bool NeedsEnvironment() const OVERRIDE { return true; }
6234 bool CanThrow() const OVERRIDE { return true; }
6235
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006236 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006237 uint32_t GetFieldIndex() const { return field_index_; }
6238
6239 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6240
6241 private:
6242 const uint32_t field_index_;
6243
6244 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
6245};
6246
Vladimir Markofcb503c2016-05-18 12:48:17 +01006247class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006248 public:
6249 HUnresolvedInstanceFieldSet(HInstruction* obj,
6250 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006251 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006252 uint32_t field_index,
6253 uint32_t dex_pc)
6254 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006255 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006256 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006257 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006258 SetRawInputAt(0, obj);
6259 SetRawInputAt(1, value);
6260 }
6261
6262 bool NeedsEnvironment() const OVERRIDE { return true; }
6263 bool CanThrow() const OVERRIDE { return true; }
6264
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006265 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006266 uint32_t GetFieldIndex() const { return field_index_; }
6267
6268 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6269
6270 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006271 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6272 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006273 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006274 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6275 kFieldFieldType + kFieldFieldTypeSize;
6276 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6277 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006278 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006279
Calin Juravlee460d1d2015-09-29 04:52:17 +01006280 const uint32_t field_index_;
6281
6282 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
6283};
6284
Vladimir Markofcb503c2016-05-18 12:48:17 +01006285class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006286 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006287 HUnresolvedStaticFieldGet(DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006288 uint32_t field_index,
6289 uint32_t dex_pc)
6290 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6291 field_index_(field_index) {
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 GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006298 uint32_t GetFieldIndex() const { return field_index_; }
6299
6300 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6301
6302 private:
6303 const uint32_t field_index_;
6304
6305 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
6306};
6307
Vladimir Markofcb503c2016-05-18 12:48:17 +01006308class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006309 public:
6310 HUnresolvedStaticFieldSet(HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006311 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006312 uint32_t field_index,
6313 uint32_t dex_pc)
6314 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006315 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006316 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006317 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006318 SetRawInputAt(0, value);
6319 }
6320
6321 bool NeedsEnvironment() const OVERRIDE { return true; }
6322 bool CanThrow() const OVERRIDE { return true; }
6323
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006324 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006325 uint32_t GetFieldIndex() const { return field_index_; }
6326
6327 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6328
6329 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006330 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6331 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006332 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006333 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6334 kFieldFieldType + kFieldFieldTypeSize;
6335 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6336 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006337 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006338
Calin Juravlee460d1d2015-09-29 04:52:17 +01006339 const uint32_t field_index_;
6340
6341 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
6342};
6343
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006344// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006345class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006346 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006347 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006348 : HExpression(DataType::Type::kReference, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006349
David Brazdilbbd733e2015-08-18 17:48:17 +01006350 bool CanBeNull() const OVERRIDE { return false; }
6351
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006352 DECLARE_INSTRUCTION(LoadException);
6353
6354 private:
6355 DISALLOW_COPY_AND_ASSIGN(HLoadException);
6356};
6357
David Brazdilcb1c0552015-08-04 16:22:25 +01006358// Implicit part of move-exception which clears thread-local exception storage.
6359// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006360class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006361 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006362 explicit HClearException(uint32_t dex_pc = kNoDexPc)
6363 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01006364
6365 DECLARE_INSTRUCTION(ClearException);
6366
6367 private:
6368 DISALLOW_COPY_AND_ASSIGN(HClearException);
6369};
6370
Vladimir Markofcb503c2016-05-18 12:48:17 +01006371class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006372 public:
6373 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006374 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006375 SetRawInputAt(0, exception);
6376 }
6377
6378 bool IsControlFlow() const OVERRIDE { return true; }
6379
6380 bool NeedsEnvironment() const OVERRIDE { return true; }
6381
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006382 bool CanThrow() const OVERRIDE { return true; }
6383
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006384
6385 DECLARE_INSTRUCTION(Throw);
6386
6387 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006388 DISALLOW_COPY_AND_ASSIGN(HThrow);
6389};
6390
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006391/**
6392 * Implementation strategies for the code generator of a HInstanceOf
6393 * or `HCheckCast`.
6394 */
6395enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006396 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006397 kExactCheck, // Can do a single class compare.
6398 kClassHierarchyCheck, // Can just walk the super class chain.
6399 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6400 kInterfaceCheck, // No optimization yet when checking against an interface.
6401 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006402 kArrayCheck, // No optimization yet when checking against a generic array.
6403 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006404};
6405
Roland Levillain86503782016-02-11 19:07:30 +00006406std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6407
Vladimir Markofcb503c2016-05-18 12:48:17 +01006408class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006409 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006410 HInstanceOf(HInstruction* object,
6411 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006412 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006413 uint32_t dex_pc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006414 : HExpression(DataType::Type::kBool,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006415 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006416 dex_pc) {
6417 SetPackedField<TypeCheckKindField>(check_kind);
6418 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006419 SetRawInputAt(0, object);
6420 SetRawInputAt(1, constant);
6421 }
6422
6423 bool CanBeMoved() const OVERRIDE { return true; }
6424
Vladimir Marko372f10e2016-05-17 16:30:10 +01006425 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006426 return true;
6427 }
6428
6429 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006430 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006431 }
6432
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006433 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006434 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6435 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6436 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6437 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006438
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006439 static bool CanCallRuntime(TypeCheckKind check_kind) {
6440 // Mips currently does runtime calls for any other checks.
6441 return check_kind != TypeCheckKind::kExactCheck;
6442 }
6443
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006444 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006445 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006446 }
6447
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006448 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006449
6450 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006451 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6452 static constexpr size_t kFieldTypeCheckKindSize =
6453 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6454 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6455 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6456 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6457 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006458
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006459 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6460};
6461
Vladimir Markofcb503c2016-05-18 12:48:17 +01006462class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006463 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07006464 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006465 : HExpression(DataType::Type::kReference, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006466 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6467 SetPackedFlag<kFlagUpperCanBeNull>(true);
6468 SetPackedFlag<kFlagCanBeNull>(true);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006469 DCHECK_EQ(input->GetType(), DataType::Type::kReference);
Calin Juravleb1498f62015-02-16 13:13:29 +00006470 SetRawInputAt(0, input);
6471 }
6472
David Brazdilf5552582015-12-27 13:36:12 +00006473 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006474 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006475 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006476 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006477
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006478 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006479 DCHECK(GetUpperCanBeNull() || !can_be_null);
6480 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006481 }
6482
Vladimir Markoa1de9182016-02-25 11:37:38 +00006483 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006484
Calin Juravleb1498f62015-02-16 13:13:29 +00006485 DECLARE_INSTRUCTION(BoundType);
6486
6487 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006488 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6489 // is false then CanBeNull() cannot be true).
6490 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6491 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6492 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6493 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6494
Calin Juravleb1498f62015-02-16 13:13:29 +00006495 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006496 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6497 // It is used to bound the type in cases like:
6498 // if (x instanceof ClassX) {
6499 // // uper_bound_ will be ClassX
6500 // }
David Brazdilf5552582015-12-27 13:36:12 +00006501 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006502
6503 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6504};
6505
Vladimir Markofcb503c2016-05-18 12:48:17 +01006506class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006507 public:
6508 HCheckCast(HInstruction* object,
6509 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006510 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006511 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006512 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6513 SetPackedField<TypeCheckKindField>(check_kind);
6514 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006515 SetRawInputAt(0, object);
6516 SetRawInputAt(1, constant);
6517 }
6518
6519 bool CanBeMoved() const OVERRIDE { return true; }
6520
Vladimir Marko372f10e2016-05-17 16:30:10 +01006521 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006522 return true;
6523 }
6524
6525 bool NeedsEnvironment() const OVERRIDE {
6526 // Instruction may throw a CheckCastError.
6527 return true;
6528 }
6529
6530 bool CanThrow() const OVERRIDE { return true; }
6531
Vladimir Markoa1de9182016-02-25 11:37:38 +00006532 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6533 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6534 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6535 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006536
6537 DECLARE_INSTRUCTION(CheckCast);
6538
6539 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006540 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6541 static constexpr size_t kFieldTypeCheckKindSize =
6542 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6543 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6544 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6545 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6546 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006547
6548 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006549};
6550
Andreas Gampe26de38b2016-07-27 17:53:11 -07006551/**
6552 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
6553 * @details We define the combined barrier types that are actually required
6554 * by the Java Memory Model, rather than using exactly the terminology from
6555 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
6556 * primitives. Note that the JSR-133 cookbook generally does not deal with
6557 * store atomicity issues, and the recipes there are not always entirely sufficient.
6558 * The current recipe is as follows:
6559 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
6560 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
6561 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
6562 * -# Use StoreStore barrier after all stores but before return from any constructor whose
6563 * class has final fields.
6564 * -# Use NTStoreStore to order non-temporal stores with respect to all later
6565 * store-to-memory instructions. Only generated together with non-temporal stores.
6566 */
6567enum MemBarrierKind {
6568 kAnyStore,
6569 kLoadAny,
6570 kStoreStore,
6571 kAnyAny,
6572 kNTStoreStore,
6573 kLastBarrierKind = kNTStoreStore
6574};
6575std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
6576
Vladimir Markofcb503c2016-05-18 12:48:17 +01006577class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006578 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006579 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006580 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006581 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6582 SetPackedField<BarrierKindField>(barrier_kind);
6583 }
Calin Juravle27df7582015-04-17 19:12:31 +01006584
Vladimir Markoa1de9182016-02-25 11:37:38 +00006585 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006586
6587 DECLARE_INSTRUCTION(MemoryBarrier);
6588
6589 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006590 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6591 static constexpr size_t kFieldBarrierKindSize =
6592 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6593 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6594 kFieldBarrierKind + kFieldBarrierKindSize;
6595 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6596 "Too many packed fields.");
6597 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006598
6599 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6600};
6601
Igor Murashkind01745e2017-04-05 16:40:31 -07006602// A constructor fence orders all prior stores to fields that could be accessed via a final field of
6603// the specified object(s), with respect to any subsequent store that might "publish"
6604// (i.e. make visible) the specified object to another thread.
6605//
6606// JLS 17.5.1 "Semantics of final fields" states that a freeze action happens
6607// for all final fields (that were set) at the end of the invoked constructor.
6608//
6609// The constructor fence models the freeze actions for the final fields of an object
6610// being constructed (semantically at the end of the constructor). Constructor fences
6611// have a per-object affinity; two separate objects being constructed get two separate
6612// constructor fences.
6613//
6614// (Note: that if calling a super-constructor or forwarding to another constructor,
6615// the freezes would happen at the end of *that* constructor being invoked).
6616//
6617// The memory model guarantees that when the object being constructed is "published" after
6618// constructor completion (i.e. escapes the current thread via a store), then any final field
6619// writes must be observable on other threads (once they observe that publication).
6620//
6621// Further, anything written before the freeze, and read by dereferencing through the final field,
6622// must also be visible (so final object field could itself have an object with non-final fields;
6623// yet the freeze must also extend to them).
6624//
6625// Constructor example:
6626//
6627// class HasFinal {
6628// final int field; Optimizing IR for <init>()V:
6629// HasFinal() {
6630// field = 123; HInstanceFieldSet(this, HasFinal.field, 123)
6631// // freeze(this.field); HConstructorFence(this)
6632// } HReturn
6633// }
6634//
6635// HConstructorFence can serve double duty as a fence for new-instance/new-array allocations of
6636// already-initialized classes; in that case the allocation must act as a "default-initializer"
6637// of the object which effectively writes the class pointer "final field".
6638//
6639// For example, we can model default-initialiation as roughly the equivalent of the following:
6640//
6641// class Object {
6642// private final Class header;
6643// }
6644//
6645// Java code: Optimizing IR:
6646//
6647// T new_instance<T>() {
6648// Object obj = allocate_memory(T.class.size); obj = HInvoke(art_quick_alloc_object, T)
6649// obj.header = T.class; // header write is done by above call.
6650// // freeze(obj.header) HConstructorFence(obj)
6651// return (T)obj;
6652// }
6653//
6654// See also:
6655// * CompilerDriver::RequiresConstructorBarrier
6656// * QuasiAtomic::ThreadFenceForConstructor
6657//
6658class HConstructorFence FINAL : public HVariableInputSizeInstruction {
6659 // A fence has variable inputs because the inputs can be removed
6660 // after prepare_for_register_allocation phase.
6661 // (TODO: In the future a fence could freeze multiple objects
6662 // after merging two fences together.)
6663 public:
6664 // `fence_object` is the reference that needs to be protected for correct publication.
6665 //
6666 // It makes sense in the following situations:
6667 // * <init> constructors, it's the "this" parameter (i.e. HParameterValue, s.t. IsThis() == true).
6668 // * new-instance-like instructions, it's the return value (i.e. HNewInstance).
6669 //
6670 // After construction the `fence_object` becomes the 0th input.
6671 // This is not an input in a real sense, but just a convenient place to stash the information
6672 // about the associated object.
6673 HConstructorFence(HInstruction* fence_object,
6674 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01006675 ArenaAllocator* allocator)
Igor Murashkind01745e2017-04-05 16:40:31 -07006676 // We strongly suspect there is not a more accurate way to describe the fine-grained reordering
6677 // constraints described in the class header. We claim that these SideEffects constraints
6678 // enforce a superset of the real constraints.
6679 //
6680 // The ordering described above is conservatively modeled with SideEffects as follows:
6681 //
6682 // * To prevent reordering of the publication stores:
6683 // ----> "Reads of objects" is the initial SideEffect.
6684 // * For every primitive final field store in the constructor:
6685 // ----> Union that field's type as a read (e.g. "Read of T") into the SideEffect.
6686 // * If there are any stores to reference final fields in the constructor:
6687 // ----> Use a more conservative "AllReads" SideEffect because any stores to any references
6688 // that are reachable from `fence_object` also need to be prevented for reordering
6689 // (and we do not want to do alias analysis to figure out what those stores are).
6690 //
6691 // In the implementation, this initially starts out as an "all reads" side effect; this is an
6692 // even more conservative approach than the one described above, and prevents all of the
6693 // above reordering without analyzing any of the instructions in the constructor.
6694 //
6695 // If in a later phase we discover that there are no writes to reference final fields,
6696 // we can refine the side effect to a smaller set of type reads (see above constraints).
6697 : HVariableInputSizeInstruction(SideEffects::AllReads(),
6698 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01006699 allocator,
Igor Murashkind01745e2017-04-05 16:40:31 -07006700 /* number_of_inputs */ 1,
6701 kArenaAllocConstructorFenceInputs) {
6702 DCHECK(fence_object != nullptr);
6703 SetRawInputAt(0, fence_object);
6704 }
6705
6706 // The object associated with this constructor fence.
6707 //
6708 // (Note: This will be null after the prepare_for_register_allocation phase,
6709 // as all constructor fence inputs are removed there).
6710 HInstruction* GetFenceObject() const {
6711 return InputAt(0);
6712 }
6713
6714 // Find all the HConstructorFence uses (`fence_use`) for `this` and:
6715 // - Delete `fence_use` from `this`'s use list.
6716 // - Delete `this` from `fence_use`'s inputs list.
6717 // - If the `fence_use` is dead, remove it from the graph.
6718 //
6719 // A fence is considered dead once it no longer has any uses
6720 // and all of the inputs are dead.
6721 //
6722 // This must *not* be called during/after prepare_for_register_allocation,
6723 // because that removes all the inputs to the fences but the fence is actually
6724 // still considered live.
Igor Murashkin6ef45672017-08-08 13:59:55 -07006725 //
6726 // Returns how many HConstructorFence instructions were removed from graph.
6727 static size_t RemoveConstructorFences(HInstruction* instruction);
Igor Murashkind01745e2017-04-05 16:40:31 -07006728
Igor Murashkindd018df2017-08-09 10:38:31 -07006729 // Combine all inputs of `this` and `other` instruction and remove
6730 // `other` from the graph.
6731 //
6732 // Inputs are unique after the merge.
6733 //
6734 // Requirement: `this` must not be the same as `other.
6735 void Merge(HConstructorFence* other);
6736
Igor Murashkin79d8fa72017-04-18 09:37:23 -07006737 // Check if this constructor fence is protecting
6738 // an HNewInstance or HNewArray that is also the immediate
6739 // predecessor of `this`.
6740 //
Igor Murashkindd018df2017-08-09 10:38:31 -07006741 // If `ignore_inputs` is true, then the immediate predecessor doesn't need
6742 // to be one of the inputs of `this`.
6743 //
Igor Murashkin79d8fa72017-04-18 09:37:23 -07006744 // Returns the associated HNewArray or HNewInstance,
6745 // or null otherwise.
Igor Murashkindd018df2017-08-09 10:38:31 -07006746 HInstruction* GetAssociatedAllocation(bool ignore_inputs = false);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07006747
Igor Murashkind01745e2017-04-05 16:40:31 -07006748 DECLARE_INSTRUCTION(ConstructorFence);
6749
6750 private:
6751 DISALLOW_COPY_AND_ASSIGN(HConstructorFence);
6752};
6753
Vladimir Markofcb503c2016-05-18 12:48:17 +01006754class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006755 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006756 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006757 kEnter,
6758 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006759 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006760 };
6761
6762 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006763 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006764 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6765 dex_pc) {
6766 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006767 SetRawInputAt(0, object);
6768 }
6769
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006770 // Instruction may go into runtime, so we need an environment.
6771 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006772
6773 bool CanThrow() const OVERRIDE {
6774 // Verifier guarantees that monitor-exit cannot throw.
6775 // This is important because it allows the HGraphBuilder to remove
6776 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6777 return IsEnter();
6778 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006779
Vladimir Markoa1de9182016-02-25 11:37:38 +00006780 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6781 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006782
6783 DECLARE_INSTRUCTION(MonitorOperation);
6784
Calin Juravle52c48962014-12-16 17:02:57 +00006785 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006786 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6787 static constexpr size_t kFieldOperationKindSize =
6788 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6789 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6790 kFieldOperationKind + kFieldOperationKindSize;
6791 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6792 "Too many packed fields.");
6793 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006794
6795 private:
6796 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6797};
6798
Vladimir Markofcb503c2016-05-18 12:48:17 +01006799class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00006800 public:
6801 HSelect(HInstruction* condition,
6802 HInstruction* true_value,
6803 HInstruction* false_value,
6804 uint32_t dex_pc)
6805 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6806 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6807
6808 // First input must be `true_value` or `false_value` to allow codegens to
6809 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6810 // that architectures which implement HSelect as a conditional move also
6811 // will not need to invert the condition.
6812 SetRawInputAt(0, false_value);
6813 SetRawInputAt(1, true_value);
6814 SetRawInputAt(2, condition);
6815 }
6816
6817 HInstruction* GetFalseValue() const { return InputAt(0); }
6818 HInstruction* GetTrueValue() const { return InputAt(1); }
6819 HInstruction* GetCondition() const { return InputAt(2); }
6820
6821 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006822 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6823 return true;
6824 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006825
6826 bool CanBeNull() const OVERRIDE {
6827 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6828 }
6829
6830 DECLARE_INSTRUCTION(Select);
6831
6832 private:
6833 DISALLOW_COPY_AND_ASSIGN(HSelect);
6834};
6835
Vladimir Markof9f64412015-09-02 14:05:49 +01006836class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006837 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006838 MoveOperands(Location source,
6839 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006840 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01006841 HInstruction* instruction)
6842 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006843
6844 Location GetSource() const { return source_; }
6845 Location GetDestination() const { return destination_; }
6846
6847 void SetSource(Location value) { source_ = value; }
6848 void SetDestination(Location value) { destination_ = value; }
6849
6850 // The parallel move resolver marks moves as "in-progress" by clearing the
6851 // destination (but not the source).
6852 Location MarkPending() {
6853 DCHECK(!IsPending());
6854 Location dest = destination_;
6855 destination_ = Location::NoLocation();
6856 return dest;
6857 }
6858
6859 void ClearPending(Location dest) {
6860 DCHECK(IsPending());
6861 destination_ = dest;
6862 }
6863
6864 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006865 DCHECK(source_.IsValid() || destination_.IsInvalid());
6866 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006867 }
6868
6869 // True if this blocks a move from the given location.
6870 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006871 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006872 }
6873
6874 // A move is redundant if it's been eliminated, if its source and
6875 // destination are the same, or if its destination is unneeded.
6876 bool IsRedundant() const {
6877 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6878 }
6879
6880 // We clear both operands to indicate move that's been eliminated.
6881 void Eliminate() {
6882 source_ = destination_ = Location::NoLocation();
6883 }
6884
6885 bool IsEliminated() const {
6886 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6887 return source_.IsInvalid();
6888 }
6889
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006890 DataType::Type GetType() const { return type_; }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006891
Nicolas Geoffray90218252015-04-15 11:56:51 +01006892 bool Is64BitMove() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006893 return DataType::Is64BitType(type_);
Nicolas Geoffray90218252015-04-15 11:56:51 +01006894 }
6895
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006896 HInstruction* GetInstruction() const { return instruction_; }
6897
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006898 private:
6899 Location source_;
6900 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006901 // The type this move is for.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006902 DataType::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006903 // The instruction this move is assocatied with. Null when this move is
6904 // for moving an input in the expected locations of user (including a phi user).
6905 // This is only used in debug mode, to ensure we do not connect interval siblings
6906 // in the same parallel move.
6907 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006908};
6909
Roland Levillainc9285912015-12-18 10:38:42 +00006910std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6911
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006912static constexpr size_t kDefaultNumberOfMoves = 4;
6913
Vladimir Markofcb503c2016-05-18 12:48:17 +01006914class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006915 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01006916 explicit HParallelMove(ArenaAllocator* allocator, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006917 : HTemplateInstruction(SideEffects::None(), dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01006918 moves_(allocator->Adapter(kArenaAllocMoveOperands)) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006919 moves_.reserve(kDefaultNumberOfMoves);
6920 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006921
Nicolas Geoffray90218252015-04-15 11:56:51 +01006922 void AddMove(Location source,
6923 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006924 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01006925 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006926 DCHECK(source.IsValid());
6927 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006928 if (kIsDebugBuild) {
6929 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006930 for (const MoveOperands& move : moves_) {
6931 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006932 // Special case the situation where the move is for the spill slot
6933 // of the instruction.
6934 if ((GetPrevious() == instruction)
6935 || ((GetPrevious() == nullptr)
6936 && instruction->IsPhi()
6937 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006938 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006939 << "Doing parallel moves for the same instruction.";
6940 } else {
6941 DCHECK(false) << "Doing parallel moves for the same instruction.";
6942 }
6943 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006944 }
6945 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006946 for (const MoveOperands& move : moves_) {
6947 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006948 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006949 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006950 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006951 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006952 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006953 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006954 }
6955
Vladimir Marko225b6462015-09-28 12:17:40 +01006956 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006957 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006958 }
6959
Vladimir Marko225b6462015-09-28 12:17:40 +01006960 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006961
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006962 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006963
6964 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006965 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006966
6967 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6968};
6969
xueliang.zhonge0eb4832017-10-30 13:43:14 +00006970// This instruction computes an intermediate address pointing in the 'middle' of an object. The
6971// result pointer cannot be handled by GC, so extra care is taken to make sure that this value is
6972// never used across anything that can trigger GC.
6973// The result of this instruction is not a pointer in the sense of `DataType::Type::kreference`.
6974// So we represent it by the type `DataType::Type::kInt`.
6975class HIntermediateAddress FINAL : public HExpression<2> {
6976 public:
6977 HIntermediateAddress(HInstruction* base_address, HInstruction* offset, uint32_t dex_pc)
6978 : HExpression(DataType::Type::kInt32, SideEffects::DependsOnGC(), dex_pc) {
6979 DCHECK_EQ(DataType::Size(DataType::Type::kInt32),
6980 DataType::Size(DataType::Type::kReference))
6981 << "kPrimInt and kPrimNot have different sizes.";
6982 SetRawInputAt(0, base_address);
6983 SetRawInputAt(1, offset);
6984 }
6985
6986 bool CanBeMoved() const OVERRIDE { return true; }
6987 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6988 return true;
6989 }
6990 bool IsActualObject() const OVERRIDE { return false; }
6991
6992 HInstruction* GetBaseAddress() const { return InputAt(0); }
6993 HInstruction* GetOffset() const { return InputAt(1); }
6994
6995 DECLARE_INSTRUCTION(IntermediateAddress);
6996
6997 private:
6998 DISALLOW_COPY_AND_ASSIGN(HIntermediateAddress);
6999};
7000
7001
Mark Mendell0616ae02015-04-17 12:49:27 -04007002} // namespace art
7003
Aart Bikf8f5a162017-02-06 15:35:29 -08007004#include "nodes_vector.h"
7005
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007006#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
7007#include "nodes_shared.h"
7008#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007009#ifdef ART_ENABLE_CODEGEN_mips
7010#include "nodes_mips.h"
7011#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04007012#ifdef ART_ENABLE_CODEGEN_x86
7013#include "nodes_x86.h"
7014#endif
7015
7016namespace art {
7017
Igor Murashkin6ef45672017-08-08 13:59:55 -07007018class OptimizingCompilerStats;
7019
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007020class HGraphVisitor : public ValueObject {
7021 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007022 explicit HGraphVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7023 : stats_(stats),
7024 graph_(graph) {}
Dave Allison20dfc792014-06-16 20:44:29 -07007025 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007026
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007027 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007028 virtual void VisitBasicBlock(HBasicBlock* block);
7029
Roland Levillain633021e2014-10-01 14:12:25 +01007030 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007031 void VisitInsertionOrder();
7032
Roland Levillain633021e2014-10-01 14:12:25 +01007033 // Visit the graph following dominator tree reverse post-order.
7034 void VisitReversePostOrder();
7035
Nicolas Geoffray787c3072014-03-17 10:20:19 +00007036 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007037
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007038 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007039#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007040 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
7041
7042 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7043
7044#undef DECLARE_VISIT_INSTRUCTION
7045
Igor Murashkin6ef45672017-08-08 13:59:55 -07007046 protected:
7047 OptimizingCompilerStats* stats_;
7048
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007049 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07007050 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007051
7052 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
7053};
7054
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007055class HGraphDelegateVisitor : public HGraphVisitor {
7056 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007057 explicit HGraphDelegateVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7058 : HGraphVisitor(graph, stats) {}
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007059 virtual ~HGraphDelegateVisitor() {}
7060
7061 // Visit functions that delegate to to super class.
7062#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00007063 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007064
7065 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7066
7067#undef DECLARE_VISIT_INSTRUCTION
7068
7069 private:
7070 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
7071};
7072
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007073// Iterator over the blocks that art part of the loop. Includes blocks part
7074// of an inner loop. The order in which the blocks are iterated is on their
7075// block id.
7076class HBlocksInLoopIterator : public ValueObject {
7077 public:
7078 explicit HBlocksInLoopIterator(const HLoopInformation& info)
7079 : blocks_in_loop_(info.GetBlocks()),
7080 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
7081 index_(0) {
7082 if (!blocks_in_loop_.IsBitSet(index_)) {
7083 Advance();
7084 }
7085 }
7086
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007087 bool Done() const { return index_ == blocks_.size(); }
7088 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007089 void Advance() {
7090 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007091 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007092 if (blocks_in_loop_.IsBitSet(index_)) {
7093 break;
7094 }
7095 }
7096 }
7097
7098 private:
7099 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007100 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007101 size_t index_;
7102
7103 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
7104};
7105
Mingyao Yang3584bce2015-05-19 16:01:59 -07007106// Iterator over the blocks that art part of the loop. Includes blocks part
7107// of an inner loop. The order in which the blocks are iterated is reverse
7108// post order.
7109class HBlocksInLoopReversePostOrderIterator : public ValueObject {
7110 public:
7111 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
7112 : blocks_in_loop_(info.GetBlocks()),
7113 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
7114 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007115 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007116 Advance();
7117 }
7118 }
7119
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007120 bool Done() const { return index_ == blocks_.size(); }
7121 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07007122 void Advance() {
7123 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007124 for (size_t e = blocks_.size(); index_ < e; ++index_) {
7125 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007126 break;
7127 }
7128 }
7129 }
7130
7131 private:
7132 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007133 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07007134 size_t index_;
7135
7136 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
7137};
7138
Aart Bikf3e61ee2017-04-12 17:09:20 -07007139// Returns int64_t value of a properly typed constant.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007140inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00007141 if (constant->IsIntConstant()) {
7142 return constant->AsIntConstant()->GetValue();
7143 } else if (constant->IsLongConstant()) {
7144 return constant->AsLongConstant()->GetValue();
7145 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00007146 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00007147 return 0;
7148 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007149}
7150
Aart Bikf3e61ee2017-04-12 17:09:20 -07007151// Returns true iff instruction is an integral constant (and sets value on success).
7152inline bool IsInt64AndGet(HInstruction* instruction, /*out*/ int64_t* value) {
7153 if (instruction->IsIntConstant()) {
7154 *value = instruction->AsIntConstant()->GetValue();
7155 return true;
7156 } else if (instruction->IsLongConstant()) {
7157 *value = instruction->AsLongConstant()->GetValue();
7158 return true;
7159 } else if (instruction->IsNullConstant()) {
7160 *value = 0;
7161 return true;
7162 }
7163 return false;
7164}
7165
Aart Bik0148de42017-09-05 09:25:01 -07007166// Returns true iff instruction is the given integral constant.
7167inline bool IsInt64Value(HInstruction* instruction, int64_t value) {
7168 int64_t val = 0;
7169 return IsInt64AndGet(instruction, &val) && val == value;
7170}
7171
7172// Returns true iff instruction is a zero bit pattern.
7173inline bool IsZeroBitPattern(HInstruction* instruction) {
7174 return instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern();
7175}
7176
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007177#define INSTRUCTION_TYPE_CHECK(type, super) \
7178 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
7179 inline const H##type* HInstruction::As##type() const { \
7180 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
7181 } \
7182 inline H##type* HInstruction::As##type() { \
7183 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
7184 }
7185
7186 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
7187#undef INSTRUCTION_TYPE_CHECK
7188
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00007189// Create space in `blocks` for adding `number_of_new_blocks` entries
7190// starting at location `at`. Blocks after `at` are moved accordingly.
7191inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
7192 size_t number_of_new_blocks,
7193 size_t after) {
7194 DCHECK_LT(after, blocks->size());
7195 size_t old_size = blocks->size();
7196 size_t new_size = old_size + number_of_new_blocks;
7197 blocks->resize(new_size);
7198 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
7199}
7200
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00007201/*
7202 * Hunt "under the hood" of array lengths (leading to array references),
7203 * null checks (also leading to array references), and new arrays
7204 * (leading to the actual length). This makes it more likely related
7205 * instructions become actually comparable.
7206 */
7207inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
7208 while (instruction->IsArrayLength() ||
7209 instruction->IsNullCheck() ||
7210 instruction->IsNewArray()) {
7211 instruction = instruction->IsNewArray()
7212 ? instruction->AsNewArray()->GetLength()
7213 : instruction->InputAt(0);
7214 }
7215 return instruction;
7216}
7217
Artem Serov21c7e6f2017-07-27 16:04:42 +01007218void RemoveEnvironmentUses(HInstruction* instruction);
7219bool HasEnvironmentUsedByOthers(HInstruction* instruction);
7220void ResetEnvironmentInputRecords(HInstruction* instruction);
7221
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007222} // namespace art
7223
7224#endif // ART_COMPILER_OPTIMIZING_NODES_H_