blob: d4382c6b4c9c395623d2a6228dd2625722c74419 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
Vladimir Marko60584552015-09-03 13:35:12 +000020#include <algorithm>
Vladimir Markof9f64412015-09-02 14:05:49 +010021#include <array>
Roland Levillain9867bc72015-08-05 10:21:34 +010022#include <type_traits>
23
Mathieu Chartiere5d80f82015-10-15 17:47:48 -070024#include "base/arena_bit_vector.h"
David Brazdil8d5b8b22015-03-24 10:51:52 +000025#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080026#include "base/arena_object.h"
David Brazdild9c90372016-09-14 16:53:55 +010027#include "base/array_ref.h"
Vladimir Marko2c45bc92016-10-25 16:54:12 +010028#include "base/iteration_range.h"
Vladimir Marko60584552015-09-03 13:35:12 +000029#include "base/stl_util.h"
David Brazdild9c90372016-09-14 16:53:55 +010030#include "base/transform_array_ref.h"
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010031#include "data_type.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070032#include "deoptimization_kind.h"
David Sehr9e734c72018-01-04 17:56:19 -080033#include "dex/dex_file.h"
34#include "dex/dex_file_types.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000035#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000036#include "handle.h"
37#include "handle_scope.h"
Nicolas Geoffray762869d2016-07-15 15:28:35 +010038#include "intrinsics_enum.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070039#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010040#include "locations.h"
Vladimir Marko58155012015-08-19 12:49:41 +000041#include "method_reference.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000042#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010043#include "offsets.h"
Vladimir Marko46817b82016-03-29 12:21:58 +010044#include "utils/intrusive_forward_list.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000045
46namespace art {
47
Vladimir Markoca6fff82017-10-03 14:49:14 +010048class ArenaStack;
David Brazdil1abb4192015-02-17 18:33:36 +000049class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000050class HBasicBlock;
Igor Murashkind01745e2017-04-05 16:40:31 -070051class HConstructorFence;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010052class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000053class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010054class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000055class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000056class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000057class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000058class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000059class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000060class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000061class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000062class HNullConstant;
Igor Murashkind01745e2017-04-05 16:40:31 -070063class HParameterValue;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010064class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010065class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010066class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010067class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000068class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010069class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000070class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000071
Mathieu Chartier736b5602015-09-02 14:54:11 -070072namespace mirror {
73class DexCache;
74} // namespace mirror
75
Nicolas Geoffray818f2102014-02-18 16:43:35 +000076static const int kDefaultNumberOfBlocks = 8;
77static const int kDefaultNumberOfSuccessors = 2;
78static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010079static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010080static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000081static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000082
Roland Levillain5b5b9312016-03-22 14:57:31 +000083// The maximum (meaningful) distance (31) that can be used in an integer shift/rotate operation.
84static constexpr int32_t kMaxIntShiftDistance = 0x1f;
85// The maximum (meaningful) distance (63) that can be used in a long shift/rotate operation.
86static constexpr int32_t kMaxLongShiftDistance = 0x3f;
Calin Juravle9aec02f2014-11-18 23:06:35 +000087
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010088static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070089static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010090
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010091static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
92
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060093static constexpr uint32_t kNoDexPc = -1;
94
Vladimir Markodbb7f5b2016-03-30 13:23:58 +010095inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
96 // For the purposes of the compiler, the dex files must actually be the same object
97 // if we want to safely treat them as the same. This is especially important for JIT
98 // as custom class loaders can open the same underlying file (or memory) multiple
99 // times and provide different class resolution but no two class loaders should ever
100 // use the same DexFile object - doing so is an unsupported hack that can lead to
101 // all sorts of weird failures.
102 return &lhs == &rhs;
103}
104
Dave Allison20dfc792014-06-16 20:44:29 -0700105enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -0700106 // All types.
107 kCondEQ, // ==
108 kCondNE, // !=
109 // Signed integers and floating-point numbers.
110 kCondLT, // <
111 kCondLE, // <=
112 kCondGT, // >
113 kCondGE, // >=
114 // Unsigned integers.
115 kCondB, // <
116 kCondBE, // <=
117 kCondA, // >
118 kCondAE, // >=
Scott Wakeling2c76e062016-08-31 09:48:54 +0100119 // First and last aliases.
120 kCondFirst = kCondEQ,
121 kCondLast = kCondAE,
Dave Allison20dfc792014-06-16 20:44:29 -0700122};
123
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000124enum GraphAnalysisResult {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000125 kAnalysisSkipped,
David Brazdilbadd8262016-02-02 16:28:56 +0000126 kAnalysisInvalidBytecode,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000127 kAnalysisFailThrowCatchLoop,
128 kAnalysisFailAmbiguousArrayOp,
129 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000130};
131
Andreas Gampe9186ced2016-12-12 14:28:21 -0800132template <typename T>
133static inline typename std::make_unsigned<T>::type MakeUnsigned(T x) {
134 return static_cast<typename std::make_unsigned<T>::type>(x);
135}
136
Vladimir Markof9f64412015-09-02 14:05:49 +0100137class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100138 public:
139 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
140
141 void AddInstruction(HInstruction* instruction);
142 void RemoveInstruction(HInstruction* instruction);
143
David Brazdilc3d743f2015-04-22 13:40:50 +0100144 // Insert `instruction` before/after an existing instruction `cursor`.
145 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
146 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
147
Roland Levillain6b469232014-09-25 10:10:38 +0100148 // Return true if this list contains `instruction`.
149 bool Contains(HInstruction* instruction) const;
150
Roland Levillainccc07a92014-09-16 14:48:16 +0100151 // Return true if `instruction1` is found before `instruction2` in
152 // this instruction list and false otherwise. Abort if none
153 // of these instructions is found.
154 bool FoundBefore(const HInstruction* instruction1,
155 const HInstruction* instruction2) const;
156
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000157 bool IsEmpty() const { return first_instruction_ == nullptr; }
158 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
159
160 // Update the block of all instructions to be `block`.
161 void SetBlockOfInstructions(HBasicBlock* block) const;
162
163 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000164 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000165 void Add(const HInstructionList& instruction_list);
166
David Brazdil2d7352b2015-04-20 14:52:42 +0100167 // Return the number of instructions in the list. This is an expensive operation.
168 size_t CountSize() const;
169
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100170 private:
171 HInstruction* first_instruction_;
172 HInstruction* last_instruction_;
173
174 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000175 friend class HGraph;
176 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100177 friend class HInstructionIterator;
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +0000178 friend class HInstructionIteratorHandleChanges;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100179 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100180
181 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
182};
183
David Brazdil4833f5a2015-12-16 10:37:39 +0000184class ReferenceTypeInfo : ValueObject {
185 public:
186 typedef Handle<mirror::Class> TypeHandle;
187
Vladimir Markoa1de9182016-02-25 11:37:38 +0000188 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
189
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700190 static ReferenceTypeInfo Create(TypeHandle type_handle) REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil94ab38f2016-06-21 17:48:19 +0100191 return Create(type_handle, type_handle->CannotBeAssignedFromOtherTypes());
192 }
193
Vladimir Markoa1de9182016-02-25 11:37:38 +0000194 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000195 return ReferenceTypeInfo(type_handle, is_exact);
196 }
197
198 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
199
Vladimir Markof39745e2016-01-26 12:16:55 +0000200 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000201 return handle.GetReference() != nullptr;
202 }
203
Vladimir Marko456307a2016-04-19 14:12:13 +0000204 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000205 return IsValidHandle(type_handle_);
206 }
207
208 bool IsExact() const { return is_exact_; }
209
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700210 bool IsObjectClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000211 DCHECK(IsValid());
212 return GetTypeHandle()->IsObjectClass();
213 }
214
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700215 bool IsStringClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000216 DCHECK(IsValid());
217 return GetTypeHandle()->IsStringClass();
218 }
219
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700220 bool IsObjectArray() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000221 DCHECK(IsValid());
222 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
223 }
224
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700225 bool IsInterface() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000226 DCHECK(IsValid());
227 return GetTypeHandle()->IsInterface();
228 }
229
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700230 bool IsArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000231 DCHECK(IsValid());
232 return GetTypeHandle()->IsArrayClass();
233 }
234
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700235 bool IsPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000236 DCHECK(IsValid());
237 return GetTypeHandle()->IsPrimitiveArray();
238 }
239
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700240 bool IsNonPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000241 DCHECK(IsValid());
242 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
243 }
244
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700245 bool CanArrayHold(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000246 DCHECK(IsValid());
247 if (!IsExact()) return false;
248 if (!IsArrayClass()) return false;
249 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
250 }
251
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700252 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000253 DCHECK(IsValid());
254 if (!IsExact()) return false;
255 if (!IsArrayClass()) return false;
256 if (!rti.IsArrayClass()) return false;
257 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
258 rti.GetTypeHandle()->GetComponentType());
259 }
260
261 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
262
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700263 bool IsSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000264 DCHECK(IsValid());
265 DCHECK(rti.IsValid());
266 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
267 }
268
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700269 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000270 DCHECK(IsValid());
271 DCHECK(rti.IsValid());
272 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
273 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
274 }
275
276 // Returns true if the type information provide the same amount of details.
277 // Note that it does not mean that the instructions have the same actual type
278 // (because the type can be the result of a merge).
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700279 bool IsEqual(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000280 if (!IsValid() && !rti.IsValid()) {
281 // Invalid types are equal.
282 return true;
283 }
284 if (!IsValid() || !rti.IsValid()) {
285 // One is valid, the other not.
286 return false;
287 }
288 return IsExact() == rti.IsExact()
289 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
290 }
291
292 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000293 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
294 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
295 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000296
297 // The class of the object.
298 TypeHandle type_handle_;
299 // Whether or not the type is exact or a superclass of the actual type.
300 // Whether or not we have any information about this type.
301 bool is_exact_;
302};
303
304std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
305
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000306// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100307class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000308 public:
Vladimir Markoca6fff82017-10-03 14:49:14 +0100309 HGraph(ArenaAllocator* allocator,
310 ArenaStack* arena_stack,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100311 const DexFile& dex_file,
312 uint32_t method_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700313 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100314 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100315 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000316 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100317 int start_instruction_id = 0)
Vladimir Markoca6fff82017-10-03 14:49:14 +0100318 : allocator_(allocator),
319 arena_stack_(arena_stack),
320 blocks_(allocator->Adapter(kArenaAllocBlockList)),
321 reverse_post_order_(allocator->Adapter(kArenaAllocReversePostOrder)),
322 linear_order_(allocator->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700323 entry_block_(nullptr),
324 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100325 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100326 number_of_vregs_(0),
327 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000328 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400329 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000330 has_try_catch_(false),
Aart Bikb13c65b2017-03-21 20:14:07 -0700331 has_simd_(false),
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800332 has_loops_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000333 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000334 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000335 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100336 dex_file_(dex_file),
337 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100338 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100339 in_ssa_form_(false),
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800340 number_of_cha_guards_(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700341 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000342 cached_null_constant_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100343 cached_int_constants_(std::less<int32_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
344 cached_float_constants_(std::less<int32_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
345 cached_long_constants_(std::less<int64_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
346 cached_double_constants_(std::less<int64_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000347 cached_current_method_(nullptr),
Nicolas Geoffray53fec082017-03-27 12:56:16 +0100348 art_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000349 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
Mingyao Yang063fc772016-08-02 11:02:54 -0700350 osr_(osr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100351 cha_single_implementation_list_(allocator->Adapter(kArenaAllocCHA)) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100352 blocks_.reserve(kDefaultNumberOfBlocks);
353 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000354
David Brazdilbadd8262016-02-02 16:28:56 +0000355 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700356 void InitializeInexactObjectRTI(VariableSizedHandleScope* handles);
David Brazdilbadd8262016-02-02 16:28:56 +0000357
Vladimir Markoca6fff82017-10-03 14:49:14 +0100358 ArenaAllocator* GetAllocator() const { return allocator_; }
359 ArenaStack* GetArenaStack() const { return arena_stack_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100360 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
361
David Brazdil69ba7b72015-06-23 18:27:30 +0100362 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000363 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100364
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000365 HBasicBlock* GetEntryBlock() const { return entry_block_; }
366 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100367 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000368
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000369 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
370 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000371
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000372 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100373
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100374 void ComputeDominanceInformation();
375 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000376 void ClearLoopInformation();
377 void FindBackEdges(ArenaBitVector* visited);
378 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100379 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100380 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000381
David Brazdil4833f5a2015-12-16 10:37:39 +0000382 // Analyze all natural loops in this graph. Returns a code specifying that it
383 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000384 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000385 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100386
David Brazdilffee3d32015-07-06 11:48:53 +0100387 // Iterate over blocks to compute try block membership. Needs reverse post
388 // order and loop information.
389 void ComputeTryBlockInformation();
390
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000391 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000392 // Returns the instruction to replace the invoke expression or null if the
393 // invoke is for a void method. Note that the caller is responsible for replacing
394 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000395 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000396
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000397 // Update the loop and try membership of `block`, which was spawned from `reference`.
398 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
399 // should be the new back edge.
400 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
401 HBasicBlock* reference,
402 bool replace_if_back_edge);
403
Mingyao Yang3584bce2015-05-19 16:01:59 -0700404 // Need to add a couple of blocks to test if the loop body is entered and
405 // put deoptimization instructions, etc.
406 void TransformLoopHeaderForBCE(HBasicBlock* header);
407
Aart Bikf8f5a162017-02-06 15:35:29 -0800408 // Adds a new loop directly after the loop with the given header and exit.
409 // Returns the new preheader.
410 HBasicBlock* TransformLoopForVectorization(HBasicBlock* header,
411 HBasicBlock* body,
412 HBasicBlock* exit);
413
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000414 // Removes `block` from the graph. Assumes `block` has been disconnected from
415 // other blocks and has no instructions or phis.
416 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000417
David Brazdilfc6a86a2015-06-26 10:33:45 +0000418 // Splits the edge between `block` and `successor` while preserving the
419 // indices in the predecessor/successor lists. If there are multiple edges
420 // between the blocks, the lowest indices are used.
421 // Returns the new block which is empty and has the same dex pc as `successor`.
422 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
423
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100424 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
Artem Serovc73ee372017-07-31 15:08:40 +0100425 void OrderLoopHeaderPredecessors(HBasicBlock* header);
Artem Serov09faaea2017-12-07 14:36:01 +0000426
427 // Transform a loop into a format with a single preheader.
428 //
429 // Each phi in the header should be split: original one in the header should only hold
430 // inputs reachable from the back edges and a single input from the preheader. The newly created
431 // phi in the preheader should collate the inputs from the original multiple incoming blocks.
432 //
433 // Loops in the graph typically have a single preheader, so this method is used to "repair" loops
434 // that no longer have this property.
435 void TransformLoopToSinglePreheaderFormat(HBasicBlock* header);
436
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100437 void SimplifyLoop(HBasicBlock* header);
438
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000439 int32_t GetNextInstructionId() {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100440 CHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000441 return current_instruction_id_++;
442 }
443
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000444 int32_t GetCurrentInstructionId() const {
445 return current_instruction_id_;
446 }
447
448 void SetCurrentInstructionId(int32_t id) {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100449 CHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000450 current_instruction_id_ = id;
451 }
452
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100453 uint16_t GetMaximumNumberOfOutVRegs() const {
454 return maximum_number_of_out_vregs_;
455 }
456
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000457 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
458 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100459 }
460
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100461 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
462 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
463 }
464
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000465 void UpdateTemporariesVRegSlots(size_t slots) {
466 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100467 }
468
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000469 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100470 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000471 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100472 }
473
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100474 void SetNumberOfVRegs(uint16_t number_of_vregs) {
475 number_of_vregs_ = number_of_vregs;
476 }
477
478 uint16_t GetNumberOfVRegs() const {
479 return number_of_vregs_;
480 }
481
482 void SetNumberOfInVRegs(uint16_t value) {
483 number_of_in_vregs_ = value;
484 }
485
David Brazdildee58d62016-04-07 09:54:26 +0000486 uint16_t GetNumberOfInVRegs() const {
487 return number_of_in_vregs_;
488 }
489
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100490 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100491 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100492 return number_of_vregs_ - number_of_in_vregs_;
493 }
494
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100495 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100496 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100497 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100498
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100499 ArrayRef<HBasicBlock* const> GetReversePostOrderSkipEntryBlock() {
500 DCHECK(GetReversePostOrder()[0] == entry_block_);
501 return ArrayRef<HBasicBlock* const>(GetReversePostOrder()).SubArray(1);
502 }
503
504 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetPostOrder() const {
505 return ReverseRange(GetReversePostOrder());
506 }
507
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100508 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100509 return linear_order_;
510 }
511
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100512 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetLinearPostOrder() const {
513 return ReverseRange(GetLinearOrder());
514 }
515
Mark Mendell1152c922015-04-24 17:06:35 -0400516 bool HasBoundsChecks() const {
517 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800518 }
519
Mark Mendell1152c922015-04-24 17:06:35 -0400520 void SetHasBoundsChecks(bool value) {
521 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800522 }
523
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000524 bool IsDebuggable() const { return debuggable_; }
525
David Brazdil8d5b8b22015-03-24 10:51:52 +0000526 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000527 // already, it is created and inserted into the graph. This method is only for
528 // integral types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100529 HConstant* GetConstant(DataType::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000530
531 // TODO: This is problematic for the consistency of reference type propagation
532 // because it can be created anytime after the pass and thus it will be left
533 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600534 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000535
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600536 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
537 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000538 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600539 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
540 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000541 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600542 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
543 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000544 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600545 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
546 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000547 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000548
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100549 HCurrentMethod* GetCurrentMethod();
550
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100551 const DexFile& GetDexFile() const {
552 return dex_file_;
553 }
554
555 uint32_t GetMethodIdx() const {
556 return method_idx_;
557 }
558
Igor Murashkind01745e2017-04-05 16:40:31 -0700559 // Get the method name (without the signature), e.g. "<init>"
560 const char* GetMethodName() const;
561
562 // Get the pretty method name (class + name + optionally signature).
563 std::string PrettyMethod(bool with_signature = true) const;
564
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100565 InvokeType GetInvokeType() const {
566 return invoke_type_;
567 }
568
Mark Mendellc4701932015-04-10 13:18:51 -0400569 InstructionSet GetInstructionSet() const {
570 return instruction_set_;
571 }
572
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000573 bool IsCompilingOsr() const { return osr_; }
574
Mingyao Yang063fc772016-08-02 11:02:54 -0700575 ArenaSet<ArtMethod*>& GetCHASingleImplementationList() {
576 return cha_single_implementation_list_;
577 }
578
579 void AddCHASingleImplementationDependency(ArtMethod* method) {
580 cha_single_implementation_list_.insert(method);
581 }
582
583 bool HasShouldDeoptimizeFlag() const {
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800584 return number_of_cha_guards_ != 0;
Mingyao Yang063fc772016-08-02 11:02:54 -0700585 }
586
David Brazdil77a48ae2015-09-15 12:34:04 +0000587 bool HasTryCatch() const { return has_try_catch_; }
588 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100589
Aart Bikb13c65b2017-03-21 20:14:07 -0700590 bool HasSIMD() const { return has_simd_; }
591 void SetHasSIMD(bool value) { has_simd_ = value; }
592
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800593 bool HasLoops() const { return has_loops_; }
594 void SetHasLoops(bool value) { has_loops_ = value; }
595
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000596 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
597 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
598
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100599 ArtMethod* GetArtMethod() const { return art_method_; }
600 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
601
Roland Levillain5e8d5f02016-10-18 18:03:43 +0100602 // Returns an instruction with the opposite Boolean value from 'cond'.
Mark Mendellf6529172015-11-17 11:16:56 -0500603 // The instruction has been inserted into the graph, either as a constant, or
604 // before cursor.
605 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
606
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000607 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
608
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800609 uint32_t GetNumberOfCHAGuards() { return number_of_cha_guards_; }
610 void SetNumberOfCHAGuards(uint32_t num) { number_of_cha_guards_ = num; }
611 void IncrementNumberOfCHAGuards() { number_of_cha_guards_++; }
612
David Brazdil2d7352b2015-04-20 14:52:42 +0100613 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000614 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100615 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000616
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000617 template <class InstructionType, typename ValueType>
618 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600619 ArenaSafeMap<ValueType, InstructionType*>* cache,
620 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000621 // Try to find an existing constant of the given value.
622 InstructionType* constant = nullptr;
623 auto cached_constant = cache->find(value);
624 if (cached_constant != cache->end()) {
625 constant = cached_constant->second;
626 }
627
628 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100629 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000630 if (constant == nullptr || constant->GetBlock() == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100631 constant = new (allocator_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000632 cache->Overwrite(value, constant);
633 InsertConstant(constant);
634 }
635 return constant;
636 }
637
David Brazdil8d5b8b22015-03-24 10:51:52 +0000638 void InsertConstant(HConstant* instruction);
639
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000640 // Cache a float constant into the graph. This method should only be
641 // called by the SsaBuilder when creating "equivalent" instructions.
642 void CacheFloatConstant(HFloatConstant* constant);
643
644 // See CacheFloatConstant comment.
645 void CacheDoubleConstant(HDoubleConstant* constant);
646
Vladimir Markoca6fff82017-10-03 14:49:14 +0100647 ArenaAllocator* const allocator_;
648 ArenaStack* const arena_stack_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000649
650 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100651 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000652
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100653 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100654 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000655
Aart Bik281c6812016-08-26 11:31:48 -0700656 // List of blocks to perform a linear order tree traversal. Unlike the reverse
657 // post order, this order is not incrementally kept up-to-date.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100658 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100659
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000660 HBasicBlock* entry_block_;
661 HBasicBlock* exit_block_;
662
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100663 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100664 uint16_t maximum_number_of_out_vregs_;
665
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100666 // The number of virtual registers in this method. Contains the parameters.
667 uint16_t number_of_vregs_;
668
669 // The number of virtual registers used by parameters of this method.
670 uint16_t number_of_in_vregs_;
671
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000672 // Number of vreg size slots that the temporaries use (used in baseline compiler).
673 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100674
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800675 // Flag whether there are bounds checks in the graph. We can skip
676 // BCE if it's false. It's only best effort to keep it up to date in
677 // the presence of code elimination so there might be false positives.
Mark Mendell1152c922015-04-24 17:06:35 -0400678 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800679
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800680 // Flag whether there are try/catch blocks in the graph. We will skip
681 // try/catch-related passes if it's false. It's only best effort to keep
682 // it up to date in the presence of code elimination so there might be
683 // false positives.
David Brazdil77a48ae2015-09-15 12:34:04 +0000684 bool has_try_catch_;
685
Aart Bikb13c65b2017-03-21 20:14:07 -0700686 // Flag whether SIMD instructions appear in the graph. If true, the
687 // code generators may have to be more careful spilling the wider
688 // contents of SIMD registers.
689 bool has_simd_;
690
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800691 // Flag whether there are any loops in the graph. We can skip loop
692 // optimization if it's false. It's only best effort to keep it up
693 // to date in the presence of code elimination so there might be false
694 // positives.
695 bool has_loops_;
696
697 // Flag whether there are any irreducible loops in the graph. It's only
698 // best effort to keep it up to date in the presence of code elimination
699 // so there might be false positives.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000700 bool has_irreducible_loops_;
701
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000702 // Indicates whether the graph should be compiled in a way that
703 // ensures full debuggability. If false, we can apply more
704 // aggressive optimizations that may limit the level of debugging.
705 const bool debuggable_;
706
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000707 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000708 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000709
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100710 // The dex file from which the method is from.
711 const DexFile& dex_file_;
712
713 // The method index in the dex file.
714 const uint32_t method_idx_;
715
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100716 // If inlined, this encodes how the callee is being invoked.
717 const InvokeType invoke_type_;
718
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100719 // Whether the graph has been transformed to SSA form. Only used
720 // in debug mode to ensure we are not using properties only valid
721 // for non-SSA form (like the number of temporaries).
722 bool in_ssa_form_;
723
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800724 // Number of CHA guards in the graph. Used to short-circuit the
725 // CHA guard optimization pass when there is no CHA guard left.
726 uint32_t number_of_cha_guards_;
727
Mathieu Chartiere401d142015-04-22 13:56:20 -0700728 const InstructionSet instruction_set_;
729
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000730 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000731 HNullConstant* cached_null_constant_;
732 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000733 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000734 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000735 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000736
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100737 HCurrentMethod* cached_current_method_;
738
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100739 // The ArtMethod this graph is for. Note that for AOT, it may be null,
740 // for example for methods whose declaring class could not be resolved
741 // (such as when the superclass could not be found).
742 ArtMethod* art_method_;
743
David Brazdil4833f5a2015-12-16 10:37:39 +0000744 // Keep the RTI of inexact Object to avoid having to pass stack handle
745 // collection pointer to passes which may create NullConstant.
746 ReferenceTypeInfo inexact_object_rti_;
747
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000748 // Whether we are compiling this graph for on stack replacement: this will
749 // make all loops seen as irreducible and emit special stack maps to mark
750 // compiled code entries which the interpreter can directly jump to.
751 const bool osr_;
752
Mingyao Yang063fc772016-08-02 11:02:54 -0700753 // List of methods that are assumed to have single implementation.
754 ArenaSet<ArtMethod*> cha_single_implementation_list_;
755
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000756 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100757 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000758 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000759 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000760 DISALLOW_COPY_AND_ASSIGN(HGraph);
761};
762
Vladimir Markof9f64412015-09-02 14:05:49 +0100763class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000764 public:
765 HLoopInformation(HBasicBlock* header, HGraph* graph)
766 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100767 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000768 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100769 contains_irreducible_loop_(false),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100770 back_edges_(graph->GetAllocator()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100771 // Make bit vector growable, as the number of blocks may change.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100772 blocks_(graph->GetAllocator(),
773 graph->GetBlocks().size(),
774 true,
775 kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100776 back_edges_.reserve(kDefaultNumberOfBackEdges);
777 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100778
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000779 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100780 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000781
782 void Dump(std::ostream& os);
783
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100784 HBasicBlock* GetHeader() const {
785 return header_;
786 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000787
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000788 void SetHeader(HBasicBlock* block) {
789 header_ = block;
790 }
791
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100792 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
793 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
794 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
795
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000796 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100797 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000798 }
799
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100800 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100801 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100802 }
803
David Brazdil46e2a392015-03-16 17:31:52 +0000804 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100805 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100806 }
807
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000808 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100809 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000810 }
811
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100812 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100813
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100814 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100815 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100816 }
817
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100818 // Returns the lifetime position of the back edge that has the
819 // greatest lifetime position.
820 size_t GetLifetimeEnd() const;
821
822 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100823 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100824 }
825
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000826 // Finds blocks that are part of this loop.
827 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100828
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100829 // Returns whether this loop information contains `block`.
830 // Note that this loop information *must* be populated before entering this function.
831 bool Contains(const HBasicBlock& block) const;
832
833 // Returns whether this loop information is an inner loop of `other`.
834 // Note that `other` *must* be populated before entering this function.
835 bool IsIn(const HLoopInformation& other) const;
836
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800837 // Returns true if instruction is not defined within this loop.
838 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700839
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100840 const ArenaBitVector& GetBlocks() const { return blocks_; }
841
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000842 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000843 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000844
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000845 void ClearAllBlocks() {
846 blocks_.ClearAllBits();
847 }
848
David Brazdil3f4a5222016-05-06 12:46:21 +0100849 bool HasBackEdgeNotDominatedByHeader() const;
850
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100851 bool IsPopulated() const {
852 return blocks_.GetHighestBitSet() != -1;
853 }
854
Anton Shaminf89381f2016-05-16 16:44:13 +0600855 bool DominatesAllBackEdges(HBasicBlock* block);
856
David Sehrc757dec2016-11-04 15:48:34 -0700857 bool HasExitEdge() const;
858
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000859 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100860 // Internal recursive implementation of `Populate`.
861 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100862 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100863
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000864 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100865 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000866 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100867 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100868 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100869 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000870
871 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
872};
873
David Brazdilec16f792015-08-19 15:04:01 +0100874// Stores try/catch information for basic blocks.
875// Note that HGraph is constructed so that catch blocks cannot simultaneously
876// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100877class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100878 public:
879 // Try block information constructor.
880 explicit TryCatchInformation(const HTryBoundary& try_entry)
881 : try_entry_(&try_entry),
882 catch_dex_file_(nullptr),
883 catch_type_index_(DexFile::kDexNoIndex16) {
884 DCHECK(try_entry_ != nullptr);
885 }
886
887 // Catch block information constructor.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800888 TryCatchInformation(dex::TypeIndex catch_type_index, const DexFile& dex_file)
David Brazdilec16f792015-08-19 15:04:01 +0100889 : try_entry_(nullptr),
890 catch_dex_file_(&dex_file),
891 catch_type_index_(catch_type_index) {}
892
893 bool IsTryBlock() const { return try_entry_ != nullptr; }
894
895 const HTryBoundary& GetTryEntry() const {
896 DCHECK(IsTryBlock());
897 return *try_entry_;
898 }
899
900 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
901
902 bool IsCatchAllTypeIndex() const {
903 DCHECK(IsCatchBlock());
Andreas Gampea5b09a62016-11-17 15:21:22 -0800904 return !catch_type_index_.IsValid();
David Brazdilec16f792015-08-19 15:04:01 +0100905 }
906
Andreas Gampea5b09a62016-11-17 15:21:22 -0800907 dex::TypeIndex GetCatchTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +0100908 DCHECK(IsCatchBlock());
909 return catch_type_index_;
910 }
911
912 const DexFile& GetCatchDexFile() const {
913 DCHECK(IsCatchBlock());
914 return *catch_dex_file_;
915 }
916
917 private:
918 // One of possibly several TryBoundary instructions entering the block's try.
919 // Only set for try blocks.
920 const HTryBoundary* try_entry_;
921
922 // Exception type information. Only set for catch blocks.
923 const DexFile* catch_dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800924 const dex::TypeIndex catch_type_index_;
David Brazdilec16f792015-08-19 15:04:01 +0100925};
926
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100927static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100928static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100929
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000930// A block in a method. Contains the list of instructions represented
931// as a double linked list. Each block knows its predecessors and
932// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100933
Vladimir Markof9f64412015-09-02 14:05:49 +0100934class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000935 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700936 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000937 : graph_(graph),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100938 predecessors_(graph->GetAllocator()->Adapter(kArenaAllocPredecessors)),
939 successors_(graph->GetAllocator()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000940 loop_information_(nullptr),
941 dominator_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100942 dominated_blocks_(graph->GetAllocator()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100943 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100944 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100945 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000946 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000947 try_catch_information_(nullptr) {
948 predecessors_.reserve(kDefaultNumberOfPredecessors);
949 successors_.reserve(kDefaultNumberOfSuccessors);
950 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
951 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000952
Vladimir Marko60584552015-09-03 13:35:12 +0000953 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100954 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000955 }
956
Vladimir Marko60584552015-09-03 13:35:12 +0000957 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100958 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000959 }
960
David Brazdild26a4112015-11-10 11:07:31 +0000961 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
962 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
963
Vladimir Marko60584552015-09-03 13:35:12 +0000964 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
965 return ContainsElement(successors_, block, start_from);
966 }
967
968 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100969 return dominated_blocks_;
970 }
971
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100972 bool IsEntryBlock() const {
973 return graph_->GetEntryBlock() == this;
974 }
975
976 bool IsExitBlock() const {
977 return graph_->GetExitBlock() == this;
978 }
979
David Brazdil46e2a392015-03-16 17:31:52 +0000980 bool IsSingleGoto() const;
Mads Ager16e52892017-07-14 13:11:37 +0200981 bool IsSingleReturn() const;
Mingyao Yang46721ef2017-10-05 14:45:17 -0700982 bool IsSingleReturnOrReturnVoidAllowingPhis() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000983 bool IsSingleTryBoundary() const;
984
985 // Returns true if this block emits nothing but a jump.
986 bool IsSingleJump() const {
987 HLoopInformation* loop_info = GetLoopInformation();
988 return (IsSingleGoto() || IsSingleTryBoundary())
989 // Back edges generate a suspend check.
990 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
991 }
David Brazdil46e2a392015-03-16 17:31:52 +0000992
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000993 void AddBackEdge(HBasicBlock* back_edge) {
994 if (loop_information_ == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100995 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000996 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100997 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000998 loop_information_->AddBackEdge(back_edge);
999 }
1000
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001001 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001002 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001003
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001004 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001005 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001006 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001007
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001008 HBasicBlock* GetDominator() const { return dominator_; }
1009 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +00001010 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
1011
1012 void RemoveDominatedBlock(HBasicBlock* block) {
1013 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +01001014 }
Vladimir Marko60584552015-09-03 13:35:12 +00001015
1016 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
1017 ReplaceElement(dominated_blocks_, existing, new_block);
1018 }
1019
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001020 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001021
1022 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001023 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001024 }
1025
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001026 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
1027 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +01001028 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001029 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +01001030 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
1031 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001032
Nicolas Geoffray09aa1472016-01-19 10:52:54 +00001033 HInstruction* GetFirstInstructionDisregardMoves() const;
1034
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001035 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001036 successors_.push_back(block);
1037 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001038 }
1039
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001040 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
1041 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001042 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001043 new_block->predecessors_.push_back(this);
1044 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001045 }
1046
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001047 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
1048 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001049 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001050 new_block->successors_.push_back(this);
1051 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001052 }
1053
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001054 // Insert `this` between `predecessor` and `successor. This method
1055 // preserves the indicies, and will update the first edge found between
1056 // `predecessor` and `successor`.
1057 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
1058 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001059 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +00001060 successor->predecessors_[predecessor_index] = this;
1061 predecessor->successors_[successor_index] = this;
1062 successors_.push_back(successor);
1063 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001064 }
1065
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001066 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001067 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001068 }
1069
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001070 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001071 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001072 }
1073
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001074 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001075 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001076 }
1077
1078 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001079 predecessors_.push_back(block);
1080 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001081 }
1082
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001083 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001084 DCHECK_EQ(predecessors_.size(), 2u);
1085 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001086 }
1087
David Brazdil769c9e52015-04-27 13:54:09 +01001088 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001089 DCHECK_EQ(successors_.size(), 2u);
1090 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +01001091 }
1092
David Brazdilfc6a86a2015-06-26 10:33:45 +00001093 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001094 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001095 }
1096
David Brazdilfc6a86a2015-06-26 10:33:45 +00001097 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001098 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001099 }
1100
David Brazdilfc6a86a2015-06-26 10:33:45 +00001101 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001102 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001103 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001104 }
1105
1106 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001107 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001108 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001109 }
1110
1111 // Returns whether the first occurrence of `predecessor` in the list of
1112 // predecessors is at index `idx`.
1113 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001114 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001115 return GetPredecessorIndexOf(predecessor) == idx;
1116 }
1117
David Brazdild7558da2015-09-22 13:04:14 +01001118 // Create a new block between this block and its predecessors. The new block
1119 // is added to the graph, all predecessor edges are relinked to it and an edge
1120 // is created to `this`. Returns the new empty block. Reverse post order or
1121 // loop and try/catch information are not updated.
1122 HBasicBlock* CreateImmediateDominator();
1123
David Brazdilfc6a86a2015-06-26 10:33:45 +00001124 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001125 // created, latter block. Note that this method will add the block to the
1126 // graph, create a Goto at the end of the former block and will create an edge
1127 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001128 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001129 HBasicBlock* SplitBefore(HInstruction* cursor);
1130
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001131 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001132 // created block. Note that this method just updates raw block information,
1133 // like predecessors, successors, dominators, and instruction list. It does not
1134 // update the graph, reverse post order, loop information, nor make sure the
1135 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001136 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1137
1138 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1139 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001140
1141 // Merge `other` at the end of `this`. Successors and dominated blocks of
1142 // `other` are changed to be successors and dominated blocks of `this`. Note
1143 // that this method does not update the graph, reverse post order, loop
1144 // information, nor make sure the blocks are consistent (for example ending
1145 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001146 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001147
1148 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1149 // of `this` are moved to `other`.
1150 // Note that this method does not update the graph, reverse post order, loop
1151 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001152 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001153 void ReplaceWith(HBasicBlock* other);
1154
Aart Bik6b69e0a2017-01-11 10:20:43 -08001155 // Merges the instructions of `other` at the end of `this`.
1156 void MergeInstructionsWith(HBasicBlock* other);
1157
David Brazdil2d7352b2015-04-20 14:52:42 +01001158 // Merge `other` at the end of `this`. This method updates loops, reverse post
1159 // order, links to predecessors, successors, dominators and deletes the block
1160 // from the graph. The two blocks must be successive, i.e. `this` the only
1161 // predecessor of `other` and vice versa.
1162 void MergeWith(HBasicBlock* other);
1163
1164 // Disconnects `this` from all its predecessors, successors and dominator,
1165 // removes it from all loops it is included in and eventually from the graph.
1166 // The block must not dominate any other block. Predecessors and successors
1167 // are safely updated.
1168 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001169
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001170 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001171 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001172 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001173 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Artem Serovcced8ba2017-07-19 18:18:09 +01001174 // Replace phi `initial` with `replacement` within this block.
1175 void ReplaceAndRemovePhiWith(HPhi* initial, HPhi* replacement);
Roland Levillainccc07a92014-09-16 14:48:16 +01001176 // Replace instruction `initial` with `replacement` within this block.
1177 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1178 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001179 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001180 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001181 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1182 // instruction list. With 'ensure_safety' set to true, it verifies that the
1183 // instruction is not in use and removes it from the use lists of its inputs.
1184 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1185 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001186 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001187
1188 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001189 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001190 }
1191
Roland Levillain6b879dd2014-09-22 17:13:44 +01001192 bool IsLoopPreHeaderFirstPredecessor() const {
1193 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001194 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001195 }
1196
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001197 bool IsFirstPredecessorBackEdge() const {
1198 DCHECK(IsLoopHeader());
1199 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1200 }
1201
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001202 HLoopInformation* GetLoopInformation() const {
1203 return loop_information_;
1204 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001205
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001206 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001207 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001208 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001209 void SetInLoop(HLoopInformation* info) {
1210 if (IsLoopHeader()) {
1211 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001212 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001213 loop_information_ = info;
1214 } else if (loop_information_->Contains(*info->GetHeader())) {
1215 // Block is currently part of an outer loop. Make it part of this inner loop.
1216 // Note that a non loop header having a loop information means this loop information
1217 // has already been populated
1218 loop_information_ = info;
1219 } else {
1220 // Block is part of an inner loop. Do not update the loop information.
1221 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1222 // at this point, because this method is being called while populating `info`.
1223 }
1224 }
1225
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001226 // Raw update of the loop information.
1227 void SetLoopInformation(HLoopInformation* info) {
1228 loop_information_ = info;
1229 }
1230
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001231 bool IsInLoop() const { return loop_information_ != nullptr; }
1232
David Brazdilec16f792015-08-19 15:04:01 +01001233 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1234
1235 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1236 try_catch_information_ = try_catch_information;
1237 }
1238
1239 bool IsTryBlock() const {
1240 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1241 }
1242
1243 bool IsCatchBlock() const {
1244 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1245 }
David Brazdilffee3d32015-07-06 11:48:53 +01001246
1247 // Returns the try entry that this block's successors should have. They will
1248 // be in the same try, unless the block ends in a try boundary. In that case,
1249 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001250 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001251
David Brazdild7558da2015-09-22 13:04:14 +01001252 bool HasThrowingInstructions() const;
1253
David Brazdila4b8c212015-05-07 09:59:30 +01001254 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001255 bool Dominates(HBasicBlock* block) const;
1256
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001257 size_t GetLifetimeStart() const { return lifetime_start_; }
1258 size_t GetLifetimeEnd() const { return lifetime_end_; }
1259
1260 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1261 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1262
David Brazdil8d5b8b22015-03-24 10:51:52 +00001263 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001264 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001265 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001266 bool HasSinglePhi() const;
1267
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001268 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001269 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001270 ArenaVector<HBasicBlock*> predecessors_;
1271 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001272 HInstructionList instructions_;
1273 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001274 HLoopInformation* loop_information_;
1275 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001276 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001277 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001278 // The dex program counter of the first instruction of this block.
1279 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001280 size_t lifetime_start_;
1281 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001282 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001283
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001284 friend class HGraph;
1285 friend class HInstruction;
1286
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001287 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1288};
1289
David Brazdilb2bd1c52015-03-25 11:17:37 +00001290// Iterates over the LoopInformation of all loops which contain 'block'
1291// from the innermost to the outermost.
1292class HLoopInformationOutwardIterator : public ValueObject {
1293 public:
1294 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1295 : current_(block.GetLoopInformation()) {}
1296
1297 bool Done() const { return current_ == nullptr; }
1298
1299 void Advance() {
1300 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001301 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001302 }
1303
1304 HLoopInformation* Current() const {
1305 DCHECK(!Done());
1306 return current_;
1307 }
1308
1309 private:
1310 HLoopInformation* current_;
1311
1312 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1313};
1314
Alexandre Ramesef20f712015-06-09 10:29:30 +01001315#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001316 M(Above, Condition) \
1317 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001318 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001319 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001320 M(ArrayGet, Instruction) \
1321 M(ArrayLength, Instruction) \
1322 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001323 M(Below, Condition) \
1324 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001325 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001326 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001327 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001328 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001329 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001330 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001331 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001332 M(Compare, BinaryOperation) \
Igor Murashkind01745e2017-04-05 16:40:31 -07001333 M(ConstructorFence, Instruction) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001334 M(CurrentMethod, Instruction) \
Mingyao Yang063fc772016-08-02 11:02:54 -07001335 M(ShouldDeoptimizeFlag, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001336 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001337 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001338 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001339 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001340 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001341 M(Exit, Instruction) \
1342 M(FloatConstant, Constant) \
1343 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001344 M(GreaterThan, Condition) \
1345 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001346 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001347 M(InstanceFieldGet, Instruction) \
1348 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001349 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001350 M(IntConstant, Constant) \
xueliang.zhonge0eb4832017-10-30 13:43:14 +00001351 M(IntermediateAddress, Instruction) \
Calin Juravle175dc732015-08-25 15:42:32 +01001352 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001353 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001354 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001355 M(InvokeVirtual, Invoke) \
Orion Hodsonac141392017-01-13 11:53:47 +00001356 M(InvokePolymorphic, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001357 M(LessThan, Condition) \
1358 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001359 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001360 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001361 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001362 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001363 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001364 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001365 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001366 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001367 M(Neg, UnaryOperation) \
1368 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001369 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001370 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001371 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001372 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001373 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001374 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001375 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001376 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001377 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001378 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001379 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001380 M(Return, Instruction) \
1381 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001382 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001383 M(Shl, BinaryOperation) \
1384 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001385 M(StaticFieldGet, Instruction) \
1386 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001387 M(UnresolvedInstanceFieldGet, Instruction) \
1388 M(UnresolvedInstanceFieldSet, Instruction) \
1389 M(UnresolvedStaticFieldGet, Instruction) \
1390 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001391 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001392 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001393 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001394 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001395 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001396 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001397 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001398 M(Xor, BinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001399 M(VecReplicateScalar, VecUnaryOperation) \
Aart Bik0148de42017-09-05 09:25:01 -07001400 M(VecExtractScalar, VecUnaryOperation) \
1401 M(VecReduce, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001402 M(VecCnv, VecUnaryOperation) \
1403 M(VecNeg, VecUnaryOperation) \
Aart Bik6daebeb2017-04-03 14:35:41 -07001404 M(VecAbs, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001405 M(VecNot, VecUnaryOperation) \
1406 M(VecAdd, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001407 M(VecHalvingAdd, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001408 M(VecSub, VecBinaryOperation) \
1409 M(VecMul, VecBinaryOperation) \
1410 M(VecDiv, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001411 M(VecMin, VecBinaryOperation) \
1412 M(VecMax, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001413 M(VecAnd, VecBinaryOperation) \
1414 M(VecAndNot, VecBinaryOperation) \
1415 M(VecOr, VecBinaryOperation) \
1416 M(VecXor, VecBinaryOperation) \
1417 M(VecShl, VecBinaryOperation) \
1418 M(VecShr, VecBinaryOperation) \
1419 M(VecUShr, VecBinaryOperation) \
Aart Bik8de59162017-04-21 09:42:01 -07001420 M(VecSetScalars, VecOperation) \
Artem Serovf34dd202017-04-10 17:41:46 +01001421 M(VecMultiplyAccumulate, VecOperation) \
Aart Bikdbbac8f2017-09-01 13:06:08 -07001422 M(VecSADAccumulate, VecOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001423 M(VecLoad, VecMemoryOperation) \
1424 M(VecStore, VecMemoryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001425
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001426/*
1427 * Instructions, shared across several (not all) architectures.
1428 */
1429#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1430#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1431#else
1432#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001433 M(BitwiseNegatedRight, Instruction) \
Anton Kirilov74234da2017-01-13 14:42:47 +00001434 M(DataProcWithShifterOp, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001435 M(MultiplyAccumulate, Instruction) \
Artem Serove1811ed2017-04-27 16:50:47 +01001436 M(IntermediateAddressIndex, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001437#endif
1438
Alexandre Ramesef20f712015-06-09 10:29:30 +01001439#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1440
1441#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1442
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001443#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001444#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001445#else
1446#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1447 M(MipsComputeBaseMethodAddress, Instruction) \
Lena Djokica2901602017-09-21 13:50:52 +02001448 M(MipsPackedSwitch, Instruction) \
1449 M(IntermediateArrayAddressIndex, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001450#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001451
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001452#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1453
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001454#ifndef ART_ENABLE_CODEGEN_x86
1455#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1456#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001457#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1458 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001459 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001460 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001461 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001462#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001463
1464#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1465
1466#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1467 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001468 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001469 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1470 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001471 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001472 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001473 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1474 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1475
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001476#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1477 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001478 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001479 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001480 M(BinaryOperation, Instruction) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001481 M(Invoke, Instruction) \
1482 M(VecOperation, Instruction) \
1483 M(VecUnaryOperation, VecOperation) \
1484 M(VecBinaryOperation, VecOperation) \
1485 M(VecMemoryOperation, VecOperation)
Dave Allison20dfc792014-06-16 20:44:29 -07001486
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001487#define FOR_EACH_INSTRUCTION(M) \
1488 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1489 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1490
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001491#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001492FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1493#undef FORWARD_DECLARATION
1494
Vladimir Marko372f10e2016-05-17 16:30:10 +01001495#define DECLARE_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001496 private: \
1497 H##type& operator=(const H##type&) = delete; \
1498 public: \
Vladimir Marko372f10e2016-05-17 16:30:10 +01001499 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
1500 const char* DebugName() const OVERRIDE { return #type; } \
1501 bool InstructionTypeEquals(const HInstruction* other) const OVERRIDE { \
1502 return other->Is##type(); \
1503 } \
Artem Serovcced8ba2017-07-19 18:18:09 +01001504 HInstruction* Clone(ArenaAllocator* arena) const OVERRIDE { \
1505 DCHECK(IsClonable()); \
1506 return new (arena) H##type(*this->As##type()); \
1507 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001508 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001509
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001510#define DECLARE_ABSTRACT_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001511 private: \
1512 H##type& operator=(const H##type&) = delete; \
1513 public: \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001514 bool Is##type() const { return As##type() != nullptr; } \
1515 const H##type* As##type() const { return this; } \
1516 H##type* As##type() { return this; }
1517
Artem Serovcced8ba2017-07-19 18:18:09 +01001518#define DEFAULT_COPY_CONSTRUCTOR(type) \
1519 explicit H##type(const H##type& other) = default;
1520
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001521template <typename T>
Vladimir Marko82b07402017-03-01 19:02:04 +00001522class HUseListNode : public ArenaObject<kArenaAllocUseListNode>,
1523 public IntrusiveForwardListNode<HUseListNode<T>> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001524 public:
Igor Murashkind01745e2017-04-05 16:40:31 -07001525 // Get the instruction which has this use as one of the inputs.
David Brazdiled596192015-01-23 10:39:45 +00001526 T GetUser() const { return user_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001527 // Get the position of the input record that this use corresponds to.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001528 size_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001529 // Set the position of the input record that this use corresponds to.
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001530 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001531
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001532 private:
David Brazdiled596192015-01-23 10:39:45 +00001533 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001534 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001535
1536 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001537 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001538
Vladimir Marko46817b82016-03-29 12:21:58 +01001539 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001540
1541 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1542};
1543
David Brazdiled596192015-01-23 10:39:45 +00001544template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001545using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001546
David Brazdil1abb4192015-02-17 18:33:36 +00001547// This class is used by HEnvironment and HInstruction classes to record the
1548// instructions they use and pointers to the corresponding HUseListNodes kept
1549// by the used instructions.
1550template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001551class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001552 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001553 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1554 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001555
Vladimir Marko46817b82016-03-29 12:21:58 +01001556 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1557 : HUserRecord(old_record.instruction_, before_use_node) {}
1558 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1559 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001560 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001561 }
1562
1563 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001564 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1565 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001566
1567 private:
1568 // Instruction used by the user.
1569 HInstruction* instruction_;
1570
Vladimir Marko46817b82016-03-29 12:21:58 +01001571 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1572 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001573};
1574
Vladimir Markoe9004912016-06-16 16:50:52 +01001575// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1576// This is used for HInstruction::GetInputs() to return a container wrapper providing
1577// HInstruction* values even though the underlying container has HUserRecord<>s.
1578struct HInputExtractor {
1579 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1580 return record.GetInstruction();
1581 }
1582 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1583 return record.GetInstruction();
1584 }
1585};
1586
1587using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1588using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1589
Aart Bik854a02b2015-07-14 16:07:00 -07001590/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001591 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001592 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001593 * For write/read dependences on fields/arrays, the dependence analysis uses
1594 * type disambiguation (e.g. a float field write cannot modify the value of an
1595 * integer field read) and the access type (e.g. a reference array write cannot
1596 * modify the value of a reference field read [although it may modify the
1597 * reference fetch prior to reading the field, which is represented by its own
1598 * write/read dependence]). The analysis makes conservative points-to
1599 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1600 * the same, and any reference read depends on any reference read without
1601 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001602 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001603 * The internal representation uses 38-bit and is described in the table below.
1604 * The first line indicates the side effect, and for field/array accesses the
1605 * second line indicates the type of the access (in the order of the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001606 * DataType::Type enum).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001607 * The two numbered lines below indicate the bit position in the bitfield (read
1608 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001609 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001610 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1611 * +-------------+---------+---------+--------------+---------+---------+
1612 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1613 * | 3 |333333322|222222221| 1 |111111110|000000000|
1614 * | 7 |654321098|765432109| 8 |765432109|876543210|
1615 *
1616 * Note that, to ease the implementation, 'changes' bits are least significant
1617 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001618 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001619class SideEffects : public ValueObject {
1620 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001621 SideEffects() : flags_(0) {}
1622
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001623 static SideEffects None() {
1624 return SideEffects(0);
1625 }
1626
1627 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001628 return SideEffects(kAllChangeBits | kAllDependOnBits);
1629 }
1630
1631 static SideEffects AllChanges() {
1632 return SideEffects(kAllChangeBits);
1633 }
1634
1635 static SideEffects AllDependencies() {
1636 return SideEffects(kAllDependOnBits);
1637 }
1638
1639 static SideEffects AllExceptGCDependency() {
1640 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1641 }
1642
1643 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001644 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001645 }
1646
Aart Bik34c3ba92015-07-20 14:08:59 -07001647 static SideEffects AllWrites() {
1648 return SideEffects(kAllWrites);
1649 }
1650
1651 static SideEffects AllReads() {
1652 return SideEffects(kAllReads);
1653 }
1654
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001655 static SideEffects FieldWriteOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001656 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001657 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001658 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001659 }
1660
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001661 static SideEffects ArrayWriteOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001662 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001663 }
1664
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001665 static SideEffects FieldReadOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001666 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001667 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001668 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001669 }
1670
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001671 static SideEffects ArrayReadOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001672 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001673 }
1674
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001675 static SideEffects CanTriggerGC() {
1676 return SideEffects(1ULL << kCanTriggerGCBit);
1677 }
1678
1679 static SideEffects DependsOnGC() {
1680 return SideEffects(1ULL << kDependsOnGCBit);
1681 }
1682
Aart Bik854a02b2015-07-14 16:07:00 -07001683 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001684 SideEffects Union(SideEffects other) const {
1685 return SideEffects(flags_ | other.flags_);
1686 }
1687
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001688 SideEffects Exclusion(SideEffects other) const {
1689 return SideEffects(flags_ & ~other.flags_);
1690 }
1691
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001692 void Add(SideEffects other) {
1693 flags_ |= other.flags_;
1694 }
1695
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001696 bool Includes(SideEffects other) const {
1697 return (other.flags_ & flags_) == other.flags_;
1698 }
1699
1700 bool HasSideEffects() const {
1701 return (flags_ & kAllChangeBits);
1702 }
1703
1704 bool HasDependencies() const {
1705 return (flags_ & kAllDependOnBits);
1706 }
1707
1708 // Returns true if there are no side effects or dependencies.
1709 bool DoesNothing() const {
1710 return flags_ == 0;
1711 }
1712
Aart Bik854a02b2015-07-14 16:07:00 -07001713 // Returns true if something is written.
1714 bool DoesAnyWrite() const {
1715 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001716 }
1717
Aart Bik854a02b2015-07-14 16:07:00 -07001718 // Returns true if something is read.
1719 bool DoesAnyRead() const {
1720 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001721 }
1722
Aart Bik854a02b2015-07-14 16:07:00 -07001723 // Returns true if potentially everything is written and read
1724 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001725 bool DoesAllReadWrite() const {
1726 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1727 }
1728
Aart Bik854a02b2015-07-14 16:07:00 -07001729 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001730 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001731 }
1732
Roland Levillain0d5a2812015-11-13 10:07:31 +00001733 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001734 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001735 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1736 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001737 }
1738
1739 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001740 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001741 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001742 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001743 for (int s = kLastBit; s >= 0; s--) {
1744 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1745 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1746 // This is a bit for the GC side effect.
1747 if (current_bit_is_set) {
1748 flags += "GC";
1749 }
Aart Bik854a02b2015-07-14 16:07:00 -07001750 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001751 } else {
1752 // This is a bit for the array/field analysis.
1753 // The underscore character stands for the 'can trigger GC' bit.
1754 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1755 if (current_bit_is_set) {
1756 flags += kDebug[s];
1757 }
1758 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1759 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1760 flags += "|";
1761 }
1762 }
Aart Bik854a02b2015-07-14 16:07:00 -07001763 }
1764 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001765 }
1766
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001767 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001768
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001769 private:
1770 static constexpr int kFieldArrayAnalysisBits = 9;
1771
1772 static constexpr int kFieldWriteOffset = 0;
1773 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1774 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1775 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1776
1777 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1778
1779 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1780 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1781 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1782 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1783
1784 static constexpr int kLastBit = kDependsOnGCBit;
1785 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1786
1787 // Aliases.
1788
1789 static_assert(kChangeBits == kDependOnBits,
1790 "the 'change' bits should match the 'depend on' bits.");
1791
1792 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1793 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1794 static constexpr uint64_t kAllWrites =
1795 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1796 static constexpr uint64_t kAllReads =
1797 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001798
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001799 // Translates type to bit flag. The type must correspond to a Java type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001800 static uint64_t TypeFlag(DataType::Type type, int offset) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001801 int shift;
1802 switch (type) {
1803 case DataType::Type::kReference: shift = 0; break;
1804 case DataType::Type::kBool: shift = 1; break;
1805 case DataType::Type::kInt8: shift = 2; break;
1806 case DataType::Type::kUint16: shift = 3; break;
1807 case DataType::Type::kInt16: shift = 4; break;
1808 case DataType::Type::kInt32: shift = 5; break;
1809 case DataType::Type::kInt64: shift = 6; break;
1810 case DataType::Type::kFloat32: shift = 7; break;
1811 case DataType::Type::kFloat64: shift = 8; break;
1812 default:
1813 LOG(FATAL) << "Unexpected data type " << type;
1814 UNREACHABLE();
1815 }
Aart Bik854a02b2015-07-14 16:07:00 -07001816 DCHECK_LE(kFieldWriteOffset, shift);
1817 DCHECK_LT(shift, kArrayWriteOffset);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001818 return UINT64_C(1) << (shift + offset);
Aart Bik854a02b2015-07-14 16:07:00 -07001819 }
1820
1821 // Private constructor on direct flags value.
1822 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1823
1824 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001825};
1826
David Brazdiled596192015-01-23 10:39:45 +00001827// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001828class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001829 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001830 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
Mingyao Yang01b47b02017-02-03 12:09:57 -08001831 size_t number_of_vregs,
1832 ArtMethod* method,
1833 uint32_t dex_pc,
1834 HInstruction* holder)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001835 : vregs_(number_of_vregs, allocator->Adapter(kArenaAllocEnvironmentVRegs)),
1836 locations_(allocator->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001837 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001838 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001839 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001840 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001841 }
1842
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001843 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
1844 const HEnvironment& to_copy,
1845 HInstruction* holder)
1846 : HEnvironment(allocator,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001847 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001848 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001849 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001850 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001851
Vladimir Markoec32f642017-06-02 10:51:55 +01001852 void AllocateLocations() {
1853 DCHECK(locations_.empty());
1854 locations_.resize(vregs_.size());
1855 }
1856
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001857 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001858 if (parent_ != nullptr) {
1859 parent_->SetAndCopyParentChain(allocator, parent);
1860 } else {
1861 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1862 parent_->CopyFrom(parent);
1863 if (parent->GetParent() != nullptr) {
1864 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1865 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001866 }
David Brazdiled596192015-01-23 10:39:45 +00001867 }
1868
Vladimir Marko69d310e2017-10-09 14:12:23 +01001869 void CopyFrom(ArrayRef<HInstruction* const> locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001870 void CopyFrom(HEnvironment* environment);
1871
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001872 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1873 // input to the loop phi instead. This is for inserting instructions that
1874 // require an environment (like HDeoptimization) in the loop pre-header.
1875 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001876
1877 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001878 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001879 }
1880
1881 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001882 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001883 }
1884
David Brazdil1abb4192015-02-17 18:33:36 +00001885 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001886
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001887 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001888
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001889 HEnvironment* GetParent() const { return parent_; }
1890
1891 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001892 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001893 }
1894
1895 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001896 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001897 }
1898
1899 uint32_t GetDexPc() const {
1900 return dex_pc_;
1901 }
1902
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001903 ArtMethod* GetMethod() const {
1904 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001905 }
1906
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001907 HInstruction* GetHolder() const {
1908 return holder_;
1909 }
1910
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001911
1912 bool IsFromInlinedInvoke() const {
1913 return GetParent() != nullptr;
1914 }
1915
David Brazdiled596192015-01-23 10:39:45 +00001916 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001917 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1918 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001919 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001920 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001921 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00001922
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001923 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001924 HInstruction* const holder_;
1925
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001926 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001927
1928 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1929};
1930
Vladimir Markof9f64412015-09-02 14:05:49 +01001931class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001932 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001933 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001934 : previous_(nullptr),
1935 next_(nullptr),
1936 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001937 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001938 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001939 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001940 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001941 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001942 locations_(nullptr),
1943 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001944 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001945 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001946 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1947 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1948 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001949
Dave Allison20dfc792014-06-16 20:44:29 -07001950 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001951
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001952#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001953 enum InstructionKind {
1954 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1955 };
1956#undef DECLARE_KIND
1957
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001958 HInstruction* GetNext() const { return next_; }
1959 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001960
Calin Juravle77520bc2015-01-12 18:45:46 +00001961 HInstruction* GetNextDisregardingMoves() const;
1962 HInstruction* GetPreviousDisregardingMoves() const;
1963
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001964 HBasicBlock* GetBlock() const { return block_; }
Vladimir Markoca6fff82017-10-03 14:49:14 +01001965 ArenaAllocator* GetAllocator() const { return block_->GetGraph()->GetAllocator(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001966 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001967 bool IsInBlock() const { return block_ != nullptr; }
1968 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001969 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1970 bool IsIrreducibleLoopHeaderPhi() const {
1971 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1972 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001973
Vladimir Marko372f10e2016-05-17 16:30:10 +01001974 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
1975
1976 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
1977 // One virtual method is enough, just const_cast<> and then re-add the const.
1978 return ArrayRef<const HUserRecord<HInstruction*>>(
1979 const_cast<HInstruction*>(this)->GetInputRecords());
1980 }
1981
Vladimir Markoe9004912016-06-16 16:50:52 +01001982 HInputsRef GetInputs() {
1983 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001984 }
1985
Vladimir Markoe9004912016-06-16 16:50:52 +01001986 HConstInputsRef GetInputs() const {
1987 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001988 }
1989
1990 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001991 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001992
Aart Bik2767f4b2016-10-28 15:03:53 -07001993 bool HasInput(HInstruction* input) const {
1994 for (const HInstruction* i : GetInputs()) {
1995 if (i == input) {
1996 return true;
1997 }
1998 }
1999 return false;
2000 }
2001
Vladimir Marko372f10e2016-05-17 16:30:10 +01002002 void SetRawInputAt(size_t index, HInstruction* input) {
2003 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
2004 }
2005
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002006 virtual void Accept(HGraphVisitor* visitor) = 0;
2007 virtual const char* DebugName() const = 0;
2008
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002009 virtual DataType::Type GetType() const { return DataType::Type::kVoid; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002010
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002011 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002012
2013 uint32_t GetDexPc() const { return dex_pc_; }
2014
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002015 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01002016
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002017 // Can the instruction throw?
2018 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
2019 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01002020 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01002021 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002022
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002023 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002024 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002025
Calin Juravle10e244f2015-01-26 18:54:32 +00002026 // Does not apply for all instructions, but having this at top level greatly
2027 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00002028 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002029 virtual bool CanBeNull() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002030 DCHECK_EQ(GetType(), DataType::Type::kReference) << "CanBeNull only applies to reference types";
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002031 return true;
2032 }
Calin Juravle10e244f2015-01-26 18:54:32 +00002033
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002034 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01002035 return false;
2036 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002037
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002038 virtual bool IsActualObject() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002039 return GetType() == DataType::Type::kReference;
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002040 }
2041
Calin Juravle2e768302015-07-28 14:41:11 +00002042 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00002043
Calin Juravle61d544b2015-02-23 16:46:57 +00002044 ReferenceTypeInfo GetReferenceTypeInfo() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002045 DCHECK_EQ(GetType(), DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002046 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00002047 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00002048 }
Calin Juravleacf735c2015-02-12 15:25:22 +00002049
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002050 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00002051 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002052 // Note: fixup_end remains valid across push_front().
2053 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
2054 HUseListNode<HInstruction*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002055 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HInstruction*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002056 uses_.push_front(*new_node);
2057 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002058 }
2059
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002060 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01002061 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002062 // Note: env_fixup_end remains valid across push_front().
2063 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
2064 HUseListNode<HEnvironment*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002065 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HEnvironment*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002066 env_uses_.push_front(*new_node);
2067 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002068 }
2069
David Brazdil1abb4192015-02-17 18:33:36 +00002070 void RemoveAsUserOfInput(size_t input) {
2071 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002072 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2073 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2074 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00002075 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002076
Vladimir Marko372f10e2016-05-17 16:30:10 +01002077 void RemoveAsUserOfAllInputs() {
2078 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2079 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2080 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2081 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
2082 }
2083 }
2084
David Brazdil1abb4192015-02-17 18:33:36 +00002085 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2086 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002087
Vladimir Marko46817b82016-03-29 12:21:58 +01002088 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2089 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2090 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002091 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002092 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002093 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002094
Aart Bikcc42be02016-10-20 16:14:16 -07002095 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002096 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002097 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002098 !CanThrow() &&
2099 !IsSuspendCheck() &&
2100 !IsControlFlow() &&
2101 !IsNativeDebugInfo() &&
2102 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002103 // If we added an explicit barrier then we should keep it.
Igor Murashkind01745e2017-04-05 16:40:31 -07002104 !IsMemoryBarrier() &&
2105 !IsConstructorFence();
Aart Bik482095d2016-10-10 15:39:10 -07002106 }
2107
Aart Bikcc42be02016-10-20 16:14:16 -07002108 bool IsDeadAndRemovable() const {
2109 return IsRemovable() && !HasUses();
2110 }
2111
Roland Levillain6c82d402014-10-13 16:10:27 +01002112 // Does this instruction strictly dominate `other_instruction`?
2113 // Returns false if this instruction and `other_instruction` are the same.
2114 // Aborts if this instruction and `other_instruction` are both phis.
2115 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002116
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002117 int GetId() const { return id_; }
2118 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002119
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002120 int GetSsaIndex() const { return ssa_index_; }
2121 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2122 bool HasSsaIndex() const { return ssa_index_ != -1; }
2123
2124 bool HasEnvironment() const { return environment_ != nullptr; }
2125 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002126 // Set the `environment_` field. Raw because this method does not
2127 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002128 void SetRawEnvironment(HEnvironment* environment) {
2129 DCHECK(environment_ == nullptr);
2130 DCHECK_EQ(environment->GetHolder(), this);
2131 environment_ = environment;
2132 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002133
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002134 void InsertRawEnvironment(HEnvironment* environment) {
2135 DCHECK(environment_ != nullptr);
2136 DCHECK_EQ(environment->GetHolder(), this);
2137 DCHECK(environment->GetParent() == nullptr);
2138 environment->parent_ = environment_;
2139 environment_ = environment;
2140 }
2141
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002142 void RemoveEnvironment();
2143
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002144 // Set the environment of this instruction, copying it from `environment`. While
2145 // copying, the uses lists are being updated.
2146 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002147 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002148 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002149 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002150 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002151 if (environment->GetParent() != nullptr) {
2152 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2153 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002154 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002155
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002156 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2157 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002158 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002159 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002160 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002161 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002162 if (environment->GetParent() != nullptr) {
2163 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2164 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002165 }
2166
Nicolas Geoffray39468442014-09-02 15:17:15 +01002167 // Returns the number of entries in the environment. Typically, that is the
2168 // number of dex registers in a method. It could be more in case of inlining.
2169 size_t EnvironmentSize() const;
2170
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002171 LocationSummary* GetLocations() const { return locations_; }
2172 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002173
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002174 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002175 void ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002176 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002177
Alexandre Rames188d4312015-04-09 18:30:21 +01002178 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2179 // uses of this instruction by `other` are *not* updated.
2180 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2181 ReplaceWith(other);
2182 other->ReplaceInput(this, use_index);
2183 }
2184
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002185 // Move `this` instruction before `cursor`
2186 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002187
Vladimir Markofb337ea2015-11-25 15:25:10 +00002188 // Move `this` before its first user and out of any loops. If there is no
2189 // out-of-loop user that dominates all other users, move the instruction
2190 // to the end of the out-of-loop common dominator of the user's blocks.
2191 //
2192 // This can be used only on non-throwing instructions with no side effects that
2193 // have at least one use but no environment uses.
2194 void MoveBeforeFirstUserAndOutOfLoops();
2195
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002196#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002197 bool Is##type() const; \
2198 const H##type* As##type() const; \
2199 H##type* As##type();
2200
2201 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2202#undef INSTRUCTION_TYPE_CHECK
2203
2204#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002205 bool Is##type() const { return (As##type() != nullptr); } \
2206 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002207 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002208 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002209#undef INSTRUCTION_TYPE_CHECK
2210
Artem Serovcced8ba2017-07-19 18:18:09 +01002211 // Return a clone of the instruction if it is clonable (shallow copy by default, custom copy
2212 // if a custom copy-constructor is provided for a particular type). If IsClonable() is false for
2213 // the instruction then the behaviour of this function is undefined.
2214 //
2215 // Note: It is semantically valid to create a clone of the instruction only until
2216 // prepare_for_register_allocator phase as lifetime, intervals and codegen info are not
2217 // copied.
2218 //
2219 // Note: HEnvironment and some other fields are not copied and are set to default values, see
2220 // 'explicit HInstruction(const HInstruction& other)' for details.
2221 virtual HInstruction* Clone(ArenaAllocator* arena ATTRIBUTE_UNUSED) const {
2222 LOG(FATAL) << "Cloning is not implemented for the instruction " <<
2223 DebugName() << " " << GetId();
2224 UNREACHABLE();
2225 }
2226
2227 // Return whether instruction can be cloned (copied).
2228 virtual bool IsClonable() const { return false; }
2229
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002230 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002231 // TODO: this method is used by LICM and GVN with possibly different
2232 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002233 virtual bool CanBeMoved() const { return false; }
2234
2235 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002236 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002237 return false;
2238 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002239
2240 // Returns whether any data encoded in the two instructions is equal.
2241 // This method does not look at the inputs. Both instructions must be
2242 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002243 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002244 return false;
2245 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002246
2247 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002248 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002249 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002250 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002251
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002252 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2253 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2254 // the virtual function because the __attribute__((__pure__)) doesn't really
2255 // apply the strong requirement for virtual functions, preventing optimizations.
2256 InstructionKind GetKind() const PURE;
2257 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002258
2259 virtual size_t ComputeHashCode() const {
2260 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002261 for (const HInstruction* input : GetInputs()) {
2262 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002263 }
2264 return result;
2265 }
2266
2267 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002268 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002269 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002270
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002271 size_t GetLifetimePosition() const { return lifetime_position_; }
2272 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2273 LiveInterval* GetLiveInterval() const { return live_interval_; }
2274 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2275 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2276
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002277 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2278
2279 // Returns whether the code generation of the instruction will require to have access
2280 // to the current method. Such instructions are:
2281 // (1): Instructions that require an environment, as calling the runtime requires
2282 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002283 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2284 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002285 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002286 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002287 }
2288
Vladimir Markodc151b22015-10-15 18:02:30 +01002289 // Returns whether the code generation of the instruction will require to have access
2290 // to the dex cache of the current method's declaring class via the current method.
2291 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002292
Mark Mendellc4701932015-04-10 13:18:51 -04002293 // Does this instruction have any use in an environment before
2294 // control flow hits 'other'?
2295 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2296
2297 // Remove all references to environment uses of this instruction.
2298 // The caller must ensure that this is safe to do.
2299 void RemoveEnvironmentUsers();
2300
Vladimir Markoa1de9182016-02-25 11:37:38 +00002301 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2302 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002303
David Brazdil1abb4192015-02-17 18:33:36 +00002304 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002305 // If set, the machine code for this instruction is assumed to be generated by
2306 // its users. Used by liveness analysis to compute use positions accordingly.
2307 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2308 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2309 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2310 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2311
Vladimir Marko372f10e2016-05-17 16:30:10 +01002312 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2313 return GetInputRecords()[i];
2314 }
2315
2316 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2317 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2318 input_records[index] = input;
2319 }
David Brazdil1abb4192015-02-17 18:33:36 +00002320
Vladimir Markoa1de9182016-02-25 11:37:38 +00002321 uint32_t GetPackedFields() const {
2322 return packed_fields_;
2323 }
2324
2325 template <size_t flag>
2326 bool GetPackedFlag() const {
2327 return (packed_fields_ & (1u << flag)) != 0u;
2328 }
2329
2330 template <size_t flag>
2331 void SetPackedFlag(bool value = true) {
2332 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2333 }
2334
2335 template <typename BitFieldType>
2336 typename BitFieldType::value_type GetPackedField() const {
2337 return BitFieldType::Decode(packed_fields_);
2338 }
2339
2340 template <typename BitFieldType>
2341 void SetPackedField(typename BitFieldType::value_type value) {
2342 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2343 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2344 }
2345
Artem Serovcced8ba2017-07-19 18:18:09 +01002346 // Copy construction for the instruction (used for Clone function).
2347 //
2348 // Fields (e.g. lifetime, intervals and codegen info) associated with phases starting from
2349 // prepare_for_register_allocator are not copied (set to default values).
2350 //
2351 // Copy constructors must be provided for every HInstruction type; default copy constructor is
2352 // fine for most of them. However for some of the instructions a custom copy constructor must be
2353 // specified (when instruction has non-trivially copyable fields and must have a special behaviour
2354 // for copying them).
2355 explicit HInstruction(const HInstruction& other)
2356 : previous_(nullptr),
2357 next_(nullptr),
2358 block_(nullptr),
2359 dex_pc_(other.dex_pc_),
2360 id_(-1),
2361 ssa_index_(-1),
2362 packed_fields_(other.packed_fields_),
2363 environment_(nullptr),
2364 locations_(nullptr),
2365 live_interval_(nullptr),
2366 lifetime_position_(kNoLifetime),
2367 side_effects_(other.side_effects_),
2368 reference_type_handle_(other.reference_type_handle_) {}
2369
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002370 private:
Vladimir Marko46817b82016-03-29 12:21:58 +01002371 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2372 auto before_use_node = uses_.before_begin();
2373 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2374 HInstruction* user = use_node->GetUser();
2375 size_t input_index = use_node->GetIndex();
2376 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2377 before_use_node = use_node;
2378 }
2379 }
2380
2381 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2382 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2383 if (next != uses_.end()) {
2384 HInstruction* next_user = next->GetUser();
2385 size_t next_index = next->GetIndex();
2386 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2387 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2388 }
2389 }
2390
2391 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2392 auto before_env_use_node = env_uses_.before_begin();
2393 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2394 HEnvironment* user = env_use_node->GetUser();
2395 size_t input_index = env_use_node->GetIndex();
2396 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2397 before_env_use_node = env_use_node;
2398 }
2399 }
2400
2401 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2402 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2403 if (next != env_uses_.end()) {
2404 HEnvironment* next_user = next->GetUser();
2405 size_t next_index = next->GetIndex();
2406 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2407 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2408 }
2409 }
David Brazdil1abb4192015-02-17 18:33:36 +00002410
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002411 HInstruction* previous_;
2412 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002413 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002414 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002415
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002416 // An instruction gets an id when it is added to the graph.
2417 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002418 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002419 int id_;
2420
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002421 // When doing liveness analysis, instructions that have uses get an SSA index.
2422 int ssa_index_;
2423
Vladimir Markoa1de9182016-02-25 11:37:38 +00002424 // Packed fields.
2425 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002426
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002427 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002428 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002429
2430 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002431 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002432
Nicolas Geoffray39468442014-09-02 15:17:15 +01002433 // The environment associated with this instruction. Not null if the instruction
2434 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002435 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002436
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002437 // Set by the code generator.
2438 LocationSummary* locations_;
2439
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002440 // Set by the liveness analysis.
2441 LiveInterval* live_interval_;
2442
2443 // Set by the liveness analysis, this is the position in a linear
2444 // order of blocks where this instruction's live interval start.
2445 size_t lifetime_position_;
2446
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002447 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002448
Vladimir Markoa1de9182016-02-25 11:37:38 +00002449 // The reference handle part of the reference type info.
2450 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002451 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002452 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002453
David Brazdil1abb4192015-02-17 18:33:36 +00002454 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002455 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002456 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002457 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002458 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002459};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002460std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002461
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002462// Iterates over the instructions, while preserving the next instruction
2463// in case the current instruction gets removed from the list by the user
2464// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002465class HInstructionIterator : public ValueObject {
2466 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002467 explicit HInstructionIterator(const HInstructionList& instructions)
2468 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002469 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002470 }
2471
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002472 bool Done() const { return instruction_ == nullptr; }
2473 HInstruction* Current() const { return instruction_; }
2474 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002475 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002476 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002477 }
2478
2479 private:
2480 HInstruction* instruction_;
2481 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002482
2483 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002484};
2485
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002486// Iterates over the instructions without saving the next instruction,
2487// therefore handling changes in the graph potentially made by the user
2488// of this iterator.
2489class HInstructionIteratorHandleChanges : public ValueObject {
2490 public:
2491 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2492 : instruction_(instructions.first_instruction_) {
2493 }
2494
2495 bool Done() const { return instruction_ == nullptr; }
2496 HInstruction* Current() const { return instruction_; }
2497 void Advance() {
2498 instruction_ = instruction_->GetNext();
2499 }
2500
2501 private:
2502 HInstruction* instruction_;
2503
2504 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2505};
2506
2507
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002508class HBackwardInstructionIterator : public ValueObject {
2509 public:
2510 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2511 : instruction_(instructions.last_instruction_) {
2512 next_ = Done() ? nullptr : instruction_->GetPrevious();
2513 }
2514
2515 bool Done() const { return instruction_ == nullptr; }
2516 HInstruction* Current() const { return instruction_; }
2517 void Advance() {
2518 instruction_ = next_;
2519 next_ = Done() ? nullptr : instruction_->GetPrevious();
2520 }
2521
2522 private:
2523 HInstruction* instruction_;
2524 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002525
2526 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002527};
2528
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002529class HVariableInputSizeInstruction : public HInstruction {
2530 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002531 using HInstruction::GetInputRecords; // Keep the const version visible.
2532 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
2533 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2534 }
2535
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002536 void AddInput(HInstruction* input);
2537 void InsertInputAt(size_t index, HInstruction* input);
2538 void RemoveInputAt(size_t index);
2539
Igor Murashkind01745e2017-04-05 16:40:31 -07002540 // Removes all the inputs.
2541 // Also removes this instructions from each input's use list
2542 // (for non-environment uses only).
2543 void RemoveAllInputs();
2544
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002545 protected:
2546 HVariableInputSizeInstruction(SideEffects side_effects,
2547 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002548 ArenaAllocator* allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002549 size_t number_of_inputs,
2550 ArenaAllocKind kind)
2551 : HInstruction(side_effects, dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002552 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002553
Artem Serovcced8ba2017-07-19 18:18:09 +01002554 DEFAULT_COPY_CONSTRUCTOR(VariableInputSizeInstruction);
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002555
Artem Serovcced8ba2017-07-19 18:18:09 +01002556 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002557};
2558
Vladimir Markof9f64412015-09-02 14:05:49 +01002559template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002560class HTemplateInstruction: public HInstruction {
2561 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002562 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002563 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002564 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002565
Vladimir Marko372f10e2016-05-17 16:30:10 +01002566 using HInstruction::GetInputRecords; // Keep the const version visible.
2567 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2568 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002569 }
2570
Artem Serovcced8ba2017-07-19 18:18:09 +01002571 protected:
2572 DEFAULT_COPY_CONSTRUCTOR(TemplateInstruction<N>);
2573
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002574 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002575 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002576
2577 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002578};
2579
Vladimir Markof9f64412015-09-02 14:05:49 +01002580// HTemplateInstruction specialization for N=0.
2581template<>
2582class HTemplateInstruction<0>: public HInstruction {
2583 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002584 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002585 : HInstruction(side_effects, dex_pc) {}
2586
Vladimir Markof9f64412015-09-02 14:05:49 +01002587 virtual ~HTemplateInstruction() {}
2588
Vladimir Marko372f10e2016-05-17 16:30:10 +01002589 using HInstruction::GetInputRecords; // Keep the const version visible.
2590 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2591 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002592 }
2593
Artem Serovcced8ba2017-07-19 18:18:09 +01002594 protected:
2595 DEFAULT_COPY_CONSTRUCTOR(TemplateInstruction<0>);
2596
Vladimir Markof9f64412015-09-02 14:05:49 +01002597 private:
2598 friend class SsaBuilder;
2599};
2600
Dave Allison20dfc792014-06-16 20:44:29 -07002601template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002602class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002603 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002604 HExpression<N>(DataType::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002605 : HTemplateInstruction<N>(side_effects, dex_pc) {
2606 this->template SetPackedField<TypeField>(type);
2607 }
Dave Allison20dfc792014-06-16 20:44:29 -07002608 virtual ~HExpression() {}
2609
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002610 DataType::Type GetType() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00002611 return TypeField::Decode(this->GetPackedFields());
2612 }
Dave Allison20dfc792014-06-16 20:44:29 -07002613
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002614 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002615 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2616 static constexpr size_t kFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002617 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00002618 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2619 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2620 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002621 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
Artem Serovcced8ba2017-07-19 18:18:09 +01002622 DEFAULT_COPY_CONSTRUCTOR(Expression<N>);
Dave Allison20dfc792014-06-16 20:44:29 -07002623};
2624
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002625// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2626// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002627class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002628 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002629 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2630 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002631
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002632 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002633
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002634 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002635
Artem Serovcced8ba2017-07-19 18:18:09 +01002636 protected:
2637 DEFAULT_COPY_CONSTRUCTOR(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002638};
2639
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002640// Represents dex's RETURN opcodes. A HReturn is a control flow
2641// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002642class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002643 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002644 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2645 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002646 SetRawInputAt(0, value);
2647 }
2648
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002649 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002650
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002651 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002652
Artem Serovcced8ba2017-07-19 18:18:09 +01002653 protected:
2654 DEFAULT_COPY_CONSTRUCTOR(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002655};
2656
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002657class HPhi FINAL : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002658 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002659 HPhi(ArenaAllocator* allocator,
David Brazdildee58d62016-04-07 09:54:26 +00002660 uint32_t reg_number,
2661 size_t number_of_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002662 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +00002663 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002664 : HVariableInputSizeInstruction(
2665 SideEffects::None(),
2666 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002667 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002668 number_of_inputs,
2669 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002670 reg_number_(reg_number) {
2671 SetPackedField<TypeField>(ToPhiType(type));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002672 DCHECK_NE(GetType(), DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +00002673 // Phis are constructed live and marked dead if conflicting or unused.
2674 // Individual steps of SsaBuilder should assume that if a phi has been
2675 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2676 SetPackedFlag<kFlagIsLive>(true);
2677 SetPackedFlag<kFlagCanBeNull>(true);
2678 }
2679
Artem Serovcced8ba2017-07-19 18:18:09 +01002680 bool IsClonable() const OVERRIDE { return true; }
2681
David Brazdildee58d62016-04-07 09:54:26 +00002682 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002683 static DataType::Type ToPhiType(DataType::Type type) {
2684 return DataType::Kind(type);
David Brazdildee58d62016-04-07 09:54:26 +00002685 }
2686
2687 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2688
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002689 DataType::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2690 void SetType(DataType::Type new_type) {
David Brazdildee58d62016-04-07 09:54:26 +00002691 // Make sure that only valid type changes occur. The following are allowed:
2692 // (1) int -> float/ref (primitive type propagation),
2693 // (2) long -> double (primitive type propagation).
2694 DCHECK(GetType() == new_type ||
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002695 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kFloat32) ||
2696 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kReference) ||
2697 (GetType() == DataType::Type::kInt64 && new_type == DataType::Type::kFloat64));
David Brazdildee58d62016-04-07 09:54:26 +00002698 SetPackedField<TypeField>(new_type);
2699 }
2700
2701 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2702 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2703
2704 uint32_t GetRegNumber() const { return reg_number_; }
2705
2706 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2707 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2708 bool IsDead() const { return !IsLive(); }
2709 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2710
Vladimir Markoe9004912016-06-16 16:50:52 +01002711 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002712 return other != nullptr
2713 && other->IsPhi()
2714 && other->AsPhi()->GetBlock() == GetBlock()
2715 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2716 }
2717
Nicolas Geoffrayf57c1ae2017-06-28 17:40:18 +01002718 bool HasEquivalentPhi() const {
2719 if (GetPrevious() != nullptr && GetPrevious()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2720 return true;
2721 }
2722 if (GetNext() != nullptr && GetNext()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2723 return true;
2724 }
2725 return false;
2726 }
2727
David Brazdildee58d62016-04-07 09:54:26 +00002728 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2729 // An equivalent phi is a phi having the same dex register and type.
2730 // It assumes that phis with the same dex register are adjacent.
2731 HPhi* GetNextEquivalentPhiWithSameType() {
2732 HInstruction* next = GetNext();
2733 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2734 if (next->GetType() == GetType()) {
2735 return next->AsPhi();
2736 }
2737 next = next->GetNext();
2738 }
2739 return nullptr;
2740 }
2741
2742 DECLARE_INSTRUCTION(Phi);
2743
Artem Serovcced8ba2017-07-19 18:18:09 +01002744 protected:
2745 DEFAULT_COPY_CONSTRUCTOR(Phi);
2746
David Brazdildee58d62016-04-07 09:54:26 +00002747 private:
2748 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2749 static constexpr size_t kFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002750 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
David Brazdildee58d62016-04-07 09:54:26 +00002751 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2752 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2753 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2754 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002755 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
David Brazdildee58d62016-04-07 09:54:26 +00002756
David Brazdildee58d62016-04-07 09:54:26 +00002757 const uint32_t reg_number_;
David Brazdildee58d62016-04-07 09:54:26 +00002758};
2759
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002760// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002761// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002762// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002763class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002764 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002765 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002766
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002767 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002768
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002769 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002770
Artem Serovcced8ba2017-07-19 18:18:09 +01002771 protected:
2772 DEFAULT_COPY_CONSTRUCTOR(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002773};
2774
2775// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002776class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002777 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002778 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002779
Artem Serovcced8ba2017-07-19 18:18:09 +01002780 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002781 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002782
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002783 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002784 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002785 }
2786
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002787 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002788
Artem Serovcced8ba2017-07-19 18:18:09 +01002789 protected:
2790 DEFAULT_COPY_CONSTRUCTOR(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002791};
2792
Roland Levillain9867bc72015-08-05 10:21:34 +01002793class HConstant : public HExpression<0> {
2794 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002795 explicit HConstant(DataType::Type type, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002796 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002797
2798 bool CanBeMoved() const OVERRIDE { return true; }
2799
Roland Levillain1a653882016-03-18 18:05:57 +00002800 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002801 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002802 // Is this constant 0 in the arithmetic sense?
2803 virtual bool IsArithmeticZero() const { return false; }
2804 // Is this constant a 0-bit pattern?
2805 virtual bool IsZeroBitPattern() const { return false; }
2806 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002807 virtual bool IsOne() const { return false; }
2808
David Brazdil77a48ae2015-09-15 12:34:04 +00002809 virtual uint64_t GetValueAsUint64() const = 0;
2810
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002811 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002812
Artem Serovcced8ba2017-07-19 18:18:09 +01002813 protected:
2814 DEFAULT_COPY_CONSTRUCTOR(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002815};
2816
Vladimir Markofcb503c2016-05-18 12:48:17 +01002817class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002818 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002819 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002820 return true;
2821 }
2822
David Brazdil77a48ae2015-09-15 12:34:04 +00002823 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2824
Roland Levillain9867bc72015-08-05 10:21:34 +01002825 size_t ComputeHashCode() const OVERRIDE { return 0; }
2826
Roland Levillain1a653882016-03-18 18:05:57 +00002827 // The null constant representation is a 0-bit pattern.
2828 virtual bool IsZeroBitPattern() const { return true; }
2829
Roland Levillain9867bc72015-08-05 10:21:34 +01002830 DECLARE_INSTRUCTION(NullConstant);
2831
Artem Serovcced8ba2017-07-19 18:18:09 +01002832 protected:
2833 DEFAULT_COPY_CONSTRUCTOR(NullConstant);
2834
Roland Levillain9867bc72015-08-05 10:21:34 +01002835 private:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002836 explicit HNullConstant(uint32_t dex_pc = kNoDexPc)
2837 : HConstant(DataType::Type::kReference, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002838
2839 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01002840};
2841
2842// Constants of the type int. Those can be from Dex instructions, or
2843// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002844class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002845 public:
2846 int32_t GetValue() const { return value_; }
2847
David Brazdil9f389d42015-10-01 14:32:56 +01002848 uint64_t GetValueAsUint64() const OVERRIDE {
2849 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2850 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002851
Vladimir Marko372f10e2016-05-17 16:30:10 +01002852 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002853 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002854 return other->AsIntConstant()->value_ == value_;
2855 }
2856
2857 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2858
2859 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002860 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2861 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002862 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2863
Roland Levillain1a653882016-03-18 18:05:57 +00002864 // Integer constants are used to encode Boolean values as well,
2865 // where 1 means true and 0 means false.
2866 bool IsTrue() const { return GetValue() == 1; }
2867 bool IsFalse() const { return GetValue() == 0; }
2868
Roland Levillain9867bc72015-08-05 10:21:34 +01002869 DECLARE_INSTRUCTION(IntConstant);
2870
Artem Serovcced8ba2017-07-19 18:18:09 +01002871 protected:
2872 DEFAULT_COPY_CONSTRUCTOR(IntConstant);
2873
Roland Levillain9867bc72015-08-05 10:21:34 +01002874 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002875 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002876 : HConstant(DataType::Type::kInt32, dex_pc), value_(value) {}
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002877 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002878 : HConstant(DataType::Type::kInt32, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002879
2880 const int32_t value_;
2881
2882 friend class HGraph;
2883 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2884 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Roland Levillain9867bc72015-08-05 10:21:34 +01002885};
2886
Vladimir Markofcb503c2016-05-18 12:48:17 +01002887class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002888 public:
2889 int64_t GetValue() const { return value_; }
2890
David Brazdil77a48ae2015-09-15 12:34:04 +00002891 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2892
Vladimir Marko372f10e2016-05-17 16:30:10 +01002893 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002894 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002895 return other->AsLongConstant()->value_ == value_;
2896 }
2897
2898 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2899
2900 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002901 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2902 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002903 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2904
2905 DECLARE_INSTRUCTION(LongConstant);
2906
Artem Serovcced8ba2017-07-19 18:18:09 +01002907 protected:
2908 DEFAULT_COPY_CONSTRUCTOR(LongConstant);
2909
Roland Levillain9867bc72015-08-05 10:21:34 +01002910 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002911 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002912 : HConstant(DataType::Type::kInt64, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002913
2914 const int64_t value_;
2915
2916 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01002917};
Dave Allison20dfc792014-06-16 20:44:29 -07002918
Vladimir Markofcb503c2016-05-18 12:48:17 +01002919class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002920 public:
2921 float GetValue() const { return value_; }
2922
2923 uint64_t GetValueAsUint64() const OVERRIDE {
2924 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2925 }
2926
Vladimir Marko372f10e2016-05-17 16:30:10 +01002927 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002928 DCHECK(other->IsFloatConstant()) << other->DebugName();
2929 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2930 }
2931
2932 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2933
2934 bool IsMinusOne() const OVERRIDE {
2935 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2936 }
Roland Levillain1a653882016-03-18 18:05:57 +00002937 bool IsArithmeticZero() const OVERRIDE {
2938 return std::fpclassify(value_) == FP_ZERO;
2939 }
2940 bool IsArithmeticPositiveZero() const {
2941 return IsArithmeticZero() && !std::signbit(value_);
2942 }
2943 bool IsArithmeticNegativeZero() const {
2944 return IsArithmeticZero() && std::signbit(value_);
2945 }
2946 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002947 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002948 }
2949 bool IsOne() const OVERRIDE {
2950 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2951 }
2952 bool IsNaN() const {
2953 return std::isnan(value_);
2954 }
2955
2956 DECLARE_INSTRUCTION(FloatConstant);
2957
Artem Serovcced8ba2017-07-19 18:18:09 +01002958 protected:
2959 DEFAULT_COPY_CONSTRUCTOR(FloatConstant);
2960
Roland Levillain31dd3d62016-02-16 12:21:02 +00002961 private:
2962 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002963 : HConstant(DataType::Type::kFloat32, dex_pc), value_(value) {}
Roland Levillain31dd3d62016-02-16 12:21:02 +00002964 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002965 : HConstant(DataType::Type::kFloat32, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
Roland Levillain31dd3d62016-02-16 12:21:02 +00002966
2967 const float value_;
2968
2969 // Only the SsaBuilder and HGraph can create floating-point constants.
2970 friend class SsaBuilder;
2971 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00002972};
2973
Vladimir Markofcb503c2016-05-18 12:48:17 +01002974class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002975 public:
2976 double GetValue() const { return value_; }
2977
2978 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2979
Vladimir Marko372f10e2016-05-17 16:30:10 +01002980 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002981 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2982 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2983 }
2984
2985 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2986
2987 bool IsMinusOne() const OVERRIDE {
2988 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2989 }
Roland Levillain1a653882016-03-18 18:05:57 +00002990 bool IsArithmeticZero() const OVERRIDE {
2991 return std::fpclassify(value_) == FP_ZERO;
2992 }
2993 bool IsArithmeticPositiveZero() const {
2994 return IsArithmeticZero() && !std::signbit(value_);
2995 }
2996 bool IsArithmeticNegativeZero() const {
2997 return IsArithmeticZero() && std::signbit(value_);
2998 }
2999 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003000 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00003001 }
3002 bool IsOne() const OVERRIDE {
3003 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3004 }
3005 bool IsNaN() const {
3006 return std::isnan(value_);
3007 }
3008
3009 DECLARE_INSTRUCTION(DoubleConstant);
3010
Artem Serovcced8ba2017-07-19 18:18:09 +01003011 protected:
3012 DEFAULT_COPY_CONSTRUCTOR(DoubleConstant);
3013
Roland Levillain31dd3d62016-02-16 12:21:02 +00003014 private:
3015 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003016 : HConstant(DataType::Type::kFloat64, dex_pc), value_(value) {}
Roland Levillain31dd3d62016-02-16 12:21:02 +00003017 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003018 : HConstant(DataType::Type::kFloat64, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
Roland Levillain31dd3d62016-02-16 12:21:02 +00003019
3020 const double value_;
3021
3022 // Only the SsaBuilder and HGraph can create floating-point constants.
3023 friend class SsaBuilder;
3024 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003025};
3026
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003027// Conditional branch. A block ending with an HIf instruction must have
3028// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003029class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003030 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003031 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
3032 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003033 SetRawInputAt(0, input);
3034 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003035
Artem Serovcced8ba2017-07-19 18:18:09 +01003036 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003037 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003038
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003039 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003040 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003041 }
3042
3043 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003044 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003045 }
3046
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003047 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003048
Artem Serovcced8ba2017-07-19 18:18:09 +01003049 protected:
3050 DEFAULT_COPY_CONSTRUCTOR(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003051};
3052
David Brazdilfc6a86a2015-06-26 10:33:45 +00003053
3054// Abstract instruction which marks the beginning and/or end of a try block and
3055// links it to the respective exception handlers. Behaves the same as a Goto in
3056// non-exceptional control flow.
3057// Normal-flow successor is stored at index zero, exception handlers under
3058// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003059class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003060 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003061 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01003062 kEntry,
3063 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003064 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01003065 };
3066
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003067 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003068 : HTemplateInstruction(SideEffects::None(), dex_pc) {
3069 SetPackedField<BoundaryKindField>(kind);
3070 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003071
3072 bool IsControlFlow() const OVERRIDE { return true; }
3073
3074 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01003075 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003076
David Brazdild26a4112015-11-10 11:07:31 +00003077 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
3078 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
3079 }
3080
David Brazdilfc6a86a2015-06-26 10:33:45 +00003081 // Returns whether `handler` is among its exception handlers (non-zero index
3082 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01003083 bool HasExceptionHandler(const HBasicBlock& handler) const {
3084 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00003085 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00003086 }
3087
3088 // If not present already, adds `handler` to its block's list of exception
3089 // handlers.
3090 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01003091 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003092 GetBlock()->AddSuccessor(handler);
3093 }
3094 }
3095
Vladimir Markoa1de9182016-02-25 11:37:38 +00003096 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
3097 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003098
David Brazdilffee3d32015-07-06 11:48:53 +01003099 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
3100
David Brazdilfc6a86a2015-06-26 10:33:45 +00003101 DECLARE_INSTRUCTION(TryBoundary);
3102
Artem Serovcced8ba2017-07-19 18:18:09 +01003103 protected:
3104 DEFAULT_COPY_CONSTRUCTOR(TryBoundary);
3105
David Brazdilfc6a86a2015-06-26 10:33:45 +00003106 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003107 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
3108 static constexpr size_t kFieldBoundaryKindSize =
3109 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
3110 static constexpr size_t kNumberOfTryBoundaryPackedBits =
3111 kFieldBoundaryKind + kFieldBoundaryKindSize;
3112 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
3113 "Too many packed fields.");
3114 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00003115};
3116
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003117// Deoptimize to interpreter, upon checking a condition.
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003118class HDeoptimize FINAL : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003119 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003120 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
3121 // across.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003122 HDeoptimize(ArenaAllocator* allocator,
3123 HInstruction* cond,
3124 DeoptimizationKind kind,
3125 uint32_t dex_pc)
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003126 : HVariableInputSizeInstruction(
3127 SideEffects::All(),
3128 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003129 allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003130 /* number_of_inputs */ 1,
3131 kArenaAllocMisc) {
3132 SetPackedFlag<kFieldCanBeMoved>(false);
3133 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003134 SetRawInputAt(0, cond);
3135 }
3136
Artem Serovcced8ba2017-07-19 18:18:09 +01003137 bool IsClonable() const OVERRIDE { return true; }
3138
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003139 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
3140 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
3141 // instead of `guard`.
3142 // We set CanTriggerGC to prevent any intermediate address to be live
3143 // at the point of the `HDeoptimize`.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003144 HDeoptimize(ArenaAllocator* allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003145 HInstruction* cond,
3146 HInstruction* guard,
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003147 DeoptimizationKind kind,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003148 uint32_t dex_pc)
3149 : HVariableInputSizeInstruction(
3150 SideEffects::CanTriggerGC(),
3151 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003152 allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003153 /* number_of_inputs */ 2,
3154 kArenaAllocMisc) {
3155 SetPackedFlag<kFieldCanBeMoved>(true);
3156 SetPackedField<DeoptimizeKindField>(kind);
3157 SetRawInputAt(0, cond);
3158 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003159 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003160
3161 bool CanBeMoved() const OVERRIDE { return GetPackedFlag<kFieldCanBeMoved>(); }
3162
3163 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
3164 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
3165 }
3166
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003167 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003168
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003169 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003170
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003171 DeoptimizationKind GetDeoptimizationKind() const { return GetPackedField<DeoptimizeKindField>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003172
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003173 DataType::Type GetType() const OVERRIDE {
3174 return GuardsAnInput() ? GuardedInput()->GetType() : DataType::Type::kVoid;
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003175 }
3176
3177 bool GuardsAnInput() const {
3178 return InputCount() == 2;
3179 }
3180
3181 HInstruction* GuardedInput() const {
3182 DCHECK(GuardsAnInput());
3183 return InputAt(1);
3184 }
3185
3186 void RemoveGuard() {
3187 RemoveInputAt(1);
3188 }
3189
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003190 DECLARE_INSTRUCTION(Deoptimize);
3191
Artem Serovcced8ba2017-07-19 18:18:09 +01003192 protected:
3193 DEFAULT_COPY_CONSTRUCTOR(Deoptimize);
3194
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003195 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003196 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3197 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3198 static constexpr size_t kFieldDeoptimizeKindSize =
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003199 MinimumBitsToStore(static_cast<size_t>(DeoptimizationKind::kLast));
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003200 static constexpr size_t kNumberOfDeoptimizePackedBits =
3201 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3202 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3203 "Too many packed fields.");
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003204 using DeoptimizeKindField =
3205 BitField<DeoptimizationKind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003206};
3207
Mingyao Yang063fc772016-08-02 11:02:54 -07003208// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3209// The compiled code checks this flag value in a guard before devirtualized call and
3210// if it's true, starts to do deoptimization.
3211// It has a 4-byte slot on stack.
3212// TODO: allocate a register for this flag.
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003213class HShouldDeoptimizeFlag FINAL : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003214 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003215 // CHA guards are only optimized in a separate pass and it has no side effects
3216 // with regard to other passes.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003217 HShouldDeoptimizeFlag(ArenaAllocator* allocator, uint32_t dex_pc)
3218 : HVariableInputSizeInstruction(SideEffects::None(), dex_pc, allocator, 0, kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003219 }
3220
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003221 DataType::Type GetType() const OVERRIDE { return DataType::Type::kInt32; }
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003222
3223 // We do all CHA guard elimination/motion in a single pass, after which there is no
3224 // further guard elimination/motion since a guard might have been used for justification
3225 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3226 // to avoid other optimizations trying to move it.
Mingyao Yang063fc772016-08-02 11:02:54 -07003227 bool CanBeMoved() const OVERRIDE { return false; }
3228
3229 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3230
Artem Serovcced8ba2017-07-19 18:18:09 +01003231 protected:
3232 DEFAULT_COPY_CONSTRUCTOR(ShouldDeoptimizeFlag);
Mingyao Yang063fc772016-08-02 11:02:54 -07003233};
3234
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003235// Represents the ArtMethod that was passed as a first argument to
3236// the method. It is used by instructions that depend on it, like
3237// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003238class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003239 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003240 explicit HCurrentMethod(DataType::Type type, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003241 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003242
3243 DECLARE_INSTRUCTION(CurrentMethod);
3244
Artem Serovcced8ba2017-07-19 18:18:09 +01003245 protected:
3246 DEFAULT_COPY_CONSTRUCTOR(CurrentMethod);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003247};
3248
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003249// Fetches an ArtMethod from the virtual table or the interface method table
3250// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003251class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003252 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003253 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003254 kVTable,
3255 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003256 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003257 };
3258 HClassTableGet(HInstruction* cls,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003259 DataType::Type type,
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003260 TableKind kind,
3261 size_t index,
3262 uint32_t dex_pc)
3263 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003264 index_(index) {
3265 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003266 SetRawInputAt(0, cls);
3267 }
3268
Artem Serovcced8ba2017-07-19 18:18:09 +01003269 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003270 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003271 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003272 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003273 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003274 }
3275
Vladimir Markoa1de9182016-02-25 11:37:38 +00003276 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003277 size_t GetIndex() const { return index_; }
3278
3279 DECLARE_INSTRUCTION(ClassTableGet);
3280
Artem Serovcced8ba2017-07-19 18:18:09 +01003281 protected:
3282 DEFAULT_COPY_CONSTRUCTOR(ClassTableGet);
3283
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003284 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003285 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
3286 static constexpr size_t kFieldTableKindSize =
3287 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3288 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3289 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3290 "Too many packed fields.");
3291 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3292
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003293 // The index of the ArtMethod in the table.
3294 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003295};
3296
Mark Mendellfe57faa2015-09-18 09:26:15 -04003297// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3298// have one successor for each entry in the switch table, and the final successor
3299// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003300class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003301 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003302 HPackedSwitch(int32_t start_value,
3303 uint32_t num_entries,
3304 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003305 uint32_t dex_pc = kNoDexPc)
3306 : HTemplateInstruction(SideEffects::None(), dex_pc),
3307 start_value_(start_value),
3308 num_entries_(num_entries) {
3309 SetRawInputAt(0, input);
3310 }
3311
Artem Serovcced8ba2017-07-19 18:18:09 +01003312 bool IsClonable() const OVERRIDE { return true; }
3313
Mark Mendellfe57faa2015-09-18 09:26:15 -04003314 bool IsControlFlow() const OVERRIDE { return true; }
3315
3316 int32_t GetStartValue() const { return start_value_; }
3317
Vladimir Marko211c2112015-09-24 16:52:33 +01003318 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003319
3320 HBasicBlock* GetDefaultBlock() const {
3321 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003322 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003323 }
3324 DECLARE_INSTRUCTION(PackedSwitch);
3325
Artem Serovcced8ba2017-07-19 18:18:09 +01003326 protected:
3327 DEFAULT_COPY_CONSTRUCTOR(PackedSwitch);
3328
Mark Mendellfe57faa2015-09-18 09:26:15 -04003329 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003330 const int32_t start_value_;
3331 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003332};
3333
Roland Levillain88cb1752014-10-20 16:36:47 +01003334class HUnaryOperation : public HExpression<1> {
3335 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003336 HUnaryOperation(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003337 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003338 SetRawInputAt(0, input);
3339 }
3340
Artem Serovcced8ba2017-07-19 18:18:09 +01003341 // All of the UnaryOperation instructions are clonable.
3342 bool IsClonable() const OVERRIDE { return true; }
3343
Roland Levillain88cb1752014-10-20 16:36:47 +01003344 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003345 DataType::Type GetResultType() const { return GetType(); }
Roland Levillain88cb1752014-10-20 16:36:47 +01003346
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003347 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003348 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003349 return true;
3350 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003351
Roland Levillain31dd3d62016-02-16 12:21:02 +00003352 // Try to statically evaluate `this` and return a HConstant
3353 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003354 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003355 HConstant* TryStaticEvaluation() const;
3356
3357 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003358 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3359 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003360 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3361 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003362
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003363 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003364
Artem Serovcced8ba2017-07-19 18:18:09 +01003365 protected:
3366 DEFAULT_COPY_CONSTRUCTOR(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003367};
3368
Dave Allison20dfc792014-06-16 20:44:29 -07003369class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003370 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003371 HBinaryOperation(DataType::Type result_type,
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003372 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003373 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003374 SideEffects side_effects = SideEffects::None(),
3375 uint32_t dex_pc = kNoDexPc)
3376 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003377 SetRawInputAt(0, left);
3378 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003379 }
3380
Artem Serovcced8ba2017-07-19 18:18:09 +01003381 // All of the BinaryOperation instructions are clonable.
3382 bool IsClonable() const OVERRIDE { return true; }
3383
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003384 HInstruction* GetLeft() const { return InputAt(0); }
3385 HInstruction* GetRight() const { return InputAt(1); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003386 DataType::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003387
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003388 virtual bool IsCommutative() const { return false; }
3389
3390 // Put constant on the right.
3391 // Returns whether order is changed.
3392 bool OrderInputsWithConstantOnTheRight() {
3393 HInstruction* left = InputAt(0);
3394 HInstruction* right = InputAt(1);
3395 if (left->IsConstant() && !right->IsConstant()) {
3396 ReplaceInput(right, 0);
3397 ReplaceInput(left, 1);
3398 return true;
3399 }
3400 return false;
3401 }
3402
3403 // Order inputs by instruction id, but favor constant on the right side.
3404 // This helps GVN for commutative ops.
3405 void OrderInputs() {
3406 DCHECK(IsCommutative());
3407 HInstruction* left = InputAt(0);
3408 HInstruction* right = InputAt(1);
3409 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3410 return;
3411 }
3412 if (OrderInputsWithConstantOnTheRight()) {
3413 return;
3414 }
3415 // Order according to instruction id.
3416 if (left->GetId() > right->GetId()) {
3417 ReplaceInput(right, 0);
3418 ReplaceInput(left, 1);
3419 }
3420 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003421
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003422 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003423 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003424 return true;
3425 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003426
Roland Levillain31dd3d62016-02-16 12:21:02 +00003427 // Try to statically evaluate `this` and return a HConstant
3428 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003429 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003430 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003431
3432 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003433 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3434 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003435 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3436 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003437 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003438 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3439 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003440 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3441 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003442 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3443 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003444 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003445 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3446 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003447
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003448 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003449 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003450 HConstant* GetConstantRight() const;
3451
3452 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003453 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003454 HInstruction* GetLeastConstantLeft() const;
3455
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003456 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003457
Artem Serovcced8ba2017-07-19 18:18:09 +01003458 protected:
3459 DEFAULT_COPY_CONSTRUCTOR(BinaryOperation);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003460};
3461
Mark Mendellc4701932015-04-10 13:18:51 -04003462// The comparison bias applies for floating point operations and indicates how NaN
3463// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003464enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003465 kNoBias, // bias is not applicable (i.e. for long operation)
3466 kGtBias, // return 1 for NaN comparisons
3467 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003468 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003469};
3470
Roland Levillain31dd3d62016-02-16 12:21:02 +00003471std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3472
Dave Allison20dfc792014-06-16 20:44:29 -07003473class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003474 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003475 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003476 : HBinaryOperation(DataType::Type::kBool, first, second, SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003477 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3478 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003479
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003480 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003481 // `instruction`, and disregard moves in between.
3482 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003483
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003484 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003485
3486 virtual IfCondition GetCondition() const = 0;
3487
Mark Mendellc4701932015-04-10 13:18:51 -04003488 virtual IfCondition GetOppositeCondition() const = 0;
3489
Vladimir Markoa1de9182016-02-25 11:37:38 +00003490 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003491 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3492
Vladimir Markoa1de9182016-02-25 11:37:38 +00003493 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3494 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003495
Vladimir Marko372f10e2016-05-17 16:30:10 +01003496 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003497 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003498 }
3499
Roland Levillain4fa13f62015-07-06 18:11:54 +01003500 bool IsFPConditionTrueIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003501 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003502 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003503 if (if_cond == kCondNE) {
3504 return true;
3505 } else if (if_cond == kCondEQ) {
3506 return false;
3507 }
3508 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003509 }
3510
3511 bool IsFPConditionFalseIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003512 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003513 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003514 if (if_cond == kCondEQ) {
3515 return true;
3516 } else if (if_cond == kCondNE) {
3517 return false;
3518 }
3519 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003520 }
3521
Roland Levillain31dd3d62016-02-16 12:21:02 +00003522 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003523 // Needed if we merge a HCompare into a HCondition.
3524 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3525 static constexpr size_t kFieldComparisonBiasSize =
3526 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3527 static constexpr size_t kNumberOfConditionPackedBits =
3528 kFieldComparisonBias + kFieldComparisonBiasSize;
3529 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3530 using ComparisonBiasField =
3531 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3532
Roland Levillain31dd3d62016-02-16 12:21:02 +00003533 template <typename T>
3534 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3535
3536 template <typename T>
3537 int32_t CompareFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003538 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003539 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3540 // Handle the bias.
3541 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3542 }
3543
3544 // Return an integer constant containing the result of a condition evaluated at compile time.
3545 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3546 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3547 }
3548
Artem Serovcced8ba2017-07-19 18:18:09 +01003549 DEFAULT_COPY_CONSTRUCTOR(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003550};
3551
3552// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003553class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003554 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003555 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3556 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003557
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003558 bool IsCommutative() const OVERRIDE { return true; }
3559
Vladimir Marko9e23df52015-11-10 17:14:35 +00003560 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3561 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003562 return MakeConstantCondition(true, GetDexPc());
3563 }
3564 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3565 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3566 }
3567 // In the following Evaluate methods, a HCompare instruction has
3568 // been merged into this HEqual instruction; evaluate it as
3569 // `Compare(x, y) == 0`.
3570 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3571 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3572 GetDexPc());
3573 }
3574 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3575 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3576 }
3577 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3578 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003579 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003580
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003581 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003582
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003583 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003584 return kCondEQ;
3585 }
3586
Mark Mendellc4701932015-04-10 13:18:51 -04003587 IfCondition GetOppositeCondition() const OVERRIDE {
3588 return kCondNE;
3589 }
3590
Artem Serovcced8ba2017-07-19 18:18:09 +01003591 protected:
3592 DEFAULT_COPY_CONSTRUCTOR(Equal);
3593
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003594 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003595 template <typename T> static bool Compute(T x, T y) { return x == y; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003596};
3597
Vladimir Markofcb503c2016-05-18 12:48:17 +01003598class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003599 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003600 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3601 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003602
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003603 bool IsCommutative() const OVERRIDE { return true; }
3604
Vladimir Marko9e23df52015-11-10 17:14:35 +00003605 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3606 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003607 return MakeConstantCondition(false, GetDexPc());
3608 }
3609 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3610 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3611 }
3612 // In the following Evaluate methods, a HCompare instruction has
3613 // been merged into this HNotEqual instruction; evaluate it as
3614 // `Compare(x, y) != 0`.
3615 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3616 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3617 }
3618 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3619 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3620 }
3621 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3622 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003623 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003624
Dave Allison20dfc792014-06-16 20:44:29 -07003625 DECLARE_INSTRUCTION(NotEqual);
3626
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003627 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003628 return kCondNE;
3629 }
3630
Mark Mendellc4701932015-04-10 13:18:51 -04003631 IfCondition GetOppositeCondition() const OVERRIDE {
3632 return kCondEQ;
3633 }
3634
Artem Serovcced8ba2017-07-19 18:18:09 +01003635 protected:
3636 DEFAULT_COPY_CONSTRUCTOR(NotEqual);
3637
Dave Allison20dfc792014-06-16 20:44:29 -07003638 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003639 template <typename T> static bool Compute(T x, T y) { return x != y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003640};
3641
Vladimir Markofcb503c2016-05-18 12:48:17 +01003642class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003643 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003644 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3645 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003646
Roland Levillain9867bc72015-08-05 10:21:34 +01003647 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003648 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003649 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003650 // In the following Evaluate methods, a HCompare instruction has
3651 // been merged into this HLessThan instruction; evaluate it as
3652 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003653 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003654 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3655 }
3656 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3657 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3658 }
3659 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3660 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003661 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003662
Dave Allison20dfc792014-06-16 20:44:29 -07003663 DECLARE_INSTRUCTION(LessThan);
3664
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003665 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003666 return kCondLT;
3667 }
3668
Mark Mendellc4701932015-04-10 13:18:51 -04003669 IfCondition GetOppositeCondition() const OVERRIDE {
3670 return kCondGE;
3671 }
3672
Artem Serovcced8ba2017-07-19 18:18:09 +01003673 protected:
3674 DEFAULT_COPY_CONSTRUCTOR(LessThan);
3675
Dave Allison20dfc792014-06-16 20:44:29 -07003676 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003677 template <typename T> static bool Compute(T x, T y) { return x < y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003678};
3679
Vladimir Markofcb503c2016-05-18 12:48:17 +01003680class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003681 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003682 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3683 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003684
Roland Levillain9867bc72015-08-05 10:21:34 +01003685 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003686 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003687 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003688 // In the following Evaluate methods, a HCompare instruction has
3689 // been merged into this HLessThanOrEqual instruction; evaluate it as
3690 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003691 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003692 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3693 }
3694 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3695 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3696 }
3697 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3698 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003699 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003700
Dave Allison20dfc792014-06-16 20:44:29 -07003701 DECLARE_INSTRUCTION(LessThanOrEqual);
3702
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003703 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003704 return kCondLE;
3705 }
3706
Mark Mendellc4701932015-04-10 13:18:51 -04003707 IfCondition GetOppositeCondition() const OVERRIDE {
3708 return kCondGT;
3709 }
3710
Artem Serovcced8ba2017-07-19 18:18:09 +01003711 protected:
3712 DEFAULT_COPY_CONSTRUCTOR(LessThanOrEqual);
3713
Dave Allison20dfc792014-06-16 20:44:29 -07003714 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003715 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003716};
3717
Vladimir Markofcb503c2016-05-18 12:48:17 +01003718class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003719 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003720 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3721 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003722
Roland Levillain9867bc72015-08-05 10:21:34 +01003723 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003724 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003725 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003726 // In the following Evaluate methods, a HCompare instruction has
3727 // been merged into this HGreaterThan instruction; evaluate it as
3728 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003729 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003730 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3731 }
3732 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3733 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3734 }
3735 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3736 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003737 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003738
Dave Allison20dfc792014-06-16 20:44:29 -07003739 DECLARE_INSTRUCTION(GreaterThan);
3740
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003741 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003742 return kCondGT;
3743 }
3744
Mark Mendellc4701932015-04-10 13:18:51 -04003745 IfCondition GetOppositeCondition() const OVERRIDE {
3746 return kCondLE;
3747 }
3748
Artem Serovcced8ba2017-07-19 18:18:09 +01003749 protected:
3750 DEFAULT_COPY_CONSTRUCTOR(GreaterThan);
3751
Dave Allison20dfc792014-06-16 20:44:29 -07003752 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003753 template <typename T> static bool Compute(T x, T y) { return x > y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003754};
3755
Vladimir Markofcb503c2016-05-18 12:48:17 +01003756class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003757 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003758 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3759 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003760
Roland Levillain9867bc72015-08-05 10:21:34 +01003761 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003762 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003763 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003764 // In the following Evaluate methods, a HCompare instruction has
3765 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3766 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003767 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003768 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3769 }
3770 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3771 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3772 }
3773 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3774 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003775 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003776
Dave Allison20dfc792014-06-16 20:44:29 -07003777 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3778
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003779 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003780 return kCondGE;
3781 }
3782
Mark Mendellc4701932015-04-10 13:18:51 -04003783 IfCondition GetOppositeCondition() const OVERRIDE {
3784 return kCondLT;
3785 }
3786
Artem Serovcced8ba2017-07-19 18:18:09 +01003787 protected:
3788 DEFAULT_COPY_CONSTRUCTOR(GreaterThanOrEqual);
3789
Dave Allison20dfc792014-06-16 20:44:29 -07003790 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003791 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003792};
3793
Vladimir Markofcb503c2016-05-18 12:48:17 +01003794class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003795 public:
3796 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3797 : HCondition(first, second, dex_pc) {}
3798
3799 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003800 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003801 }
3802 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003803 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3804 }
3805 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3806 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3807 LOG(FATAL) << DebugName() << " is not defined for float values";
3808 UNREACHABLE();
3809 }
3810 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3811 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3812 LOG(FATAL) << DebugName() << " is not defined for double values";
3813 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003814 }
3815
3816 DECLARE_INSTRUCTION(Below);
3817
3818 IfCondition GetCondition() const OVERRIDE {
3819 return kCondB;
3820 }
3821
3822 IfCondition GetOppositeCondition() const OVERRIDE {
3823 return kCondAE;
3824 }
3825
Artem Serovcced8ba2017-07-19 18:18:09 +01003826 protected:
3827 DEFAULT_COPY_CONSTRUCTOR(Below);
3828
Aart Bike9f37602015-10-09 11:15:55 -07003829 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003830 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003831 return MakeUnsigned(x) < MakeUnsigned(y);
3832 }
Aart Bike9f37602015-10-09 11:15:55 -07003833};
3834
Vladimir Markofcb503c2016-05-18 12:48:17 +01003835class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003836 public:
3837 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3838 : HCondition(first, second, dex_pc) {}
3839
3840 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003841 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003842 }
3843 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003844 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3845 }
3846 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3847 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3848 LOG(FATAL) << DebugName() << " is not defined for float values";
3849 UNREACHABLE();
3850 }
3851 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3852 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3853 LOG(FATAL) << DebugName() << " is not defined for double values";
3854 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003855 }
3856
3857 DECLARE_INSTRUCTION(BelowOrEqual);
3858
3859 IfCondition GetCondition() const OVERRIDE {
3860 return kCondBE;
3861 }
3862
3863 IfCondition GetOppositeCondition() const OVERRIDE {
3864 return kCondA;
3865 }
3866
Artem Serovcced8ba2017-07-19 18:18:09 +01003867 protected:
3868 DEFAULT_COPY_CONSTRUCTOR(BelowOrEqual);
3869
Aart Bike9f37602015-10-09 11:15:55 -07003870 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003871 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003872 return MakeUnsigned(x) <= MakeUnsigned(y);
3873 }
Aart Bike9f37602015-10-09 11:15:55 -07003874};
3875
Vladimir Markofcb503c2016-05-18 12:48:17 +01003876class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003877 public:
3878 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3879 : HCondition(first, second, dex_pc) {}
3880
3881 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003882 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003883 }
3884 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003885 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3886 }
3887 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3888 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3889 LOG(FATAL) << DebugName() << " is not defined for float values";
3890 UNREACHABLE();
3891 }
3892 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3893 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3894 LOG(FATAL) << DebugName() << " is not defined for double values";
3895 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003896 }
3897
3898 DECLARE_INSTRUCTION(Above);
3899
3900 IfCondition GetCondition() const OVERRIDE {
3901 return kCondA;
3902 }
3903
3904 IfCondition GetOppositeCondition() const OVERRIDE {
3905 return kCondBE;
3906 }
3907
Artem Serovcced8ba2017-07-19 18:18:09 +01003908 protected:
3909 DEFAULT_COPY_CONSTRUCTOR(Above);
3910
Aart Bike9f37602015-10-09 11:15:55 -07003911 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003912 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003913 return MakeUnsigned(x) > MakeUnsigned(y);
3914 }
Aart Bike9f37602015-10-09 11:15:55 -07003915};
3916
Vladimir Markofcb503c2016-05-18 12:48:17 +01003917class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003918 public:
3919 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3920 : HCondition(first, second, dex_pc) {}
3921
3922 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003923 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003924 }
3925 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003926 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3927 }
3928 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3929 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3930 LOG(FATAL) << DebugName() << " is not defined for float values";
3931 UNREACHABLE();
3932 }
3933 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3934 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3935 LOG(FATAL) << DebugName() << " is not defined for double values";
3936 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003937 }
3938
3939 DECLARE_INSTRUCTION(AboveOrEqual);
3940
3941 IfCondition GetCondition() const OVERRIDE {
3942 return kCondAE;
3943 }
3944
3945 IfCondition GetOppositeCondition() const OVERRIDE {
3946 return kCondB;
3947 }
3948
Artem Serovcced8ba2017-07-19 18:18:09 +01003949 protected:
3950 DEFAULT_COPY_CONSTRUCTOR(AboveOrEqual);
3951
Aart Bike9f37602015-10-09 11:15:55 -07003952 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003953 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003954 return MakeUnsigned(x) >= MakeUnsigned(y);
3955 }
Aart Bike9f37602015-10-09 11:15:55 -07003956};
Dave Allison20dfc792014-06-16 20:44:29 -07003957
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003958// Instruction to check how two inputs compare to each other.
3959// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003960class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003961 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003962 // Note that `comparison_type` is the type of comparison performed
3963 // between the comparison's inputs, not the type of the instantiated
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003964 // HCompare instruction (which is always DataType::Type::kInt).
3965 HCompare(DataType::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003966 HInstruction* first,
3967 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003968 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003969 uint32_t dex_pc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003970 : HBinaryOperation(DataType::Type::kInt32,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003971 first,
3972 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003973 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003974 dex_pc) {
3975 SetPackedField<ComparisonBiasField>(bias);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003976 DCHECK_EQ(comparison_type, DataType::Kind(first->GetType()));
3977 DCHECK_EQ(comparison_type, DataType::Kind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003978 }
3979
Roland Levillain9867bc72015-08-05 10:21:34 +01003980 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003981 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3982
3983 template <typename T>
3984 int32_t ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003985 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003986 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3987 // Handle the bias.
3988 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3989 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003990
Roland Levillain9867bc72015-08-05 10:21:34 +01003991 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003992 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3993 // reach this code path when processing a freshly built HIR
3994 // graph. However HCompare integer instructions can be synthesized
3995 // by the instruction simplifier to implement IntegerCompare and
3996 // IntegerSignum intrinsics, so we have to handle this case.
3997 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003998 }
3999 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004000 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4001 }
4002 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4003 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4004 }
4005 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4006 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01004007 }
4008
Vladimir Marko372f10e2016-05-17 16:30:10 +01004009 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004010 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00004011 }
4012
Vladimir Markoa1de9182016-02-25 11:37:38 +00004013 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04004014
Roland Levillain31dd3d62016-02-16 12:21:02 +00004015 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00004016 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00004017 bool IsGtBias() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004018 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004019 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00004020 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004021
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004022 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type type ATTRIBUTE_UNUSED) {
Roland Levillain1693a1f2016-03-15 14:57:31 +00004023 // Comparisons do not require a runtime call in any back end.
4024 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004025 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004026
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004027 DECLARE_INSTRUCTION(Compare);
4028
Roland Levillain31dd3d62016-02-16 12:21:02 +00004029 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004030 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
4031 static constexpr size_t kFieldComparisonBiasSize =
4032 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
4033 static constexpr size_t kNumberOfComparePackedBits =
4034 kFieldComparisonBias + kFieldComparisonBiasSize;
4035 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4036 using ComparisonBiasField =
4037 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
4038
Roland Levillain31dd3d62016-02-16 12:21:02 +00004039 // Return an integer constant containing the result of a comparison evaluated at compile time.
4040 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
4041 DCHECK(value == -1 || value == 0 || value == 1) << value;
4042 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
4043 }
4044
Artem Serovcced8ba2017-07-19 18:18:09 +01004045 DEFAULT_COPY_CONSTRUCTOR(Compare);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004046};
4047
Nicolas Geoffray2b615ba2017-01-06 14:40:07 +00004048class HNewInstance FINAL : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00004049 public:
4050 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00004051 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004052 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00004053 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00004054 bool finalizable,
4055 QuickEntrypointEnum entrypoint)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004056 : HExpression(DataType::Type::kReference, SideEffects::CanTriggerGC(), dex_pc),
David Brazdil6de19382016-01-08 17:37:10 +00004057 type_index_(type_index),
4058 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00004059 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004060 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00004061 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00004062 }
4063
Artem Serovcced8ba2017-07-19 18:18:09 +01004064 bool IsClonable() const OVERRIDE { return true; }
4065
Andreas Gampea5b09a62016-11-17 15:21:22 -08004066 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00004067 const DexFile& GetDexFile() const { return dex_file_; }
4068
4069 // Calls runtime so needs an environment.
4070 bool NeedsEnvironment() const OVERRIDE { return true; }
4071
Mingyao Yang062157f2016-03-02 10:15:36 -08004072 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
4073 bool CanThrow() const OVERRIDE { return true; }
4074
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004075 bool NeedsChecks() const {
4076 return entrypoint_ == kQuickAllocObjectWithChecks;
4077 }
David Brazdil6de19382016-01-08 17:37:10 +00004078
Vladimir Markoa1de9182016-02-25 11:37:38 +00004079 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00004080
4081 bool CanBeNull() const OVERRIDE { return false; }
4082
4083 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4084
4085 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
4086 entrypoint_ = entrypoint;
4087 }
4088
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004089 HLoadClass* GetLoadClass() const {
4090 HInstruction* input = InputAt(0);
4091 if (input->IsClinitCheck()) {
4092 input = input->InputAt(0);
4093 }
4094 DCHECK(input->IsLoadClass());
4095 return input->AsLoadClass();
4096 }
4097
David Brazdil6de19382016-01-08 17:37:10 +00004098 bool IsStringAlloc() const;
4099
4100 DECLARE_INSTRUCTION(NewInstance);
4101
Artem Serovcced8ba2017-07-19 18:18:09 +01004102 protected:
4103 DEFAULT_COPY_CONSTRUCTOR(NewInstance);
4104
David Brazdil6de19382016-01-08 17:37:10 +00004105 private:
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004106 static constexpr size_t kFlagFinalizable = kNumberOfExpressionPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004107 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
4108 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
4109 "Too many packed fields.");
4110
Andreas Gampea5b09a62016-11-17 15:21:22 -08004111 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00004112 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00004113 QuickEntrypointEnum entrypoint_;
David Brazdil6de19382016-01-08 17:37:10 +00004114};
4115
Agi Csaki05f20562015-08-19 14:58:14 -07004116enum IntrinsicNeedsEnvironmentOrCache {
4117 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
4118 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07004119};
4120
Aart Bik5d75afe2015-12-14 11:57:01 -08004121enum IntrinsicSideEffects {
4122 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
4123 kReadSideEffects, // Intrinsic may read heap memory.
4124 kWriteSideEffects, // Intrinsic may write heap memory.
4125 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
4126};
4127
4128enum IntrinsicExceptions {
4129 kNoThrow, // Intrinsic does not throw any exceptions.
4130 kCanThrow // Intrinsic may throw exceptions.
4131};
4132
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004133class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004134 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004135 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004136
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01004137 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004138 SetRawInputAt(index, argument);
4139 }
4140
Roland Levillain3e3d7332015-04-28 11:00:54 +01004141 // Return the number of arguments. This number can be lower than
4142 // the number of inputs returned by InputCount(), as some invoke
4143 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
4144 // inputs at the end of their list of inputs.
4145 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
4146
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004147 DataType::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004148
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004149 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4150
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004151 InvokeType GetInvokeType() const {
4152 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004153 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004154
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01004155 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004156 return intrinsic_;
4157 }
4158
Aart Bik5d75afe2015-12-14 11:57:01 -08004159 void SetIntrinsic(Intrinsics intrinsic,
4160 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
4161 IntrinsicSideEffects side_effects,
4162 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004163
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004164 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00004165 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004166 }
4167
Aart Bikff7d89c2016-11-07 08:49:28 -08004168 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4169
Vladimir Markoa1de9182016-02-25 11:37:38 +00004170 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004171
Aart Bik71bf7b42016-11-16 10:17:46 -08004172 bool CanBeMoved() const OVERRIDE { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004173
Vladimir Marko372f10e2016-05-17 16:30:10 +01004174 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08004175 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4176 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004177
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004178 uint32_t* GetIntrinsicOptimizations() {
4179 return &intrinsic_optimizations_;
4180 }
4181
4182 const uint32_t* GetIntrinsicOptimizations() const {
4183 return &intrinsic_optimizations_;
4184 }
4185
4186 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4187
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004188 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004189 void SetResolvedMethod(ArtMethod* method) { resolved_method_ = method; }
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004190
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004191 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004192
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004193 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004194 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4195 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004196 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4197 static constexpr size_t kFieldReturnType =
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004198 kFieldInvokeType + kFieldInvokeTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004199 static constexpr size_t kFieldReturnTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004200 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00004201 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
4202 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
4203 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004204 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004205 using ReturnTypeField = BitField<DataType::Type, kFieldReturnType, kFieldReturnTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004206
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004207 HInvoke(ArenaAllocator* allocator,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004208 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004209 uint32_t number_of_other_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004210 DataType::Type return_type,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004211 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004212 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004213 ArtMethod* resolved_method,
4214 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004215 : HVariableInputSizeInstruction(
4216 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4217 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004218 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004219 number_of_arguments + number_of_other_inputs,
4220 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004221 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004222 resolved_method_(resolved_method),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004223 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07004224 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004225 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004226 SetPackedField<ReturnTypeField>(return_type);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004227 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004228 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004229 }
4230
Artem Serovcced8ba2017-07-19 18:18:09 +01004231 DEFAULT_COPY_CONSTRUCTOR(Invoke);
4232
Roland Levillain3e3d7332015-04-28 11:00:54 +01004233 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004234 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004235 const uint32_t dex_method_index_;
4236 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004237
4238 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4239 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004240};
4241
Vladimir Markofcb503c2016-05-18 12:48:17 +01004242class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004243 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004244 HInvokeUnresolved(ArenaAllocator* allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004245 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004246 DataType::Type return_type,
Calin Juravle175dc732015-08-25 15:42:32 +01004247 uint32_t dex_pc,
4248 uint32_t dex_method_index,
4249 InvokeType invoke_type)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004250 : HInvoke(allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004251 number_of_arguments,
4252 0u /* number_of_other_inputs */,
4253 return_type,
4254 dex_pc,
4255 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004256 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01004257 invoke_type) {
4258 }
4259
Artem Serovcced8ba2017-07-19 18:18:09 +01004260 bool IsClonable() const OVERRIDE { return true; }
4261
Calin Juravle175dc732015-08-25 15:42:32 +01004262 DECLARE_INSTRUCTION(InvokeUnresolved);
4263
Artem Serovcced8ba2017-07-19 18:18:09 +01004264 protected:
4265 DEFAULT_COPY_CONSTRUCTOR(InvokeUnresolved);
Calin Juravle175dc732015-08-25 15:42:32 +01004266};
4267
Orion Hodsonac141392017-01-13 11:53:47 +00004268class HInvokePolymorphic FINAL : public HInvoke {
4269 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004270 HInvokePolymorphic(ArenaAllocator* allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004271 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004272 DataType::Type return_type,
Orion Hodsonac141392017-01-13 11:53:47 +00004273 uint32_t dex_pc,
4274 uint32_t dex_method_index)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004275 : HInvoke(allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004276 number_of_arguments,
4277 0u /* number_of_other_inputs */,
4278 return_type,
4279 dex_pc,
4280 dex_method_index,
4281 nullptr,
4282 kVirtual) {}
4283
Artem Serovcced8ba2017-07-19 18:18:09 +01004284 bool IsClonable() const OVERRIDE { return true; }
4285
Orion Hodsonac141392017-01-13 11:53:47 +00004286 DECLARE_INSTRUCTION(InvokePolymorphic);
4287
Artem Serovcced8ba2017-07-19 18:18:09 +01004288 protected:
4289 DEFAULT_COPY_CONSTRUCTOR(InvokePolymorphic);
Orion Hodsonac141392017-01-13 11:53:47 +00004290};
4291
Vladimir Markofcb503c2016-05-18 12:48:17 +01004292class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004293 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004294 // Requirements of this method call regarding the class
4295 // initialization (clinit) check of its declaring class.
4296 enum class ClinitCheckRequirement {
4297 kNone, // Class already initialized.
4298 kExplicit, // Static call having explicit clinit check as last input.
4299 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004300 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004301 };
4302
Vladimir Marko58155012015-08-19 12:49:41 +00004303 // Determines how to load the target ArtMethod*.
4304 enum class MethodLoadKind {
4305 // Use a String init ArtMethod* loaded from Thread entrypoints.
4306 kStringInit,
4307
4308 // Use the method's own ArtMethod* loaded by the register allocator.
4309 kRecursive,
4310
Vladimir Marko65979462017-05-19 17:25:12 +01004311 // Use PC-relative boot image ArtMethod* address that will be known at link time.
4312 // Used for boot image methods referenced by boot image code.
4313 kBootImageLinkTimePcRelative,
4314
Vladimir Marko58155012015-08-19 12:49:41 +00004315 // Use ArtMethod* at a known address, embed the direct address in the code.
4316 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
4317 kDirectAddress,
4318
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004319 // Load from an entry in the .bss section using a PC-relative load.
4320 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
4321 kBssEntry,
Vladimir Marko58155012015-08-19 12:49:41 +00004322
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004323 // Make a runtime call to resolve and call the method. This is the last-resort-kind
4324 // used when other kinds are unimplemented on a particular architecture.
4325 kRuntimeCall,
Vladimir Marko58155012015-08-19 12:49:41 +00004326 };
4327
4328 // Determines the location of the code pointer.
4329 enum class CodePtrLocation {
4330 // Recursive call, use local PC-relative call instruction.
4331 kCallSelf,
4332
Vladimir Marko58155012015-08-19 12:49:41 +00004333 // Use code pointer from the ArtMethod*.
4334 // Used when we don't know the target code. This is also the last-resort-kind used when
4335 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4336 kCallArtMethod,
4337 };
4338
4339 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004340 MethodLoadKind method_load_kind;
4341 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004342 // The method load data holds
4343 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4344 // Note that there are multiple string init methods, each having its own offset.
4345 // - the method address for kDirectAddress
Vladimir Markodc151b22015-10-15 18:02:30 +01004346 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004347 };
4348
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004349 HInvokeStaticOrDirect(ArenaAllocator* allocator,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004350 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004351 DataType::Type return_type,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004352 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004353 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004354 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004355 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004356 InvokeType invoke_type,
4357 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004358 ClinitCheckRequirement clinit_check_requirement)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004359 : HInvoke(allocator,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004360 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004361 // There is potentially one extra argument for the HCurrentMethod node, and
4362 // potentially one other if the clinit check is explicit, and potentially
4363 // one other if the method is a string factory.
4364 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004365 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004366 return_type,
4367 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004368 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004369 resolved_method,
4370 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004371 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004372 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004373 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4374 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004375
Artem Serovcced8ba2017-07-19 18:18:09 +01004376 bool IsClonable() const OVERRIDE { return true; }
4377
Vladimir Markodc151b22015-10-15 18:02:30 +01004378 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004379 bool had_current_method_input = HasCurrentMethodInput();
4380 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4381
4382 // Using the current method is the default and once we find a better
4383 // method load kind, we should not go back to using the current method.
4384 DCHECK(had_current_method_input || !needs_current_method_input);
4385
4386 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004387 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4388 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004389 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004390 dispatch_info_ = dispatch_info;
4391 }
4392
Aart Bik6daebeb2017-04-03 14:35:41 -07004393 DispatchInfo GetDispatchInfo() const {
4394 return dispatch_info_;
4395 }
4396
Vladimir Markoc53c0792015-11-19 15:48:33 +00004397 void AddSpecialInput(HInstruction* input) {
4398 // We allow only one special input.
4399 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4400 DCHECK(InputCount() == GetSpecialInputIndex() ||
4401 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4402 InsertInputAt(GetSpecialInputIndex(), input);
4403 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004404
Vladimir Marko372f10e2016-05-17 16:30:10 +01004405 using HInstruction::GetInputRecords; // Keep the const version visible.
4406 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
4407 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4408 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4409 DCHECK(!input_records.empty());
4410 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4411 HInstruction* last_input = input_records.back().GetInstruction();
4412 // Note: `last_input` may be null during arguments setup.
4413 if (last_input != nullptr) {
4414 // `last_input` is the last input of a static invoke marked as having
4415 // an explicit clinit check. It must either be:
4416 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4417 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4418 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4419 }
4420 }
4421 return input_records;
4422 }
4423
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004424 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004425 // We access the method via the dex cache so we can't do an implicit null check.
4426 // TODO: for intrinsics we can generate implicit null checks.
4427 return false;
4428 }
4429
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004430 bool CanBeNull() const OVERRIDE {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004431 return GetPackedField<ReturnTypeField>() == DataType::Type::kReference && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004432 }
4433
Vladimir Markoc53c0792015-11-19 15:48:33 +00004434 // Get the index of the special input, if any.
4435 //
David Brazdil6de19382016-01-08 17:37:10 +00004436 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4437 // method pointer; otherwise there may be one platform-specific special input,
4438 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004439 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004440 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004441
Vladimir Marko58155012015-08-19 12:49:41 +00004442 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4443 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4444 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004445 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004446 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004447 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko65979462017-05-19 17:25:12 +01004448 bool HasPcRelativeMethodLoadKind() const {
4449 return GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004450 GetMethodLoadKind() == MethodLoadKind::kBssEntry;
Vladimir Marko65979462017-05-19 17:25:12 +01004451 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004452 bool HasCurrentMethodInput() const {
4453 // This function can be called only after the invoke has been fully initialized by the builder.
4454 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004455 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004456 return true;
4457 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004458 DCHECK(InputCount() == GetSpecialInputIndex() ||
4459 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004460 return false;
4461 }
4462 }
Vladimir Marko58155012015-08-19 12:49:41 +00004463
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004464 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004465 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004466 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004467 }
4468
4469 uint64_t GetMethodAddress() const {
4470 DCHECK(HasMethodAddress());
4471 return dispatch_info_.method_load_data;
4472 }
4473
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004474 const DexFile& GetDexFileForPcRelativeDexCache() const;
4475
Vladimir Markoa1de9182016-02-25 11:37:38 +00004476 ClinitCheckRequirement GetClinitCheckRequirement() const {
4477 return GetPackedField<ClinitCheckRequirementField>();
4478 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004479
Roland Levillain4c0eb422015-04-24 16:43:49 +01004480 // Is this instruction a call to a static method?
4481 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004482 return GetInvokeType() == kStatic;
4483 }
4484
4485 MethodReference GetTargetMethod() const {
4486 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004487 }
4488
Vladimir Markofbb184a2015-11-13 14:47:00 +00004489 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4490 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4491 // instruction; only relevant for static calls with explicit clinit check.
4492 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004493 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004494 size_t last_input_index = inputs_.size() - 1u;
4495 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004496 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004497 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004498 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004499 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004500 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004501 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004502 }
4503
4504 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004505 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004506 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004507 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004508 }
4509
4510 // Is this a call to a static method whose declaring class has an
4511 // implicit intialization check requirement?
4512 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004513 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004514 }
4515
Vladimir Markob554b5a2015-11-06 12:57:55 +00004516 // Does this method load kind need the current method as an input?
4517 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004518 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kRuntimeCall;
Vladimir Markob554b5a2015-11-06 12:57:55 +00004519 }
4520
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004521 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004522
Artem Serovcced8ba2017-07-19 18:18:09 +01004523 protected:
4524 DEFAULT_COPY_CONSTRUCTOR(InvokeStaticOrDirect);
4525
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004526 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004527 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004528 static constexpr size_t kFieldClinitCheckRequirementSize =
4529 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4530 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4531 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4532 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4533 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004534 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4535 kFieldClinitCheckRequirement,
4536 kFieldClinitCheckRequirementSize>;
4537
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004538 // Cached values of the resolved method, to avoid needing the mutator lock.
Vladimir Marko58155012015-08-19 12:49:41 +00004539 MethodReference target_method_;
4540 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004541};
Vladimir Markof64242a2015-12-01 14:58:23 +00004542std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004543std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004544
Vladimir Markofcb503c2016-05-18 12:48:17 +01004545class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004546 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004547 HInvokeVirtual(ArenaAllocator* allocator,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004548 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004549 DataType::Type return_type,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004550 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004551 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004552 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004553 uint32_t vtable_index)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004554 : HInvoke(allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004555 number_of_arguments,
4556 0u,
4557 return_type,
4558 dex_pc,
4559 dex_method_index,
4560 resolved_method,
4561 kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004562 vtable_index_(vtable_index) {}
4563
Artem Serovcced8ba2017-07-19 18:18:09 +01004564 bool IsClonable() const OVERRIDE { return true; }
4565
Aart Bik71bf7b42016-11-16 10:17:46 -08004566 bool CanBeNull() const OVERRIDE {
4567 switch (GetIntrinsic()) {
4568 case Intrinsics::kThreadCurrentThread:
4569 case Intrinsics::kStringBufferAppend:
4570 case Intrinsics::kStringBufferToString:
4571 case Intrinsics::kStringBuilderAppend:
4572 case Intrinsics::kStringBuilderToString:
4573 return false;
4574 default:
4575 return HInvoke::CanBeNull();
4576 }
4577 }
4578
Calin Juravle641547a2015-04-21 22:08:51 +01004579 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004580 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004581 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004582 }
4583
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004584 uint32_t GetVTableIndex() const { return vtable_index_; }
4585
4586 DECLARE_INSTRUCTION(InvokeVirtual);
4587
Artem Serovcced8ba2017-07-19 18:18:09 +01004588 protected:
4589 DEFAULT_COPY_CONSTRUCTOR(InvokeVirtual);
4590
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004591 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004592 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004593 const uint32_t vtable_index_;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004594};
4595
Vladimir Markofcb503c2016-05-18 12:48:17 +01004596class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004597 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004598 HInvokeInterface(ArenaAllocator* allocator,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004599 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004600 DataType::Type return_type,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004601 uint32_t dex_pc,
4602 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004603 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004604 uint32_t imt_index)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004605 : HInvoke(allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004606 number_of_arguments,
4607 0u,
4608 return_type,
4609 dex_pc,
4610 dex_method_index,
4611 resolved_method,
4612 kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004613 imt_index_(imt_index) {}
4614
Artem Serovcced8ba2017-07-19 18:18:09 +01004615 bool IsClonable() const OVERRIDE { return true; }
4616
Calin Juravle641547a2015-04-21 22:08:51 +01004617 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004618 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004619 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004620 }
4621
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004622 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
4623 // The assembly stub currently needs it.
4624 return true;
4625 }
4626
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004627 uint32_t GetImtIndex() const { return imt_index_; }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004628
4629 DECLARE_INSTRUCTION(InvokeInterface);
4630
Artem Serovcced8ba2017-07-19 18:18:09 +01004631 protected:
4632 DEFAULT_COPY_CONSTRUCTOR(InvokeInterface);
4633
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004634 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004635 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004636 const uint32_t imt_index_;
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004637};
4638
Vladimir Markofcb503c2016-05-18 12:48:17 +01004639class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004640 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004641 HNeg(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004642 : HUnaryOperation(result_type, input, dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004643 DCHECK_EQ(result_type, DataType::Kind(input->GetType()));
Roland Levillain937e6cd2016-03-22 11:54:37 +00004644 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004645
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004646 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004647
4648 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004649 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004650 }
4651 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004652 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004653 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004654 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4655 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4656 }
4657 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4658 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4659 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004660
Roland Levillain88cb1752014-10-20 16:36:47 +01004661 DECLARE_INSTRUCTION(Neg);
4662
Artem Serovcced8ba2017-07-19 18:18:09 +01004663 protected:
4664 DEFAULT_COPY_CONSTRUCTOR(Neg);
Roland Levillain88cb1752014-10-20 16:36:47 +01004665};
4666
Vladimir Markofcb503c2016-05-18 12:48:17 +01004667class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004668 public:
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004669 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004670 : HExpression(DataType::Type::kReference, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004671 SetRawInputAt(0, cls);
4672 SetRawInputAt(1, length);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004673 }
4674
Artem Serovcced8ba2017-07-19 18:18:09 +01004675 bool IsClonable() const OVERRIDE { return true; }
4676
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004677 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004678 bool NeedsEnvironment() const OVERRIDE { return true; }
4679
Mingyao Yang0c365e62015-03-31 15:09:29 -07004680 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4681 bool CanThrow() const OVERRIDE { return true; }
4682
Calin Juravle10e244f2015-01-26 18:54:32 +00004683 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004684
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004685 HLoadClass* GetLoadClass() const {
4686 DCHECK(InputAt(0)->IsLoadClass());
4687 return InputAt(0)->AsLoadClass();
4688 }
4689
4690 HInstruction* GetLength() const {
4691 return InputAt(1);
4692 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004693
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004694 DECLARE_INSTRUCTION(NewArray);
4695
Artem Serovcced8ba2017-07-19 18:18:09 +01004696 protected:
4697 DEFAULT_COPY_CONSTRUCTOR(NewArray);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004698};
4699
Vladimir Markofcb503c2016-05-18 12:48:17 +01004700class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004701 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004702 HAdd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004703 HInstruction* left,
4704 HInstruction* right,
4705 uint32_t dex_pc = kNoDexPc)
4706 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004707
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004708 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004709
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004710 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004711
4712 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004713 return GetBlock()->GetGraph()->GetIntConstant(
4714 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004715 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004716 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004717 return GetBlock()->GetGraph()->GetLongConstant(
4718 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004719 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004720 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4721 return GetBlock()->GetGraph()->GetFloatConstant(
4722 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4723 }
4724 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4725 return GetBlock()->GetGraph()->GetDoubleConstant(
4726 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4727 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004728
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004729 DECLARE_INSTRUCTION(Add);
4730
Artem Serovcced8ba2017-07-19 18:18:09 +01004731 protected:
4732 DEFAULT_COPY_CONSTRUCTOR(Add);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004733};
4734
Vladimir Markofcb503c2016-05-18 12:48:17 +01004735class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004736 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004737 HSub(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004738 HInstruction* left,
4739 HInstruction* right,
4740 uint32_t dex_pc = kNoDexPc)
4741 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004742
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004743 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004744
4745 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004746 return GetBlock()->GetGraph()->GetIntConstant(
4747 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004748 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004749 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004750 return GetBlock()->GetGraph()->GetLongConstant(
4751 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004752 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004753 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4754 return GetBlock()->GetGraph()->GetFloatConstant(
4755 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4756 }
4757 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4758 return GetBlock()->GetGraph()->GetDoubleConstant(
4759 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4760 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004761
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004762 DECLARE_INSTRUCTION(Sub);
4763
Artem Serovcced8ba2017-07-19 18:18:09 +01004764 protected:
4765 DEFAULT_COPY_CONSTRUCTOR(Sub);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004766};
4767
Vladimir Markofcb503c2016-05-18 12:48:17 +01004768class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004769 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004770 HMul(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004771 HInstruction* left,
4772 HInstruction* right,
4773 uint32_t dex_pc = kNoDexPc)
4774 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004775
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004776 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004777
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004778 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004779
4780 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004781 return GetBlock()->GetGraph()->GetIntConstant(
4782 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004783 }
4784 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004785 return GetBlock()->GetGraph()->GetLongConstant(
4786 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004787 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004788 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4789 return GetBlock()->GetGraph()->GetFloatConstant(
4790 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4791 }
4792 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4793 return GetBlock()->GetGraph()->GetDoubleConstant(
4794 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4795 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004796
4797 DECLARE_INSTRUCTION(Mul);
4798
Artem Serovcced8ba2017-07-19 18:18:09 +01004799 protected:
4800 DEFAULT_COPY_CONSTRUCTOR(Mul);
Calin Juravle34bacdf2014-10-07 20:23:36 +01004801};
4802
Vladimir Markofcb503c2016-05-18 12:48:17 +01004803class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004804 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004805 HDiv(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004806 HInstruction* left,
4807 HInstruction* right,
4808 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004809 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004810
Roland Levillain9867bc72015-08-05 10:21:34 +01004811 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004812 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004813 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004814 // Our graph structure ensures we never have 0 for `y` during
4815 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004816 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004817 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004818 return (y == -1) ? -x : x / y;
4819 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004820
Roland Levillain31dd3d62016-02-16 12:21:02 +00004821 template <typename T>
4822 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004823 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004824 return x / y;
4825 }
4826
Roland Levillain9867bc72015-08-05 10:21:34 +01004827 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004828 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004829 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004830 }
4831 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004832 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004833 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4834 }
4835 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4836 return GetBlock()->GetGraph()->GetFloatConstant(
4837 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4838 }
4839 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4840 return GetBlock()->GetGraph()->GetDoubleConstant(
4841 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004842 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004843
4844 DECLARE_INSTRUCTION(Div);
4845
Artem Serovcced8ba2017-07-19 18:18:09 +01004846 protected:
4847 DEFAULT_COPY_CONSTRUCTOR(Div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00004848};
4849
Vladimir Markofcb503c2016-05-18 12:48:17 +01004850class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004851 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004852 HRem(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004853 HInstruction* left,
4854 HInstruction* right,
4855 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004856 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004857
Roland Levillain9867bc72015-08-05 10:21:34 +01004858 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004859 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004860 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004861 // Our graph structure ensures we never have 0 for `y` during
4862 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004863 DCHECK_NE(y, 0);
4864 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4865 return (y == -1) ? 0 : x % y;
4866 }
4867
Roland Levillain31dd3d62016-02-16 12:21:02 +00004868 template <typename T>
4869 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004870 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004871 return std::fmod(x, y);
4872 }
4873
Roland Levillain9867bc72015-08-05 10:21:34 +01004874 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004875 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004876 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004877 }
4878 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004879 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004880 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004881 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004882 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4883 return GetBlock()->GetGraph()->GetFloatConstant(
4884 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4885 }
4886 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4887 return GetBlock()->GetGraph()->GetDoubleConstant(
4888 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4889 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004890
4891 DECLARE_INSTRUCTION(Rem);
4892
Artem Serovcced8ba2017-07-19 18:18:09 +01004893 protected:
4894 DEFAULT_COPY_CONSTRUCTOR(Rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00004895};
4896
Vladimir Markofcb503c2016-05-18 12:48:17 +01004897class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00004898 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004899 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4900 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004901 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004902 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004903 SetRawInputAt(0, value);
4904 }
4905
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004906 DataType::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004907
Calin Juravled0d48522014-11-04 16:40:20 +00004908 bool CanBeMoved() const OVERRIDE { return true; }
4909
Vladimir Marko372f10e2016-05-17 16:30:10 +01004910 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004911 return true;
4912 }
4913
4914 bool NeedsEnvironment() const OVERRIDE { return true; }
4915 bool CanThrow() const OVERRIDE { return true; }
4916
Calin Juravled0d48522014-11-04 16:40:20 +00004917 DECLARE_INSTRUCTION(DivZeroCheck);
4918
Artem Serovcced8ba2017-07-19 18:18:09 +01004919 protected:
4920 DEFAULT_COPY_CONSTRUCTOR(DivZeroCheck);
Calin Juravled0d48522014-11-04 16:40:20 +00004921};
4922
Vladimir Markofcb503c2016-05-18 12:48:17 +01004923class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004924 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004925 HShl(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004926 HInstruction* value,
4927 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004928 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004929 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004930 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
4931 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004932 }
4933
Roland Levillain5b5b9312016-03-22 14:57:31 +00004934 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004935 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004936 return value << (distance & max_shift_distance);
4937 }
4938
4939 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004940 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004941 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004942 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004943 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004944 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004945 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004946 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004947 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4948 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4949 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4950 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004951 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004952 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4953 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004954 LOG(FATAL) << DebugName() << " is not defined for float values";
4955 UNREACHABLE();
4956 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004957 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4958 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004959 LOG(FATAL) << DebugName() << " is not defined for double values";
4960 UNREACHABLE();
4961 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004962
4963 DECLARE_INSTRUCTION(Shl);
4964
Artem Serovcced8ba2017-07-19 18:18:09 +01004965 protected:
4966 DEFAULT_COPY_CONSTRUCTOR(Shl);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004967};
4968
Vladimir Markofcb503c2016-05-18 12:48:17 +01004969class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004970 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004971 HShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004972 HInstruction* value,
4973 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004974 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004975 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004976 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
4977 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004978 }
4979
Roland Levillain5b5b9312016-03-22 14:57:31 +00004980 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004981 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004982 return value >> (distance & max_shift_distance);
4983 }
4984
4985 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004986 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004987 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004988 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004989 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004990 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004991 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004992 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004993 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4994 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4995 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4996 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004997 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004998 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4999 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005000 LOG(FATAL) << DebugName() << " is not defined for float values";
5001 UNREACHABLE();
5002 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005003 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5004 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005005 LOG(FATAL) << DebugName() << " is not defined for double values";
5006 UNREACHABLE();
5007 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005008
5009 DECLARE_INSTRUCTION(Shr);
5010
Artem Serovcced8ba2017-07-19 18:18:09 +01005011 protected:
5012 DEFAULT_COPY_CONSTRUCTOR(Shr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005013};
5014
Vladimir Markofcb503c2016-05-18 12:48:17 +01005015class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005016 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005017 HUShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005018 HInstruction* value,
5019 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005020 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00005021 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005022 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5023 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00005024 }
5025
Roland Levillain5b5b9312016-03-22 14:57:31 +00005026 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005027 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005028 typedef typename std::make_unsigned<T>::type V;
5029 V ux = static_cast<V>(value);
5030 return static_cast<T>(ux >> (distance & max_shift_distance));
5031 }
5032
5033 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005034 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005035 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005036 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005037 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005038 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005039 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005040 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005041 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5042 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5043 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5044 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00005045 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005046 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5047 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005048 LOG(FATAL) << DebugName() << " is not defined for float values";
5049 UNREACHABLE();
5050 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005051 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5052 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005053 LOG(FATAL) << DebugName() << " is not defined for double values";
5054 UNREACHABLE();
5055 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005056
5057 DECLARE_INSTRUCTION(UShr);
5058
Artem Serovcced8ba2017-07-19 18:18:09 +01005059 protected:
5060 DEFAULT_COPY_CONSTRUCTOR(UShr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005061};
5062
Vladimir Markofcb503c2016-05-18 12:48:17 +01005063class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005064 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005065 HAnd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005066 HInstruction* left,
5067 HInstruction* right,
5068 uint32_t dex_pc = kNoDexPc)
5069 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005070
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005071 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005072
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005073 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005074
5075 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005076 return GetBlock()->GetGraph()->GetIntConstant(
5077 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005078 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005079 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005080 return GetBlock()->GetGraph()->GetLongConstant(
5081 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005082 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005083 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5084 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5085 LOG(FATAL) << DebugName() << " is not defined for float values";
5086 UNREACHABLE();
5087 }
5088 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5089 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5090 LOG(FATAL) << DebugName() << " is not defined for double values";
5091 UNREACHABLE();
5092 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005093
5094 DECLARE_INSTRUCTION(And);
5095
Artem Serovcced8ba2017-07-19 18:18:09 +01005096 protected:
5097 DEFAULT_COPY_CONSTRUCTOR(And);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005098};
5099
Vladimir Markofcb503c2016-05-18 12:48:17 +01005100class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005101 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005102 HOr(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005103 HInstruction* left,
5104 HInstruction* right,
5105 uint32_t dex_pc = kNoDexPc)
5106 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005107
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005108 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005109
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005110 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005111
5112 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005113 return GetBlock()->GetGraph()->GetIntConstant(
5114 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005115 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005116 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005117 return GetBlock()->GetGraph()->GetLongConstant(
5118 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005119 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005120 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5121 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5122 LOG(FATAL) << DebugName() << " is not defined for float values";
5123 UNREACHABLE();
5124 }
5125 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5126 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5127 LOG(FATAL) << DebugName() << " is not defined for double values";
5128 UNREACHABLE();
5129 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005130
5131 DECLARE_INSTRUCTION(Or);
5132
Artem Serovcced8ba2017-07-19 18:18:09 +01005133 protected:
5134 DEFAULT_COPY_CONSTRUCTOR(Or);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005135};
5136
Vladimir Markofcb503c2016-05-18 12:48:17 +01005137class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005138 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005139 HXor(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005140 HInstruction* left,
5141 HInstruction* right,
5142 uint32_t dex_pc = kNoDexPc)
5143 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005144
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005145 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005146
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005147 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005148
5149 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005150 return GetBlock()->GetGraph()->GetIntConstant(
5151 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005152 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005153 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005154 return GetBlock()->GetGraph()->GetLongConstant(
5155 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005156 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005157 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5158 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5159 LOG(FATAL) << DebugName() << " is not defined for float values";
5160 UNREACHABLE();
5161 }
5162 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5163 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5164 LOG(FATAL) << DebugName() << " is not defined for double values";
5165 UNREACHABLE();
5166 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005167
5168 DECLARE_INSTRUCTION(Xor);
5169
Artem Serovcced8ba2017-07-19 18:18:09 +01005170 protected:
5171 DEFAULT_COPY_CONSTRUCTOR(Xor);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005172};
5173
Vladimir Markofcb503c2016-05-18 12:48:17 +01005174class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005175 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005176 HRor(DataType::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00005177 : HBinaryOperation(result_type, value, distance) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005178 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5179 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00005180 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005181
Roland Levillain5b5b9312016-03-22 14:57:31 +00005182 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005183 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005184 typedef typename std::make_unsigned<T>::type V;
5185 V ux = static_cast<V>(value);
5186 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005187 return static_cast<T>(ux);
5188 } else {
5189 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005190 return static_cast<T>(ux >> (distance & max_shift_value)) |
5191 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005192 }
5193 }
5194
Roland Levillain5b5b9312016-03-22 14:57:31 +00005195 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005196 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005197 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005198 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005199 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005200 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005201 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005202 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005203 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5204 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5205 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5206 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005207 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005208 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5209 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005210 LOG(FATAL) << DebugName() << " is not defined for float values";
5211 UNREACHABLE();
5212 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005213 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5214 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005215 LOG(FATAL) << DebugName() << " is not defined for double values";
5216 UNREACHABLE();
5217 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005218
5219 DECLARE_INSTRUCTION(Ror);
5220
Artem Serovcced8ba2017-07-19 18:18:09 +01005221 protected:
5222 DEFAULT_COPY_CONSTRUCTOR(Ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005223};
5224
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005225// The value of a parameter in this method. Its location depends on
5226// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01005227class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005228 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005229 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005230 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005231 uint8_t index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005232 DataType::Type parameter_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005233 bool is_this = false)
5234 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005235 dex_file_(dex_file),
5236 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005237 index_(index) {
5238 SetPackedFlag<kFlagIsThis>(is_this);
5239 SetPackedFlag<kFlagCanBeNull>(!is_this);
5240 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005241
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005242 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005243 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005244 uint8_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07005245 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005246
Vladimir Markoa1de9182016-02-25 11:37:38 +00005247 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
5248 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005249
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005250 DECLARE_INSTRUCTION(ParameterValue);
5251
Artem Serovcced8ba2017-07-19 18:18:09 +01005252 protected:
5253 DEFAULT_COPY_CONSTRUCTOR(ParameterValue);
5254
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005255 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005256 // Whether or not the parameter value corresponds to 'this' argument.
5257 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
5258 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5259 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5260 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5261 "Too many packed fields.");
5262
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005263 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005264 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005265 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005266 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005267 const uint8_t index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005268};
5269
Vladimir Markofcb503c2016-05-18 12:48:17 +01005270class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005271 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005272 HNot(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005273 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005274
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005275 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005276 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005277 return true;
5278 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005279
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005280 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005281
5282 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005283 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005284 }
5285 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005286 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005287 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005288 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5289 LOG(FATAL) << DebugName() << " is not defined for float values";
5290 UNREACHABLE();
5291 }
5292 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5293 LOG(FATAL) << DebugName() << " is not defined for double values";
5294 UNREACHABLE();
5295 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005296
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005297 DECLARE_INSTRUCTION(Not);
5298
Artem Serovcced8ba2017-07-19 18:18:09 +01005299 protected:
5300 DEFAULT_COPY_CONSTRUCTOR(Not);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005301};
5302
Vladimir Markofcb503c2016-05-18 12:48:17 +01005303class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005304 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005305 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005306 : HUnaryOperation(DataType::Type::kBool, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01005307
5308 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005309 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01005310 return true;
5311 }
5312
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005313 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005314 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005315 return !x;
5316 }
5317
Roland Levillain9867bc72015-08-05 10:21:34 +01005318 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005319 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005320 }
5321 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5322 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005323 UNREACHABLE();
5324 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005325 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5326 LOG(FATAL) << DebugName() << " is not defined for float values";
5327 UNREACHABLE();
5328 }
5329 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5330 LOG(FATAL) << DebugName() << " is not defined for double values";
5331 UNREACHABLE();
5332 }
David Brazdil66d126e2015-04-03 16:02:44 +01005333
5334 DECLARE_INSTRUCTION(BooleanNot);
5335
Artem Serovcced8ba2017-07-19 18:18:09 +01005336 protected:
5337 DEFAULT_COPY_CONSTRUCTOR(BooleanNot);
David Brazdil66d126e2015-04-03 16:02:44 +01005338};
5339
Vladimir Markofcb503c2016-05-18 12:48:17 +01005340class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005341 public:
5342 // Instantiate a type conversion of `input` to `result_type`.
Vladimir Markoc8fb2112017-10-03 11:37:52 +01005343 HTypeConversion(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005344 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005345 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005346 // Invariant: We should never generate a conversion to a Boolean value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005347 DCHECK_NE(DataType::Type::kBool, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005348 }
5349
5350 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005351 DataType::Type GetInputType() const { return GetInput()->GetType(); }
5352 DataType::Type GetResultType() const { return GetType(); }
Roland Levillaindff1f282014-11-05 14:15:05 +00005353
5354 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005355 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5356 return true;
5357 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005358
Mark Mendelle82549b2015-05-06 10:55:34 -04005359 // Try to statically evaluate the conversion and return a HConstant
5360 // containing the result. If the input cannot be converted, return nullptr.
5361 HConstant* TryStaticEvaluation() const;
5362
Roland Levillaindff1f282014-11-05 14:15:05 +00005363 DECLARE_INSTRUCTION(TypeConversion);
5364
Artem Serovcced8ba2017-07-19 18:18:09 +01005365 protected:
5366 DEFAULT_COPY_CONSTRUCTOR(TypeConversion);
Roland Levillaindff1f282014-11-05 14:15:05 +00005367};
5368
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005369static constexpr uint32_t kNoRegNumber = -1;
5370
Vladimir Markofcb503c2016-05-18 12:48:17 +01005371class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005372 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005373 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
5374 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005375 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005376 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005377 SetRawInputAt(0, value);
5378 }
5379
Artem Serovcced8ba2017-07-19 18:18:09 +01005380 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00005381 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005382 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005383 return true;
5384 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005385
Calin Juravle10e244f2015-01-26 18:54:32 +00005386 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005387
Calin Juravle10e244f2015-01-26 18:54:32 +00005388 bool CanThrow() const OVERRIDE { return true; }
5389
5390 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005391
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005392 DECLARE_INSTRUCTION(NullCheck);
5393
Artem Serovcced8ba2017-07-19 18:18:09 +01005394 protected:
5395 DEFAULT_COPY_CONSTRUCTOR(NullCheck);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005396};
5397
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005398// Embeds an ArtField and all the information required by the compiler. We cache
5399// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005400class FieldInfo : public ValueObject {
5401 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005402 FieldInfo(ArtField* field,
5403 MemberOffset field_offset,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005404 DataType::Type field_type,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005405 bool is_volatile,
5406 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005407 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005408 const DexFile& dex_file)
5409 : field_(field),
5410 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005411 field_type_(field_type),
5412 is_volatile_(is_volatile),
5413 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005414 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005415 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005416
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005417 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005418 MemberOffset GetFieldOffset() const { return field_offset_; }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005419 DataType::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005420 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005421 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005422 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005423 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005424
5425 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005426 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005427 const MemberOffset field_offset_;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005428 const DataType::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005429 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005430 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005431 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005432 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005433};
5434
Vladimir Markofcb503c2016-05-18 12:48:17 +01005435class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005436 public:
5437 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005438 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005439 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005440 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005441 bool is_volatile,
5442 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005443 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005444 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005445 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005446 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005447 field_info_(field,
5448 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005449 field_type,
5450 is_volatile,
5451 field_idx,
5452 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005453 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005454 SetRawInputAt(0, value);
5455 }
5456
Artem Serovcced8ba2017-07-19 18:18:09 +01005457 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005458 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005459
Vladimir Marko372f10e2016-05-17 16:30:10 +01005460 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5461 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005462 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005463 }
5464
Calin Juravle641547a2015-04-21 22:08:51 +01005465 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005466 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005467 }
5468
5469 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005470 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5471 }
5472
Calin Juravle52c48962014-12-16 17:02:57 +00005473 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005474 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005475 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005476 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005477
Vladimir Marko61b92282017-10-11 13:23:17 +01005478 void SetType(DataType::Type new_type) {
5479 DCHECK(DataType::IsIntegralType(GetType()));
5480 DCHECK(DataType::IsIntegralType(new_type));
5481 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5482 SetPackedField<TypeField>(new_type);
5483 }
5484
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005485 DECLARE_INSTRUCTION(InstanceFieldGet);
5486
Artem Serovcced8ba2017-07-19 18:18:09 +01005487 protected:
5488 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldGet);
5489
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005490 private:
5491 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005492};
5493
Vladimir Markofcb503c2016-05-18 12:48:17 +01005494class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005495 public:
5496 HInstanceFieldSet(HInstruction* object,
5497 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005498 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005499 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005500 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005501 bool is_volatile,
5502 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005503 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005504 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005505 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005506 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005507 field_info_(field,
5508 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005509 field_type,
5510 is_volatile,
5511 field_idx,
5512 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005513 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005514 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005515 SetRawInputAt(0, object);
5516 SetRawInputAt(1, value);
5517 }
5518
Artem Serovcced8ba2017-07-19 18:18:09 +01005519 bool IsClonable() const OVERRIDE { return true; }
5520
Calin Juravle641547a2015-04-21 22:08:51 +01005521 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005522 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005523 }
5524
Calin Juravle52c48962014-12-16 17:02:57 +00005525 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005526 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005527 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005528 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005529 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005530 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5531 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005532
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005533 DECLARE_INSTRUCTION(InstanceFieldSet);
5534
Artem Serovcced8ba2017-07-19 18:18:09 +01005535 protected:
5536 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldSet);
5537
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005538 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005539 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5540 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5541 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5542 "Too many packed fields.");
5543
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005544 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005545};
5546
Vladimir Markofcb503c2016-05-18 12:48:17 +01005547class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005548 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005549 HArrayGet(HInstruction* array,
5550 HInstruction* index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005551 DataType::Type type,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005552 uint32_t dex_pc)
5553 : HArrayGet(array,
5554 index,
5555 type,
5556 SideEffects::ArrayReadOfType(type),
5557 dex_pc,
5558 /* is_string_char_at */ false) {}
5559
5560 HArrayGet(HInstruction* array,
5561 HInstruction* index,
5562 DataType::Type type,
5563 SideEffects side_effects,
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005564 uint32_t dex_pc,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005565 bool is_string_char_at)
5566 : HExpression(type, side_effects, dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005567 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005568 SetRawInputAt(0, array);
5569 SetRawInputAt(1, index);
5570 }
5571
Artem Serovcced8ba2017-07-19 18:18:09 +01005572 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005573 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005574 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005575 return true;
5576 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005577 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005578 // TODO: We can be smarter here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01005579 // Currently, unless the array is the result of NewArray, the array access is always
5580 // preceded by some form of null NullCheck necessary for the bounds check, usually
5581 // implicit null check on the ArrayLength input to BoundsCheck or Deoptimize for
5582 // dynamic BCE. There are cases when these could be removed to produce better code.
5583 // If we ever add optimizations to do so we should allow an implicit check here
5584 // (as long as the address falls in the first page).
5585 //
5586 // As an example of such fancy optimization, we could eliminate BoundsCheck for
5587 // a = cond ? new int[1] : null;
5588 // a[0]; // The Phi does not need bounds check for either input.
Calin Juravle77520bc2015-01-12 18:45:46 +00005589 return false;
5590 }
5591
David Brazdil4833f5a2015-12-16 10:37:39 +00005592 bool IsEquivalentOf(HArrayGet* other) const {
5593 bool result = (GetDexPc() == other->GetDexPc());
5594 if (kIsDebugBuild && result) {
5595 DCHECK_EQ(GetBlock(), other->GetBlock());
5596 DCHECK_EQ(GetArray(), other->GetArray());
5597 DCHECK_EQ(GetIndex(), other->GetIndex());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005598 if (DataType::IsIntOrLongType(GetType())) {
5599 DCHECK(DataType::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005600 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005601 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
5602 DCHECK(DataType::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005603 }
5604 }
5605 return result;
5606 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005607
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005608 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5609
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005610 HInstruction* GetArray() const { return InputAt(0); }
5611 HInstruction* GetIndex() const { return InputAt(1); }
5612
Vladimir Marko61b92282017-10-11 13:23:17 +01005613 void SetType(DataType::Type new_type) {
5614 DCHECK(DataType::IsIntegralType(GetType()));
5615 DCHECK(DataType::IsIntegralType(new_type));
5616 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5617 SetPackedField<TypeField>(new_type);
5618 }
5619
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005620 DECLARE_INSTRUCTION(ArrayGet);
5621
Artem Serovcced8ba2017-07-19 18:18:09 +01005622 protected:
5623 DEFAULT_COPY_CONSTRUCTOR(ArrayGet);
5624
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005625 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005626 // We treat a String as an array, creating the HArrayGet from String.charAt()
5627 // intrinsic in the instruction simplifier. We can always determine whether
5628 // a particular HArrayGet is actually a String.charAt() by looking at the type
5629 // of the input but that requires holding the mutator lock, so we prefer to use
5630 // a flag, so that code generators don't need to do the locking.
5631 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
5632 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5633 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5634 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005635};
5636
Vladimir Markofcb503c2016-05-18 12:48:17 +01005637class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005638 public:
5639 HArraySet(HInstruction* array,
5640 HInstruction* index,
5641 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005642 DataType::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005643 uint32_t dex_pc)
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005644 : HArraySet(array,
5645 index,
5646 value,
5647 expected_component_type,
5648 // Make a best guess for side effects now, may be refined during SSA building.
5649 ComputeSideEffects(GetComponentType(value->GetType(), expected_component_type)),
5650 dex_pc) {}
5651
5652 HArraySet(HInstruction* array,
5653 HInstruction* index,
5654 HInstruction* value,
5655 DataType::Type expected_component_type,
5656 SideEffects side_effects,
5657 uint32_t dex_pc)
5658 : HTemplateInstruction(side_effects, dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005659 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005660 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005661 SetPackedFlag<kFlagValueCanBeNull>(true);
5662 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005663 SetRawInputAt(0, array);
5664 SetRawInputAt(1, index);
5665 SetRawInputAt(2, value);
5666 }
5667
Artem Serovcced8ba2017-07-19 18:18:09 +01005668 bool IsClonable() const OVERRIDE { return true; }
5669
Calin Juravle77520bc2015-01-12 18:45:46 +00005670 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005671 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005672 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005673 }
5674
Mingyao Yang81014cb2015-06-02 03:16:27 -07005675 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005676 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005677
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005678 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005679 // TODO: Same as for ArrayGet.
5680 return false;
5681 }
5682
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005683 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005684 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005685 }
5686
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005687 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005688 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005689 }
5690
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005691 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005692 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005693 }
5694
Vladimir Markoa1de9182016-02-25 11:37:38 +00005695 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5696 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5697 bool StaticTypeOfArrayIsObjectArray() const {
5698 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5699 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005700
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005701 HInstruction* GetArray() const { return InputAt(0); }
5702 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005703 HInstruction* GetValue() const { return InputAt(2); }
5704
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005705 DataType::Type GetComponentType() const {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005706 return GetComponentType(GetValue()->GetType(), GetRawExpectedComponentType());
5707 }
5708
5709 static DataType::Type GetComponentType(DataType::Type value_type,
5710 DataType::Type expected_component_type) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005711 // The Dex format does not type floating point index operations. Since the
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005712 // `expected_component_type` comes from SSA building and can therefore not
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005713 // be correct, we also check what is the value type. If it is a floating
5714 // point type, we must use that type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005715 return ((value_type == DataType::Type::kFloat32) || (value_type == DataType::Type::kFloat64))
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005716 ? value_type
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005717 : expected_component_type;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005718 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005719
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005720 DataType::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005721 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005722 }
5723
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005724 static SideEffects ComputeSideEffects(DataType::Type type) {
5725 return SideEffects::ArrayWriteOfType(type).Union(SideEffectsForArchRuntimeCalls(type));
Aart Bik18b36ab2016-04-13 16:41:35 -07005726 }
5727
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005728 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type value_type) {
5729 return (value_type == DataType::Type::kReference) ? SideEffects::CanTriggerGC()
5730 : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005731 }
5732
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005733 DECLARE_INSTRUCTION(ArraySet);
5734
Artem Serovcced8ba2017-07-19 18:18:09 +01005735 protected:
5736 DEFAULT_COPY_CONSTRUCTOR(ArraySet);
5737
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005738 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005739 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5740 static constexpr size_t kFieldExpectedComponentTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005741 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00005742 static constexpr size_t kFlagNeedsTypeCheck =
5743 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5744 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005745 // Cached information for the reference_type_info_ so that codegen
5746 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005747 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5748 static constexpr size_t kNumberOfArraySetPackedBits =
5749 kFlagStaticTypeOfArrayIsObjectArray + 1;
5750 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5751 using ExpectedComponentTypeField =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005752 BitField<DataType::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005753};
5754
Vladimir Markofcb503c2016-05-18 12:48:17 +01005755class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005756 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005757 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005758 : HExpression(DataType::Type::kInt32, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005759 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005760 // Note that arrays do not change length, so the instruction does not
5761 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005762 SetRawInputAt(0, array);
5763 }
5764
Artem Serovcced8ba2017-07-19 18:18:09 +01005765 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle77520bc2015-01-12 18:45:46 +00005766 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005767 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005768 return true;
5769 }
Calin Juravle641547a2015-04-21 22:08:51 +01005770 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5771 return obj == InputAt(0);
5772 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005773
Vladimir Markodce016e2016-04-28 13:10:02 +01005774 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5775
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005776 DECLARE_INSTRUCTION(ArrayLength);
5777
Artem Serovcced8ba2017-07-19 18:18:09 +01005778 protected:
5779 DEFAULT_COPY_CONSTRUCTOR(ArrayLength);
5780
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005781 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005782 // We treat a String as an array, creating the HArrayLength from String.length()
5783 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5784 // determine whether a particular HArrayLength is actually a String.length() by
5785 // looking at the type of the input but that requires holding the mutator lock, so
5786 // we prefer to use a flag, so that code generators don't need to do the locking.
5787 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5788 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5789 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5790 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005791};
5792
Vladimir Markofcb503c2016-05-18 12:48:17 +01005793class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005794 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005795 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5796 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005797 HBoundsCheck(HInstruction* index,
5798 HInstruction* length,
5799 uint32_t dex_pc,
Vladimir Marko0259c242017-12-04 11:27:47 +00005800 bool is_string_char_at = false)
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005801 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005802 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(index->GetType()));
Vladimir Marko0259c242017-12-04 11:27:47 +00005803 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005804 SetRawInputAt(0, index);
5805 SetRawInputAt(1, length);
5806 }
5807
Artem Serovcced8ba2017-07-19 18:18:09 +01005808 bool IsClonable() const OVERRIDE { return true; }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005809 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005810 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005811 return true;
5812 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005813
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005814 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005815
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005816 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005817
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005818 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005819
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005820 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005821
5822 DECLARE_INSTRUCTION(BoundsCheck);
5823
Artem Serovcced8ba2017-07-19 18:18:09 +01005824 protected:
5825 DEFAULT_COPY_CONSTRUCTOR(BoundsCheck);
5826
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005827 private:
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005828 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005829};
5830
Vladimir Markofcb503c2016-05-18 12:48:17 +01005831class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005832 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005833 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005834 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005835
Artem Serovcced8ba2017-07-19 18:18:09 +01005836 bool IsClonable() const OVERRIDE { return true; }
5837
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005838 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005839 return true;
5840 }
5841
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005842 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5843 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005844
5845 DECLARE_INSTRUCTION(SuspendCheck);
5846
Artem Serovcced8ba2017-07-19 18:18:09 +01005847 protected:
5848 DEFAULT_COPY_CONSTRUCTOR(SuspendCheck);
5849
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005850 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005851 // Only used for code generation, in order to share the same slow path between back edges
5852 // of a same loop.
5853 SlowPathCode* slow_path_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005854};
5855
David Srbecky0cf44932015-12-09 14:09:59 +00005856// Pseudo-instruction which provides the native debugger with mapping information.
5857// It ensures that we can generate line number and local variables at this point.
5858class HNativeDebugInfo : public HTemplateInstruction<0> {
5859 public:
5860 explicit HNativeDebugInfo(uint32_t dex_pc)
5861 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5862
5863 bool NeedsEnvironment() const OVERRIDE {
5864 return true;
5865 }
5866
5867 DECLARE_INSTRUCTION(NativeDebugInfo);
5868
Artem Serovcced8ba2017-07-19 18:18:09 +01005869 protected:
5870 DEFAULT_COPY_CONSTRUCTOR(NativeDebugInfo);
David Srbecky0cf44932015-12-09 14:09:59 +00005871};
5872
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005873/**
5874 * Instruction to load a Class object.
5875 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005876class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005877 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005878 // Determines how to load the Class.
5879 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005880 // We cannot load this class. See HSharpening::SharpenLoadClass.
5881 kInvalid = -1,
5882
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005883 // Use the Class* from the method's own ArtMethod*.
5884 kReferrersClass,
5885
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005886 // Use PC-relative boot image Class* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01005887 // Used for boot image classes referenced by boot image code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005888 kBootImageLinkTimePcRelative,
5889
5890 // Use a known boot image Class* address, embedded in the code by the codegen.
5891 // Used for boot image classes referenced by apps in AOT- and JIT-compiled code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005892 kBootImageAddress,
5893
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005894 // Use a PC-relative load from a boot image ClassTable mmapped into the .bss
5895 // of the oat file.
5896 kBootImageClassTable,
5897
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005898 // Load from an entry in the .bss section using a PC-relative load.
5899 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
5900 kBssEntry,
5901
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005902 // Load from the root table associated with the JIT compiled method.
5903 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005904
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005905 // Load using a simple runtime call. This is the fall-back load kind when
5906 // the codegen is unable to use another appropriate kind.
5907 kRuntimeCall,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005908
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005909 kLast = kRuntimeCall
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005910 };
5911
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005912 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005913 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005914 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005915 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005916 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005917 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00005918 bool needs_access_check)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005919 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5920 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005921 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005922 dex_file_(dex_file),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005923 klass_(klass),
Calin Juravle2e768302015-07-28 14:41:11 +00005924 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005925 // Referrers class should not need access check. We never inline unverified
5926 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005927 DCHECK(!is_referrers_class || !needs_access_check);
5928
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005929 SetPackedField<LoadKindField>(
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005930 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kRuntimeCall);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005931 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00005932 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005933 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005934 }
5935
Artem Serovcced8ba2017-07-19 18:18:09 +01005936 bool IsClonable() const OVERRIDE { return true; }
5937
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005938 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005939
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005940 LoadKind GetLoadKind() const {
5941 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005942 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005943
5944 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005945
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005946 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005947
Andreas Gampea5b09a62016-11-17 15:21:22 -08005948 size_t ComputeHashCode() const OVERRIDE { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005949
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005950 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005951
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005952 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005953 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005954 }
5955
Calin Juravle0ba218d2015-05-19 18:46:01 +01005956 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005957 // The entrypoint the code generator is going to call does not do
5958 // clinit of the class.
5959 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005960 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005961 }
5962
5963 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00005964 return NeedsAccessCheck() ||
5965 MustGenerateClinitCheck() ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005966 GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005967 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01005968 }
5969
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005970 bool CanThrow() const OVERRIDE {
Vladimir Marko41559982017-01-06 14:04:23 +00005971 return NeedsAccessCheck() ||
5972 MustGenerateClinitCheck() ||
5973 // If the class is in the boot image, the lookup in the runtime call cannot throw.
5974 // This keeps CanThrow() consistent between non-PIC (using kBootImageAddress) and
5975 // PIC and subsequently avoids a DCE behavior dependency on the PIC option.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005976 ((GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005977 GetLoadKind() == LoadKind::kBssEntry) &&
5978 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005979 }
5980
Calin Juravleacf735c2015-02-12 15:25:22 +00005981 ReferenceTypeInfo GetLoadedClassRTI() {
5982 return loaded_class_rti_;
5983 }
5984
5985 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5986 // Make sure we only set exact types (the loaded class should never be merged).
5987 DCHECK(rti.IsExact());
5988 loaded_class_rti_ = rti;
5989 }
5990
Andreas Gampea5b09a62016-11-17 15:21:22 -08005991 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005992 const DexFile& GetDexFile() const { return dex_file_; }
5993
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005994 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005995 return GetLoadKind() == LoadKind::kRuntimeCall;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005996 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005997
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005998 static SideEffects SideEffectsForArchRuntimeCalls() {
5999 return SideEffects::CanTriggerGC();
6000 }
6001
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006002 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006003 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006004 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006005 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006006
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006007 void MarkInBootImage() {
6008 SetPackedFlag<kFlagIsInBootImage>(true);
6009 }
6010
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006011 void AddSpecialInput(HInstruction* special_input);
6012
6013 using HInstruction::GetInputRecords; // Keep the const version visible.
6014 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
6015 return ArrayRef<HUserRecord<HInstruction*>>(
6016 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6017 }
6018
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006019 DataType::Type GetType() const OVERRIDE {
6020 return DataType::Type::kReference;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006021 }
6022
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006023 Handle<mirror::Class> GetClass() const {
6024 return klass_;
6025 }
6026
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006027 DECLARE_INSTRUCTION(LoadClass);
6028
Artem Serovcced8ba2017-07-19 18:18:09 +01006029 protected:
6030 DEFAULT_COPY_CONSTRUCTOR(LoadClass);
6031
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006032 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006033 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00006034 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006035 // Whether this instruction must generate the initialization check.
6036 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006037 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006038 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
6039 static constexpr size_t kFieldLoadKindSize =
6040 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
6041 static constexpr size_t kNumberOfLoadClassPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006042 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006043 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
6044
6045 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006046 return load_kind == LoadKind::kReferrersClass ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006047 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006048 load_kind == LoadKind::kBootImageClassTable ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006049 load_kind == LoadKind::kBssEntry ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006050 load_kind == LoadKind::kRuntimeCall;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006051 }
6052
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006053 void SetLoadKindInternal(LoadKind load_kind);
6054
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006055 // The special input is the HCurrentMethod for kRuntimeCall or kReferrersClass.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006056 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006057 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006058 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006059
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006060 // A type index and dex file where the class can be accessed. The dex file can be:
6061 // - The compiling method's dex file if the class is defined there too.
6062 // - The compiling method's dex file if the class is referenced there.
6063 // - The dex file where the class is defined. When the load kind can only be
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006064 // kBssEntry or kRuntimeCall, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08006065 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006066 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006067
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006068 Handle<mirror::Class> klass_;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006069
Calin Juravleacf735c2015-02-12 15:25:22 +00006070 ReferenceTypeInfo loaded_class_rti_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006071};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006072std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
6073
6074// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006075inline void HLoadClass::SetLoadKind(LoadKind load_kind) {
6076 // The load kind should be determined before inserting the instruction to the graph.
6077 DCHECK(GetBlock() == nullptr);
6078 DCHECK(GetEnvironment() == nullptr);
6079 SetPackedField<LoadKindField>(load_kind);
6080 if (load_kind != LoadKind::kRuntimeCall && load_kind != LoadKind::kReferrersClass) {
6081 special_input_ = HUserRecord<HInstruction*>(nullptr);
6082 }
6083 if (!NeedsEnvironment()) {
6084 SetSideEffects(SideEffects::None());
6085 }
6086}
6087
6088// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006089inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006090 // The special input is used for PC-relative loads on some architectures,
6091 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006092 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006093 GetLoadKind() == LoadKind::kBootImageAddress ||
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006094 GetLoadKind() == LoadKind::kBootImageClassTable ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006095 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006096 DCHECK(special_input_.GetInstruction() == nullptr);
6097 special_input_ = HUserRecord<HInstruction*>(special_input);
6098 special_input->AddUseAt(this, 0);
6099}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006100
Vladimir Marko372f10e2016-05-17 16:30:10 +01006101class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006102 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006103 // Determines how to load the String.
6104 enum class LoadKind {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006105 // Use PC-relative boot image String* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006106 // Used for boot image strings referenced by boot image code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006107 kBootImageLinkTimePcRelative,
6108
6109 // Use a known boot image String* address, embedded in the code by the codegen.
6110 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006111 kBootImageAddress,
6112
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006113 // Use a PC-relative load from a boot image InternTable mmapped into the .bss
6114 // of the oat file.
6115 kBootImageInternTable,
6116
Vladimir Markoaad75c62016-10-03 08:46:48 +00006117 // Load from an entry in the .bss section using a PC-relative load.
6118 // Used for strings outside boot image when .bss is accessible with a PC-relative load.
6119 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006120
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006121 // Load from the root table associated with the JIT compiled method.
6122 kJitTableAddress,
6123
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006124 // Load using a simple runtime call. This is the fall-back load kind when
6125 // the codegen is unable to use another appropriate kind.
6126 kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006127
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006128 kLast = kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006129 };
6130
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006131 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006132 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006133 const DexFile& dex_file,
6134 uint32_t dex_pc)
Vladimir Marko372f10e2016-05-17 16:30:10 +01006135 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
6136 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006137 string_index_(string_index),
6138 dex_file_(dex_file) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006139 SetPackedField<LoadKindField>(LoadKind::kRuntimeCall);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006140 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006141
Artem Serovcced8ba2017-07-19 18:18:09 +01006142 bool IsClonable() const OVERRIDE { return true; }
6143
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006144 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006145
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006146 LoadKind GetLoadKind() const {
6147 return GetPackedField<LoadKindField>();
6148 }
6149
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006150 const DexFile& GetDexFile() const {
6151 return dex_file_;
6152 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006153
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006154 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006155 return string_index_;
6156 }
6157
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006158 Handle<mirror::String> GetString() const {
6159 return string_;
6160 }
6161
6162 void SetString(Handle<mirror::String> str) {
6163 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006164 }
6165
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006166 bool CanBeMoved() const OVERRIDE { return true; }
6167
Vladimir Marko372f10e2016-05-17 16:30:10 +01006168 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006169
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006170 size_t ComputeHashCode() const OVERRIDE { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006171
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006172 // Will call the runtime if we need to load the string through
6173 // the dex cache and the string is not guaranteed to be there yet.
6174 bool NeedsEnvironment() const OVERRIDE {
6175 LoadKind load_kind = GetLoadKind();
Vladimir Marko764d4542017-05-16 10:31:41 +01006176 if (load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006177 load_kind == LoadKind::kBootImageAddress ||
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006178 load_kind == LoadKind::kBootImageInternTable ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006179 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006180 return false;
6181 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006182 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006183 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006184
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006185 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006186 return GetLoadKind() == LoadKind::kRuntimeCall;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006187 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006188
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07006189 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006190 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006191
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006192 static SideEffects SideEffectsForArchRuntimeCalls() {
6193 return SideEffects::CanTriggerGC();
6194 }
6195
Vladimir Marko372f10e2016-05-17 16:30:10 +01006196 void AddSpecialInput(HInstruction* special_input);
6197
6198 using HInstruction::GetInputRecords; // Keep the const version visible.
6199 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
6200 return ArrayRef<HUserRecord<HInstruction*>>(
6201 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006202 }
6203
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006204 DataType::Type GetType() const OVERRIDE {
6205 return DataType::Type::kReference;
Vladimir Marko372f10e2016-05-17 16:30:10 +01006206 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006207
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006208 DECLARE_INSTRUCTION(LoadString);
6209
Artem Serovcced8ba2017-07-19 18:18:09 +01006210 protected:
6211 DEFAULT_COPY_CONSTRUCTOR(LoadString);
6212
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006213 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006214 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006215 static constexpr size_t kFieldLoadKindSize =
6216 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
6217 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006218 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006219 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006220
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006221 void SetLoadKindInternal(LoadKind load_kind);
6222
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006223 // The special input is the HCurrentMethod for kRuntimeCall.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006224 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00006225 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01006226 HUserRecord<HInstruction*> special_input_;
6227
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006228 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006229 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006230
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006231 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006232};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006233std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
6234
6235// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006236inline void HLoadString::SetLoadKind(LoadKind load_kind) {
6237 // The load kind should be determined before inserting the instruction to the graph.
6238 DCHECK(GetBlock() == nullptr);
6239 DCHECK(GetEnvironment() == nullptr);
6240 DCHECK_EQ(GetLoadKind(), LoadKind::kRuntimeCall);
6241 SetPackedField<LoadKindField>(load_kind);
6242 if (load_kind != LoadKind::kRuntimeCall) {
6243 special_input_ = HUserRecord<HInstruction*>(nullptr);
6244 }
6245 if (!NeedsEnvironment()) {
6246 SetSideEffects(SideEffects::None());
6247 }
6248}
6249
6250// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006251inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006252 // The special input is used for PC-relative loads on some architectures,
6253 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006254 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006255 GetLoadKind() == LoadKind::kBootImageAddress ||
6256 GetLoadKind() == LoadKind::kBootImageInternTable ||
6257 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01006258 // HLoadString::GetInputRecords() returns an empty array at this point,
6259 // so use the GetInputRecords() from the base class to set the input record.
6260 DCHECK(special_input_.GetInstruction() == nullptr);
6261 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006262 special_input->AddUseAt(this, 0);
6263}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006264
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006265/**
6266 * Performs an initialization check on its Class object input.
6267 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01006268class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006269 public:
Roland Levillain3887c462015-08-12 18:15:42 +01006270 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07006271 : HExpression(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006272 DataType::Type::kReference,
Mingyao Yang217eb062017-12-11 15:20:07 -08006273 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006274 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006275 SetRawInputAt(0, constant);
6276 }
6277
Artem Serovcced8ba2017-07-19 18:18:09 +01006278 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006279 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006280 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006281 return true;
6282 }
6283
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006284 bool NeedsEnvironment() const OVERRIDE {
6285 // May call runtime to initialize the class.
6286 return true;
6287 }
6288
Nicolas Geoffray729645a2015-11-19 13:29:02 +00006289 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006290
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006291 HLoadClass* GetLoadClass() const {
6292 DCHECK(InputAt(0)->IsLoadClass());
6293 return InputAt(0)->AsLoadClass();
6294 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006295
6296 DECLARE_INSTRUCTION(ClinitCheck);
6297
Artem Serovcced8ba2017-07-19 18:18:09 +01006298
6299 protected:
6300 DEFAULT_COPY_CONSTRUCTOR(ClinitCheck);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006301};
6302
Vladimir Markofcb503c2016-05-18 12:48:17 +01006303class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006304 public:
6305 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006306 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006307 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006308 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006309 bool is_volatile,
6310 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006311 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006312 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006313 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01006314 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006315 field_info_(field,
6316 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006317 field_type,
6318 is_volatile,
6319 field_idx,
6320 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006321 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006322 SetRawInputAt(0, cls);
6323 }
6324
Calin Juravle52c48962014-12-16 17:02:57 +00006325
Artem Serovcced8ba2017-07-19 18:18:09 +01006326 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle10c9cbe2014-12-19 10:50:19 +00006327 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006328
Vladimir Marko372f10e2016-05-17 16:30:10 +01006329 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
6330 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006331 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006332 }
6333
6334 size_t ComputeHashCode() const OVERRIDE {
6335 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6336 }
6337
Calin Juravle52c48962014-12-16 17:02:57 +00006338 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006339 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006340 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006341 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006342
Vladimir Marko61b92282017-10-11 13:23:17 +01006343 void SetType(DataType::Type new_type) {
6344 DCHECK(DataType::IsIntegralType(GetType()));
6345 DCHECK(DataType::IsIntegralType(new_type));
6346 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6347 SetPackedField<TypeField>(new_type);
6348 }
6349
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006350 DECLARE_INSTRUCTION(StaticFieldGet);
6351
Artem Serovcced8ba2017-07-19 18:18:09 +01006352 protected:
6353 DEFAULT_COPY_CONSTRUCTOR(StaticFieldGet);
6354
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006355 private:
6356 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006357};
6358
Vladimir Markofcb503c2016-05-18 12:48:17 +01006359class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006360 public:
6361 HStaticFieldSet(HInstruction* cls,
6362 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006363 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006364 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006365 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006366 bool is_volatile,
6367 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006368 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006369 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006370 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01006371 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006372 field_info_(field,
6373 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006374 field_type,
6375 is_volatile,
6376 field_idx,
6377 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006378 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006379 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006380 SetRawInputAt(0, cls);
6381 SetRawInputAt(1, value);
6382 }
6383
Artem Serovcced8ba2017-07-19 18:18:09 +01006384 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle52c48962014-12-16 17:02:57 +00006385 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006386 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006387 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006388 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006389
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006390 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006391 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6392 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006393
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006394 DECLARE_INSTRUCTION(StaticFieldSet);
6395
Artem Serovcced8ba2017-07-19 18:18:09 +01006396 protected:
6397 DEFAULT_COPY_CONSTRUCTOR(StaticFieldSet);
6398
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006399 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006400 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6401 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6402 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6403 "Too many packed fields.");
6404
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006405 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006406};
6407
Vladimir Markofcb503c2016-05-18 12:48:17 +01006408class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006409 public:
6410 HUnresolvedInstanceFieldGet(HInstruction* obj,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006411 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006412 uint32_t field_index,
6413 uint32_t dex_pc)
6414 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6415 field_index_(field_index) {
6416 SetRawInputAt(0, obj);
6417 }
6418
Artem Serovcced8ba2017-07-19 18:18:09 +01006419 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006420 bool NeedsEnvironment() const OVERRIDE { return true; }
6421 bool CanThrow() const OVERRIDE { return true; }
6422
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006423 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006424 uint32_t GetFieldIndex() const { return field_index_; }
6425
6426 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6427
Artem Serovcced8ba2017-07-19 18:18:09 +01006428 protected:
6429 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldGet);
6430
Calin Juravlee460d1d2015-09-29 04:52:17 +01006431 private:
6432 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006433};
6434
Vladimir Markofcb503c2016-05-18 12:48:17 +01006435class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006436 public:
6437 HUnresolvedInstanceFieldSet(HInstruction* obj,
6438 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006439 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006440 uint32_t field_index,
6441 uint32_t dex_pc)
6442 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006443 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006444 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006445 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006446 SetRawInputAt(0, obj);
6447 SetRawInputAt(1, value);
6448 }
6449
Artem Serovcced8ba2017-07-19 18:18:09 +01006450 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006451 bool NeedsEnvironment() const OVERRIDE { return true; }
6452 bool CanThrow() const OVERRIDE { return true; }
6453
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006454 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006455 uint32_t GetFieldIndex() const { return field_index_; }
6456
6457 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6458
Artem Serovcced8ba2017-07-19 18:18:09 +01006459 protected:
6460 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldSet);
6461
Calin Juravlee460d1d2015-09-29 04:52:17 +01006462 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006463 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6464 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006465 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006466 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6467 kFieldFieldType + kFieldFieldTypeSize;
6468 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6469 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006470 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006471
Calin Juravlee460d1d2015-09-29 04:52:17 +01006472 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006473};
6474
Vladimir Markofcb503c2016-05-18 12:48:17 +01006475class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006476 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006477 HUnresolvedStaticFieldGet(DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006478 uint32_t field_index,
6479 uint32_t dex_pc)
6480 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6481 field_index_(field_index) {
6482 }
6483
Artem Serovcced8ba2017-07-19 18:18:09 +01006484 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006485 bool NeedsEnvironment() const OVERRIDE { return true; }
6486 bool CanThrow() const OVERRIDE { return true; }
6487
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006488 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006489 uint32_t GetFieldIndex() const { return field_index_; }
6490
6491 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6492
Artem Serovcced8ba2017-07-19 18:18:09 +01006493 protected:
6494 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldGet);
6495
Calin Juravlee460d1d2015-09-29 04:52:17 +01006496 private:
6497 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006498};
6499
Vladimir Markofcb503c2016-05-18 12:48:17 +01006500class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006501 public:
6502 HUnresolvedStaticFieldSet(HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006503 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006504 uint32_t field_index,
6505 uint32_t dex_pc)
6506 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006507 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006508 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006509 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006510 SetRawInputAt(0, value);
6511 }
6512
Artem Serovcced8ba2017-07-19 18:18:09 +01006513 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006514 bool NeedsEnvironment() const OVERRIDE { return true; }
6515 bool CanThrow() const OVERRIDE { return true; }
6516
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006517 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006518 uint32_t GetFieldIndex() const { return field_index_; }
6519
6520 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6521
Artem Serovcced8ba2017-07-19 18:18:09 +01006522 protected:
6523 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldSet);
6524
Calin Juravlee460d1d2015-09-29 04:52:17 +01006525 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006526 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6527 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006528 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006529 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6530 kFieldFieldType + kFieldFieldTypeSize;
6531 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6532 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006533 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006534
Calin Juravlee460d1d2015-09-29 04:52:17 +01006535 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006536};
6537
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006538// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006539class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006540 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006541 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006542 : HExpression(DataType::Type::kReference, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006543
David Brazdilbbd733e2015-08-18 17:48:17 +01006544 bool CanBeNull() const OVERRIDE { return false; }
6545
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006546 DECLARE_INSTRUCTION(LoadException);
6547
Artem Serovcced8ba2017-07-19 18:18:09 +01006548 protected:
6549 DEFAULT_COPY_CONSTRUCTOR(LoadException);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006550};
6551
David Brazdilcb1c0552015-08-04 16:22:25 +01006552// Implicit part of move-exception which clears thread-local exception storage.
6553// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006554class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006555 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006556 explicit HClearException(uint32_t dex_pc = kNoDexPc)
6557 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01006558
6559 DECLARE_INSTRUCTION(ClearException);
6560
Artem Serovcced8ba2017-07-19 18:18:09 +01006561 protected:
6562 DEFAULT_COPY_CONSTRUCTOR(ClearException);
David Brazdilcb1c0552015-08-04 16:22:25 +01006563};
6564
Vladimir Markofcb503c2016-05-18 12:48:17 +01006565class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006566 public:
6567 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006568 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006569 SetRawInputAt(0, exception);
6570 }
6571
6572 bool IsControlFlow() const OVERRIDE { return true; }
6573
6574 bool NeedsEnvironment() const OVERRIDE { return true; }
6575
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006576 bool CanThrow() const OVERRIDE { return true; }
6577
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006578 DECLARE_INSTRUCTION(Throw);
6579
Artem Serovcced8ba2017-07-19 18:18:09 +01006580 protected:
6581 DEFAULT_COPY_CONSTRUCTOR(Throw);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006582};
6583
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006584/**
6585 * Implementation strategies for the code generator of a HInstanceOf
6586 * or `HCheckCast`.
6587 */
6588enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006589 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006590 kExactCheck, // Can do a single class compare.
6591 kClassHierarchyCheck, // Can just walk the super class chain.
6592 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6593 kInterfaceCheck, // No optimization yet when checking against an interface.
6594 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006595 kArrayCheck, // No optimization yet when checking against a generic array.
6596 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006597};
6598
Roland Levillain86503782016-02-11 19:07:30 +00006599std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6600
Vladimir Markofcb503c2016-05-18 12:48:17 +01006601class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006602 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006603 HInstanceOf(HInstruction* object,
Vladimir Marko87584542017-12-12 17:47:52 +00006604 HLoadClass* target_class,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006605 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006606 uint32_t dex_pc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006607 : HExpression(DataType::Type::kBool,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006608 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006609 dex_pc) {
6610 SetPackedField<TypeCheckKindField>(check_kind);
6611 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006612 SetRawInputAt(0, object);
Vladimir Marko87584542017-12-12 17:47:52 +00006613 SetRawInputAt(1, target_class);
6614 }
6615
6616 HLoadClass* GetTargetClass() const {
6617 HInstruction* load_class = InputAt(1);
6618 DCHECK(load_class->IsLoadClass());
6619 return load_class->AsLoadClass();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006620 }
6621
Artem Serovcced8ba2017-07-19 18:18:09 +01006622 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006623 bool CanBeMoved() const OVERRIDE { return true; }
6624
Vladimir Marko372f10e2016-05-17 16:30:10 +01006625 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006626 return true;
6627 }
6628
6629 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006630 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006631 }
6632
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006633 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006634 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6635 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6636 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6637 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006638
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006639 static bool CanCallRuntime(TypeCheckKind check_kind) {
6640 // Mips currently does runtime calls for any other checks.
6641 return check_kind != TypeCheckKind::kExactCheck;
6642 }
6643
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006644 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006645 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006646 }
6647
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006648 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006649
Artem Serovcced8ba2017-07-19 18:18:09 +01006650 protected:
6651 DEFAULT_COPY_CONSTRUCTOR(InstanceOf);
6652
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006653 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006654 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6655 static constexpr size_t kFieldTypeCheckKindSize =
6656 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6657 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6658 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6659 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6660 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006661};
6662
Vladimir Markofcb503c2016-05-18 12:48:17 +01006663class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006664 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07006665 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006666 : HExpression(DataType::Type::kReference, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006667 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6668 SetPackedFlag<kFlagUpperCanBeNull>(true);
6669 SetPackedFlag<kFlagCanBeNull>(true);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006670 DCHECK_EQ(input->GetType(), DataType::Type::kReference);
Calin Juravleb1498f62015-02-16 13:13:29 +00006671 SetRawInputAt(0, input);
6672 }
6673
Artem Serovcced8ba2017-07-19 18:18:09 +01006674 bool IsClonable() const OVERRIDE { return true; }
6675
David Brazdilf5552582015-12-27 13:36:12 +00006676 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006677 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006678 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006679 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006680
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006681 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006682 DCHECK(GetUpperCanBeNull() || !can_be_null);
6683 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006684 }
6685
Vladimir Markoa1de9182016-02-25 11:37:38 +00006686 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006687
Calin Juravleb1498f62015-02-16 13:13:29 +00006688 DECLARE_INSTRUCTION(BoundType);
6689
Artem Serovcced8ba2017-07-19 18:18:09 +01006690 protected:
6691 DEFAULT_COPY_CONSTRUCTOR(BoundType);
6692
Calin Juravleb1498f62015-02-16 13:13:29 +00006693 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006694 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6695 // is false then CanBeNull() cannot be true).
6696 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6697 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6698 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6699 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6700
Calin Juravleb1498f62015-02-16 13:13:29 +00006701 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006702 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6703 // It is used to bound the type in cases like:
6704 // if (x instanceof ClassX) {
6705 // // uper_bound_ will be ClassX
6706 // }
David Brazdilf5552582015-12-27 13:36:12 +00006707 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006708};
6709
Vladimir Markofcb503c2016-05-18 12:48:17 +01006710class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006711 public:
6712 HCheckCast(HInstruction* object,
Vladimir Marko87584542017-12-12 17:47:52 +00006713 HLoadClass* target_class,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006714 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006715 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006716 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6717 SetPackedField<TypeCheckKindField>(check_kind);
6718 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006719 SetRawInputAt(0, object);
Vladimir Marko87584542017-12-12 17:47:52 +00006720 SetRawInputAt(1, target_class);
6721 }
6722
6723 HLoadClass* GetTargetClass() const {
6724 HInstruction* load_class = InputAt(1);
6725 DCHECK(load_class->IsLoadClass());
6726 return load_class->AsLoadClass();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006727 }
6728
Artem Serovcced8ba2017-07-19 18:18:09 +01006729 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006730 bool CanBeMoved() const OVERRIDE { return true; }
6731
Vladimir Marko372f10e2016-05-17 16:30:10 +01006732 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006733 return true;
6734 }
6735
6736 bool NeedsEnvironment() const OVERRIDE {
6737 // Instruction may throw a CheckCastError.
6738 return true;
6739 }
6740
6741 bool CanThrow() const OVERRIDE { return true; }
6742
Vladimir Markoa1de9182016-02-25 11:37:38 +00006743 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6744 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6745 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6746 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006747
6748 DECLARE_INSTRUCTION(CheckCast);
6749
Artem Serovcced8ba2017-07-19 18:18:09 +01006750 protected:
6751 DEFAULT_COPY_CONSTRUCTOR(CheckCast);
6752
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006753 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006754 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6755 static constexpr size_t kFieldTypeCheckKindSize =
6756 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6757 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6758 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6759 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6760 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006761};
6762
Andreas Gampe26de38b2016-07-27 17:53:11 -07006763/**
6764 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
6765 * @details We define the combined barrier types that are actually required
6766 * by the Java Memory Model, rather than using exactly the terminology from
6767 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
6768 * primitives. Note that the JSR-133 cookbook generally does not deal with
6769 * store atomicity issues, and the recipes there are not always entirely sufficient.
6770 * The current recipe is as follows:
6771 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
6772 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
6773 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
6774 * -# Use StoreStore barrier after all stores but before return from any constructor whose
6775 * class has final fields.
6776 * -# Use NTStoreStore to order non-temporal stores with respect to all later
6777 * store-to-memory instructions. Only generated together with non-temporal stores.
6778 */
6779enum MemBarrierKind {
6780 kAnyStore,
6781 kLoadAny,
6782 kStoreStore,
6783 kAnyAny,
6784 kNTStoreStore,
6785 kLastBarrierKind = kNTStoreStore
6786};
6787std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
6788
Vladimir Markofcb503c2016-05-18 12:48:17 +01006789class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006790 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006791 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006792 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006793 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6794 SetPackedField<BarrierKindField>(barrier_kind);
6795 }
Calin Juravle27df7582015-04-17 19:12:31 +01006796
Artem Serovcced8ba2017-07-19 18:18:09 +01006797 bool IsClonable() const OVERRIDE { return true; }
6798
Vladimir Markoa1de9182016-02-25 11:37:38 +00006799 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006800
6801 DECLARE_INSTRUCTION(MemoryBarrier);
6802
Artem Serovcced8ba2017-07-19 18:18:09 +01006803 protected:
6804 DEFAULT_COPY_CONSTRUCTOR(MemoryBarrier);
6805
Calin Juravle27df7582015-04-17 19:12:31 +01006806 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006807 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6808 static constexpr size_t kFieldBarrierKindSize =
6809 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6810 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6811 kFieldBarrierKind + kFieldBarrierKindSize;
6812 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6813 "Too many packed fields.");
6814 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006815};
6816
Igor Murashkind01745e2017-04-05 16:40:31 -07006817// A constructor fence orders all prior stores to fields that could be accessed via a final field of
6818// the specified object(s), with respect to any subsequent store that might "publish"
6819// (i.e. make visible) the specified object to another thread.
6820//
6821// JLS 17.5.1 "Semantics of final fields" states that a freeze action happens
6822// for all final fields (that were set) at the end of the invoked constructor.
6823//
6824// The constructor fence models the freeze actions for the final fields of an object
6825// being constructed (semantically at the end of the constructor). Constructor fences
6826// have a per-object affinity; two separate objects being constructed get two separate
6827// constructor fences.
6828//
6829// (Note: that if calling a super-constructor or forwarding to another constructor,
6830// the freezes would happen at the end of *that* constructor being invoked).
6831//
6832// The memory model guarantees that when the object being constructed is "published" after
6833// constructor completion (i.e. escapes the current thread via a store), then any final field
6834// writes must be observable on other threads (once they observe that publication).
6835//
6836// Further, anything written before the freeze, and read by dereferencing through the final field,
6837// must also be visible (so final object field could itself have an object with non-final fields;
6838// yet the freeze must also extend to them).
6839//
6840// Constructor example:
6841//
6842// class HasFinal {
6843// final int field; Optimizing IR for <init>()V:
6844// HasFinal() {
6845// field = 123; HInstanceFieldSet(this, HasFinal.field, 123)
6846// // freeze(this.field); HConstructorFence(this)
6847// } HReturn
6848// }
6849//
6850// HConstructorFence can serve double duty as a fence for new-instance/new-array allocations of
6851// already-initialized classes; in that case the allocation must act as a "default-initializer"
6852// of the object which effectively writes the class pointer "final field".
6853//
6854// For example, we can model default-initialiation as roughly the equivalent of the following:
6855//
6856// class Object {
6857// private final Class header;
6858// }
6859//
6860// Java code: Optimizing IR:
6861//
6862// T new_instance<T>() {
6863// Object obj = allocate_memory(T.class.size); obj = HInvoke(art_quick_alloc_object, T)
6864// obj.header = T.class; // header write is done by above call.
6865// // freeze(obj.header) HConstructorFence(obj)
6866// return (T)obj;
6867// }
6868//
6869// See also:
6870// * CompilerDriver::RequiresConstructorBarrier
6871// * QuasiAtomic::ThreadFenceForConstructor
6872//
6873class HConstructorFence FINAL : public HVariableInputSizeInstruction {
6874 // A fence has variable inputs because the inputs can be removed
6875 // after prepare_for_register_allocation phase.
6876 // (TODO: In the future a fence could freeze multiple objects
6877 // after merging two fences together.)
6878 public:
6879 // `fence_object` is the reference that needs to be protected for correct publication.
6880 //
6881 // It makes sense in the following situations:
6882 // * <init> constructors, it's the "this" parameter (i.e. HParameterValue, s.t. IsThis() == true).
6883 // * new-instance-like instructions, it's the return value (i.e. HNewInstance).
6884 //
6885 // After construction the `fence_object` becomes the 0th input.
6886 // This is not an input in a real sense, but just a convenient place to stash the information
6887 // about the associated object.
6888 HConstructorFence(HInstruction* fence_object,
6889 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01006890 ArenaAllocator* allocator)
Igor Murashkind01745e2017-04-05 16:40:31 -07006891 // We strongly suspect there is not a more accurate way to describe the fine-grained reordering
6892 // constraints described in the class header. We claim that these SideEffects constraints
6893 // enforce a superset of the real constraints.
6894 //
6895 // The ordering described above is conservatively modeled with SideEffects as follows:
6896 //
6897 // * To prevent reordering of the publication stores:
6898 // ----> "Reads of objects" is the initial SideEffect.
6899 // * For every primitive final field store in the constructor:
6900 // ----> Union that field's type as a read (e.g. "Read of T") into the SideEffect.
6901 // * If there are any stores to reference final fields in the constructor:
6902 // ----> Use a more conservative "AllReads" SideEffect because any stores to any references
6903 // that are reachable from `fence_object` also need to be prevented for reordering
6904 // (and we do not want to do alias analysis to figure out what those stores are).
6905 //
6906 // In the implementation, this initially starts out as an "all reads" side effect; this is an
6907 // even more conservative approach than the one described above, and prevents all of the
6908 // above reordering without analyzing any of the instructions in the constructor.
6909 //
6910 // If in a later phase we discover that there are no writes to reference final fields,
6911 // we can refine the side effect to a smaller set of type reads (see above constraints).
6912 : HVariableInputSizeInstruction(SideEffects::AllReads(),
6913 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01006914 allocator,
Igor Murashkind01745e2017-04-05 16:40:31 -07006915 /* number_of_inputs */ 1,
6916 kArenaAllocConstructorFenceInputs) {
6917 DCHECK(fence_object != nullptr);
6918 SetRawInputAt(0, fence_object);
6919 }
6920
6921 // The object associated with this constructor fence.
6922 //
6923 // (Note: This will be null after the prepare_for_register_allocation phase,
6924 // as all constructor fence inputs are removed there).
6925 HInstruction* GetFenceObject() const {
6926 return InputAt(0);
6927 }
6928
6929 // Find all the HConstructorFence uses (`fence_use`) for `this` and:
6930 // - Delete `fence_use` from `this`'s use list.
6931 // - Delete `this` from `fence_use`'s inputs list.
6932 // - If the `fence_use` is dead, remove it from the graph.
6933 //
6934 // A fence is considered dead once it no longer has any uses
6935 // and all of the inputs are dead.
6936 //
6937 // This must *not* be called during/after prepare_for_register_allocation,
6938 // because that removes all the inputs to the fences but the fence is actually
6939 // still considered live.
Igor Murashkin6ef45672017-08-08 13:59:55 -07006940 //
6941 // Returns how many HConstructorFence instructions were removed from graph.
6942 static size_t RemoveConstructorFences(HInstruction* instruction);
Igor Murashkind01745e2017-04-05 16:40:31 -07006943
Igor Murashkindd018df2017-08-09 10:38:31 -07006944 // Combine all inputs of `this` and `other` instruction and remove
6945 // `other` from the graph.
6946 //
6947 // Inputs are unique after the merge.
6948 //
6949 // Requirement: `this` must not be the same as `other.
6950 void Merge(HConstructorFence* other);
6951
Igor Murashkin79d8fa72017-04-18 09:37:23 -07006952 // Check if this constructor fence is protecting
6953 // an HNewInstance or HNewArray that is also the immediate
6954 // predecessor of `this`.
6955 //
Igor Murashkindd018df2017-08-09 10:38:31 -07006956 // If `ignore_inputs` is true, then the immediate predecessor doesn't need
6957 // to be one of the inputs of `this`.
6958 //
Igor Murashkin79d8fa72017-04-18 09:37:23 -07006959 // Returns the associated HNewArray or HNewInstance,
6960 // or null otherwise.
Igor Murashkindd018df2017-08-09 10:38:31 -07006961 HInstruction* GetAssociatedAllocation(bool ignore_inputs = false);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07006962
Igor Murashkind01745e2017-04-05 16:40:31 -07006963 DECLARE_INSTRUCTION(ConstructorFence);
6964
Artem Serovcced8ba2017-07-19 18:18:09 +01006965 protected:
6966 DEFAULT_COPY_CONSTRUCTOR(ConstructorFence);
Igor Murashkind01745e2017-04-05 16:40:31 -07006967};
6968
Vladimir Markofcb503c2016-05-18 12:48:17 +01006969class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006970 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006971 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006972 kEnter,
6973 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006974 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006975 };
6976
6977 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006978 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006979 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6980 dex_pc) {
6981 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006982 SetRawInputAt(0, object);
6983 }
6984
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006985 // Instruction may go into runtime, so we need an environment.
6986 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006987
6988 bool CanThrow() const OVERRIDE {
6989 // Verifier guarantees that monitor-exit cannot throw.
6990 // This is important because it allows the HGraphBuilder to remove
6991 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6992 return IsEnter();
6993 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006994
Vladimir Markoa1de9182016-02-25 11:37:38 +00006995 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6996 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006997
6998 DECLARE_INSTRUCTION(MonitorOperation);
6999
Artem Serovcced8ba2017-07-19 18:18:09 +01007000 protected:
7001 DEFAULT_COPY_CONSTRUCTOR(MonitorOperation);
7002
Calin Juravle52c48962014-12-16 17:02:57 +00007003 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007004 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
7005 static constexpr size_t kFieldOperationKindSize =
7006 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
7007 static constexpr size_t kNumberOfMonitorOperationPackedBits =
7008 kFieldOperationKind + kFieldOperationKindSize;
7009 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7010 "Too many packed fields.");
7011 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007012};
7013
Vladimir Markofcb503c2016-05-18 12:48:17 +01007014class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00007015 public:
7016 HSelect(HInstruction* condition,
7017 HInstruction* true_value,
7018 HInstruction* false_value,
7019 uint32_t dex_pc)
7020 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
7021 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
7022
7023 // First input must be `true_value` or `false_value` to allow codegens to
7024 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
7025 // that architectures which implement HSelect as a conditional move also
7026 // will not need to invert the condition.
7027 SetRawInputAt(0, false_value);
7028 SetRawInputAt(1, true_value);
7029 SetRawInputAt(2, condition);
7030 }
7031
Artem Serovcced8ba2017-07-19 18:18:09 +01007032 bool IsClonable() const OVERRIDE { return true; }
David Brazdil74eb1b22015-12-14 11:44:01 +00007033 HInstruction* GetFalseValue() const { return InputAt(0); }
7034 HInstruction* GetTrueValue() const { return InputAt(1); }
7035 HInstruction* GetCondition() const { return InputAt(2); }
7036
7037 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01007038 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
7039 return true;
7040 }
David Brazdil74eb1b22015-12-14 11:44:01 +00007041
7042 bool CanBeNull() const OVERRIDE {
7043 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
7044 }
7045
7046 DECLARE_INSTRUCTION(Select);
7047
Artem Serovcced8ba2017-07-19 18:18:09 +01007048 protected:
7049 DEFAULT_COPY_CONSTRUCTOR(Select);
David Brazdil74eb1b22015-12-14 11:44:01 +00007050};
7051
Vladimir Markof9f64412015-09-02 14:05:49 +01007052class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007053 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01007054 MoveOperands(Location source,
7055 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007056 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007057 HInstruction* instruction)
7058 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007059
7060 Location GetSource() const { return source_; }
7061 Location GetDestination() const { return destination_; }
7062
7063 void SetSource(Location value) { source_ = value; }
7064 void SetDestination(Location value) { destination_ = value; }
7065
7066 // The parallel move resolver marks moves as "in-progress" by clearing the
7067 // destination (but not the source).
7068 Location MarkPending() {
7069 DCHECK(!IsPending());
7070 Location dest = destination_;
7071 destination_ = Location::NoLocation();
7072 return dest;
7073 }
7074
7075 void ClearPending(Location dest) {
7076 DCHECK(IsPending());
7077 destination_ = dest;
7078 }
7079
7080 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00007081 DCHECK(source_.IsValid() || destination_.IsInvalid());
7082 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007083 }
7084
7085 // True if this blocks a move from the given location.
7086 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08007087 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007088 }
7089
7090 // A move is redundant if it's been eliminated, if its source and
7091 // destination are the same, or if its destination is unneeded.
7092 bool IsRedundant() const {
7093 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
7094 }
7095
7096 // We clear both operands to indicate move that's been eliminated.
7097 void Eliminate() {
7098 source_ = destination_ = Location::NoLocation();
7099 }
7100
7101 bool IsEliminated() const {
7102 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
7103 return source_.IsInvalid();
7104 }
7105
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007106 DataType::Type GetType() const { return type_; }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007107
Nicolas Geoffray90218252015-04-15 11:56:51 +01007108 bool Is64BitMove() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007109 return DataType::Is64BitType(type_);
Nicolas Geoffray90218252015-04-15 11:56:51 +01007110 }
7111
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007112 HInstruction* GetInstruction() const { return instruction_; }
7113
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007114 private:
7115 Location source_;
7116 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01007117 // The type this move is for.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007118 DataType::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007119 // The instruction this move is assocatied with. Null when this move is
7120 // for moving an input in the expected locations of user (including a phi user).
7121 // This is only used in debug mode, to ensure we do not connect interval siblings
7122 // in the same parallel move.
7123 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007124};
7125
Roland Levillainc9285912015-12-18 10:38:42 +00007126std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
7127
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007128static constexpr size_t kDefaultNumberOfMoves = 4;
7129
Vladimir Markofcb503c2016-05-18 12:48:17 +01007130class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007131 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007132 explicit HParallelMove(ArenaAllocator* allocator, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01007133 : HTemplateInstruction(SideEffects::None(), dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007134 moves_(allocator->Adapter(kArenaAllocMoveOperands)) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007135 moves_.reserve(kDefaultNumberOfMoves);
7136 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007137
Nicolas Geoffray90218252015-04-15 11:56:51 +01007138 void AddMove(Location source,
7139 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007140 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007141 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00007142 DCHECK(source.IsValid());
7143 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007144 if (kIsDebugBuild) {
7145 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007146 for (const MoveOperands& move : moves_) {
7147 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007148 // Special case the situation where the move is for the spill slot
7149 // of the instruction.
7150 if ((GetPrevious() == instruction)
7151 || ((GetPrevious() == nullptr)
7152 && instruction->IsPhi()
7153 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007154 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007155 << "Doing parallel moves for the same instruction.";
7156 } else {
7157 DCHECK(false) << "Doing parallel moves for the same instruction.";
7158 }
7159 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00007160 }
7161 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007162 for (const MoveOperands& move : moves_) {
7163 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007164 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01007165 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007166 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007167 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007168 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007169 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007170 }
7171
Vladimir Marko225b6462015-09-28 12:17:40 +01007172 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007173 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007174 }
7175
Vladimir Marko225b6462015-09-28 12:17:40 +01007176 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007177
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01007178 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007179
Artem Serovcced8ba2017-07-19 18:18:09 +01007180 protected:
7181 DEFAULT_COPY_CONSTRUCTOR(ParallelMove);
7182
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007183 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01007184 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007185};
7186
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007187// This instruction computes an intermediate address pointing in the 'middle' of an object. The
7188// result pointer cannot be handled by GC, so extra care is taken to make sure that this value is
7189// never used across anything that can trigger GC.
7190// The result of this instruction is not a pointer in the sense of `DataType::Type::kreference`.
7191// So we represent it by the type `DataType::Type::kInt`.
7192class HIntermediateAddress FINAL : public HExpression<2> {
7193 public:
7194 HIntermediateAddress(HInstruction* base_address, HInstruction* offset, uint32_t dex_pc)
7195 : HExpression(DataType::Type::kInt32, SideEffects::DependsOnGC(), dex_pc) {
7196 DCHECK_EQ(DataType::Size(DataType::Type::kInt32),
7197 DataType::Size(DataType::Type::kReference))
7198 << "kPrimInt and kPrimNot have different sizes.";
7199 SetRawInputAt(0, base_address);
7200 SetRawInputAt(1, offset);
7201 }
7202
Artem Serovcced8ba2017-07-19 18:18:09 +01007203 bool IsClonable() const OVERRIDE { return true; }
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007204 bool CanBeMoved() const OVERRIDE { return true; }
7205 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
7206 return true;
7207 }
7208 bool IsActualObject() const OVERRIDE { return false; }
7209
7210 HInstruction* GetBaseAddress() const { return InputAt(0); }
7211 HInstruction* GetOffset() const { return InputAt(1); }
7212
7213 DECLARE_INSTRUCTION(IntermediateAddress);
7214
Artem Serovcced8ba2017-07-19 18:18:09 +01007215 protected:
7216 DEFAULT_COPY_CONSTRUCTOR(IntermediateAddress);
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007217};
7218
7219
Mark Mendell0616ae02015-04-17 12:49:27 -04007220} // namespace art
7221
Aart Bikf8f5a162017-02-06 15:35:29 -08007222#include "nodes_vector.h"
7223
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007224#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
7225#include "nodes_shared.h"
7226#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007227#ifdef ART_ENABLE_CODEGEN_mips
7228#include "nodes_mips.h"
7229#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04007230#ifdef ART_ENABLE_CODEGEN_x86
7231#include "nodes_x86.h"
7232#endif
7233
7234namespace art {
7235
Igor Murashkin6ef45672017-08-08 13:59:55 -07007236class OptimizingCompilerStats;
7237
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007238class HGraphVisitor : public ValueObject {
7239 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007240 explicit HGraphVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7241 : stats_(stats),
7242 graph_(graph) {}
Dave Allison20dfc792014-06-16 20:44:29 -07007243 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007244
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007245 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007246 virtual void VisitBasicBlock(HBasicBlock* block);
7247
Roland Levillain633021e2014-10-01 14:12:25 +01007248 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007249 void VisitInsertionOrder();
7250
Roland Levillain633021e2014-10-01 14:12:25 +01007251 // Visit the graph following dominator tree reverse post-order.
7252 void VisitReversePostOrder();
7253
Nicolas Geoffray787c3072014-03-17 10:20:19 +00007254 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007255
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007256 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007257#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007258 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
7259
7260 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7261
7262#undef DECLARE_VISIT_INSTRUCTION
7263
Igor Murashkin6ef45672017-08-08 13:59:55 -07007264 protected:
7265 OptimizingCompilerStats* stats_;
7266
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007267 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07007268 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007269
7270 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
7271};
7272
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007273class HGraphDelegateVisitor : public HGraphVisitor {
7274 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007275 explicit HGraphDelegateVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7276 : HGraphVisitor(graph, stats) {}
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007277 virtual ~HGraphDelegateVisitor() {}
7278
7279 // Visit functions that delegate to to super class.
7280#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00007281 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007282
7283 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7284
7285#undef DECLARE_VISIT_INSTRUCTION
7286
7287 private:
7288 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
7289};
7290
Artem Serovcced8ba2017-07-19 18:18:09 +01007291// Create a clone of the instruction, insert it into the graph; replace the old one with a new
7292// and remove the old instruction.
7293HInstruction* ReplaceInstrOrPhiByClone(HInstruction* instr);
7294
7295// Create a clone for each clonable instructions/phis and replace the original with the clone.
7296//
7297// Used for testing individual instruction cloner.
7298class CloneAndReplaceInstructionVisitor : public HGraphDelegateVisitor {
7299 public:
7300 explicit CloneAndReplaceInstructionVisitor(HGraph* graph)
7301 : HGraphDelegateVisitor(graph), instr_replaced_by_clones_count(0) {}
7302
7303 void VisitInstruction(HInstruction* instruction) OVERRIDE {
7304 if (instruction->IsClonable()) {
7305 ReplaceInstrOrPhiByClone(instruction);
7306 instr_replaced_by_clones_count++;
7307 }
7308 }
7309
7310 size_t GetInstrReplacedByClonesCount() const { return instr_replaced_by_clones_count; }
7311
7312 private:
7313 size_t instr_replaced_by_clones_count;
7314
7315 DISALLOW_COPY_AND_ASSIGN(CloneAndReplaceInstructionVisitor);
7316};
7317
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007318// Iterator over the blocks that art part of the loop. Includes blocks part
7319// of an inner loop. The order in which the blocks are iterated is on their
7320// block id.
7321class HBlocksInLoopIterator : public ValueObject {
7322 public:
7323 explicit HBlocksInLoopIterator(const HLoopInformation& info)
7324 : blocks_in_loop_(info.GetBlocks()),
7325 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
7326 index_(0) {
7327 if (!blocks_in_loop_.IsBitSet(index_)) {
7328 Advance();
7329 }
7330 }
7331
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007332 bool Done() const { return index_ == blocks_.size(); }
7333 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007334 void Advance() {
7335 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007336 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007337 if (blocks_in_loop_.IsBitSet(index_)) {
7338 break;
7339 }
7340 }
7341 }
7342
7343 private:
7344 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007345 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007346 size_t index_;
7347
7348 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
7349};
7350
Mingyao Yang3584bce2015-05-19 16:01:59 -07007351// Iterator over the blocks that art part of the loop. Includes blocks part
7352// of an inner loop. The order in which the blocks are iterated is reverse
7353// post order.
7354class HBlocksInLoopReversePostOrderIterator : public ValueObject {
7355 public:
7356 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
7357 : blocks_in_loop_(info.GetBlocks()),
7358 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
7359 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007360 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007361 Advance();
7362 }
7363 }
7364
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007365 bool Done() const { return index_ == blocks_.size(); }
7366 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07007367 void Advance() {
7368 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007369 for (size_t e = blocks_.size(); index_ < e; ++index_) {
7370 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007371 break;
7372 }
7373 }
7374 }
7375
7376 private:
7377 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007378 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07007379 size_t index_;
7380
7381 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
7382};
7383
Aart Bikf3e61ee2017-04-12 17:09:20 -07007384// Returns int64_t value of a properly typed constant.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007385inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00007386 if (constant->IsIntConstant()) {
7387 return constant->AsIntConstant()->GetValue();
7388 } else if (constant->IsLongConstant()) {
7389 return constant->AsLongConstant()->GetValue();
7390 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00007391 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00007392 return 0;
7393 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007394}
7395
Aart Bikf3e61ee2017-04-12 17:09:20 -07007396// Returns true iff instruction is an integral constant (and sets value on success).
7397inline bool IsInt64AndGet(HInstruction* instruction, /*out*/ int64_t* value) {
7398 if (instruction->IsIntConstant()) {
7399 *value = instruction->AsIntConstant()->GetValue();
7400 return true;
7401 } else if (instruction->IsLongConstant()) {
7402 *value = instruction->AsLongConstant()->GetValue();
7403 return true;
7404 } else if (instruction->IsNullConstant()) {
7405 *value = 0;
7406 return true;
7407 }
7408 return false;
7409}
7410
Aart Bik0148de42017-09-05 09:25:01 -07007411// Returns true iff instruction is the given integral constant.
7412inline bool IsInt64Value(HInstruction* instruction, int64_t value) {
7413 int64_t val = 0;
7414 return IsInt64AndGet(instruction, &val) && val == value;
7415}
7416
7417// Returns true iff instruction is a zero bit pattern.
7418inline bool IsZeroBitPattern(HInstruction* instruction) {
7419 return instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern();
7420}
7421
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007422#define INSTRUCTION_TYPE_CHECK(type, super) \
7423 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
7424 inline const H##type* HInstruction::As##type() const { \
7425 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
7426 } \
7427 inline H##type* HInstruction::As##type() { \
7428 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
7429 }
7430
7431 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
7432#undef INSTRUCTION_TYPE_CHECK
7433
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00007434// Create space in `blocks` for adding `number_of_new_blocks` entries
7435// starting at location `at`. Blocks after `at` are moved accordingly.
7436inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
7437 size_t number_of_new_blocks,
7438 size_t after) {
7439 DCHECK_LT(after, blocks->size());
7440 size_t old_size = blocks->size();
7441 size_t new_size = old_size + number_of_new_blocks;
7442 blocks->resize(new_size);
7443 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
7444}
7445
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00007446/*
7447 * Hunt "under the hood" of array lengths (leading to array references),
7448 * null checks (also leading to array references), and new arrays
7449 * (leading to the actual length). This makes it more likely related
7450 * instructions become actually comparable.
7451 */
7452inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
7453 while (instruction->IsArrayLength() ||
7454 instruction->IsNullCheck() ||
7455 instruction->IsNewArray()) {
7456 instruction = instruction->IsNewArray()
7457 ? instruction->AsNewArray()->GetLength()
7458 : instruction->InputAt(0);
7459 }
7460 return instruction;
7461}
7462
Artem Serov21c7e6f2017-07-27 16:04:42 +01007463void RemoveEnvironmentUses(HInstruction* instruction);
7464bool HasEnvironmentUsedByOthers(HInstruction* instruction);
7465void ResetEnvironmentInputRecords(HInstruction* instruction);
7466
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007467} // namespace art
7468
7469#endif // ART_COMPILER_OPTIMIZING_NODES_H_