blob: 09d9c57a3306e5ccf6b16b1516cb37d99901bae2 [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"
David Sehrc431b9d2018-03-02 12:01:51 -080029#include "base/quasi_atomic.h"
Vladimir Marko60584552015-09-03 13:35:12 +000030#include "base/stl_util.h"
David Brazdild9c90372016-09-14 16:53:55 +010031#include "base/transform_array_ref.h"
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010032#include "data_type.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070033#include "deoptimization_kind.h"
David Sehr9e734c72018-01-04 17:56:19 -080034#include "dex/dex_file.h"
35#include "dex/dex_file_types.h"
David Sehr8c0961f2018-01-23 16:11:38 -080036#include "dex/invoke_type.h"
David Sehr312f3b22018-03-19 08:39:26 -070037#include "dex/method_reference.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000038#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000039#include "handle.h"
40#include "handle_scope.h"
Nicolas Geoffray762869d2016-07-15 15:28:35 +010041#include "intrinsics_enum.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010042#include "locations.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000043#include "mirror/class.h"
Orion Hodson18259d72018-04-12 11:18:23 +010044#include "mirror/method_type.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010045#include "offsets.h"
Vladimir Marko46817b82016-03-29 12:21:58 +010046#include "utils/intrusive_forward_list.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000047
48namespace art {
49
Vladimir Markoca6fff82017-10-03 14:49:14 +010050class ArenaStack;
David Brazdil1abb4192015-02-17 18:33:36 +000051class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000052class HBasicBlock;
Igor Murashkind01745e2017-04-05 16:40:31 -070053class HConstructorFence;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010054class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000055class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010056class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000057class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000058class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000059class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000060class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000061class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000062class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000063class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000064class HNullConstant;
Igor Murashkind01745e2017-04-05 16:40:31 -070065class HParameterValue;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010066class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010067class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010068class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010069class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000070class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010071class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000072class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000073
Mathieu Chartier736b5602015-09-02 14:54:11 -070074namespace mirror {
75class DexCache;
76} // namespace mirror
77
Nicolas Geoffray818f2102014-02-18 16:43:35 +000078static const int kDefaultNumberOfBlocks = 8;
79static const int kDefaultNumberOfSuccessors = 2;
80static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010081static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010082static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000083static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000084
Roland Levillain5b5b9312016-03-22 14:57:31 +000085// The maximum (meaningful) distance (31) that can be used in an integer shift/rotate operation.
86static constexpr int32_t kMaxIntShiftDistance = 0x1f;
87// The maximum (meaningful) distance (63) that can be used in a long shift/rotate operation.
88static constexpr int32_t kMaxLongShiftDistance = 0x3f;
Calin Juravle9aec02f2014-11-18 23:06:35 +000089
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010090static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070091static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010092
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010093static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
94
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060095static constexpr uint32_t kNoDexPc = -1;
96
Vladimir Markodbb7f5b2016-03-30 13:23:58 +010097inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
98 // For the purposes of the compiler, the dex files must actually be the same object
99 // if we want to safely treat them as the same. This is especially important for JIT
100 // as custom class loaders can open the same underlying file (or memory) multiple
101 // times and provide different class resolution but no two class loaders should ever
102 // use the same DexFile object - doing so is an unsupported hack that can lead to
103 // all sorts of weird failures.
104 return &lhs == &rhs;
105}
106
Dave Allison20dfc792014-06-16 20:44:29 -0700107enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -0700108 // All types.
109 kCondEQ, // ==
110 kCondNE, // !=
111 // Signed integers and floating-point numbers.
112 kCondLT, // <
113 kCondLE, // <=
114 kCondGT, // >
115 kCondGE, // >=
116 // Unsigned integers.
117 kCondB, // <
118 kCondBE, // <=
119 kCondA, // >
120 kCondAE, // >=
Scott Wakeling2c76e062016-08-31 09:48:54 +0100121 // First and last aliases.
122 kCondFirst = kCondEQ,
123 kCondLast = kCondAE,
Dave Allison20dfc792014-06-16 20:44:29 -0700124};
125
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000126enum GraphAnalysisResult {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000127 kAnalysisSkipped,
David Brazdilbadd8262016-02-02 16:28:56 +0000128 kAnalysisInvalidBytecode,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000129 kAnalysisFailThrowCatchLoop,
130 kAnalysisFailAmbiguousArrayOp,
131 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000132};
133
Andreas Gampe9186ced2016-12-12 14:28:21 -0800134template <typename T>
135static inline typename std::make_unsigned<T>::type MakeUnsigned(T x) {
136 return static_cast<typename std::make_unsigned<T>::type>(x);
137}
138
Vladimir Markof9f64412015-09-02 14:05:49 +0100139class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100140 public:
141 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
142
143 void AddInstruction(HInstruction* instruction);
144 void RemoveInstruction(HInstruction* instruction);
145
David Brazdilc3d743f2015-04-22 13:40:50 +0100146 // Insert `instruction` before/after an existing instruction `cursor`.
147 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
148 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
149
Roland Levillain6b469232014-09-25 10:10:38 +0100150 // Return true if this list contains `instruction`.
151 bool Contains(HInstruction* instruction) const;
152
Roland Levillainccc07a92014-09-16 14:48:16 +0100153 // Return true if `instruction1` is found before `instruction2` in
154 // this instruction list and false otherwise. Abort if none
155 // of these instructions is found.
156 bool FoundBefore(const HInstruction* instruction1,
157 const HInstruction* instruction2) const;
158
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000159 bool IsEmpty() const { return first_instruction_ == nullptr; }
160 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
161
162 // Update the block of all instructions to be `block`.
163 void SetBlockOfInstructions(HBasicBlock* block) const;
164
165 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000166 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000167 void Add(const HInstructionList& instruction_list);
168
David Brazdil2d7352b2015-04-20 14:52:42 +0100169 // Return the number of instructions in the list. This is an expensive operation.
170 size_t CountSize() const;
171
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100172 private:
173 HInstruction* first_instruction_;
174 HInstruction* last_instruction_;
175
176 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000177 friend class HGraph;
178 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100179 friend class HInstructionIterator;
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +0000180 friend class HInstructionIteratorHandleChanges;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100181 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100182
183 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
184};
185
David Brazdil4833f5a2015-12-16 10:37:39 +0000186class ReferenceTypeInfo : ValueObject {
187 public:
188 typedef Handle<mirror::Class> TypeHandle;
189
Vladimir Markoa1de9182016-02-25 11:37:38 +0000190 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
191
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700192 static ReferenceTypeInfo Create(TypeHandle type_handle) REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil94ab38f2016-06-21 17:48:19 +0100193 return Create(type_handle, type_handle->CannotBeAssignedFromOtherTypes());
194 }
195
Vladimir Markoa1de9182016-02-25 11:37:38 +0000196 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000197 return ReferenceTypeInfo(type_handle, is_exact);
198 }
199
200 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
201
Vladimir Markof39745e2016-01-26 12:16:55 +0000202 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000203 return handle.GetReference() != nullptr;
204 }
205
Vladimir Marko456307a2016-04-19 14:12:13 +0000206 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000207 return IsValidHandle(type_handle_);
208 }
209
210 bool IsExact() const { return is_exact_; }
211
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700212 bool IsObjectClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000213 DCHECK(IsValid());
214 return GetTypeHandle()->IsObjectClass();
215 }
216
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700217 bool IsStringClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000218 DCHECK(IsValid());
219 return GetTypeHandle()->IsStringClass();
220 }
221
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700222 bool IsObjectArray() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000223 DCHECK(IsValid());
224 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
225 }
226
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700227 bool IsInterface() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000228 DCHECK(IsValid());
229 return GetTypeHandle()->IsInterface();
230 }
231
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700232 bool IsArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000233 DCHECK(IsValid());
234 return GetTypeHandle()->IsArrayClass();
235 }
236
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700237 bool IsPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000238 DCHECK(IsValid());
239 return GetTypeHandle()->IsPrimitiveArray();
240 }
241
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700242 bool IsNonPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000243 DCHECK(IsValid());
244 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
245 }
246
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700247 bool CanArrayHold(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000248 DCHECK(IsValid());
249 if (!IsExact()) return false;
250 if (!IsArrayClass()) return false;
251 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
252 }
253
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700254 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000255 DCHECK(IsValid());
256 if (!IsExact()) return false;
257 if (!IsArrayClass()) return false;
258 if (!rti.IsArrayClass()) return false;
259 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
260 rti.GetTypeHandle()->GetComponentType());
261 }
262
263 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
264
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700265 bool IsSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000266 DCHECK(IsValid());
267 DCHECK(rti.IsValid());
268 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
269 }
270
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700271 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000272 DCHECK(IsValid());
273 DCHECK(rti.IsValid());
274 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
275 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
276 }
277
278 // Returns true if the type information provide the same amount of details.
279 // Note that it does not mean that the instructions have the same actual type
280 // (because the type can be the result of a merge).
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700281 bool IsEqual(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000282 if (!IsValid() && !rti.IsValid()) {
283 // Invalid types are equal.
284 return true;
285 }
286 if (!IsValid() || !rti.IsValid()) {
287 // One is valid, the other not.
288 return false;
289 }
290 return IsExact() == rti.IsExact()
291 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
292 }
293
294 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000295 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
296 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
297 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000298
299 // The class of the object.
300 TypeHandle type_handle_;
301 // Whether or not the type is exact or a superclass of the actual type.
302 // Whether or not we have any information about this type.
303 bool is_exact_;
304};
305
306std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
307
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000308// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100309class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000310 public:
Vladimir Markoca6fff82017-10-03 14:49:14 +0100311 HGraph(ArenaAllocator* allocator,
312 ArenaStack* arena_stack,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100313 const DexFile& dex_file,
314 uint32_t method_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700315 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100316 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100317 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000318 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100319 int start_instruction_id = 0)
Vladimir Markoca6fff82017-10-03 14:49:14 +0100320 : allocator_(allocator),
321 arena_stack_(arena_stack),
322 blocks_(allocator->Adapter(kArenaAllocBlockList)),
323 reverse_post_order_(allocator->Adapter(kArenaAllocReversePostOrder)),
324 linear_order_(allocator->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700325 entry_block_(nullptr),
326 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100327 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100328 number_of_vregs_(0),
329 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000330 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400331 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000332 has_try_catch_(false),
Aart Bikb13c65b2017-03-21 20:14:07 -0700333 has_simd_(false),
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800334 has_loops_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000335 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000336 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000337 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100338 dex_file_(dex_file),
339 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100340 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100341 in_ssa_form_(false),
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800342 number_of_cha_guards_(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700343 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000344 cached_null_constant_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100345 cached_int_constants_(std::less<int32_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
346 cached_float_constants_(std::less<int32_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
347 cached_long_constants_(std::less<int64_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
348 cached_double_constants_(std::less<int64_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000349 cached_current_method_(nullptr),
Nicolas Geoffray53fec082017-03-27 12:56:16 +0100350 art_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000351 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
Mingyao Yang063fc772016-08-02 11:02:54 -0700352 osr_(osr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100353 cha_single_implementation_list_(allocator->Adapter(kArenaAllocCHA)) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100354 blocks_.reserve(kDefaultNumberOfBlocks);
355 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000356
David Brazdilbadd8262016-02-02 16:28:56 +0000357 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700358 void InitializeInexactObjectRTI(VariableSizedHandleScope* handles);
David Brazdilbadd8262016-02-02 16:28:56 +0000359
Vladimir Markoca6fff82017-10-03 14:49:14 +0100360 ArenaAllocator* GetAllocator() const { return allocator_; }
361 ArenaStack* GetArenaStack() const { return arena_stack_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100362 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
363
David Brazdil69ba7b72015-06-23 18:27:30 +0100364 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000365 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100366
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000367 HBasicBlock* GetEntryBlock() const { return entry_block_; }
368 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100369 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000370
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000371 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
372 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000373
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000374 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100375
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100376 void ComputeDominanceInformation();
377 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000378 void ClearLoopInformation();
379 void FindBackEdges(ArenaBitVector* visited);
380 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100381 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100382 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000383
David Brazdil4833f5a2015-12-16 10:37:39 +0000384 // Analyze all natural loops in this graph. Returns a code specifying that it
385 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000386 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000387 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100388
David Brazdilffee3d32015-07-06 11:48:53 +0100389 // Iterate over blocks to compute try block membership. Needs reverse post
390 // order and loop information.
391 void ComputeTryBlockInformation();
392
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000393 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000394 // Returns the instruction to replace the invoke expression or null if the
395 // invoke is for a void method. Note that the caller is responsible for replacing
396 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000397 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000398
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000399 // Update the loop and try membership of `block`, which was spawned from `reference`.
400 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
401 // should be the new back edge.
402 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
403 HBasicBlock* reference,
404 bool replace_if_back_edge);
405
Mingyao Yang3584bce2015-05-19 16:01:59 -0700406 // Need to add a couple of blocks to test if the loop body is entered and
407 // put deoptimization instructions, etc.
408 void TransformLoopHeaderForBCE(HBasicBlock* header);
409
Aart Bikf8f5a162017-02-06 15:35:29 -0800410 // Adds a new loop directly after the loop with the given header and exit.
411 // Returns the new preheader.
412 HBasicBlock* TransformLoopForVectorization(HBasicBlock* header,
413 HBasicBlock* body,
414 HBasicBlock* exit);
415
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000416 // Removes `block` from the graph. Assumes `block` has been disconnected from
417 // other blocks and has no instructions or phis.
418 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000419
David Brazdilfc6a86a2015-06-26 10:33:45 +0000420 // Splits the edge between `block` and `successor` while preserving the
421 // indices in the predecessor/successor lists. If there are multiple edges
422 // between the blocks, the lowest indices are used.
423 // Returns the new block which is empty and has the same dex pc as `successor`.
424 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
425
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100426 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
Artem Serovc73ee372017-07-31 15:08:40 +0100427 void OrderLoopHeaderPredecessors(HBasicBlock* header);
Artem Serov09faaea2017-12-07 14:36:01 +0000428
429 // Transform a loop into a format with a single preheader.
430 //
431 // Each phi in the header should be split: original one in the header should only hold
432 // inputs reachable from the back edges and a single input from the preheader. The newly created
433 // phi in the preheader should collate the inputs from the original multiple incoming blocks.
434 //
435 // Loops in the graph typically have a single preheader, so this method is used to "repair" loops
436 // that no longer have this property.
437 void TransformLoopToSinglePreheaderFormat(HBasicBlock* header);
438
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100439 void SimplifyLoop(HBasicBlock* header);
440
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000441 int32_t GetNextInstructionId() {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100442 CHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000443 return current_instruction_id_++;
444 }
445
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000446 int32_t GetCurrentInstructionId() const {
447 return current_instruction_id_;
448 }
449
450 void SetCurrentInstructionId(int32_t id) {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100451 CHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000452 current_instruction_id_ = id;
453 }
454
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100455 uint16_t GetMaximumNumberOfOutVRegs() const {
456 return maximum_number_of_out_vregs_;
457 }
458
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000459 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
460 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100461 }
462
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100463 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
464 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
465 }
466
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000467 void UpdateTemporariesVRegSlots(size_t slots) {
468 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100469 }
470
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000471 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100472 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000473 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100474 }
475
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100476 void SetNumberOfVRegs(uint16_t number_of_vregs) {
477 number_of_vregs_ = number_of_vregs;
478 }
479
480 uint16_t GetNumberOfVRegs() const {
481 return number_of_vregs_;
482 }
483
484 void SetNumberOfInVRegs(uint16_t value) {
485 number_of_in_vregs_ = value;
486 }
487
David Brazdildee58d62016-04-07 09:54:26 +0000488 uint16_t GetNumberOfInVRegs() const {
489 return number_of_in_vregs_;
490 }
491
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100492 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100493 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100494 return number_of_vregs_ - number_of_in_vregs_;
495 }
496
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100497 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100498 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100499 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100500
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100501 ArrayRef<HBasicBlock* const> GetReversePostOrderSkipEntryBlock() {
502 DCHECK(GetReversePostOrder()[0] == entry_block_);
503 return ArrayRef<HBasicBlock* const>(GetReversePostOrder()).SubArray(1);
504 }
505
506 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetPostOrder() const {
507 return ReverseRange(GetReversePostOrder());
508 }
509
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100510 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100511 return linear_order_;
512 }
513
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100514 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetLinearPostOrder() const {
515 return ReverseRange(GetLinearOrder());
516 }
517
Mark Mendell1152c922015-04-24 17:06:35 -0400518 bool HasBoundsChecks() const {
519 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800520 }
521
Mark Mendell1152c922015-04-24 17:06:35 -0400522 void SetHasBoundsChecks(bool value) {
523 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800524 }
525
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000526 bool IsDebuggable() const { return debuggable_; }
527
David Brazdil8d5b8b22015-03-24 10:51:52 +0000528 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000529 // already, it is created and inserted into the graph. This method is only for
530 // integral types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100531 HConstant* GetConstant(DataType::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000532
533 // TODO: This is problematic for the consistency of reference type propagation
534 // because it can be created anytime after the pass and thus it will be left
535 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600536 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000537
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600538 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
539 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000540 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600541 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
542 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000543 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600544 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
545 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000546 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600547 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
548 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000549 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000550
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100551 HCurrentMethod* GetCurrentMethod();
552
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100553 const DexFile& GetDexFile() const {
554 return dex_file_;
555 }
556
557 uint32_t GetMethodIdx() const {
558 return method_idx_;
559 }
560
Igor Murashkind01745e2017-04-05 16:40:31 -0700561 // Get the method name (without the signature), e.g. "<init>"
562 const char* GetMethodName() const;
563
564 // Get the pretty method name (class + name + optionally signature).
565 std::string PrettyMethod(bool with_signature = true) const;
566
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100567 InvokeType GetInvokeType() const {
568 return invoke_type_;
569 }
570
Mark Mendellc4701932015-04-10 13:18:51 -0400571 InstructionSet GetInstructionSet() const {
572 return instruction_set_;
573 }
574
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000575 bool IsCompilingOsr() const { return osr_; }
576
Mingyao Yang063fc772016-08-02 11:02:54 -0700577 ArenaSet<ArtMethod*>& GetCHASingleImplementationList() {
578 return cha_single_implementation_list_;
579 }
580
581 void AddCHASingleImplementationDependency(ArtMethod* method) {
582 cha_single_implementation_list_.insert(method);
583 }
584
585 bool HasShouldDeoptimizeFlag() const {
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800586 return number_of_cha_guards_ != 0;
Mingyao Yang063fc772016-08-02 11:02:54 -0700587 }
588
David Brazdil77a48ae2015-09-15 12:34:04 +0000589 bool HasTryCatch() const { return has_try_catch_; }
590 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100591
Aart Bikb13c65b2017-03-21 20:14:07 -0700592 bool HasSIMD() const { return has_simd_; }
593 void SetHasSIMD(bool value) { has_simd_ = value; }
594
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800595 bool HasLoops() const { return has_loops_; }
596 void SetHasLoops(bool value) { has_loops_ = value; }
597
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000598 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
599 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
600
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100601 ArtMethod* GetArtMethod() const { return art_method_; }
602 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
603
Roland Levillain5e8d5f02016-10-18 18:03:43 +0100604 // Returns an instruction with the opposite Boolean value from 'cond'.
Mark Mendellf6529172015-11-17 11:16:56 -0500605 // The instruction has been inserted into the graph, either as a constant, or
606 // before cursor.
607 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
608
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000609 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
610
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800611 uint32_t GetNumberOfCHAGuards() { return number_of_cha_guards_; }
612 void SetNumberOfCHAGuards(uint32_t num) { number_of_cha_guards_ = num; }
613 void IncrementNumberOfCHAGuards() { number_of_cha_guards_++; }
614
David Brazdil2d7352b2015-04-20 14:52:42 +0100615 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000616 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100617 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000618
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000619 template <class InstructionType, typename ValueType>
620 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600621 ArenaSafeMap<ValueType, InstructionType*>* cache,
622 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000623 // Try to find an existing constant of the given value.
624 InstructionType* constant = nullptr;
625 auto cached_constant = cache->find(value);
626 if (cached_constant != cache->end()) {
627 constant = cached_constant->second;
628 }
629
630 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100631 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000632 if (constant == nullptr || constant->GetBlock() == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100633 constant = new (allocator_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000634 cache->Overwrite(value, constant);
635 InsertConstant(constant);
636 }
637 return constant;
638 }
639
David Brazdil8d5b8b22015-03-24 10:51:52 +0000640 void InsertConstant(HConstant* instruction);
641
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000642 // Cache a float constant into the graph. This method should only be
643 // called by the SsaBuilder when creating "equivalent" instructions.
644 void CacheFloatConstant(HFloatConstant* constant);
645
646 // See CacheFloatConstant comment.
647 void CacheDoubleConstant(HDoubleConstant* constant);
648
Vladimir Markoca6fff82017-10-03 14:49:14 +0100649 ArenaAllocator* const allocator_;
650 ArenaStack* const arena_stack_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000651
652 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100653 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000654
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100655 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100656 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000657
Aart Bik281c6812016-08-26 11:31:48 -0700658 // List of blocks to perform a linear order tree traversal. Unlike the reverse
659 // post order, this order is not incrementally kept up-to-date.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100660 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100661
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000662 HBasicBlock* entry_block_;
663 HBasicBlock* exit_block_;
664
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100665 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100666 uint16_t maximum_number_of_out_vregs_;
667
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100668 // The number of virtual registers in this method. Contains the parameters.
669 uint16_t number_of_vregs_;
670
671 // The number of virtual registers used by parameters of this method.
672 uint16_t number_of_in_vregs_;
673
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000674 // Number of vreg size slots that the temporaries use (used in baseline compiler).
675 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100676
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800677 // Flag whether there are bounds checks in the graph. We can skip
678 // BCE if it's false. It's only best effort to keep it up to date in
679 // the presence of code elimination so there might be false positives.
Mark Mendell1152c922015-04-24 17:06:35 -0400680 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800681
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800682 // Flag whether there are try/catch blocks in the graph. We will skip
683 // try/catch-related passes if it's false. It's only best effort to keep
684 // it up to date in the presence of code elimination so there might be
685 // false positives.
David Brazdil77a48ae2015-09-15 12:34:04 +0000686 bool has_try_catch_;
687
Aart Bikb13c65b2017-03-21 20:14:07 -0700688 // Flag whether SIMD instructions appear in the graph. If true, the
689 // code generators may have to be more careful spilling the wider
690 // contents of SIMD registers.
691 bool has_simd_;
692
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800693 // Flag whether there are any loops in the graph. We can skip loop
694 // optimization if it's false. It's only best effort to keep it up
695 // to date in the presence of code elimination so there might be false
696 // positives.
697 bool has_loops_;
698
699 // Flag whether there are any irreducible loops in the graph. It's only
700 // best effort to keep it up to date in the presence of code elimination
701 // so there might be false positives.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000702 bool has_irreducible_loops_;
703
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000704 // Indicates whether the graph should be compiled in a way that
705 // ensures full debuggability. If false, we can apply more
706 // aggressive optimizations that may limit the level of debugging.
707 const bool debuggable_;
708
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000709 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000710 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000711
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100712 // The dex file from which the method is from.
713 const DexFile& dex_file_;
714
715 // The method index in the dex file.
716 const uint32_t method_idx_;
717
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100718 // If inlined, this encodes how the callee is being invoked.
719 const InvokeType invoke_type_;
720
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100721 // Whether the graph has been transformed to SSA form. Only used
722 // in debug mode to ensure we are not using properties only valid
723 // for non-SSA form (like the number of temporaries).
724 bool in_ssa_form_;
725
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800726 // Number of CHA guards in the graph. Used to short-circuit the
727 // CHA guard optimization pass when there is no CHA guard left.
728 uint32_t number_of_cha_guards_;
729
Mathieu Chartiere401d142015-04-22 13:56:20 -0700730 const InstructionSet instruction_set_;
731
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000732 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000733 HNullConstant* cached_null_constant_;
734 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000735 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000736 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000737 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000738
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100739 HCurrentMethod* cached_current_method_;
740
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100741 // The ArtMethod this graph is for. Note that for AOT, it may be null,
742 // for example for methods whose declaring class could not be resolved
743 // (such as when the superclass could not be found).
744 ArtMethod* art_method_;
745
David Brazdil4833f5a2015-12-16 10:37:39 +0000746 // Keep the RTI of inexact Object to avoid having to pass stack handle
747 // collection pointer to passes which may create NullConstant.
748 ReferenceTypeInfo inexact_object_rti_;
749
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000750 // Whether we are compiling this graph for on stack replacement: this will
751 // make all loops seen as irreducible and emit special stack maps to mark
752 // compiled code entries which the interpreter can directly jump to.
753 const bool osr_;
754
Mingyao Yang063fc772016-08-02 11:02:54 -0700755 // List of methods that are assumed to have single implementation.
756 ArenaSet<ArtMethod*> cha_single_implementation_list_;
757
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000758 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100759 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000760 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000761 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000762 DISALLOW_COPY_AND_ASSIGN(HGraph);
763};
764
Vladimir Markof9f64412015-09-02 14:05:49 +0100765class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000766 public:
767 HLoopInformation(HBasicBlock* header, HGraph* graph)
768 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100769 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000770 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100771 contains_irreducible_loop_(false),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100772 back_edges_(graph->GetAllocator()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100773 // Make bit vector growable, as the number of blocks may change.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100774 blocks_(graph->GetAllocator(),
775 graph->GetBlocks().size(),
776 true,
777 kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100778 back_edges_.reserve(kDefaultNumberOfBackEdges);
779 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100780
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000781 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100782 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000783
784 void Dump(std::ostream& os);
785
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100786 HBasicBlock* GetHeader() const {
787 return header_;
788 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000789
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000790 void SetHeader(HBasicBlock* block) {
791 header_ = block;
792 }
793
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100794 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
795 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
796 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
797
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000798 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100799 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000800 }
801
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100802 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100803 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100804 }
805
David Brazdil46e2a392015-03-16 17:31:52 +0000806 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100807 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100808 }
809
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000810 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100811 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000812 }
813
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100814 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100815
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100816 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100817 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100818 }
819
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100820 // Returns the lifetime position of the back edge that has the
821 // greatest lifetime position.
822 size_t GetLifetimeEnd() const;
823
824 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100825 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100826 }
827
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000828 // Finds blocks that are part of this loop.
829 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100830
Artem Serov7f4aff62017-06-21 17:02:18 +0100831 // Updates blocks population of the loop and all of its outer' ones recursively after the
832 // population of the inner loop is updated.
833 void PopulateInnerLoopUpwards(HLoopInformation* inner_loop);
834
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100835 // Returns whether this loop information contains `block`.
836 // Note that this loop information *must* be populated before entering this function.
837 bool Contains(const HBasicBlock& block) const;
838
839 // Returns whether this loop information is an inner loop of `other`.
840 // Note that `other` *must* be populated before entering this function.
841 bool IsIn(const HLoopInformation& other) const;
842
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800843 // Returns true if instruction is not defined within this loop.
844 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700845
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100846 const ArenaBitVector& GetBlocks() const { return blocks_; }
847
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000848 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000849 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000850
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000851 void ClearAllBlocks() {
852 blocks_.ClearAllBits();
853 }
854
David Brazdil3f4a5222016-05-06 12:46:21 +0100855 bool HasBackEdgeNotDominatedByHeader() const;
856
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100857 bool IsPopulated() const {
858 return blocks_.GetHighestBitSet() != -1;
859 }
860
Anton Shaminf89381f2016-05-16 16:44:13 +0600861 bool DominatesAllBackEdges(HBasicBlock* block);
862
David Sehrc757dec2016-11-04 15:48:34 -0700863 bool HasExitEdge() const;
864
Artem Serov7f4aff62017-06-21 17:02:18 +0100865 // Resets back edge and blocks-in-loop data.
866 void ResetBasicBlockData() {
867 back_edges_.clear();
868 ClearAllBlocks();
869 }
870
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000871 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100872 // Internal recursive implementation of `Populate`.
873 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100874 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100875
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000876 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100877 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000878 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100879 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100880 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100881 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000882
883 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
884};
885
David Brazdilec16f792015-08-19 15:04:01 +0100886// Stores try/catch information for basic blocks.
887// Note that HGraph is constructed so that catch blocks cannot simultaneously
888// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100889class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100890 public:
891 // Try block information constructor.
892 explicit TryCatchInformation(const HTryBoundary& try_entry)
893 : try_entry_(&try_entry),
894 catch_dex_file_(nullptr),
895 catch_type_index_(DexFile::kDexNoIndex16) {
896 DCHECK(try_entry_ != nullptr);
897 }
898
899 // Catch block information constructor.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800900 TryCatchInformation(dex::TypeIndex catch_type_index, const DexFile& dex_file)
David Brazdilec16f792015-08-19 15:04:01 +0100901 : try_entry_(nullptr),
902 catch_dex_file_(&dex_file),
903 catch_type_index_(catch_type_index) {}
904
905 bool IsTryBlock() const { return try_entry_ != nullptr; }
906
907 const HTryBoundary& GetTryEntry() const {
908 DCHECK(IsTryBlock());
909 return *try_entry_;
910 }
911
912 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
913
914 bool IsCatchAllTypeIndex() const {
915 DCHECK(IsCatchBlock());
Andreas Gampea5b09a62016-11-17 15:21:22 -0800916 return !catch_type_index_.IsValid();
David Brazdilec16f792015-08-19 15:04:01 +0100917 }
918
Andreas Gampea5b09a62016-11-17 15:21:22 -0800919 dex::TypeIndex GetCatchTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +0100920 DCHECK(IsCatchBlock());
921 return catch_type_index_;
922 }
923
924 const DexFile& GetCatchDexFile() const {
925 DCHECK(IsCatchBlock());
926 return *catch_dex_file_;
927 }
928
929 private:
930 // One of possibly several TryBoundary instructions entering the block's try.
931 // Only set for try blocks.
932 const HTryBoundary* try_entry_;
933
934 // Exception type information. Only set for catch blocks.
935 const DexFile* catch_dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800936 const dex::TypeIndex catch_type_index_;
David Brazdilec16f792015-08-19 15:04:01 +0100937};
938
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100939static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100940static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100941
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000942// A block in a method. Contains the list of instructions represented
943// as a double linked list. Each block knows its predecessors and
944// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100945
Vladimir Markof9f64412015-09-02 14:05:49 +0100946class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000947 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700948 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000949 : graph_(graph),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100950 predecessors_(graph->GetAllocator()->Adapter(kArenaAllocPredecessors)),
951 successors_(graph->GetAllocator()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000952 loop_information_(nullptr),
953 dominator_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100954 dominated_blocks_(graph->GetAllocator()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100955 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100956 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100957 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000958 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000959 try_catch_information_(nullptr) {
960 predecessors_.reserve(kDefaultNumberOfPredecessors);
961 successors_.reserve(kDefaultNumberOfSuccessors);
962 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
963 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000964
Vladimir Marko60584552015-09-03 13:35:12 +0000965 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100966 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000967 }
968
Vladimir Marko60584552015-09-03 13:35:12 +0000969 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100970 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000971 }
972
David Brazdild26a4112015-11-10 11:07:31 +0000973 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
974 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
975
Vladimir Marko60584552015-09-03 13:35:12 +0000976 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
977 return ContainsElement(successors_, block, start_from);
978 }
979
980 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100981 return dominated_blocks_;
982 }
983
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100984 bool IsEntryBlock() const {
985 return graph_->GetEntryBlock() == this;
986 }
987
988 bool IsExitBlock() const {
989 return graph_->GetExitBlock() == this;
990 }
991
David Brazdil46e2a392015-03-16 17:31:52 +0000992 bool IsSingleGoto() const;
Mads Ager16e52892017-07-14 13:11:37 +0200993 bool IsSingleReturn() const;
Mingyao Yang46721ef2017-10-05 14:45:17 -0700994 bool IsSingleReturnOrReturnVoidAllowingPhis() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000995 bool IsSingleTryBoundary() const;
996
997 // Returns true if this block emits nothing but a jump.
998 bool IsSingleJump() const {
999 HLoopInformation* loop_info = GetLoopInformation();
1000 return (IsSingleGoto() || IsSingleTryBoundary())
1001 // Back edges generate a suspend check.
1002 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
1003 }
David Brazdil46e2a392015-03-16 17:31:52 +00001004
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001005 void AddBackEdge(HBasicBlock* back_edge) {
1006 if (loop_information_ == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001007 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001008 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001009 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001010 loop_information_->AddBackEdge(back_edge);
1011 }
1012
Artem Serov7f4aff62017-06-21 17:02:18 +01001013 // Registers a back edge; if the block was not a loop header before the call associates a newly
1014 // created loop info with it.
1015 //
1016 // Used in SuperblockCloner to preserve LoopInformation object instead of reseting loop
1017 // info for all blocks during back edges recalculation.
1018 void AddBackEdgeWhileUpdating(HBasicBlock* back_edge) {
1019 if (loop_information_ == nullptr || loop_information_->GetHeader() != this) {
1020 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
1021 }
1022 loop_information_->AddBackEdge(back_edge);
1023 }
1024
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001025 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001026 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001027
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001028 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001029 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001030 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001031
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001032 HBasicBlock* GetDominator() const { return dominator_; }
1033 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +00001034 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
1035
1036 void RemoveDominatedBlock(HBasicBlock* block) {
1037 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +01001038 }
Vladimir Marko60584552015-09-03 13:35:12 +00001039
1040 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
1041 ReplaceElement(dominated_blocks_, existing, new_block);
1042 }
1043
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001044 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001045
1046 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001047 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001048 }
1049
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001050 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
1051 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +01001052 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001053 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +01001054 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
1055 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001056
Nicolas Geoffray09aa1472016-01-19 10:52:54 +00001057 HInstruction* GetFirstInstructionDisregardMoves() const;
1058
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001059 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001060 successors_.push_back(block);
1061 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001062 }
1063
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001064 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
1065 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001066 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001067 new_block->predecessors_.push_back(this);
1068 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001069 }
1070
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001071 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
1072 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001073 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001074 new_block->successors_.push_back(this);
1075 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001076 }
1077
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001078 // Insert `this` between `predecessor` and `successor. This method
1079 // preserves the indicies, and will update the first edge found between
1080 // `predecessor` and `successor`.
1081 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
1082 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001083 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +00001084 successor->predecessors_[predecessor_index] = this;
1085 predecessor->successors_[successor_index] = this;
1086 successors_.push_back(successor);
1087 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001088 }
1089
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001090 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001091 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001092 }
1093
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001094 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001095 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001096 }
1097
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001098 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001099 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001100 }
1101
1102 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001103 predecessors_.push_back(block);
1104 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001105 }
1106
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001107 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001108 DCHECK_EQ(predecessors_.size(), 2u);
1109 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001110 }
1111
David Brazdil769c9e52015-04-27 13:54:09 +01001112 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001113 DCHECK_EQ(successors_.size(), 2u);
1114 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +01001115 }
1116
David Brazdilfc6a86a2015-06-26 10:33:45 +00001117 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001118 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001119 }
1120
David Brazdilfc6a86a2015-06-26 10:33:45 +00001121 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001122 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001123 }
1124
David Brazdilfc6a86a2015-06-26 10:33:45 +00001125 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001126 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001127 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001128 }
1129
1130 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001131 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001132 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001133 }
1134
1135 // Returns whether the first occurrence of `predecessor` in the list of
1136 // predecessors is at index `idx`.
1137 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001138 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001139 return GetPredecessorIndexOf(predecessor) == idx;
1140 }
1141
David Brazdild7558da2015-09-22 13:04:14 +01001142 // Create a new block between this block and its predecessors. The new block
1143 // is added to the graph, all predecessor edges are relinked to it and an edge
1144 // is created to `this`. Returns the new empty block. Reverse post order or
1145 // loop and try/catch information are not updated.
1146 HBasicBlock* CreateImmediateDominator();
1147
David Brazdilfc6a86a2015-06-26 10:33:45 +00001148 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001149 // created, latter block. Note that this method will add the block to the
1150 // graph, create a Goto at the end of the former block and will create an edge
1151 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001152 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001153 HBasicBlock* SplitBefore(HInstruction* cursor);
1154
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001155 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001156 // created block. Note that this method just updates raw block information,
1157 // like predecessors, successors, dominators, and instruction list. It does not
1158 // update the graph, reverse post order, loop information, nor make sure the
1159 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001160 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1161
1162 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1163 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001164
1165 // Merge `other` at the end of `this`. Successors and dominated blocks of
1166 // `other` are changed to be successors and dominated blocks of `this`. Note
1167 // that this method does not update the graph, reverse post order, loop
1168 // information, nor make sure the blocks are consistent (for example ending
1169 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001170 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001171
1172 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1173 // of `this` are moved to `other`.
1174 // Note that this method does not update the graph, reverse post order, loop
1175 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001176 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001177 void ReplaceWith(HBasicBlock* other);
1178
Aart Bik6b69e0a2017-01-11 10:20:43 -08001179 // Merges the instructions of `other` at the end of `this`.
1180 void MergeInstructionsWith(HBasicBlock* other);
1181
David Brazdil2d7352b2015-04-20 14:52:42 +01001182 // Merge `other` at the end of `this`. This method updates loops, reverse post
1183 // order, links to predecessors, successors, dominators and deletes the block
1184 // from the graph. The two blocks must be successive, i.e. `this` the only
1185 // predecessor of `other` and vice versa.
1186 void MergeWith(HBasicBlock* other);
1187
1188 // Disconnects `this` from all its predecessors, successors and dominator,
1189 // removes it from all loops it is included in and eventually from the graph.
1190 // The block must not dominate any other block. Predecessors and successors
1191 // are safely updated.
1192 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001193
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001194 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001195 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001196 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001197 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Artem Serovcced8ba2017-07-19 18:18:09 +01001198 // Replace phi `initial` with `replacement` within this block.
1199 void ReplaceAndRemovePhiWith(HPhi* initial, HPhi* replacement);
Roland Levillainccc07a92014-09-16 14:48:16 +01001200 // Replace instruction `initial` with `replacement` within this block.
1201 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1202 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001203 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001204 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001205 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1206 // instruction list. With 'ensure_safety' set to true, it verifies that the
1207 // instruction is not in use and removes it from the use lists of its inputs.
1208 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1209 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001210 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001211
1212 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001213 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001214 }
1215
Roland Levillain6b879dd2014-09-22 17:13:44 +01001216 bool IsLoopPreHeaderFirstPredecessor() const {
1217 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001218 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001219 }
1220
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001221 bool IsFirstPredecessorBackEdge() const {
1222 DCHECK(IsLoopHeader());
1223 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1224 }
1225
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001226 HLoopInformation* GetLoopInformation() const {
1227 return loop_information_;
1228 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001229
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001230 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001231 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001232 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001233 void SetInLoop(HLoopInformation* info) {
1234 if (IsLoopHeader()) {
1235 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001236 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001237 loop_information_ = info;
1238 } else if (loop_information_->Contains(*info->GetHeader())) {
1239 // Block is currently part of an outer loop. Make it part of this inner loop.
1240 // Note that a non loop header having a loop information means this loop information
1241 // has already been populated
1242 loop_information_ = info;
1243 } else {
1244 // Block is part of an inner loop. Do not update the loop information.
1245 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1246 // at this point, because this method is being called while populating `info`.
1247 }
1248 }
1249
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001250 // Raw update of the loop information.
1251 void SetLoopInformation(HLoopInformation* info) {
1252 loop_information_ = info;
1253 }
1254
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001255 bool IsInLoop() const { return loop_information_ != nullptr; }
1256
David Brazdilec16f792015-08-19 15:04:01 +01001257 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1258
1259 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1260 try_catch_information_ = try_catch_information;
1261 }
1262
1263 bool IsTryBlock() const {
1264 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1265 }
1266
1267 bool IsCatchBlock() const {
1268 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1269 }
David Brazdilffee3d32015-07-06 11:48:53 +01001270
1271 // Returns the try entry that this block's successors should have. They will
1272 // be in the same try, unless the block ends in a try boundary. In that case,
1273 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001274 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001275
Aart Bik75ff2c92018-04-21 01:28:11 +00001276 bool HasThrowingInstructions() const;
1277
David Brazdila4b8c212015-05-07 09:59:30 +01001278 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001279 bool Dominates(HBasicBlock* block) const;
1280
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001281 size_t GetLifetimeStart() const { return lifetime_start_; }
1282 size_t GetLifetimeEnd() const { return lifetime_end_; }
1283
1284 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1285 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1286
David Brazdil8d5b8b22015-03-24 10:51:52 +00001287 bool EndsWithControlFlowInstruction() const;
Aart Bik4dc09e72018-05-11 14:40:31 -07001288 bool EndsWithReturn() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001289 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001290 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001291 bool HasSinglePhi() const;
1292
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001293 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001294 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001295 ArenaVector<HBasicBlock*> predecessors_;
1296 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001297 HInstructionList instructions_;
1298 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001299 HLoopInformation* loop_information_;
1300 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001301 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001302 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001303 // The dex program counter of the first instruction of this block.
1304 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001305 size_t lifetime_start_;
1306 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001307 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001308
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001309 friend class HGraph;
1310 friend class HInstruction;
1311
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001312 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1313};
1314
David Brazdilb2bd1c52015-03-25 11:17:37 +00001315// Iterates over the LoopInformation of all loops which contain 'block'
1316// from the innermost to the outermost.
1317class HLoopInformationOutwardIterator : public ValueObject {
1318 public:
1319 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1320 : current_(block.GetLoopInformation()) {}
1321
1322 bool Done() const { return current_ == nullptr; }
1323
1324 void Advance() {
1325 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001326 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001327 }
1328
1329 HLoopInformation* Current() const {
1330 DCHECK(!Done());
1331 return current_;
1332 }
1333
1334 private:
1335 HLoopInformation* current_;
1336
1337 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1338};
1339
Alexandre Ramesef20f712015-06-09 10:29:30 +01001340#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001341 M(Above, Condition) \
1342 M(AboveOrEqual, Condition) \
Aart Bik3dad3412018-02-28 12:01:46 -08001343 M(Abs, UnaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001344 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001345 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001346 M(ArrayGet, Instruction) \
1347 M(ArrayLength, Instruction) \
1348 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001349 M(Below, Condition) \
1350 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001351 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001352 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001353 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001354 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001355 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001356 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001357 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001358 M(Compare, BinaryOperation) \
Igor Murashkind01745e2017-04-05 16:40:31 -07001359 M(ConstructorFence, Instruction) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001360 M(CurrentMethod, Instruction) \
Mingyao Yang063fc772016-08-02 11:02:54 -07001361 M(ShouldDeoptimizeFlag, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001362 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001363 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001364 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001365 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001366 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001367 M(Exit, Instruction) \
1368 M(FloatConstant, Constant) \
1369 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001370 M(GreaterThan, Condition) \
1371 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001372 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001373 M(InstanceFieldGet, Instruction) \
1374 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001375 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001376 M(IntConstant, Constant) \
xueliang.zhonge0eb4832017-10-30 13:43:14 +00001377 M(IntermediateAddress, Instruction) \
Calin Juravle175dc732015-08-25 15:42:32 +01001378 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001379 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001380 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001381 M(InvokeVirtual, Invoke) \
Orion Hodsonac141392017-01-13 11:53:47 +00001382 M(InvokePolymorphic, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001383 M(LessThan, Condition) \
1384 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001385 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001386 M(LoadException, Instruction) \
Orion Hodsondbaa5c72018-05-10 08:22:46 +01001387 M(LoadMethodHandle, Instruction) \
Orion Hodson18259d72018-04-12 11:18:23 +01001388 M(LoadMethodType, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001389 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001390 M(LongConstant, Constant) \
Aart Bik1f8d51b2018-02-15 10:42:37 -08001391 M(Max, Instruction) \
Calin Juravle27df7582015-04-17 19:12:31 +01001392 M(MemoryBarrier, Instruction) \
Aart Bik1f8d51b2018-02-15 10:42:37 -08001393 M(Min, BinaryOperation) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001394 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001395 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001396 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001397 M(Neg, UnaryOperation) \
1398 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001399 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001400 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001401 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001402 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001403 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001404 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001405 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001406 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001407 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001408 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001409 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001410 M(Return, Instruction) \
1411 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001412 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001413 M(Shl, BinaryOperation) \
1414 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001415 M(StaticFieldGet, Instruction) \
1416 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001417 M(UnresolvedInstanceFieldGet, Instruction) \
1418 M(UnresolvedInstanceFieldSet, Instruction) \
1419 M(UnresolvedStaticFieldGet, Instruction) \
1420 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001421 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001422 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001423 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001424 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001425 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001426 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001427 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001428 M(Xor, BinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001429 M(VecReplicateScalar, VecUnaryOperation) \
Aart Bik0148de42017-09-05 09:25:01 -07001430 M(VecExtractScalar, VecUnaryOperation) \
1431 M(VecReduce, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001432 M(VecCnv, VecUnaryOperation) \
1433 M(VecNeg, VecUnaryOperation) \
Aart Bik6daebeb2017-04-03 14:35:41 -07001434 M(VecAbs, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001435 M(VecNot, VecUnaryOperation) \
1436 M(VecAdd, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001437 M(VecHalvingAdd, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001438 M(VecSub, VecBinaryOperation) \
1439 M(VecMul, VecBinaryOperation) \
1440 M(VecDiv, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001441 M(VecMin, VecBinaryOperation) \
1442 M(VecMax, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001443 M(VecAnd, VecBinaryOperation) \
1444 M(VecAndNot, VecBinaryOperation) \
1445 M(VecOr, VecBinaryOperation) \
1446 M(VecXor, VecBinaryOperation) \
Aart Bik29aa0822018-03-08 11:28:00 -08001447 M(VecSaturationAdd, VecBinaryOperation) \
1448 M(VecSaturationSub, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001449 M(VecShl, VecBinaryOperation) \
1450 M(VecShr, VecBinaryOperation) \
1451 M(VecUShr, VecBinaryOperation) \
Aart Bik8de59162017-04-21 09:42:01 -07001452 M(VecSetScalars, VecOperation) \
Artem Serovf34dd202017-04-10 17:41:46 +01001453 M(VecMultiplyAccumulate, VecOperation) \
Aart Bikdbbac8f2017-09-01 13:06:08 -07001454 M(VecSADAccumulate, VecOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001455 M(VecLoad, VecMemoryOperation) \
1456 M(VecStore, VecMemoryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001457
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001458/*
1459 * Instructions, shared across several (not all) architectures.
1460 */
1461#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1462#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1463#else
1464#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001465 M(BitwiseNegatedRight, Instruction) \
Anton Kirilov74234da2017-01-13 14:42:47 +00001466 M(DataProcWithShifterOp, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001467 M(MultiplyAccumulate, Instruction) \
Artem Serove1811ed2017-04-27 16:50:47 +01001468 M(IntermediateAddressIndex, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001469#endif
1470
Alexandre Ramesef20f712015-06-09 10:29:30 +01001471#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1472
1473#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1474
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001475#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001476#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001477#else
1478#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1479 M(MipsComputeBaseMethodAddress, Instruction) \
Lena Djokica2901602017-09-21 13:50:52 +02001480 M(MipsPackedSwitch, Instruction) \
1481 M(IntermediateArrayAddressIndex, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001482#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001483
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001484#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1485
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001486#ifndef ART_ENABLE_CODEGEN_x86
1487#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1488#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001489#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1490 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001491 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001492 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001493 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001494#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001495
1496#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1497
1498#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1499 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001500 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001501 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1502 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001503 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001504 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001505 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1506 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1507
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001508#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1509 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001510 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001511 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001512 M(BinaryOperation, Instruction) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001513 M(Invoke, Instruction) \
1514 M(VecOperation, Instruction) \
1515 M(VecUnaryOperation, VecOperation) \
1516 M(VecBinaryOperation, VecOperation) \
1517 M(VecMemoryOperation, VecOperation)
Dave Allison20dfc792014-06-16 20:44:29 -07001518
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001519#define FOR_EACH_INSTRUCTION(M) \
1520 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1521 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1522
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001523#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001524FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1525#undef FORWARD_DECLARATION
1526
Vladimir Marko372f10e2016-05-17 16:30:10 +01001527#define DECLARE_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001528 private: \
1529 H##type& operator=(const H##type&) = delete; \
1530 public: \
Vladimir Marko372f10e2016-05-17 16:30:10 +01001531 const char* DebugName() const OVERRIDE { return #type; } \
Artem Serovcced8ba2017-07-19 18:18:09 +01001532 HInstruction* Clone(ArenaAllocator* arena) const OVERRIDE { \
1533 DCHECK(IsClonable()); \
1534 return new (arena) H##type(*this->As##type()); \
1535 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001536 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001537
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001538#define DECLARE_ABSTRACT_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001539 private: \
1540 H##type& operator=(const H##type&) = delete; \
Vladimir Markoa90dd512018-05-04 15:04:45 +01001541 public:
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001542
Artem Serovcced8ba2017-07-19 18:18:09 +01001543#define DEFAULT_COPY_CONSTRUCTOR(type) \
1544 explicit H##type(const H##type& other) = default;
1545
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001546template <typename T>
Vladimir Marko82b07402017-03-01 19:02:04 +00001547class HUseListNode : public ArenaObject<kArenaAllocUseListNode>,
1548 public IntrusiveForwardListNode<HUseListNode<T>> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001549 public:
Igor Murashkind01745e2017-04-05 16:40:31 -07001550 // Get the instruction which has this use as one of the inputs.
David Brazdiled596192015-01-23 10:39:45 +00001551 T GetUser() const { return user_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001552 // Get the position of the input record that this use corresponds to.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001553 size_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001554 // Set the position of the input record that this use corresponds to.
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001555 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001556
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001557 private:
David Brazdiled596192015-01-23 10:39:45 +00001558 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001559 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001560
1561 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001562 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001563
Vladimir Marko46817b82016-03-29 12:21:58 +01001564 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001565
1566 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1567};
1568
David Brazdiled596192015-01-23 10:39:45 +00001569template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001570using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001571
David Brazdil1abb4192015-02-17 18:33:36 +00001572// This class is used by HEnvironment and HInstruction classes to record the
1573// instructions they use and pointers to the corresponding HUseListNodes kept
1574// by the used instructions.
1575template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001576class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001577 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001578 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1579 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001580
Vladimir Marko46817b82016-03-29 12:21:58 +01001581 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1582 : HUserRecord(old_record.instruction_, before_use_node) {}
1583 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1584 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001585 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001586 }
1587
1588 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001589 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1590 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001591
1592 private:
1593 // Instruction used by the user.
1594 HInstruction* instruction_;
1595
Vladimir Marko46817b82016-03-29 12:21:58 +01001596 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1597 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001598};
1599
Vladimir Markoe9004912016-06-16 16:50:52 +01001600// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1601// This is used for HInstruction::GetInputs() to return a container wrapper providing
1602// HInstruction* values even though the underlying container has HUserRecord<>s.
1603struct HInputExtractor {
1604 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1605 return record.GetInstruction();
1606 }
1607 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1608 return record.GetInstruction();
1609 }
1610};
1611
1612using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1613using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1614
Aart Bik854a02b2015-07-14 16:07:00 -07001615/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001616 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001617 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001618 * For write/read dependences on fields/arrays, the dependence analysis uses
1619 * type disambiguation (e.g. a float field write cannot modify the value of an
1620 * integer field read) and the access type (e.g. a reference array write cannot
1621 * modify the value of a reference field read [although it may modify the
1622 * reference fetch prior to reading the field, which is represented by its own
1623 * write/read dependence]). The analysis makes conservative points-to
1624 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1625 * the same, and any reference read depends on any reference read without
1626 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001627 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001628 * The internal representation uses 38-bit and is described in the table below.
1629 * The first line indicates the side effect, and for field/array accesses the
1630 * second line indicates the type of the access (in the order of the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001631 * DataType::Type enum).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001632 * The two numbered lines below indicate the bit position in the bitfield (read
1633 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001634 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001635 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1636 * +-------------+---------+---------+--------------+---------+---------+
1637 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1638 * | 3 |333333322|222222221| 1 |111111110|000000000|
1639 * | 7 |654321098|765432109| 8 |765432109|876543210|
1640 *
1641 * Note that, to ease the implementation, 'changes' bits are least significant
1642 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001643 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001644class SideEffects : public ValueObject {
1645 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001646 SideEffects() : flags_(0) {}
1647
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001648 static SideEffects None() {
1649 return SideEffects(0);
1650 }
1651
1652 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001653 return SideEffects(kAllChangeBits | kAllDependOnBits);
1654 }
1655
1656 static SideEffects AllChanges() {
1657 return SideEffects(kAllChangeBits);
1658 }
1659
1660 static SideEffects AllDependencies() {
1661 return SideEffects(kAllDependOnBits);
1662 }
1663
1664 static SideEffects AllExceptGCDependency() {
1665 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1666 }
1667
1668 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001669 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001670 }
1671
Aart Bik34c3ba92015-07-20 14:08:59 -07001672 static SideEffects AllWrites() {
1673 return SideEffects(kAllWrites);
1674 }
1675
1676 static SideEffects AllReads() {
1677 return SideEffects(kAllReads);
1678 }
1679
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001680 static SideEffects FieldWriteOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001681 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001682 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001683 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001684 }
1685
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001686 static SideEffects ArrayWriteOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001687 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001688 }
1689
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001690 static SideEffects FieldReadOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001691 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001692 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001693 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001694 }
1695
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001696 static SideEffects ArrayReadOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001697 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001698 }
1699
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001700 static SideEffects CanTriggerGC() {
1701 return SideEffects(1ULL << kCanTriggerGCBit);
1702 }
1703
1704 static SideEffects DependsOnGC() {
1705 return SideEffects(1ULL << kDependsOnGCBit);
1706 }
1707
Aart Bik854a02b2015-07-14 16:07:00 -07001708 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001709 SideEffects Union(SideEffects other) const {
1710 return SideEffects(flags_ | other.flags_);
1711 }
1712
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001713 SideEffects Exclusion(SideEffects other) const {
1714 return SideEffects(flags_ & ~other.flags_);
1715 }
1716
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001717 void Add(SideEffects other) {
1718 flags_ |= other.flags_;
1719 }
1720
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001721 bool Includes(SideEffects other) const {
1722 return (other.flags_ & flags_) == other.flags_;
1723 }
1724
1725 bool HasSideEffects() const {
1726 return (flags_ & kAllChangeBits);
1727 }
1728
1729 bool HasDependencies() const {
1730 return (flags_ & kAllDependOnBits);
1731 }
1732
1733 // Returns true if there are no side effects or dependencies.
1734 bool DoesNothing() const {
1735 return flags_ == 0;
1736 }
1737
Aart Bik854a02b2015-07-14 16:07:00 -07001738 // Returns true if something is written.
1739 bool DoesAnyWrite() const {
1740 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001741 }
1742
Aart Bik854a02b2015-07-14 16:07:00 -07001743 // Returns true if something is read.
1744 bool DoesAnyRead() const {
1745 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001746 }
1747
Aart Bik854a02b2015-07-14 16:07:00 -07001748 // Returns true if potentially everything is written and read
1749 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001750 bool DoesAllReadWrite() const {
1751 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1752 }
1753
Aart Bik854a02b2015-07-14 16:07:00 -07001754 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001755 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001756 }
1757
Roland Levillain0d5a2812015-11-13 10:07:31 +00001758 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001759 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001760 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1761 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001762 }
1763
1764 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001765 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001766 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001767 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001768 for (int s = kLastBit; s >= 0; s--) {
1769 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1770 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1771 // This is a bit for the GC side effect.
1772 if (current_bit_is_set) {
1773 flags += "GC";
1774 }
Aart Bik854a02b2015-07-14 16:07:00 -07001775 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001776 } else {
1777 // This is a bit for the array/field analysis.
1778 // The underscore character stands for the 'can trigger GC' bit.
1779 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1780 if (current_bit_is_set) {
1781 flags += kDebug[s];
1782 }
1783 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1784 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1785 flags += "|";
1786 }
1787 }
Aart Bik854a02b2015-07-14 16:07:00 -07001788 }
1789 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001790 }
1791
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001792 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001793
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001794 private:
1795 static constexpr int kFieldArrayAnalysisBits = 9;
1796
1797 static constexpr int kFieldWriteOffset = 0;
1798 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1799 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1800 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1801
1802 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1803
1804 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1805 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1806 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1807 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1808
1809 static constexpr int kLastBit = kDependsOnGCBit;
1810 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1811
1812 // Aliases.
1813
1814 static_assert(kChangeBits == kDependOnBits,
1815 "the 'change' bits should match the 'depend on' bits.");
1816
1817 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1818 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1819 static constexpr uint64_t kAllWrites =
1820 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1821 static constexpr uint64_t kAllReads =
1822 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001823
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001824 // Translates type to bit flag. The type must correspond to a Java type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001825 static uint64_t TypeFlag(DataType::Type type, int offset) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001826 int shift;
1827 switch (type) {
1828 case DataType::Type::kReference: shift = 0; break;
1829 case DataType::Type::kBool: shift = 1; break;
1830 case DataType::Type::kInt8: shift = 2; break;
1831 case DataType::Type::kUint16: shift = 3; break;
1832 case DataType::Type::kInt16: shift = 4; break;
1833 case DataType::Type::kInt32: shift = 5; break;
1834 case DataType::Type::kInt64: shift = 6; break;
1835 case DataType::Type::kFloat32: shift = 7; break;
1836 case DataType::Type::kFloat64: shift = 8; break;
1837 default:
1838 LOG(FATAL) << "Unexpected data type " << type;
1839 UNREACHABLE();
1840 }
Aart Bik854a02b2015-07-14 16:07:00 -07001841 DCHECK_LE(kFieldWriteOffset, shift);
1842 DCHECK_LT(shift, kArrayWriteOffset);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001843 return UINT64_C(1) << (shift + offset);
Aart Bik854a02b2015-07-14 16:07:00 -07001844 }
1845
1846 // Private constructor on direct flags value.
1847 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1848
1849 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001850};
1851
David Brazdiled596192015-01-23 10:39:45 +00001852// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001853class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001854 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001855 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
Mingyao Yang01b47b02017-02-03 12:09:57 -08001856 size_t number_of_vregs,
1857 ArtMethod* method,
1858 uint32_t dex_pc,
1859 HInstruction* holder)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001860 : vregs_(number_of_vregs, allocator->Adapter(kArenaAllocEnvironmentVRegs)),
1861 locations_(allocator->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001862 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001863 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001864 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001865 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001866 }
1867
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001868 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
1869 const HEnvironment& to_copy,
1870 HInstruction* holder)
1871 : HEnvironment(allocator,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001872 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001873 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001874 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001875 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001876
Vladimir Markoec32f642017-06-02 10:51:55 +01001877 void AllocateLocations() {
1878 DCHECK(locations_.empty());
1879 locations_.resize(vregs_.size());
1880 }
1881
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001882 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001883 if (parent_ != nullptr) {
1884 parent_->SetAndCopyParentChain(allocator, parent);
1885 } else {
1886 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1887 parent_->CopyFrom(parent);
1888 if (parent->GetParent() != nullptr) {
1889 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1890 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001891 }
David Brazdiled596192015-01-23 10:39:45 +00001892 }
1893
Vladimir Marko69d310e2017-10-09 14:12:23 +01001894 void CopyFrom(ArrayRef<HInstruction* const> locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001895 void CopyFrom(HEnvironment* environment);
1896
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001897 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1898 // input to the loop phi instead. This is for inserting instructions that
1899 // require an environment (like HDeoptimization) in the loop pre-header.
1900 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001901
1902 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001903 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001904 }
1905
1906 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001907 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001908 }
1909
David Brazdil1abb4192015-02-17 18:33:36 +00001910 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001911
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001912 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001913
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001914 HEnvironment* GetParent() const { return parent_; }
1915
1916 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001917 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001918 }
1919
1920 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001921 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001922 }
1923
1924 uint32_t GetDexPc() const {
1925 return dex_pc_;
1926 }
1927
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001928 ArtMethod* GetMethod() const {
1929 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001930 }
1931
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001932 HInstruction* GetHolder() const {
1933 return holder_;
1934 }
1935
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001936
1937 bool IsFromInlinedInvoke() const {
1938 return GetParent() != nullptr;
1939 }
1940
David Brazdiled596192015-01-23 10:39:45 +00001941 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001942 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1943 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001944 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001945 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001946 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00001947
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001948 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001949 HInstruction* const holder_;
1950
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001951 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001952
1953 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1954};
1955
Vladimir Markof9f64412015-09-02 14:05:49 +01001956class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001957 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05301958#define DECLARE_KIND(type, super) k##type,
1959 enum InstructionKind {
Vladimir Markoe3946222018-05-04 14:18:47 +01001960 FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_KIND)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05301961 kLastInstructionKind
1962 };
1963#undef DECLARE_KIND
1964
1965 HInstruction(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01001966 : HInstruction(kind, DataType::Type::kVoid, side_effects, dex_pc) {}
1967
1968 HInstruction(InstructionKind kind, DataType::Type type, SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001969 : previous_(nullptr),
1970 next_(nullptr),
1971 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001972 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001973 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001974 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001975 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001976 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001977 locations_(nullptr),
1978 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001979 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001980 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001981 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05301982 SetPackedField<InstructionKindField>(kind);
Vladimir Markobd785672018-05-03 17:09:09 +01001983 SetPackedField<TypeField>(type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001984 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1985 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001986
Dave Allison20dfc792014-06-16 20:44:29 -07001987 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001988
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001989
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001990 HInstruction* GetNext() const { return next_; }
1991 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001992
Calin Juravle77520bc2015-01-12 18:45:46 +00001993 HInstruction* GetNextDisregardingMoves() const;
1994 HInstruction* GetPreviousDisregardingMoves() const;
1995
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001996 HBasicBlock* GetBlock() const { return block_; }
Vladimir Markoca6fff82017-10-03 14:49:14 +01001997 ArenaAllocator* GetAllocator() const { return block_->GetGraph()->GetAllocator(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001998 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001999 bool IsInBlock() const { return block_ != nullptr; }
2000 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00002001 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
2002 bool IsIrreducibleLoopHeaderPhi() const {
2003 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
2004 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002005
Vladimir Marko372f10e2016-05-17 16:30:10 +01002006 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
2007
2008 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
2009 // One virtual method is enough, just const_cast<> and then re-add the const.
2010 return ArrayRef<const HUserRecord<HInstruction*>>(
2011 const_cast<HInstruction*>(this)->GetInputRecords());
2012 }
2013
Vladimir Markoe9004912016-06-16 16:50:52 +01002014 HInputsRef GetInputs() {
2015 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002016 }
2017
Vladimir Markoe9004912016-06-16 16:50:52 +01002018 HConstInputsRef GetInputs() const {
2019 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002020 }
2021
2022 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00002023 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002024
Aart Bik2767f4b2016-10-28 15:03:53 -07002025 bool HasInput(HInstruction* input) const {
2026 for (const HInstruction* i : GetInputs()) {
2027 if (i == input) {
2028 return true;
2029 }
2030 }
2031 return false;
2032 }
2033
Vladimir Marko372f10e2016-05-17 16:30:10 +01002034 void SetRawInputAt(size_t index, HInstruction* input) {
2035 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
2036 }
2037
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002038 virtual void Accept(HGraphVisitor* visitor) = 0;
2039 virtual const char* DebugName() const = 0;
2040
Vladimir Markobd785672018-05-03 17:09:09 +01002041 DataType::Type GetType() const {
2042 return TypeField::Decode(GetPackedFields());
2043 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002044
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002045 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002046
2047 uint32_t GetDexPc() const { return dex_pc_; }
2048
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002049 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01002050
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002051 // Can the instruction throw?
2052 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
2053 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01002054 virtual bool CanThrow() const { return false; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08002055
2056 // Does the instruction always throw an exception unconditionally?
2057 virtual bool AlwaysThrows() const { return false; }
2058
David Brazdilec16f792015-08-19 15:04:01 +01002059 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002060
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002061 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002062 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002063
Calin Juravle10e244f2015-01-26 18:54:32 +00002064 // Does not apply for all instructions, but having this at top level greatly
2065 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00002066 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002067 virtual bool CanBeNull() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002068 DCHECK_EQ(GetType(), DataType::Type::kReference) << "CanBeNull only applies to reference types";
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002069 return true;
2070 }
Calin Juravle10e244f2015-01-26 18:54:32 +00002071
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002072 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01002073 return false;
2074 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002075
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002076 virtual bool IsActualObject() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002077 return GetType() == DataType::Type::kReference;
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002078 }
2079
Calin Juravle2e768302015-07-28 14:41:11 +00002080 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00002081
Calin Juravle61d544b2015-02-23 16:46:57 +00002082 ReferenceTypeInfo GetReferenceTypeInfo() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002083 DCHECK_EQ(GetType(), DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002084 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00002085 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00002086 }
Calin Juravleacf735c2015-02-12 15:25:22 +00002087
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002088 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00002089 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002090 // Note: fixup_end remains valid across push_front().
2091 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
2092 HUseListNode<HInstruction*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002093 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HInstruction*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002094 uses_.push_front(*new_node);
2095 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002096 }
2097
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002098 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01002099 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002100 // Note: env_fixup_end remains valid across push_front().
2101 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
2102 HUseListNode<HEnvironment*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002103 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HEnvironment*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002104 env_uses_.push_front(*new_node);
2105 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002106 }
2107
David Brazdil1abb4192015-02-17 18:33:36 +00002108 void RemoveAsUserOfInput(size_t input) {
2109 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002110 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2111 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2112 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00002113 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002114
Vladimir Marko372f10e2016-05-17 16:30:10 +01002115 void RemoveAsUserOfAllInputs() {
2116 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2117 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2118 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2119 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
2120 }
2121 }
2122
David Brazdil1abb4192015-02-17 18:33:36 +00002123 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2124 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002125
Vladimir Marko46817b82016-03-29 12:21:58 +01002126 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2127 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2128 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002129 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002130 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002131 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002132
Aart Bikcc42be02016-10-20 16:14:16 -07002133 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002134 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002135 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002136 !CanThrow() &&
2137 !IsSuspendCheck() &&
2138 !IsControlFlow() &&
2139 !IsNativeDebugInfo() &&
2140 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002141 // If we added an explicit barrier then we should keep it.
Igor Murashkind01745e2017-04-05 16:40:31 -07002142 !IsMemoryBarrier() &&
2143 !IsConstructorFence();
Aart Bik482095d2016-10-10 15:39:10 -07002144 }
2145
Aart Bikcc42be02016-10-20 16:14:16 -07002146 bool IsDeadAndRemovable() const {
2147 return IsRemovable() && !HasUses();
2148 }
2149
Roland Levillain6c82d402014-10-13 16:10:27 +01002150 // Does this instruction strictly dominate `other_instruction`?
2151 // Returns false if this instruction and `other_instruction` are the same.
2152 // Aborts if this instruction and `other_instruction` are both phis.
2153 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002154
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002155 int GetId() const { return id_; }
2156 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002157
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002158 int GetSsaIndex() const { return ssa_index_; }
2159 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2160 bool HasSsaIndex() const { return ssa_index_ != -1; }
2161
2162 bool HasEnvironment() const { return environment_ != nullptr; }
2163 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002164 // Set the `environment_` field. Raw because this method does not
2165 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002166 void SetRawEnvironment(HEnvironment* environment) {
2167 DCHECK(environment_ == nullptr);
2168 DCHECK_EQ(environment->GetHolder(), this);
2169 environment_ = environment;
2170 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002171
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002172 void InsertRawEnvironment(HEnvironment* environment) {
2173 DCHECK(environment_ != nullptr);
2174 DCHECK_EQ(environment->GetHolder(), this);
2175 DCHECK(environment->GetParent() == nullptr);
2176 environment->parent_ = environment_;
2177 environment_ = environment;
2178 }
2179
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002180 void RemoveEnvironment();
2181
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002182 // Set the environment of this instruction, copying it from `environment`. While
2183 // copying, the uses lists are being updated.
2184 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002185 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002186 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002187 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002188 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002189 if (environment->GetParent() != nullptr) {
2190 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2191 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002192 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002193
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002194 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2195 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002196 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002197 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002198 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002199 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002200 if (environment->GetParent() != nullptr) {
2201 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2202 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002203 }
2204
Nicolas Geoffray39468442014-09-02 15:17:15 +01002205 // Returns the number of entries in the environment. Typically, that is the
2206 // number of dex registers in a method. It could be more in case of inlining.
2207 size_t EnvironmentSize() const;
2208
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002209 LocationSummary* GetLocations() const { return locations_; }
2210 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002211
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002212 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002213 void ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002214 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002215
Alexandre Rames188d4312015-04-09 18:30:21 +01002216 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2217 // uses of this instruction by `other` are *not* updated.
2218 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2219 ReplaceWith(other);
2220 other->ReplaceInput(this, use_index);
2221 }
2222
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002223 // Move `this` instruction before `cursor`
2224 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002225
Vladimir Markofb337ea2015-11-25 15:25:10 +00002226 // Move `this` before its first user and out of any loops. If there is no
2227 // out-of-loop user that dominates all other users, move the instruction
2228 // to the end of the out-of-loop common dominator of the user's blocks.
2229 //
2230 // This can be used only on non-throwing instructions with no side effects that
2231 // have at least one use but no environment uses.
2232 void MoveBeforeFirstUserAndOutOfLoops();
2233
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002234#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01002235 bool Is##type() const;
2236
2237 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2238#undef INSTRUCTION_TYPE_CHECK
2239
2240#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002241 const H##type* As##type() const; \
2242 H##type* As##type();
2243
Vladimir Markoa90dd512018-05-04 15:04:45 +01002244 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
2245#undef INSTRUCTION_TYPE_CAST
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002246
Artem Serovcced8ba2017-07-19 18:18:09 +01002247 // Return a clone of the instruction if it is clonable (shallow copy by default, custom copy
2248 // if a custom copy-constructor is provided for a particular type). If IsClonable() is false for
2249 // the instruction then the behaviour of this function is undefined.
2250 //
2251 // Note: It is semantically valid to create a clone of the instruction only until
2252 // prepare_for_register_allocator phase as lifetime, intervals and codegen info are not
2253 // copied.
2254 //
2255 // Note: HEnvironment and some other fields are not copied and are set to default values, see
2256 // 'explicit HInstruction(const HInstruction& other)' for details.
2257 virtual HInstruction* Clone(ArenaAllocator* arena ATTRIBUTE_UNUSED) const {
2258 LOG(FATAL) << "Cloning is not implemented for the instruction " <<
2259 DebugName() << " " << GetId();
2260 UNREACHABLE();
2261 }
2262
2263 // Return whether instruction can be cloned (copied).
2264 virtual bool IsClonable() const { return false; }
2265
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002266 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002267 // TODO: this method is used by LICM and GVN with possibly different
2268 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002269 virtual bool CanBeMoved() const { return false; }
2270
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002271 // Returns whether any data encoded in the two instructions is equal.
2272 // This method does not look at the inputs. Both instructions must be
2273 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002274 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002275 return false;
2276 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002277
2278 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002279 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002280 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002281 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002282
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002283 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2284 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2285 // the virtual function because the __attribute__((__pure__)) doesn't really
2286 // apply the strong requirement for virtual functions, preventing optimizations.
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302287 InstructionKind GetKind() const { return GetPackedField<InstructionKindField>(); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002288
2289 virtual size_t ComputeHashCode() const {
2290 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002291 for (const HInstruction* input : GetInputs()) {
2292 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002293 }
2294 return result;
2295 }
2296
2297 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002298 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002299 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002300
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002301 size_t GetLifetimePosition() const { return lifetime_position_; }
2302 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2303 LiveInterval* GetLiveInterval() const { return live_interval_; }
2304 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2305 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2306
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002307 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2308
2309 // Returns whether the code generation of the instruction will require to have access
2310 // to the current method. Such instructions are:
2311 // (1): Instructions that require an environment, as calling the runtime requires
2312 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002313 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2314 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002315 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002316 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002317 }
2318
Vladimir Markodc151b22015-10-15 18:02:30 +01002319 // Returns whether the code generation of the instruction will require to have access
2320 // to the dex cache of the current method's declaring class via the current method.
2321 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002322
Mark Mendellc4701932015-04-10 13:18:51 -04002323 // Does this instruction have any use in an environment before
2324 // control flow hits 'other'?
2325 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2326
2327 // Remove all references to environment uses of this instruction.
2328 // The caller must ensure that this is safe to do.
2329 void RemoveEnvironmentUsers();
2330
Vladimir Markoa1de9182016-02-25 11:37:38 +00002331 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2332 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002333
David Brazdil1abb4192015-02-17 18:33:36 +00002334 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002335 // If set, the machine code for this instruction is assumed to be generated by
2336 // its users. Used by liveness analysis to compute use positions accordingly.
2337 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2338 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302339 static constexpr size_t kFieldInstructionKind = kFlagReferenceTypeIsExact + 1;
2340 static constexpr size_t kFieldInstructionKindSize =
2341 MinimumBitsToStore(static_cast<size_t>(InstructionKind::kLastInstructionKind - 1));
Vladimir Markobd785672018-05-03 17:09:09 +01002342 static constexpr size_t kFieldType =
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302343 kFieldInstructionKind + kFieldInstructionKindSize;
Vladimir Markobd785672018-05-03 17:09:09 +01002344 static constexpr size_t kFieldTypeSize =
2345 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
2346 static constexpr size_t kNumberOfGenericPackedBits = kFieldType + kFieldTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00002347 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2348
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302349 static_assert(kNumberOfGenericPackedBits <= kMaxNumberOfPackedBits,
2350 "Too many generic packed fields");
2351
Vladimir Markobd785672018-05-03 17:09:09 +01002352 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
2353
Vladimir Marko372f10e2016-05-17 16:30:10 +01002354 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2355 return GetInputRecords()[i];
2356 }
2357
2358 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2359 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2360 input_records[index] = input;
2361 }
David Brazdil1abb4192015-02-17 18:33:36 +00002362
Vladimir Markoa1de9182016-02-25 11:37:38 +00002363 uint32_t GetPackedFields() const {
2364 return packed_fields_;
2365 }
2366
2367 template <size_t flag>
2368 bool GetPackedFlag() const {
2369 return (packed_fields_ & (1u << flag)) != 0u;
2370 }
2371
2372 template <size_t flag>
2373 void SetPackedFlag(bool value = true) {
2374 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2375 }
2376
2377 template <typename BitFieldType>
2378 typename BitFieldType::value_type GetPackedField() const {
2379 return BitFieldType::Decode(packed_fields_);
2380 }
2381
2382 template <typename BitFieldType>
2383 void SetPackedField(typename BitFieldType::value_type value) {
2384 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2385 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2386 }
2387
Artem Serovcced8ba2017-07-19 18:18:09 +01002388 // Copy construction for the instruction (used for Clone function).
2389 //
2390 // Fields (e.g. lifetime, intervals and codegen info) associated with phases starting from
2391 // prepare_for_register_allocator are not copied (set to default values).
2392 //
2393 // Copy constructors must be provided for every HInstruction type; default copy constructor is
2394 // fine for most of them. However for some of the instructions a custom copy constructor must be
2395 // specified (when instruction has non-trivially copyable fields and must have a special behaviour
2396 // for copying them).
2397 explicit HInstruction(const HInstruction& other)
2398 : previous_(nullptr),
2399 next_(nullptr),
2400 block_(nullptr),
2401 dex_pc_(other.dex_pc_),
2402 id_(-1),
2403 ssa_index_(-1),
2404 packed_fields_(other.packed_fields_),
2405 environment_(nullptr),
2406 locations_(nullptr),
2407 live_interval_(nullptr),
2408 lifetime_position_(kNoLifetime),
2409 side_effects_(other.side_effects_),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302410 reference_type_handle_(other.reference_type_handle_) {
2411 }
Artem Serovcced8ba2017-07-19 18:18:09 +01002412
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002413 private:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302414 using InstructionKindField =
2415 BitField<InstructionKind, kFieldInstructionKind, kFieldInstructionKindSize>;
2416
Vladimir Marko46817b82016-03-29 12:21:58 +01002417 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2418 auto before_use_node = uses_.before_begin();
2419 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2420 HInstruction* user = use_node->GetUser();
2421 size_t input_index = use_node->GetIndex();
2422 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2423 before_use_node = use_node;
2424 }
2425 }
2426
2427 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2428 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2429 if (next != uses_.end()) {
2430 HInstruction* next_user = next->GetUser();
2431 size_t next_index = next->GetIndex();
2432 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2433 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2434 }
2435 }
2436
2437 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2438 auto before_env_use_node = env_uses_.before_begin();
2439 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2440 HEnvironment* user = env_use_node->GetUser();
2441 size_t input_index = env_use_node->GetIndex();
2442 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2443 before_env_use_node = env_use_node;
2444 }
2445 }
2446
2447 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2448 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2449 if (next != env_uses_.end()) {
2450 HEnvironment* next_user = next->GetUser();
2451 size_t next_index = next->GetIndex();
2452 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2453 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2454 }
2455 }
David Brazdil1abb4192015-02-17 18:33:36 +00002456
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002457 HInstruction* previous_;
2458 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002459 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002460 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002461
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002462 // An instruction gets an id when it is added to the graph.
2463 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002464 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002465 int id_;
2466
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002467 // When doing liveness analysis, instructions that have uses get an SSA index.
2468 int ssa_index_;
2469
Vladimir Markoa1de9182016-02-25 11:37:38 +00002470 // Packed fields.
2471 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002472
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002473 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002474 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002475
2476 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002477 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002478
Nicolas Geoffray39468442014-09-02 15:17:15 +01002479 // The environment associated with this instruction. Not null if the instruction
2480 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002481 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002482
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002483 // Set by the code generator.
2484 LocationSummary* locations_;
2485
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002486 // Set by the liveness analysis.
2487 LiveInterval* live_interval_;
2488
2489 // Set by the liveness analysis, this is the position in a linear
2490 // order of blocks where this instruction's live interval start.
2491 size_t lifetime_position_;
2492
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002493 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002494
Vladimir Markoa1de9182016-02-25 11:37:38 +00002495 // The reference handle part of the reference type info.
2496 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002497 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002498 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002499
David Brazdil1abb4192015-02-17 18:33:36 +00002500 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002501 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002502 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002503 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002504 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002505};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002506std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002507
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002508// Iterates over the instructions, while preserving the next instruction
2509// in case the current instruction gets removed from the list by the user
2510// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002511class HInstructionIterator : public ValueObject {
2512 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002513 explicit HInstructionIterator(const HInstructionList& instructions)
2514 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002515 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002516 }
2517
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002518 bool Done() const { return instruction_ == nullptr; }
2519 HInstruction* Current() const { return instruction_; }
2520 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002521 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002522 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002523 }
2524
2525 private:
2526 HInstruction* instruction_;
2527 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002528
2529 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002530};
2531
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002532// Iterates over the instructions without saving the next instruction,
2533// therefore handling changes in the graph potentially made by the user
2534// of this iterator.
2535class HInstructionIteratorHandleChanges : public ValueObject {
2536 public:
2537 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2538 : instruction_(instructions.first_instruction_) {
2539 }
2540
2541 bool Done() const { return instruction_ == nullptr; }
2542 HInstruction* Current() const { return instruction_; }
2543 void Advance() {
2544 instruction_ = instruction_->GetNext();
2545 }
2546
2547 private:
2548 HInstruction* instruction_;
2549
2550 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2551};
2552
2553
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002554class HBackwardInstructionIterator : public ValueObject {
2555 public:
2556 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2557 : instruction_(instructions.last_instruction_) {
2558 next_ = Done() ? nullptr : instruction_->GetPrevious();
2559 }
2560
2561 bool Done() const { return instruction_ == nullptr; }
2562 HInstruction* Current() const { return instruction_; }
2563 void Advance() {
2564 instruction_ = next_;
2565 next_ = Done() ? nullptr : instruction_->GetPrevious();
2566 }
2567
2568 private:
2569 HInstruction* instruction_;
2570 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002571
2572 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002573};
2574
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002575class HVariableInputSizeInstruction : public HInstruction {
2576 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002577 using HInstruction::GetInputRecords; // Keep the const version visible.
2578 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
2579 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2580 }
2581
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002582 void AddInput(HInstruction* input);
2583 void InsertInputAt(size_t index, HInstruction* input);
2584 void RemoveInputAt(size_t index);
2585
Igor Murashkind01745e2017-04-05 16:40:31 -07002586 // Removes all the inputs.
2587 // Also removes this instructions from each input's use list
2588 // (for non-environment uses only).
2589 void RemoveAllInputs();
2590
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002591 protected:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302592 HVariableInputSizeInstruction(InstructionKind inst_kind,
2593 SideEffects side_effects,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002594 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002595 ArenaAllocator* allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002596 size_t number_of_inputs,
2597 ArenaAllocKind kind)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302598 : HInstruction(inst_kind, side_effects, dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002599 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Vladimir Markobd785672018-05-03 17:09:09 +01002600 HVariableInputSizeInstruction(InstructionKind inst_kind,
2601 DataType::Type type,
2602 SideEffects side_effects,
2603 uint32_t dex_pc,
2604 ArenaAllocator* allocator,
2605 size_t number_of_inputs,
2606 ArenaAllocKind kind)
2607 : HInstruction(inst_kind, type, side_effects, dex_pc),
2608 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002609
Artem Serovcced8ba2017-07-19 18:18:09 +01002610 DEFAULT_COPY_CONSTRUCTOR(VariableInputSizeInstruction);
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002611
Artem Serovcced8ba2017-07-19 18:18:09 +01002612 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002613};
2614
Vladimir Markof9f64412015-09-02 14:05:49 +01002615template<size_t N>
Vladimir Markobd785672018-05-03 17:09:09 +01002616class HExpression : public HInstruction {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002617 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002618 HExpression<N>(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302619 : HInstruction(kind, side_effects, dex_pc), inputs_() {}
Vladimir Markobd785672018-05-03 17:09:09 +01002620 HExpression<N>(InstructionKind kind,
2621 DataType::Type type,
2622 SideEffects side_effects,
2623 uint32_t dex_pc)
2624 : HInstruction(kind, type, side_effects, dex_pc), inputs_() {}
2625 virtual ~HExpression() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002626
Vladimir Marko372f10e2016-05-17 16:30:10 +01002627 using HInstruction::GetInputRecords; // Keep the const version visible.
2628 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2629 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002630 }
2631
Artem Serovcced8ba2017-07-19 18:18:09 +01002632 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002633 DEFAULT_COPY_CONSTRUCTOR(Expression<N>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002634
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002635 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002636 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002637
2638 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002639};
2640
Vladimir Markobd785672018-05-03 17:09:09 +01002641// HExpression specialization for N=0.
Vladimir Markof9f64412015-09-02 14:05:49 +01002642template<>
Vladimir Markobd785672018-05-03 17:09:09 +01002643class HExpression<0> : public HInstruction {
Vladimir Markof9f64412015-09-02 14:05:49 +01002644 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002645 using HInstruction::HInstruction;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002646
Vladimir Markobd785672018-05-03 17:09:09 +01002647 virtual ~HExpression() {}
Vladimir Markof9f64412015-09-02 14:05:49 +01002648
Vladimir Marko372f10e2016-05-17 16:30:10 +01002649 using HInstruction::GetInputRecords; // Keep the const version visible.
2650 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2651 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002652 }
2653
Artem Serovcced8ba2017-07-19 18:18:09 +01002654 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002655 DEFAULT_COPY_CONSTRUCTOR(Expression<0>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002656
Vladimir Markof9f64412015-09-02 14:05:49 +01002657 private:
2658 friend class SsaBuilder;
2659};
2660
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002661// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2662// instruction that branches to the exit block.
Vladimir Markobd785672018-05-03 17:09:09 +01002663class HReturnVoid FINAL : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002664 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002665 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002666 : HExpression(kReturnVoid, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302667 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002668
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002669 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002670
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002671 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002672
Artem Serovcced8ba2017-07-19 18:18:09 +01002673 protected:
2674 DEFAULT_COPY_CONSTRUCTOR(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002675};
2676
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002677// Represents dex's RETURN opcodes. A HReturn is a control flow
2678// instruction that branches to the exit block.
Vladimir Markobd785672018-05-03 17:09:09 +01002679class HReturn FINAL : public HExpression<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002680 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002681 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002682 : HExpression(kReturn, SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002683 SetRawInputAt(0, value);
2684 }
2685
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002686 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002687
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002688 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002689
Artem Serovcced8ba2017-07-19 18:18:09 +01002690 protected:
2691 DEFAULT_COPY_CONSTRUCTOR(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002692};
2693
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002694class HPhi FINAL : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002695 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002696 HPhi(ArenaAllocator* allocator,
David Brazdildee58d62016-04-07 09:54:26 +00002697 uint32_t reg_number,
2698 size_t number_of_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002699 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +00002700 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002701 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302702 kPhi,
Vladimir Markobd785672018-05-03 17:09:09 +01002703 ToPhiType(type),
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002704 SideEffects::None(),
2705 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002706 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002707 number_of_inputs,
2708 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002709 reg_number_(reg_number) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002710 DCHECK_NE(GetType(), DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +00002711 // Phis are constructed live and marked dead if conflicting or unused.
2712 // Individual steps of SsaBuilder should assume that if a phi has been
2713 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2714 SetPackedFlag<kFlagIsLive>(true);
2715 SetPackedFlag<kFlagCanBeNull>(true);
2716 }
2717
Artem Serovcced8ba2017-07-19 18:18:09 +01002718 bool IsClonable() const OVERRIDE { return true; }
2719
David Brazdildee58d62016-04-07 09:54:26 +00002720 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002721 static DataType::Type ToPhiType(DataType::Type type) {
2722 return DataType::Kind(type);
David Brazdildee58d62016-04-07 09:54:26 +00002723 }
2724
2725 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2726
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002727 void SetType(DataType::Type new_type) {
David Brazdildee58d62016-04-07 09:54:26 +00002728 // Make sure that only valid type changes occur. The following are allowed:
2729 // (1) int -> float/ref (primitive type propagation),
2730 // (2) long -> double (primitive type propagation).
2731 DCHECK(GetType() == new_type ||
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002732 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kFloat32) ||
2733 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kReference) ||
2734 (GetType() == DataType::Type::kInt64 && new_type == DataType::Type::kFloat64));
David Brazdildee58d62016-04-07 09:54:26 +00002735 SetPackedField<TypeField>(new_type);
2736 }
2737
2738 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2739 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2740
2741 uint32_t GetRegNumber() const { return reg_number_; }
2742
2743 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2744 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2745 bool IsDead() const { return !IsLive(); }
2746 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2747
Vladimir Markoe9004912016-06-16 16:50:52 +01002748 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002749 return other != nullptr
2750 && other->IsPhi()
2751 && other->AsPhi()->GetBlock() == GetBlock()
2752 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2753 }
2754
Nicolas Geoffrayf57c1ae2017-06-28 17:40:18 +01002755 bool HasEquivalentPhi() const {
2756 if (GetPrevious() != nullptr && GetPrevious()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2757 return true;
2758 }
2759 if (GetNext() != nullptr && GetNext()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2760 return true;
2761 }
2762 return false;
2763 }
2764
David Brazdildee58d62016-04-07 09:54:26 +00002765 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2766 // An equivalent phi is a phi having the same dex register and type.
2767 // It assumes that phis with the same dex register are adjacent.
2768 HPhi* GetNextEquivalentPhiWithSameType() {
2769 HInstruction* next = GetNext();
2770 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2771 if (next->GetType() == GetType()) {
2772 return next->AsPhi();
2773 }
2774 next = next->GetNext();
2775 }
2776 return nullptr;
2777 }
2778
2779 DECLARE_INSTRUCTION(Phi);
2780
Artem Serovcced8ba2017-07-19 18:18:09 +01002781 protected:
2782 DEFAULT_COPY_CONSTRUCTOR(Phi);
2783
David Brazdildee58d62016-04-07 09:54:26 +00002784 private:
Vladimir Markobd785672018-05-03 17:09:09 +01002785 static constexpr size_t kFlagIsLive = HInstruction::kNumberOfGenericPackedBits;
David Brazdildee58d62016-04-07 09:54:26 +00002786 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2787 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2788 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
David Brazdildee58d62016-04-07 09:54:26 +00002789
David Brazdildee58d62016-04-07 09:54:26 +00002790 const uint32_t reg_number_;
David Brazdildee58d62016-04-07 09:54:26 +00002791};
2792
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002793// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002794// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002795// exit block.
Vladimir Markobd785672018-05-03 17:09:09 +01002796class HExit FINAL : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002797 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302798 explicit HExit(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002799 : HExpression(kExit, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302800 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002801
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002802 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002803
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002804 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002805
Artem Serovcced8ba2017-07-19 18:18:09 +01002806 protected:
2807 DEFAULT_COPY_CONSTRUCTOR(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002808};
2809
2810// Jumps from one block to another.
Vladimir Markobd785672018-05-03 17:09:09 +01002811class HGoto FINAL : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002812 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302813 explicit HGoto(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002814 : HExpression(kGoto, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302815 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002816
Artem Serovcced8ba2017-07-19 18:18:09 +01002817 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002818 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002819
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002820 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002821 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002822 }
2823
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002824 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002825
Artem Serovcced8ba2017-07-19 18:18:09 +01002826 protected:
2827 DEFAULT_COPY_CONSTRUCTOR(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002828};
2829
Roland Levillain9867bc72015-08-05 10:21:34 +01002830class HConstant : public HExpression<0> {
2831 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302832 explicit HConstant(InstructionKind kind, DataType::Type type, uint32_t dex_pc = kNoDexPc)
2833 : HExpression(kind, type, SideEffects::None(), dex_pc) {
2834 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002835
2836 bool CanBeMoved() const OVERRIDE { return true; }
2837
Roland Levillain1a653882016-03-18 18:05:57 +00002838 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002839 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002840 // Is this constant 0 in the arithmetic sense?
2841 virtual bool IsArithmeticZero() const { return false; }
2842 // Is this constant a 0-bit pattern?
2843 virtual bool IsZeroBitPattern() const { return false; }
2844 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002845 virtual bool IsOne() const { return false; }
2846
David Brazdil77a48ae2015-09-15 12:34:04 +00002847 virtual uint64_t GetValueAsUint64() const = 0;
2848
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002849 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002850
Artem Serovcced8ba2017-07-19 18:18:09 +01002851 protected:
2852 DEFAULT_COPY_CONSTRUCTOR(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002853};
2854
Vladimir Markofcb503c2016-05-18 12:48:17 +01002855class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002856 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002857 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002858 return true;
2859 }
2860
David Brazdil77a48ae2015-09-15 12:34:04 +00002861 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2862
Roland Levillain9867bc72015-08-05 10:21:34 +01002863 size_t ComputeHashCode() const OVERRIDE { return 0; }
2864
Roland Levillain1a653882016-03-18 18:05:57 +00002865 // The null constant representation is a 0-bit pattern.
2866 virtual bool IsZeroBitPattern() const { return true; }
2867
Roland Levillain9867bc72015-08-05 10:21:34 +01002868 DECLARE_INSTRUCTION(NullConstant);
2869
Artem Serovcced8ba2017-07-19 18:18:09 +01002870 protected:
2871 DEFAULT_COPY_CONSTRUCTOR(NullConstant);
2872
Roland Levillain9867bc72015-08-05 10:21:34 +01002873 private:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002874 explicit HNullConstant(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302875 : HConstant(kNullConstant, DataType::Type::kReference, dex_pc) {
2876 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002877
2878 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01002879};
2880
2881// Constants of the type int. Those can be from Dex instructions, or
2882// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002883class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002884 public:
2885 int32_t GetValue() const { return value_; }
2886
David Brazdil9f389d42015-10-01 14:32:56 +01002887 uint64_t GetValueAsUint64() const OVERRIDE {
2888 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2889 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002890
Vladimir Marko372f10e2016-05-17 16:30:10 +01002891 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002892 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002893 return other->AsIntConstant()->value_ == value_;
2894 }
2895
2896 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2897
2898 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002899 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2900 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002901 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2902
Roland Levillain1a653882016-03-18 18:05:57 +00002903 // Integer constants are used to encode Boolean values as well,
2904 // where 1 means true and 0 means false.
2905 bool IsTrue() const { return GetValue() == 1; }
2906 bool IsFalse() const { return GetValue() == 0; }
2907
Roland Levillain9867bc72015-08-05 10:21:34 +01002908 DECLARE_INSTRUCTION(IntConstant);
2909
Artem Serovcced8ba2017-07-19 18:18:09 +01002910 protected:
2911 DEFAULT_COPY_CONSTRUCTOR(IntConstant);
2912
Roland Levillain9867bc72015-08-05 10:21:34 +01002913 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002914 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302915 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc), value_(value) {
2916 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002917 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302918 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc),
2919 value_(value ? 1 : 0) {
2920 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002921
2922 const int32_t value_;
2923
2924 friend class HGraph;
2925 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2926 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Roland Levillain9867bc72015-08-05 10:21:34 +01002927};
2928
Vladimir Markofcb503c2016-05-18 12:48:17 +01002929class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002930 public:
2931 int64_t GetValue() const { return value_; }
2932
David Brazdil77a48ae2015-09-15 12:34:04 +00002933 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2934
Vladimir Marko372f10e2016-05-17 16:30:10 +01002935 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002936 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002937 return other->AsLongConstant()->value_ == value_;
2938 }
2939
2940 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2941
2942 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002943 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2944 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002945 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2946
2947 DECLARE_INSTRUCTION(LongConstant);
2948
Artem Serovcced8ba2017-07-19 18:18:09 +01002949 protected:
2950 DEFAULT_COPY_CONSTRUCTOR(LongConstant);
2951
Roland Levillain9867bc72015-08-05 10:21:34 +01002952 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002953 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302954 : HConstant(kLongConstant, DataType::Type::kInt64, dex_pc),
2955 value_(value) {
2956 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002957
2958 const int64_t value_;
2959
2960 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01002961};
Dave Allison20dfc792014-06-16 20:44:29 -07002962
Vladimir Markofcb503c2016-05-18 12:48:17 +01002963class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002964 public:
2965 float GetValue() const { return value_; }
2966
2967 uint64_t GetValueAsUint64() const OVERRIDE {
2968 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2969 }
2970
Vladimir Marko372f10e2016-05-17 16:30:10 +01002971 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002972 DCHECK(other->IsFloatConstant()) << other->DebugName();
2973 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2974 }
2975
2976 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2977
2978 bool IsMinusOne() const OVERRIDE {
2979 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2980 }
Roland Levillain1a653882016-03-18 18:05:57 +00002981 bool IsArithmeticZero() const OVERRIDE {
2982 return std::fpclassify(value_) == FP_ZERO;
2983 }
2984 bool IsArithmeticPositiveZero() const {
2985 return IsArithmeticZero() && !std::signbit(value_);
2986 }
2987 bool IsArithmeticNegativeZero() const {
2988 return IsArithmeticZero() && std::signbit(value_);
2989 }
2990 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002991 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002992 }
2993 bool IsOne() const OVERRIDE {
2994 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2995 }
2996 bool IsNaN() const {
2997 return std::isnan(value_);
2998 }
2999
3000 DECLARE_INSTRUCTION(FloatConstant);
3001
Artem Serovcced8ba2017-07-19 18:18:09 +01003002 protected:
3003 DEFAULT_COPY_CONSTRUCTOR(FloatConstant);
3004
Roland Levillain31dd3d62016-02-16 12:21:02 +00003005 private:
3006 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303007 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3008 value_(value) {
3009 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003010 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303011 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3012 value_(bit_cast<float, int32_t>(value)) {
3013 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003014
3015 const float value_;
3016
3017 // Only the SsaBuilder and HGraph can create floating-point constants.
3018 friend class SsaBuilder;
3019 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003020};
3021
Vladimir Markofcb503c2016-05-18 12:48:17 +01003022class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003023 public:
3024 double GetValue() const { return value_; }
3025
3026 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
3027
Vladimir Marko372f10e2016-05-17 16:30:10 +01003028 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003029 DCHECK(other->IsDoubleConstant()) << other->DebugName();
3030 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
3031 }
3032
3033 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
3034
3035 bool IsMinusOne() const OVERRIDE {
3036 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
3037 }
Roland Levillain1a653882016-03-18 18:05:57 +00003038 bool IsArithmeticZero() const OVERRIDE {
3039 return std::fpclassify(value_) == FP_ZERO;
3040 }
3041 bool IsArithmeticPositiveZero() const {
3042 return IsArithmeticZero() && !std::signbit(value_);
3043 }
3044 bool IsArithmeticNegativeZero() const {
3045 return IsArithmeticZero() && std::signbit(value_);
3046 }
3047 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003048 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00003049 }
3050 bool IsOne() const OVERRIDE {
3051 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3052 }
3053 bool IsNaN() const {
3054 return std::isnan(value_);
3055 }
3056
3057 DECLARE_INSTRUCTION(DoubleConstant);
3058
Artem Serovcced8ba2017-07-19 18:18:09 +01003059 protected:
3060 DEFAULT_COPY_CONSTRUCTOR(DoubleConstant);
3061
Roland Levillain31dd3d62016-02-16 12:21:02 +00003062 private:
3063 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303064 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3065 value_(value) {
3066 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003067 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303068 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3069 value_(bit_cast<double, int64_t>(value)) {
3070 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003071
3072 const double value_;
3073
3074 // Only the SsaBuilder and HGraph can create floating-point constants.
3075 friend class SsaBuilder;
3076 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003077};
3078
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003079// Conditional branch. A block ending with an HIf instruction must have
3080// two successors.
Vladimir Markobd785672018-05-03 17:09:09 +01003081class HIf FINAL : public HExpression<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003082 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003083 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003084 : HExpression(kIf, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003085 SetRawInputAt(0, input);
3086 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003087
Artem Serovcced8ba2017-07-19 18:18:09 +01003088 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003089 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003090
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003091 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003092 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003093 }
3094
3095 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003096 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003097 }
3098
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003099 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003100
Artem Serovcced8ba2017-07-19 18:18:09 +01003101 protected:
3102 DEFAULT_COPY_CONSTRUCTOR(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003103};
3104
David Brazdilfc6a86a2015-06-26 10:33:45 +00003105
3106// Abstract instruction which marks the beginning and/or end of a try block and
3107// links it to the respective exception handlers. Behaves the same as a Goto in
3108// non-exceptional control flow.
3109// Normal-flow successor is stored at index zero, exception handlers under
3110// higher indices in no particular order.
Vladimir Markobd785672018-05-03 17:09:09 +01003111class HTryBoundary FINAL : public HExpression<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003112 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003113 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01003114 kEntry,
3115 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003116 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01003117 };
3118
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003119 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003120 : HExpression(kTryBoundary, SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003121 SetPackedField<BoundaryKindField>(kind);
3122 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003123
3124 bool IsControlFlow() const OVERRIDE { return true; }
3125
3126 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01003127 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003128
David Brazdild26a4112015-11-10 11:07:31 +00003129 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
3130 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
3131 }
3132
David Brazdilfc6a86a2015-06-26 10:33:45 +00003133 // Returns whether `handler` is among its exception handlers (non-zero index
3134 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01003135 bool HasExceptionHandler(const HBasicBlock& handler) const {
3136 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00003137 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00003138 }
3139
3140 // If not present already, adds `handler` to its block's list of exception
3141 // handlers.
3142 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01003143 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003144 GetBlock()->AddSuccessor(handler);
3145 }
3146 }
3147
Vladimir Markoa1de9182016-02-25 11:37:38 +00003148 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
3149 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003150
David Brazdilffee3d32015-07-06 11:48:53 +01003151 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
3152
David Brazdilfc6a86a2015-06-26 10:33:45 +00003153 DECLARE_INSTRUCTION(TryBoundary);
3154
Artem Serovcced8ba2017-07-19 18:18:09 +01003155 protected:
3156 DEFAULT_COPY_CONSTRUCTOR(TryBoundary);
3157
David Brazdilfc6a86a2015-06-26 10:33:45 +00003158 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003159 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
3160 static constexpr size_t kFieldBoundaryKindSize =
3161 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
3162 static constexpr size_t kNumberOfTryBoundaryPackedBits =
3163 kFieldBoundaryKind + kFieldBoundaryKindSize;
3164 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
3165 "Too many packed fields.");
3166 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00003167};
3168
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003169// Deoptimize to interpreter, upon checking a condition.
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003170class HDeoptimize FINAL : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003171 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003172 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
3173 // across.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003174 HDeoptimize(ArenaAllocator* allocator,
3175 HInstruction* cond,
3176 DeoptimizationKind kind,
3177 uint32_t dex_pc)
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003178 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303179 kDeoptimize,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003180 SideEffects::All(),
3181 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003182 allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003183 /* number_of_inputs */ 1,
3184 kArenaAllocMisc) {
3185 SetPackedFlag<kFieldCanBeMoved>(false);
3186 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003187 SetRawInputAt(0, cond);
3188 }
3189
Artem Serovcced8ba2017-07-19 18:18:09 +01003190 bool IsClonable() const OVERRIDE { return true; }
3191
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003192 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
3193 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
3194 // instead of `guard`.
3195 // We set CanTriggerGC to prevent any intermediate address to be live
3196 // at the point of the `HDeoptimize`.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003197 HDeoptimize(ArenaAllocator* allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003198 HInstruction* cond,
3199 HInstruction* guard,
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003200 DeoptimizationKind kind,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003201 uint32_t dex_pc)
3202 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303203 kDeoptimize,
Vladimir Markobd785672018-05-03 17:09:09 +01003204 guard->GetType(),
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003205 SideEffects::CanTriggerGC(),
3206 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003207 allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003208 /* number_of_inputs */ 2,
3209 kArenaAllocMisc) {
3210 SetPackedFlag<kFieldCanBeMoved>(true);
3211 SetPackedField<DeoptimizeKindField>(kind);
3212 SetRawInputAt(0, cond);
3213 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003214 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003215
3216 bool CanBeMoved() const OVERRIDE { return GetPackedFlag<kFieldCanBeMoved>(); }
3217
3218 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
3219 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
3220 }
3221
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003222 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003223
Aart Bik75ff2c92018-04-21 01:28:11 +00003224 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003225
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003226 DeoptimizationKind GetDeoptimizationKind() const { return GetPackedField<DeoptimizeKindField>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003227
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003228 bool GuardsAnInput() const {
3229 return InputCount() == 2;
3230 }
3231
3232 HInstruction* GuardedInput() const {
3233 DCHECK(GuardsAnInput());
3234 return InputAt(1);
3235 }
3236
3237 void RemoveGuard() {
3238 RemoveInputAt(1);
3239 }
3240
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003241 DECLARE_INSTRUCTION(Deoptimize);
3242
Artem Serovcced8ba2017-07-19 18:18:09 +01003243 protected:
3244 DEFAULT_COPY_CONSTRUCTOR(Deoptimize);
3245
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003246 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003247 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3248 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3249 static constexpr size_t kFieldDeoptimizeKindSize =
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003250 MinimumBitsToStore(static_cast<size_t>(DeoptimizationKind::kLast));
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003251 static constexpr size_t kNumberOfDeoptimizePackedBits =
3252 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3253 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3254 "Too many packed fields.");
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003255 using DeoptimizeKindField =
3256 BitField<DeoptimizationKind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003257};
3258
Mingyao Yang063fc772016-08-02 11:02:54 -07003259// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3260// The compiled code checks this flag value in a guard before devirtualized call and
3261// if it's true, starts to do deoptimization.
3262// It has a 4-byte slot on stack.
3263// TODO: allocate a register for this flag.
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003264class HShouldDeoptimizeFlag FINAL : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003265 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003266 // CHA guards are only optimized in a separate pass and it has no side effects
3267 // with regard to other passes.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003268 HShouldDeoptimizeFlag(ArenaAllocator* allocator, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303269 : HVariableInputSizeInstruction(kShouldDeoptimizeFlag,
Vladimir Markobd785672018-05-03 17:09:09 +01003270 DataType::Type::kInt32,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303271 SideEffects::None(),
3272 dex_pc,
3273 allocator,
3274 0,
3275 kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003276 }
3277
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003278 // We do all CHA guard elimination/motion in a single pass, after which there is no
3279 // further guard elimination/motion since a guard might have been used for justification
3280 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3281 // to avoid other optimizations trying to move it.
Mingyao Yang063fc772016-08-02 11:02:54 -07003282 bool CanBeMoved() const OVERRIDE { return false; }
3283
3284 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3285
Artem Serovcced8ba2017-07-19 18:18:09 +01003286 protected:
3287 DEFAULT_COPY_CONSTRUCTOR(ShouldDeoptimizeFlag);
Mingyao Yang063fc772016-08-02 11:02:54 -07003288};
3289
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003290// Represents the ArtMethod that was passed as a first argument to
3291// the method. It is used by instructions that depend on it, like
3292// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003293class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003294 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003295 explicit HCurrentMethod(DataType::Type type, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303296 : HExpression(kCurrentMethod, type, SideEffects::None(), dex_pc) {
3297 }
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003298
3299 DECLARE_INSTRUCTION(CurrentMethod);
3300
Artem Serovcced8ba2017-07-19 18:18:09 +01003301 protected:
3302 DEFAULT_COPY_CONSTRUCTOR(CurrentMethod);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003303};
3304
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003305// Fetches an ArtMethod from the virtual table or the interface method table
3306// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003307class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003308 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003309 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003310 kVTable,
3311 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003312 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003313 };
3314 HClassTableGet(HInstruction* cls,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003315 DataType::Type type,
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003316 TableKind kind,
3317 size_t index,
3318 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303319 : HExpression(kClassTableGet, type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003320 index_(index) {
3321 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003322 SetRawInputAt(0, cls);
3323 }
3324
Artem Serovcced8ba2017-07-19 18:18:09 +01003325 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003326 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003327 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003328 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003329 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003330 }
3331
Vladimir Markoa1de9182016-02-25 11:37:38 +00003332 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003333 size_t GetIndex() const { return index_; }
3334
3335 DECLARE_INSTRUCTION(ClassTableGet);
3336
Artem Serovcced8ba2017-07-19 18:18:09 +01003337 protected:
3338 DEFAULT_COPY_CONSTRUCTOR(ClassTableGet);
3339
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003340 private:
Vladimir Markobd785672018-05-03 17:09:09 +01003341 static constexpr size_t kFieldTableKind = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003342 static constexpr size_t kFieldTableKindSize =
3343 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3344 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3345 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3346 "Too many packed fields.");
3347 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3348
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003349 // The index of the ArtMethod in the table.
3350 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003351};
3352
Mark Mendellfe57faa2015-09-18 09:26:15 -04003353// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3354// have one successor for each entry in the switch table, and the final successor
3355// will be the block containing the next Dex opcode.
Vladimir Markobd785672018-05-03 17:09:09 +01003356class HPackedSwitch FINAL : public HExpression<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003357 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003358 HPackedSwitch(int32_t start_value,
3359 uint32_t num_entries,
3360 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003361 uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003362 : HExpression(kPackedSwitch, SideEffects::None(), dex_pc),
Mark Mendellfe57faa2015-09-18 09:26:15 -04003363 start_value_(start_value),
3364 num_entries_(num_entries) {
3365 SetRawInputAt(0, input);
3366 }
3367
Artem Serovcced8ba2017-07-19 18:18:09 +01003368 bool IsClonable() const OVERRIDE { return true; }
3369
Mark Mendellfe57faa2015-09-18 09:26:15 -04003370 bool IsControlFlow() const OVERRIDE { return true; }
3371
3372 int32_t GetStartValue() const { return start_value_; }
3373
Vladimir Marko211c2112015-09-24 16:52:33 +01003374 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003375
3376 HBasicBlock* GetDefaultBlock() const {
3377 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003378 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003379 }
3380 DECLARE_INSTRUCTION(PackedSwitch);
3381
Artem Serovcced8ba2017-07-19 18:18:09 +01003382 protected:
3383 DEFAULT_COPY_CONSTRUCTOR(PackedSwitch);
3384
Mark Mendellfe57faa2015-09-18 09:26:15 -04003385 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003386 const int32_t start_value_;
3387 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003388};
3389
Roland Levillain88cb1752014-10-20 16:36:47 +01003390class HUnaryOperation : public HExpression<1> {
3391 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303392 HUnaryOperation(InstructionKind kind,
3393 DataType::Type result_type,
3394 HInstruction* input,
3395 uint32_t dex_pc = kNoDexPc)
3396 : HExpression(kind, result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003397 SetRawInputAt(0, input);
3398 }
3399
Artem Serovcced8ba2017-07-19 18:18:09 +01003400 // All of the UnaryOperation instructions are clonable.
3401 bool IsClonable() const OVERRIDE { return true; }
3402
Roland Levillain88cb1752014-10-20 16:36:47 +01003403 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003404 DataType::Type GetResultType() const { return GetType(); }
Roland Levillain88cb1752014-10-20 16:36:47 +01003405
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003406 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003407 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003408 return true;
3409 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003410
Roland Levillain31dd3d62016-02-16 12:21:02 +00003411 // Try to statically evaluate `this` and return a HConstant
3412 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003413 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003414 HConstant* TryStaticEvaluation() const;
3415
3416 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003417 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3418 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003419 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3420 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003421
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003422 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003423
Artem Serovcced8ba2017-07-19 18:18:09 +01003424 protected:
3425 DEFAULT_COPY_CONSTRUCTOR(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003426};
3427
Dave Allison20dfc792014-06-16 20:44:29 -07003428class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003429 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303430 HBinaryOperation(InstructionKind kind,
3431 DataType::Type result_type,
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003432 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003433 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003434 SideEffects side_effects = SideEffects::None(),
3435 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303436 : HExpression(kind, result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003437 SetRawInputAt(0, left);
3438 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003439 }
3440
Artem Serovcced8ba2017-07-19 18:18:09 +01003441 // All of the BinaryOperation instructions are clonable.
3442 bool IsClonable() const OVERRIDE { return true; }
3443
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003444 HInstruction* GetLeft() const { return InputAt(0); }
3445 HInstruction* GetRight() const { return InputAt(1); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003446 DataType::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003447
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003448 virtual bool IsCommutative() const { return false; }
3449
3450 // Put constant on the right.
3451 // Returns whether order is changed.
3452 bool OrderInputsWithConstantOnTheRight() {
3453 HInstruction* left = InputAt(0);
3454 HInstruction* right = InputAt(1);
3455 if (left->IsConstant() && !right->IsConstant()) {
3456 ReplaceInput(right, 0);
3457 ReplaceInput(left, 1);
3458 return true;
3459 }
3460 return false;
3461 }
3462
3463 // Order inputs by instruction id, but favor constant on the right side.
3464 // This helps GVN for commutative ops.
3465 void OrderInputs() {
3466 DCHECK(IsCommutative());
3467 HInstruction* left = InputAt(0);
3468 HInstruction* right = InputAt(1);
3469 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3470 return;
3471 }
3472 if (OrderInputsWithConstantOnTheRight()) {
3473 return;
3474 }
3475 // Order according to instruction id.
3476 if (left->GetId() > right->GetId()) {
3477 ReplaceInput(right, 0);
3478 ReplaceInput(left, 1);
3479 }
3480 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003481
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003482 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003483 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003484 return true;
3485 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003486
Roland Levillain31dd3d62016-02-16 12:21:02 +00003487 // Try to statically evaluate `this` and return a HConstant
3488 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003489 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003490 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003491
3492 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003493 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3494 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003495 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3496 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003497 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003498 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3499 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003500 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3501 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003502 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3503 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003504 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003505 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3506 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003507
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003508 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003509 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003510 HConstant* GetConstantRight() const;
3511
3512 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003513 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003514 HInstruction* GetLeastConstantLeft() const;
3515
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003516 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003517
Artem Serovcced8ba2017-07-19 18:18:09 +01003518 protected:
3519 DEFAULT_COPY_CONSTRUCTOR(BinaryOperation);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003520};
3521
Mark Mendellc4701932015-04-10 13:18:51 -04003522// The comparison bias applies for floating point operations and indicates how NaN
3523// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003524enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003525 kNoBias, // bias is not applicable (i.e. for long operation)
3526 kGtBias, // return 1 for NaN comparisons
3527 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003528 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003529};
3530
Roland Levillain31dd3d62016-02-16 12:21:02 +00003531std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3532
Dave Allison20dfc792014-06-16 20:44:29 -07003533class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003534 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303535 HCondition(InstructionKind kind,
3536 HInstruction* first,
3537 HInstruction* second,
3538 uint32_t dex_pc = kNoDexPc)
3539 : HBinaryOperation(kind,
3540 DataType::Type::kBool,
3541 first,
3542 second,
3543 SideEffects::None(),
3544 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003545 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3546 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003547
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003548 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003549 // `instruction`, and disregard moves in between.
3550 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003551
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003552 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003553
3554 virtual IfCondition GetCondition() const = 0;
3555
Mark Mendellc4701932015-04-10 13:18:51 -04003556 virtual IfCondition GetOppositeCondition() const = 0;
3557
Vladimir Markoa1de9182016-02-25 11:37:38 +00003558 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003559 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3560
Vladimir Markoa1de9182016-02-25 11:37:38 +00003561 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3562 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003563
Vladimir Marko372f10e2016-05-17 16:30:10 +01003564 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003565 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003566 }
3567
Roland Levillain4fa13f62015-07-06 18:11:54 +01003568 bool IsFPConditionTrueIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003569 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003570 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003571 if (if_cond == kCondNE) {
3572 return true;
3573 } else if (if_cond == kCondEQ) {
3574 return false;
3575 }
3576 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003577 }
3578
3579 bool IsFPConditionFalseIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003580 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003581 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003582 if (if_cond == kCondEQ) {
3583 return true;
3584 } else if (if_cond == kCondNE) {
3585 return false;
3586 }
3587 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003588 }
3589
Roland Levillain31dd3d62016-02-16 12:21:02 +00003590 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003591 // Needed if we merge a HCompare into a HCondition.
Vladimir Markobd785672018-05-03 17:09:09 +01003592 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003593 static constexpr size_t kFieldComparisonBiasSize =
3594 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3595 static constexpr size_t kNumberOfConditionPackedBits =
3596 kFieldComparisonBias + kFieldComparisonBiasSize;
3597 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3598 using ComparisonBiasField =
3599 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3600
Roland Levillain31dd3d62016-02-16 12:21:02 +00003601 template <typename T>
3602 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3603
3604 template <typename T>
3605 int32_t CompareFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003606 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003607 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3608 // Handle the bias.
3609 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3610 }
3611
3612 // Return an integer constant containing the result of a condition evaluated at compile time.
3613 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3614 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3615 }
3616
Artem Serovcced8ba2017-07-19 18:18:09 +01003617 DEFAULT_COPY_CONSTRUCTOR(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003618};
3619
3620// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003621class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003622 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003623 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303624 : HCondition(kEqual, first, second, dex_pc) {
3625 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003626
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003627 bool IsCommutative() const OVERRIDE { return true; }
3628
Vladimir Marko9e23df52015-11-10 17:14:35 +00003629 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3630 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003631 return MakeConstantCondition(true, GetDexPc());
3632 }
3633 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3634 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3635 }
3636 // In the following Evaluate methods, a HCompare instruction has
3637 // been merged into this HEqual instruction; evaluate it as
3638 // `Compare(x, y) == 0`.
3639 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3640 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3641 GetDexPc());
3642 }
3643 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3644 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3645 }
3646 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3647 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003648 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003649
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003650 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003651
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003652 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003653 return kCondEQ;
3654 }
3655
Mark Mendellc4701932015-04-10 13:18:51 -04003656 IfCondition GetOppositeCondition() const OVERRIDE {
3657 return kCondNE;
3658 }
3659
Artem Serovcced8ba2017-07-19 18:18:09 +01003660 protected:
3661 DEFAULT_COPY_CONSTRUCTOR(Equal);
3662
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003663 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003664 template <typename T> static bool Compute(T x, T y) { return x == y; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003665};
3666
Vladimir Markofcb503c2016-05-18 12:48:17 +01003667class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003668 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303669 HNotEqual(HInstruction* first, HInstruction* second,
3670 uint32_t dex_pc = kNoDexPc)
3671 : HCondition(kNotEqual, first, second, dex_pc) {
3672 }
Dave Allison20dfc792014-06-16 20:44:29 -07003673
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003674 bool IsCommutative() const OVERRIDE { return true; }
3675
Vladimir Marko9e23df52015-11-10 17:14:35 +00003676 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3677 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003678 return MakeConstantCondition(false, GetDexPc());
3679 }
3680 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3681 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3682 }
3683 // In the following Evaluate methods, a HCompare instruction has
3684 // been merged into this HNotEqual instruction; evaluate it as
3685 // `Compare(x, y) != 0`.
3686 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3687 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3688 }
3689 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3690 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3691 }
3692 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3693 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003694 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003695
Dave Allison20dfc792014-06-16 20:44:29 -07003696 DECLARE_INSTRUCTION(NotEqual);
3697
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003698 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003699 return kCondNE;
3700 }
3701
Mark Mendellc4701932015-04-10 13:18:51 -04003702 IfCondition GetOppositeCondition() const OVERRIDE {
3703 return kCondEQ;
3704 }
3705
Artem Serovcced8ba2017-07-19 18:18:09 +01003706 protected:
3707 DEFAULT_COPY_CONSTRUCTOR(NotEqual);
3708
Dave Allison20dfc792014-06-16 20:44:29 -07003709 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003710 template <typename T> static bool Compute(T x, T y) { return x != y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003711};
3712
Vladimir Markofcb503c2016-05-18 12:48:17 +01003713class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003714 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303715 HLessThan(HInstruction* first, HInstruction* second,
3716 uint32_t dex_pc = kNoDexPc)
3717 : HCondition(kLessThan, first, second, dex_pc) {
3718 }
Dave Allison20dfc792014-06-16 20:44:29 -07003719
Roland Levillain9867bc72015-08-05 10:21:34 +01003720 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003721 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003722 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003723 // In the following Evaluate methods, a HCompare instruction has
3724 // been merged into this HLessThan instruction; evaluate it as
3725 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003726 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003727 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3728 }
3729 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3730 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3731 }
3732 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3733 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003734 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003735
Dave Allison20dfc792014-06-16 20:44:29 -07003736 DECLARE_INSTRUCTION(LessThan);
3737
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003738 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003739 return kCondLT;
3740 }
3741
Mark Mendellc4701932015-04-10 13:18:51 -04003742 IfCondition GetOppositeCondition() const OVERRIDE {
3743 return kCondGE;
3744 }
3745
Artem Serovcced8ba2017-07-19 18:18:09 +01003746 protected:
3747 DEFAULT_COPY_CONSTRUCTOR(LessThan);
3748
Dave Allison20dfc792014-06-16 20:44:29 -07003749 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003750 template <typename T> static bool Compute(T x, T y) { return x < y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003751};
3752
Vladimir Markofcb503c2016-05-18 12:48:17 +01003753class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003754 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303755 HLessThanOrEqual(HInstruction* first, HInstruction* second,
3756 uint32_t dex_pc = kNoDexPc)
3757 : HCondition(kLessThanOrEqual, first, second, dex_pc) {
3758 }
Dave Allison20dfc792014-06-16 20:44:29 -07003759
Roland Levillain9867bc72015-08-05 10:21:34 +01003760 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003761 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003762 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003763 // In the following Evaluate methods, a HCompare instruction has
3764 // been merged into this HLessThanOrEqual instruction; evaluate it as
3765 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003766 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003767 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3768 }
3769 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3770 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3771 }
3772 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3773 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003774 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003775
Dave Allison20dfc792014-06-16 20:44:29 -07003776 DECLARE_INSTRUCTION(LessThanOrEqual);
3777
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003778 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003779 return kCondLE;
3780 }
3781
Mark Mendellc4701932015-04-10 13:18:51 -04003782 IfCondition GetOppositeCondition() const OVERRIDE {
3783 return kCondGT;
3784 }
3785
Artem Serovcced8ba2017-07-19 18:18:09 +01003786 protected:
3787 DEFAULT_COPY_CONSTRUCTOR(LessThanOrEqual);
3788
Dave Allison20dfc792014-06-16 20:44:29 -07003789 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003790 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003791};
3792
Vladimir Markofcb503c2016-05-18 12:48:17 +01003793class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003794 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003795 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303796 : HCondition(kGreaterThan, first, second, dex_pc) {
3797 }
Dave Allison20dfc792014-06-16 20:44:29 -07003798
Roland Levillain9867bc72015-08-05 10:21:34 +01003799 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003800 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003801 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003802 // In the following Evaluate methods, a HCompare instruction has
3803 // been merged into this HGreaterThan instruction; evaluate it as
3804 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003805 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003806 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3807 }
3808 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3809 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3810 }
3811 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3812 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003813 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003814
Dave Allison20dfc792014-06-16 20:44:29 -07003815 DECLARE_INSTRUCTION(GreaterThan);
3816
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003817 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003818 return kCondGT;
3819 }
3820
Mark Mendellc4701932015-04-10 13:18:51 -04003821 IfCondition GetOppositeCondition() const OVERRIDE {
3822 return kCondLE;
3823 }
3824
Artem Serovcced8ba2017-07-19 18:18:09 +01003825 protected:
3826 DEFAULT_COPY_CONSTRUCTOR(GreaterThan);
3827
Dave Allison20dfc792014-06-16 20:44:29 -07003828 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003829 template <typename T> static bool Compute(T x, T y) { return x > y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003830};
3831
Vladimir Markofcb503c2016-05-18 12:48:17 +01003832class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003833 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003834 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303835 : HCondition(kGreaterThanOrEqual, first, second, dex_pc) {
3836 }
Dave Allison20dfc792014-06-16 20:44:29 -07003837
Roland Levillain9867bc72015-08-05 10:21:34 +01003838 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003839 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003840 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003841 // In the following Evaluate methods, a HCompare instruction has
3842 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3843 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003844 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003845 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3846 }
3847 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3848 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3849 }
3850 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3851 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003852 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003853
Dave Allison20dfc792014-06-16 20:44:29 -07003854 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3855
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003856 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003857 return kCondGE;
3858 }
3859
Mark Mendellc4701932015-04-10 13:18:51 -04003860 IfCondition GetOppositeCondition() const OVERRIDE {
3861 return kCondLT;
3862 }
3863
Artem Serovcced8ba2017-07-19 18:18:09 +01003864 protected:
3865 DEFAULT_COPY_CONSTRUCTOR(GreaterThanOrEqual);
3866
Dave Allison20dfc792014-06-16 20:44:29 -07003867 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003868 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003869};
3870
Vladimir Markofcb503c2016-05-18 12:48:17 +01003871class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003872 public:
3873 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303874 : HCondition(kBelow, first, second, dex_pc) {
3875 }
Aart Bike9f37602015-10-09 11:15:55 -07003876
3877 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003878 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003879 }
3880 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003881 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3882 }
3883 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3884 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3885 LOG(FATAL) << DebugName() << " is not defined for float values";
3886 UNREACHABLE();
3887 }
3888 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3889 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3890 LOG(FATAL) << DebugName() << " is not defined for double values";
3891 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003892 }
3893
3894 DECLARE_INSTRUCTION(Below);
3895
3896 IfCondition GetCondition() const OVERRIDE {
3897 return kCondB;
3898 }
3899
3900 IfCondition GetOppositeCondition() const OVERRIDE {
3901 return kCondAE;
3902 }
3903
Artem Serovcced8ba2017-07-19 18:18:09 +01003904 protected:
3905 DEFAULT_COPY_CONSTRUCTOR(Below);
3906
Aart Bike9f37602015-10-09 11:15:55 -07003907 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003908 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003909 return MakeUnsigned(x) < MakeUnsigned(y);
3910 }
Aart Bike9f37602015-10-09 11:15:55 -07003911};
3912
Vladimir Markofcb503c2016-05-18 12:48:17 +01003913class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003914 public:
3915 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303916 : HCondition(kBelowOrEqual, first, second, dex_pc) {
3917 }
Aart Bike9f37602015-10-09 11:15:55 -07003918
3919 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003920 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003921 }
3922 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003923 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3924 }
3925 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3926 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3927 LOG(FATAL) << DebugName() << " is not defined for float values";
3928 UNREACHABLE();
3929 }
3930 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3931 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3932 LOG(FATAL) << DebugName() << " is not defined for double values";
3933 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003934 }
3935
3936 DECLARE_INSTRUCTION(BelowOrEqual);
3937
3938 IfCondition GetCondition() const OVERRIDE {
3939 return kCondBE;
3940 }
3941
3942 IfCondition GetOppositeCondition() const OVERRIDE {
3943 return kCondA;
3944 }
3945
Artem Serovcced8ba2017-07-19 18:18:09 +01003946 protected:
3947 DEFAULT_COPY_CONSTRUCTOR(BelowOrEqual);
3948
Aart Bike9f37602015-10-09 11:15:55 -07003949 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003950 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003951 return MakeUnsigned(x) <= MakeUnsigned(y);
3952 }
Aart Bike9f37602015-10-09 11:15:55 -07003953};
3954
Vladimir Markofcb503c2016-05-18 12:48:17 +01003955class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003956 public:
3957 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303958 : HCondition(kAbove, first, second, dex_pc) {
3959 }
Aart Bike9f37602015-10-09 11:15:55 -07003960
3961 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003962 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003963 }
3964 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003965 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3966 }
3967 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3968 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3969 LOG(FATAL) << DebugName() << " is not defined for float values";
3970 UNREACHABLE();
3971 }
3972 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3973 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3974 LOG(FATAL) << DebugName() << " is not defined for double values";
3975 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003976 }
3977
3978 DECLARE_INSTRUCTION(Above);
3979
3980 IfCondition GetCondition() const OVERRIDE {
3981 return kCondA;
3982 }
3983
3984 IfCondition GetOppositeCondition() const OVERRIDE {
3985 return kCondBE;
3986 }
3987
Artem Serovcced8ba2017-07-19 18:18:09 +01003988 protected:
3989 DEFAULT_COPY_CONSTRUCTOR(Above);
3990
Aart Bike9f37602015-10-09 11:15:55 -07003991 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003992 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003993 return MakeUnsigned(x) > MakeUnsigned(y);
3994 }
Aart Bike9f37602015-10-09 11:15:55 -07003995};
3996
Vladimir Markofcb503c2016-05-18 12:48:17 +01003997class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003998 public:
3999 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304000 : HCondition(kAboveOrEqual, first, second, dex_pc) {
4001 }
Aart Bike9f37602015-10-09 11:15:55 -07004002
4003 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004004 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004005 }
4006 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004007 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4008 }
4009 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4010 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4011 LOG(FATAL) << DebugName() << " is not defined for float values";
4012 UNREACHABLE();
4013 }
4014 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4015 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4016 LOG(FATAL) << DebugName() << " is not defined for double values";
4017 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004018 }
4019
4020 DECLARE_INSTRUCTION(AboveOrEqual);
4021
4022 IfCondition GetCondition() const OVERRIDE {
4023 return kCondAE;
4024 }
4025
4026 IfCondition GetOppositeCondition() const OVERRIDE {
4027 return kCondB;
4028 }
4029
Artem Serovcced8ba2017-07-19 18:18:09 +01004030 protected:
4031 DEFAULT_COPY_CONSTRUCTOR(AboveOrEqual);
4032
Aart Bike9f37602015-10-09 11:15:55 -07004033 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004034 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004035 return MakeUnsigned(x) >= MakeUnsigned(y);
4036 }
Aart Bike9f37602015-10-09 11:15:55 -07004037};
Dave Allison20dfc792014-06-16 20:44:29 -07004038
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004039// Instruction to check how two inputs compare to each other.
4040// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01004041class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004042 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00004043 // Note that `comparison_type` is the type of comparison performed
4044 // between the comparison's inputs, not the type of the instantiated
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004045 // HCompare instruction (which is always DataType::Type::kInt).
4046 HCompare(DataType::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004047 HInstruction* first,
4048 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04004049 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004050 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304051 : HBinaryOperation(kCompare,
4052 DataType::Type::kInt32,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004053 first,
4054 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00004055 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004056 dex_pc) {
4057 SetPackedField<ComparisonBiasField>(bias);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004058 DCHECK_EQ(comparison_type, DataType::Kind(first->GetType()));
4059 DCHECK_EQ(comparison_type, DataType::Kind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004060 }
4061
Roland Levillain9867bc72015-08-05 10:21:34 +01004062 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004063 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
4064
4065 template <typename T>
4066 int32_t ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004067 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004068 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
4069 // Handle the bias.
4070 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
4071 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004072
Roland Levillain9867bc72015-08-05 10:21:34 +01004073 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004074 // Note that there is no "cmp-int" Dex instruction so we shouldn't
4075 // reach this code path when processing a freshly built HIR
4076 // graph. However HCompare integer instructions can be synthesized
4077 // by the instruction simplifier to implement IntegerCompare and
4078 // IntegerSignum intrinsics, so we have to handle this case.
4079 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004080 }
4081 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004082 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4083 }
4084 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4085 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4086 }
4087 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4088 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01004089 }
4090
Vladimir Marko372f10e2016-05-17 16:30:10 +01004091 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004092 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00004093 }
4094
Vladimir Markoa1de9182016-02-25 11:37:38 +00004095 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04004096
Roland Levillain31dd3d62016-02-16 12:21:02 +00004097 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00004098 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00004099 bool IsGtBias() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004100 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004101 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00004102 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004103
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004104 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type type ATTRIBUTE_UNUSED) {
Roland Levillain1693a1f2016-03-15 14:57:31 +00004105 // Comparisons do not require a runtime call in any back end.
4106 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004107 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004108
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004109 DECLARE_INSTRUCTION(Compare);
4110
Roland Levillain31dd3d62016-02-16 12:21:02 +00004111 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01004112 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004113 static constexpr size_t kFieldComparisonBiasSize =
4114 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
4115 static constexpr size_t kNumberOfComparePackedBits =
4116 kFieldComparisonBias + kFieldComparisonBiasSize;
4117 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4118 using ComparisonBiasField =
4119 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
4120
Roland Levillain31dd3d62016-02-16 12:21:02 +00004121 // Return an integer constant containing the result of a comparison evaluated at compile time.
4122 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
4123 DCHECK(value == -1 || value == 0 || value == 1) << value;
4124 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
4125 }
4126
Artem Serovcced8ba2017-07-19 18:18:09 +01004127 DEFAULT_COPY_CONSTRUCTOR(Compare);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004128};
4129
Nicolas Geoffray2b615ba2017-01-06 14:40:07 +00004130class HNewInstance FINAL : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00004131 public:
4132 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00004133 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004134 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00004135 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00004136 bool finalizable,
4137 QuickEntrypointEnum entrypoint)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304138 : HExpression(kNewInstance,
4139 DataType::Type::kReference,
4140 SideEffects::CanTriggerGC(),
4141 dex_pc),
David Brazdil6de19382016-01-08 17:37:10 +00004142 type_index_(type_index),
4143 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00004144 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004145 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00004146 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00004147 }
4148
Artem Serovcced8ba2017-07-19 18:18:09 +01004149 bool IsClonable() const OVERRIDE { return true; }
4150
Andreas Gampea5b09a62016-11-17 15:21:22 -08004151 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00004152 const DexFile& GetDexFile() const { return dex_file_; }
4153
4154 // Calls runtime so needs an environment.
4155 bool NeedsEnvironment() const OVERRIDE { return true; }
4156
Mingyao Yang062157f2016-03-02 10:15:36 -08004157 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
4158 bool CanThrow() const OVERRIDE { return true; }
4159
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004160 bool NeedsChecks() const {
4161 return entrypoint_ == kQuickAllocObjectWithChecks;
4162 }
David Brazdil6de19382016-01-08 17:37:10 +00004163
Vladimir Markoa1de9182016-02-25 11:37:38 +00004164 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00004165
4166 bool CanBeNull() const OVERRIDE { return false; }
4167
4168 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4169
4170 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
4171 entrypoint_ = entrypoint;
4172 }
4173
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004174 HLoadClass* GetLoadClass() const {
4175 HInstruction* input = InputAt(0);
4176 if (input->IsClinitCheck()) {
4177 input = input->InputAt(0);
4178 }
4179 DCHECK(input->IsLoadClass());
4180 return input->AsLoadClass();
4181 }
4182
David Brazdil6de19382016-01-08 17:37:10 +00004183 bool IsStringAlloc() const;
4184
4185 DECLARE_INSTRUCTION(NewInstance);
4186
Artem Serovcced8ba2017-07-19 18:18:09 +01004187 protected:
4188 DEFAULT_COPY_CONSTRUCTOR(NewInstance);
4189
David Brazdil6de19382016-01-08 17:37:10 +00004190 private:
Vladimir Markobd785672018-05-03 17:09:09 +01004191 static constexpr size_t kFlagFinalizable = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004192 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
4193 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
4194 "Too many packed fields.");
4195
Andreas Gampea5b09a62016-11-17 15:21:22 -08004196 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00004197 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00004198 QuickEntrypointEnum entrypoint_;
David Brazdil6de19382016-01-08 17:37:10 +00004199};
4200
Agi Csaki05f20562015-08-19 14:58:14 -07004201enum IntrinsicNeedsEnvironmentOrCache {
4202 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
4203 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07004204};
4205
Aart Bik5d75afe2015-12-14 11:57:01 -08004206enum IntrinsicSideEffects {
4207 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
4208 kReadSideEffects, // Intrinsic may read heap memory.
4209 kWriteSideEffects, // Intrinsic may write heap memory.
4210 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
4211};
4212
4213enum IntrinsicExceptions {
4214 kNoThrow, // Intrinsic does not throw any exceptions.
4215 kCanThrow // Intrinsic may throw exceptions.
4216};
4217
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004218class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004219 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004220 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004221
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01004222 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004223 SetRawInputAt(index, argument);
4224 }
4225
Roland Levillain3e3d7332015-04-28 11:00:54 +01004226 // Return the number of arguments. This number can be lower than
4227 // the number of inputs returned by InputCount(), as some invoke
4228 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
4229 // inputs at the end of their list of inputs.
4230 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
4231
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004232 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4233
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004234 InvokeType GetInvokeType() const {
4235 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004236 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004237
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01004238 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004239 return intrinsic_;
4240 }
4241
Aart Bik5d75afe2015-12-14 11:57:01 -08004242 void SetIntrinsic(Intrinsics intrinsic,
4243 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
4244 IntrinsicSideEffects side_effects,
4245 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004246
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004247 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00004248 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004249 }
4250
Aart Bikff7d89c2016-11-07 08:49:28 -08004251 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4252
Vladimir Markoa1de9182016-02-25 11:37:38 +00004253 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004254
Aart Bika8b8e9b2018-01-09 11:01:02 -08004255 void SetAlwaysThrows(bool always_throws) { SetPackedFlag<kFlagAlwaysThrows>(always_throws); }
4256
4257 bool AlwaysThrows() const OVERRIDE { return GetPackedFlag<kFlagAlwaysThrows>(); }
4258
Aart Bik71bf7b42016-11-16 10:17:46 -08004259 bool CanBeMoved() const OVERRIDE { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004260
Vladimir Marko372f10e2016-05-17 16:30:10 +01004261 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08004262 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4263 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004264
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004265 uint32_t* GetIntrinsicOptimizations() {
4266 return &intrinsic_optimizations_;
4267 }
4268
4269 const uint32_t* GetIntrinsicOptimizations() const {
4270 return &intrinsic_optimizations_;
4271 }
4272
4273 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4274
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004275 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004276 void SetResolvedMethod(ArtMethod* method) { resolved_method_ = method; }
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004277
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004278 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004279
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004280 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004281 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4282 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004283 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
Vladimir Markobd785672018-05-03 17:09:09 +01004284 static constexpr size_t kFlagCanThrow = kFieldInvokeType + kFieldInvokeTypeSize;
Aart Bika8b8e9b2018-01-09 11:01:02 -08004285 static constexpr size_t kFlagAlwaysThrows = kFlagCanThrow + 1;
4286 static constexpr size_t kNumberOfInvokePackedBits = kFlagAlwaysThrows + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004287 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004288 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004289
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304290 HInvoke(InstructionKind kind,
4291 ArenaAllocator* allocator,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004292 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004293 uint32_t number_of_other_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004294 DataType::Type return_type,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004295 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004296 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004297 ArtMethod* resolved_method,
4298 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004299 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304300 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01004301 return_type,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004302 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4303 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004304 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004305 number_of_arguments + number_of_other_inputs,
4306 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004307 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004308 resolved_method_(resolved_method),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004309 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07004310 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004311 intrinsic_optimizations_(0) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004312 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004313 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004314 }
4315
Artem Serovcced8ba2017-07-19 18:18:09 +01004316 DEFAULT_COPY_CONSTRUCTOR(Invoke);
4317
Roland Levillain3e3d7332015-04-28 11:00:54 +01004318 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004319 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004320 const uint32_t dex_method_index_;
4321 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004322
4323 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4324 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004325};
4326
Vladimir Markofcb503c2016-05-18 12:48:17 +01004327class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004328 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004329 HInvokeUnresolved(ArenaAllocator* allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004330 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004331 DataType::Type return_type,
Calin Juravle175dc732015-08-25 15:42:32 +01004332 uint32_t dex_pc,
4333 uint32_t dex_method_index,
4334 InvokeType invoke_type)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304335 : HInvoke(kInvokeUnresolved,
4336 allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004337 number_of_arguments,
4338 0u /* number_of_other_inputs */,
4339 return_type,
4340 dex_pc,
4341 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004342 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01004343 invoke_type) {
4344 }
4345
Artem Serovcced8ba2017-07-19 18:18:09 +01004346 bool IsClonable() const OVERRIDE { return true; }
4347
Calin Juravle175dc732015-08-25 15:42:32 +01004348 DECLARE_INSTRUCTION(InvokeUnresolved);
4349
Artem Serovcced8ba2017-07-19 18:18:09 +01004350 protected:
4351 DEFAULT_COPY_CONSTRUCTOR(InvokeUnresolved);
Calin Juravle175dc732015-08-25 15:42:32 +01004352};
4353
Orion Hodsonac141392017-01-13 11:53:47 +00004354class HInvokePolymorphic FINAL : public HInvoke {
4355 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004356 HInvokePolymorphic(ArenaAllocator* allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004357 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004358 DataType::Type return_type,
Orion Hodsonac141392017-01-13 11:53:47 +00004359 uint32_t dex_pc,
4360 uint32_t dex_method_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304361 : HInvoke(kInvokePolymorphic,
4362 allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004363 number_of_arguments,
4364 0u /* number_of_other_inputs */,
4365 return_type,
4366 dex_pc,
4367 dex_method_index,
4368 nullptr,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304369 kVirtual) {
4370 }
Orion Hodsonac141392017-01-13 11:53:47 +00004371
Artem Serovcced8ba2017-07-19 18:18:09 +01004372 bool IsClonable() const OVERRIDE { return true; }
4373
Orion Hodsonac141392017-01-13 11:53:47 +00004374 DECLARE_INSTRUCTION(InvokePolymorphic);
4375
Artem Serovcced8ba2017-07-19 18:18:09 +01004376 protected:
4377 DEFAULT_COPY_CONSTRUCTOR(InvokePolymorphic);
Orion Hodsonac141392017-01-13 11:53:47 +00004378};
4379
Vladimir Markofcb503c2016-05-18 12:48:17 +01004380class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004381 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004382 // Requirements of this method call regarding the class
4383 // initialization (clinit) check of its declaring class.
4384 enum class ClinitCheckRequirement {
4385 kNone, // Class already initialized.
4386 kExplicit, // Static call having explicit clinit check as last input.
4387 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004388 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004389 };
4390
Vladimir Marko58155012015-08-19 12:49:41 +00004391 // Determines how to load the target ArtMethod*.
4392 enum class MethodLoadKind {
4393 // Use a String init ArtMethod* loaded from Thread entrypoints.
4394 kStringInit,
4395
4396 // Use the method's own ArtMethod* loaded by the register allocator.
4397 kRecursive,
4398
Vladimir Marko65979462017-05-19 17:25:12 +01004399 // Use PC-relative boot image ArtMethod* address that will be known at link time.
4400 // Used for boot image methods referenced by boot image code.
4401 kBootImageLinkTimePcRelative,
4402
Vladimir Marko58155012015-08-19 12:49:41 +00004403 // Use ArtMethod* at a known address, embed the direct address in the code.
4404 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
4405 kDirectAddress,
4406
Vladimir Markob066d432018-01-03 13:14:37 +00004407 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
4408 // Used for app->boot calls with relocatable image.
4409 kBootImageRelRo,
4410
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004411 // Load from an entry in the .bss section using a PC-relative load.
4412 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
4413 kBssEntry,
Vladimir Marko58155012015-08-19 12:49:41 +00004414
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004415 // Make a runtime call to resolve and call the method. This is the last-resort-kind
4416 // used when other kinds are unimplemented on a particular architecture.
4417 kRuntimeCall,
Vladimir Marko58155012015-08-19 12:49:41 +00004418 };
4419
4420 // Determines the location of the code pointer.
4421 enum class CodePtrLocation {
4422 // Recursive call, use local PC-relative call instruction.
4423 kCallSelf,
4424
Vladimir Marko58155012015-08-19 12:49:41 +00004425 // Use code pointer from the ArtMethod*.
4426 // Used when we don't know the target code. This is also the last-resort-kind used when
4427 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4428 kCallArtMethod,
4429 };
4430
4431 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004432 MethodLoadKind method_load_kind;
4433 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004434 // The method load data holds
4435 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4436 // Note that there are multiple string init methods, each having its own offset.
4437 // - the method address for kDirectAddress
Vladimir Markodc151b22015-10-15 18:02:30 +01004438 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004439 };
4440
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004441 HInvokeStaticOrDirect(ArenaAllocator* allocator,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004442 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004443 DataType::Type return_type,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004444 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004445 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004446 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004447 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004448 InvokeType invoke_type,
4449 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004450 ClinitCheckRequirement clinit_check_requirement)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304451 : HInvoke(kInvokeStaticOrDirect,
4452 allocator,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004453 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004454 // There is potentially one extra argument for the HCurrentMethod node, and
4455 // potentially one other if the clinit check is explicit, and potentially
4456 // one other if the method is a string factory.
4457 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004458 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004459 return_type,
4460 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004461 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004462 resolved_method,
4463 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004464 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004465 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004466 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4467 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004468
Artem Serovcced8ba2017-07-19 18:18:09 +01004469 bool IsClonable() const OVERRIDE { return true; }
4470
Vladimir Markodc151b22015-10-15 18:02:30 +01004471 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004472 bool had_current_method_input = HasCurrentMethodInput();
4473 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4474
4475 // Using the current method is the default and once we find a better
4476 // method load kind, we should not go back to using the current method.
4477 DCHECK(had_current_method_input || !needs_current_method_input);
4478
4479 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004480 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4481 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004482 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004483 dispatch_info_ = dispatch_info;
4484 }
4485
Aart Bik6daebeb2017-04-03 14:35:41 -07004486 DispatchInfo GetDispatchInfo() const {
4487 return dispatch_info_;
4488 }
4489
Vladimir Markoc53c0792015-11-19 15:48:33 +00004490 void AddSpecialInput(HInstruction* input) {
4491 // We allow only one special input.
4492 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4493 DCHECK(InputCount() == GetSpecialInputIndex() ||
4494 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4495 InsertInputAt(GetSpecialInputIndex(), input);
4496 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004497
Vladimir Marko372f10e2016-05-17 16:30:10 +01004498 using HInstruction::GetInputRecords; // Keep the const version visible.
4499 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
4500 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4501 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4502 DCHECK(!input_records.empty());
4503 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4504 HInstruction* last_input = input_records.back().GetInstruction();
4505 // Note: `last_input` may be null during arguments setup.
4506 if (last_input != nullptr) {
4507 // `last_input` is the last input of a static invoke marked as having
4508 // an explicit clinit check. It must either be:
4509 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4510 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4511 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4512 }
4513 }
4514 return input_records;
4515 }
4516
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004517 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004518 // We access the method via the dex cache so we can't do an implicit null check.
4519 // TODO: for intrinsics we can generate implicit null checks.
4520 return false;
4521 }
4522
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004523 bool CanBeNull() const OVERRIDE {
Vladimir Markobd785672018-05-03 17:09:09 +01004524 return GetType() == DataType::Type::kReference && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004525 }
4526
Vladimir Markoc53c0792015-11-19 15:48:33 +00004527 // Get the index of the special input, if any.
4528 //
David Brazdil6de19382016-01-08 17:37:10 +00004529 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4530 // method pointer; otherwise there may be one platform-specific special input,
4531 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004532 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004533 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004534
Vladimir Marko58155012015-08-19 12:49:41 +00004535 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4536 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4537 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004538 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004539 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004540 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko65979462017-05-19 17:25:12 +01004541 bool HasPcRelativeMethodLoadKind() const {
4542 return GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markob066d432018-01-03 13:14:37 +00004543 GetMethodLoadKind() == MethodLoadKind::kBootImageRelRo ||
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004544 GetMethodLoadKind() == MethodLoadKind::kBssEntry;
Vladimir Marko65979462017-05-19 17:25:12 +01004545 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004546 bool HasCurrentMethodInput() const {
4547 // This function can be called only after the invoke has been fully initialized by the builder.
4548 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004549 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004550 return true;
4551 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004552 DCHECK(InputCount() == GetSpecialInputIndex() ||
4553 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004554 return false;
4555 }
4556 }
Vladimir Marko58155012015-08-19 12:49:41 +00004557
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004558 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004559 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004560 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004561 }
4562
4563 uint64_t GetMethodAddress() const {
4564 DCHECK(HasMethodAddress());
4565 return dispatch_info_.method_load_data;
4566 }
4567
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004568 const DexFile& GetDexFileForPcRelativeDexCache() const;
4569
Vladimir Markoa1de9182016-02-25 11:37:38 +00004570 ClinitCheckRequirement GetClinitCheckRequirement() const {
4571 return GetPackedField<ClinitCheckRequirementField>();
4572 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004573
Roland Levillain4c0eb422015-04-24 16:43:49 +01004574 // Is this instruction a call to a static method?
4575 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004576 return GetInvokeType() == kStatic;
4577 }
4578
4579 MethodReference GetTargetMethod() const {
4580 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004581 }
4582
Vladimir Markofbb184a2015-11-13 14:47:00 +00004583 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4584 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4585 // instruction; only relevant for static calls with explicit clinit check.
4586 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004587 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004588 size_t last_input_index = inputs_.size() - 1u;
4589 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004590 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004591 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004592 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004593 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004594 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004595 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004596 }
4597
4598 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004599 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004600 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004601 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004602 }
4603
4604 // Is this a call to a static method whose declaring class has an
4605 // implicit intialization check requirement?
4606 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004607 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004608 }
4609
Vladimir Markob554b5a2015-11-06 12:57:55 +00004610 // Does this method load kind need the current method as an input?
4611 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004612 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kRuntimeCall;
Vladimir Markob554b5a2015-11-06 12:57:55 +00004613 }
4614
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004615 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004616
Artem Serovcced8ba2017-07-19 18:18:09 +01004617 protected:
4618 DEFAULT_COPY_CONSTRUCTOR(InvokeStaticOrDirect);
4619
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004620 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004621 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004622 static constexpr size_t kFieldClinitCheckRequirementSize =
4623 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4624 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4625 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4626 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4627 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004628 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4629 kFieldClinitCheckRequirement,
4630 kFieldClinitCheckRequirementSize>;
4631
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004632 // Cached values of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffrayea179f42018-02-08 22:30:18 +00004633 const MethodReference target_method_;
Vladimir Marko58155012015-08-19 12:49:41 +00004634 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004635};
Vladimir Markof64242a2015-12-01 14:58:23 +00004636std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004637std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004638
Vladimir Markofcb503c2016-05-18 12:48:17 +01004639class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004640 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004641 HInvokeVirtual(ArenaAllocator* allocator,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004642 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004643 DataType::Type return_type,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004644 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004645 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004646 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004647 uint32_t vtable_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304648 : HInvoke(kInvokeVirtual,
4649 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004650 number_of_arguments,
4651 0u,
4652 return_type,
4653 dex_pc,
4654 dex_method_index,
4655 resolved_method,
4656 kVirtual),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304657 vtable_index_(vtable_index) {
4658 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004659
Artem Serovcced8ba2017-07-19 18:18:09 +01004660 bool IsClonable() const OVERRIDE { return true; }
4661
Aart Bik71bf7b42016-11-16 10:17:46 -08004662 bool CanBeNull() const OVERRIDE {
4663 switch (GetIntrinsic()) {
4664 case Intrinsics::kThreadCurrentThread:
4665 case Intrinsics::kStringBufferAppend:
4666 case Intrinsics::kStringBufferToString:
4667 case Intrinsics::kStringBuilderAppend:
4668 case Intrinsics::kStringBuilderToString:
4669 return false;
4670 default:
4671 return HInvoke::CanBeNull();
4672 }
4673 }
4674
Calin Juravle641547a2015-04-21 22:08:51 +01004675 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004676 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004677 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004678 }
4679
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004680 uint32_t GetVTableIndex() const { return vtable_index_; }
4681
4682 DECLARE_INSTRUCTION(InvokeVirtual);
4683
Artem Serovcced8ba2017-07-19 18:18:09 +01004684 protected:
4685 DEFAULT_COPY_CONSTRUCTOR(InvokeVirtual);
4686
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004687 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004688 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004689 const uint32_t vtable_index_;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004690};
4691
Vladimir Markofcb503c2016-05-18 12:48:17 +01004692class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004693 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004694 HInvokeInterface(ArenaAllocator* allocator,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004695 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004696 DataType::Type return_type,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004697 uint32_t dex_pc,
4698 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004699 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004700 uint32_t imt_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304701 : HInvoke(kInvokeInterface,
4702 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004703 number_of_arguments,
4704 0u,
4705 return_type,
4706 dex_pc,
4707 dex_method_index,
4708 resolved_method,
4709 kInterface),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304710 imt_index_(imt_index) {
4711 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004712
Artem Serovcced8ba2017-07-19 18:18:09 +01004713 bool IsClonable() const OVERRIDE { return true; }
4714
Calin Juravle641547a2015-04-21 22:08:51 +01004715 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004716 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004717 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004718 }
4719
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004720 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
4721 // The assembly stub currently needs it.
4722 return true;
4723 }
4724
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004725 uint32_t GetImtIndex() const { return imt_index_; }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004726
4727 DECLARE_INSTRUCTION(InvokeInterface);
4728
Artem Serovcced8ba2017-07-19 18:18:09 +01004729 protected:
4730 DEFAULT_COPY_CONSTRUCTOR(InvokeInterface);
4731
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004732 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004733 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004734 const uint32_t imt_index_;
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004735};
4736
Vladimir Markofcb503c2016-05-18 12:48:17 +01004737class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004738 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004739 HNeg(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304740 : HUnaryOperation(kNeg, result_type, input, dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004741 DCHECK_EQ(result_type, DataType::Kind(input->GetType()));
Roland Levillain937e6cd2016-03-22 11:54:37 +00004742 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004743
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004744 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004745
4746 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004747 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004748 }
4749 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004750 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004751 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004752 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4753 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4754 }
4755 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4756 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4757 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004758
Roland Levillain88cb1752014-10-20 16:36:47 +01004759 DECLARE_INSTRUCTION(Neg);
4760
Artem Serovcced8ba2017-07-19 18:18:09 +01004761 protected:
4762 DEFAULT_COPY_CONSTRUCTOR(Neg);
Roland Levillain88cb1752014-10-20 16:36:47 +01004763};
4764
Vladimir Markofcb503c2016-05-18 12:48:17 +01004765class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004766 public:
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004767 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304768 : HExpression(kNewArray, DataType::Type::kReference, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004769 SetRawInputAt(0, cls);
4770 SetRawInputAt(1, length);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004771 }
4772
Artem Serovcced8ba2017-07-19 18:18:09 +01004773 bool IsClonable() const OVERRIDE { return true; }
4774
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004775 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004776 bool NeedsEnvironment() const OVERRIDE { return true; }
4777
Mingyao Yang0c365e62015-03-31 15:09:29 -07004778 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4779 bool CanThrow() const OVERRIDE { return true; }
4780
Calin Juravle10e244f2015-01-26 18:54:32 +00004781 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004782
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004783 HLoadClass* GetLoadClass() const {
4784 DCHECK(InputAt(0)->IsLoadClass());
4785 return InputAt(0)->AsLoadClass();
4786 }
4787
4788 HInstruction* GetLength() const {
4789 return InputAt(1);
4790 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004791
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004792 DECLARE_INSTRUCTION(NewArray);
4793
Artem Serovcced8ba2017-07-19 18:18:09 +01004794 protected:
4795 DEFAULT_COPY_CONSTRUCTOR(NewArray);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004796};
4797
Vladimir Markofcb503c2016-05-18 12:48:17 +01004798class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004799 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004800 HAdd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004801 HInstruction* left,
4802 HInstruction* right,
4803 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304804 : HBinaryOperation(kAdd, result_type, left, right, SideEffects::None(), dex_pc) {
4805 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004806
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004807 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004808
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004809 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004810
4811 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004812 return GetBlock()->GetGraph()->GetIntConstant(
4813 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004814 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004815 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004816 return GetBlock()->GetGraph()->GetLongConstant(
4817 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004818 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004819 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4820 return GetBlock()->GetGraph()->GetFloatConstant(
4821 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4822 }
4823 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4824 return GetBlock()->GetGraph()->GetDoubleConstant(
4825 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4826 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004827
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004828 DECLARE_INSTRUCTION(Add);
4829
Artem Serovcced8ba2017-07-19 18:18:09 +01004830 protected:
4831 DEFAULT_COPY_CONSTRUCTOR(Add);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004832};
4833
Vladimir Markofcb503c2016-05-18 12:48:17 +01004834class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004835 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004836 HSub(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004837 HInstruction* left,
4838 HInstruction* right,
4839 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304840 : HBinaryOperation(kSub, result_type, left, right, SideEffects::None(), dex_pc) {
4841 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004842
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004843 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004844
4845 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004846 return GetBlock()->GetGraph()->GetIntConstant(
4847 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004848 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004849 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004850 return GetBlock()->GetGraph()->GetLongConstant(
4851 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004852 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004853 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4854 return GetBlock()->GetGraph()->GetFloatConstant(
4855 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4856 }
4857 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4858 return GetBlock()->GetGraph()->GetDoubleConstant(
4859 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4860 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004861
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004862 DECLARE_INSTRUCTION(Sub);
4863
Artem Serovcced8ba2017-07-19 18:18:09 +01004864 protected:
4865 DEFAULT_COPY_CONSTRUCTOR(Sub);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004866};
4867
Vladimir Markofcb503c2016-05-18 12:48:17 +01004868class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004869 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004870 HMul(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004871 HInstruction* left,
4872 HInstruction* right,
4873 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304874 : HBinaryOperation(kMul, result_type, left, right, SideEffects::None(), dex_pc) {
4875 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004876
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004877 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004878
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004879 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004880
4881 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004882 return GetBlock()->GetGraph()->GetIntConstant(
4883 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004884 }
4885 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004886 return GetBlock()->GetGraph()->GetLongConstant(
4887 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004888 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004889 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4890 return GetBlock()->GetGraph()->GetFloatConstant(
4891 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4892 }
4893 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4894 return GetBlock()->GetGraph()->GetDoubleConstant(
4895 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4896 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004897
4898 DECLARE_INSTRUCTION(Mul);
4899
Artem Serovcced8ba2017-07-19 18:18:09 +01004900 protected:
4901 DEFAULT_COPY_CONSTRUCTOR(Mul);
Calin Juravle34bacdf2014-10-07 20:23:36 +01004902};
4903
Vladimir Markofcb503c2016-05-18 12:48:17 +01004904class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004905 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004906 HDiv(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004907 HInstruction* left,
4908 HInstruction* right,
4909 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304910 : HBinaryOperation(kDiv, result_type, left, right, SideEffects::None(), dex_pc) {
4911 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004912
Roland Levillain9867bc72015-08-05 10:21:34 +01004913 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004914 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004915 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004916 // Our graph structure ensures we never have 0 for `y` during
4917 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004918 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004919 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004920 return (y == -1) ? -x : x / y;
4921 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004922
Roland Levillain31dd3d62016-02-16 12:21:02 +00004923 template <typename T>
4924 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004925 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004926 return x / y;
4927 }
4928
Roland Levillain9867bc72015-08-05 10:21:34 +01004929 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004930 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004931 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004932 }
4933 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004934 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004935 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4936 }
4937 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4938 return GetBlock()->GetGraph()->GetFloatConstant(
4939 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4940 }
4941 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4942 return GetBlock()->GetGraph()->GetDoubleConstant(
4943 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004944 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004945
4946 DECLARE_INSTRUCTION(Div);
4947
Artem Serovcced8ba2017-07-19 18:18:09 +01004948 protected:
4949 DEFAULT_COPY_CONSTRUCTOR(Div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00004950};
4951
Vladimir Markofcb503c2016-05-18 12:48:17 +01004952class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004953 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004954 HRem(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004955 HInstruction* left,
4956 HInstruction* right,
4957 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304958 : HBinaryOperation(kRem, result_type, left, right, SideEffects::None(), dex_pc) {
4959 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004960
Roland Levillain9867bc72015-08-05 10:21:34 +01004961 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004962 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004963 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004964 // Our graph structure ensures we never have 0 for `y` during
4965 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004966 DCHECK_NE(y, 0);
4967 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4968 return (y == -1) ? 0 : x % y;
4969 }
4970
Roland Levillain31dd3d62016-02-16 12:21:02 +00004971 template <typename T>
4972 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004973 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004974 return std::fmod(x, y);
4975 }
4976
Roland Levillain9867bc72015-08-05 10:21:34 +01004977 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004978 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004979 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004980 }
4981 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004982 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004983 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004984 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004985 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4986 return GetBlock()->GetGraph()->GetFloatConstant(
4987 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4988 }
4989 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4990 return GetBlock()->GetGraph()->GetDoubleConstant(
4991 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4992 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004993
4994 DECLARE_INSTRUCTION(Rem);
4995
Artem Serovcced8ba2017-07-19 18:18:09 +01004996 protected:
4997 DEFAULT_COPY_CONSTRUCTOR(Rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00004998};
4999
Aart Bik1f8d51b2018-02-15 10:42:37 -08005000class HMin FINAL : public HBinaryOperation {
5001 public:
5002 HMin(DataType::Type result_type,
5003 HInstruction* left,
5004 HInstruction* right,
5005 uint32_t dex_pc)
5006 : HBinaryOperation(kMin, result_type, left, right, SideEffects::None(), dex_pc) {}
5007
5008 bool IsCommutative() const OVERRIDE { return true; }
5009
5010 // Evaluation for integral values.
5011 template <typename T> static T ComputeIntegral(T x, T y) {
5012 return (x <= y) ? x : y;
5013 }
5014
5015 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
5016 return GetBlock()->GetGraph()->GetIntConstant(
5017 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5018 }
5019 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
5020 return GetBlock()->GetGraph()->GetLongConstant(
5021 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5022 }
5023 // TODO: Evaluation for floating-point values.
5024 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5025 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { return nullptr; }
5026 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5027 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { return nullptr; }
5028
5029 DECLARE_INSTRUCTION(Min);
5030
5031 protected:
5032 DEFAULT_COPY_CONSTRUCTOR(Min);
5033};
5034
5035class HMax FINAL : public HBinaryOperation {
5036 public:
5037 HMax(DataType::Type result_type,
5038 HInstruction* left,
5039 HInstruction* right,
5040 uint32_t dex_pc)
5041 : HBinaryOperation(kMax, result_type, left, right, SideEffects::None(), dex_pc) {}
5042
5043 bool IsCommutative() const OVERRIDE { return true; }
5044
5045 // Evaluation for integral values.
5046 template <typename T> static T ComputeIntegral(T x, T y) {
5047 return (x >= y) ? x : y;
5048 }
5049
5050 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
5051 return GetBlock()->GetGraph()->GetIntConstant(
5052 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5053 }
5054 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
5055 return GetBlock()->GetGraph()->GetLongConstant(
5056 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5057 }
5058 // TODO: Evaluation for floating-point values.
5059 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5060 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { return nullptr; }
5061 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5062 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { return nullptr; }
5063
5064 DECLARE_INSTRUCTION(Max);
5065
5066 protected:
5067 DEFAULT_COPY_CONSTRUCTOR(Max);
5068};
5069
Aart Bik3dad3412018-02-28 12:01:46 -08005070class HAbs FINAL : public HUnaryOperation {
5071 public:
5072 HAbs(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
5073 : HUnaryOperation(kAbs, result_type, input, dex_pc) {}
5074
5075 // Evaluation for integral values.
5076 template <typename T> static T ComputeIntegral(T x) {
5077 return x < 0 ? -x : x;
5078 }
5079
5080 // Evaluation for floating-point values.
5081 // Note, as a "quality of implementation", rather than pure "spec compliance",
5082 // we require that Math.abs() clears the sign bit (but changes nothing else)
5083 // for all floating-point numbers, including NaN (signaling NaN may become quiet though).
5084 // http://b/30758343
5085 template <typename T, typename S> static T ComputeFP(T x) {
5086 S bits = bit_cast<S, T>(x);
5087 return bit_cast<T, S>(bits & std::numeric_limits<S>::max());
5088 }
5089
5090 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
5091 return GetBlock()->GetGraph()->GetIntConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5092 }
5093 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
5094 return GetBlock()->GetGraph()->GetLongConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5095 }
5096 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
5097 return GetBlock()->GetGraph()->GetFloatConstant(
5098 ComputeFP<float, int32_t>(x->GetValue()), GetDexPc());
5099 }
5100 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
5101 return GetBlock()->GetGraph()->GetDoubleConstant(
5102 ComputeFP<double, int64_t>(x->GetValue()), GetDexPc());
5103 }
5104
5105 DECLARE_INSTRUCTION(Abs);
5106
5107 protected:
5108 DEFAULT_COPY_CONSTRUCTOR(Abs);
5109};
5110
Vladimir Markofcb503c2016-05-18 12:48:17 +01005111class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00005112 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00005113 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
5114 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00005115 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305116 : HExpression(kDivZeroCheck, value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00005117 SetRawInputAt(0, value);
5118 }
5119
5120 bool CanBeMoved() const OVERRIDE { return true; }
5121
Vladimir Marko372f10e2016-05-17 16:30:10 +01005122 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00005123 return true;
5124 }
5125
5126 bool NeedsEnvironment() const OVERRIDE { return true; }
5127 bool CanThrow() const OVERRIDE { return true; }
5128
Calin Juravled0d48522014-11-04 16:40:20 +00005129 DECLARE_INSTRUCTION(DivZeroCheck);
5130
Artem Serovcced8ba2017-07-19 18:18:09 +01005131 protected:
5132 DEFAULT_COPY_CONSTRUCTOR(DivZeroCheck);
Calin Juravled0d48522014-11-04 16:40:20 +00005133};
5134
Vladimir Markofcb503c2016-05-18 12:48:17 +01005135class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005136 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005137 HShl(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005138 HInstruction* value,
5139 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005140 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305141 : HBinaryOperation(kShl, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005142 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5143 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005144 }
5145
Roland Levillain5b5b9312016-03-22 14:57:31 +00005146 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005147 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005148 return value << (distance & max_shift_distance);
5149 }
5150
5151 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005152 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005153 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005154 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005155 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005156 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005157 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005158 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005159 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5160 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5161 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5162 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005163 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005164 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5165 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005166 LOG(FATAL) << DebugName() << " is not defined for float values";
5167 UNREACHABLE();
5168 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005169 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5170 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005171 LOG(FATAL) << DebugName() << " is not defined for double values";
5172 UNREACHABLE();
5173 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005174
5175 DECLARE_INSTRUCTION(Shl);
5176
Artem Serovcced8ba2017-07-19 18:18:09 +01005177 protected:
5178 DEFAULT_COPY_CONSTRUCTOR(Shl);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005179};
5180
Vladimir Markofcb503c2016-05-18 12:48:17 +01005181class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005182 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005183 HShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005184 HInstruction* value,
5185 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005186 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305187 : HBinaryOperation(kShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005188 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5189 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005190 }
5191
Roland Levillain5b5b9312016-03-22 14:57:31 +00005192 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005193 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005194 return value >> (distance & max_shift_distance);
5195 }
5196
5197 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005198 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005199 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005200 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005201 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005202 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005203 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005204 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005205 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5206 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5207 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5208 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005209 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005210 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5211 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005212 LOG(FATAL) << DebugName() << " is not defined for float values";
5213 UNREACHABLE();
5214 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005215 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5216 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005217 LOG(FATAL) << DebugName() << " is not defined for double values";
5218 UNREACHABLE();
5219 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005220
5221 DECLARE_INSTRUCTION(Shr);
5222
Artem Serovcced8ba2017-07-19 18:18:09 +01005223 protected:
5224 DEFAULT_COPY_CONSTRUCTOR(Shr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005225};
5226
Vladimir Markofcb503c2016-05-18 12:48:17 +01005227class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005228 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005229 HUShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005230 HInstruction* value,
5231 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005232 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305233 : HBinaryOperation(kUShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005234 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5235 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00005236 }
5237
Roland Levillain5b5b9312016-03-22 14:57:31 +00005238 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005239 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005240 typedef typename std::make_unsigned<T>::type V;
5241 V ux = static_cast<V>(value);
5242 return static_cast<T>(ux >> (distance & max_shift_distance));
5243 }
5244
5245 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005246 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005247 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005248 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005249 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005250 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005251 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005252 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005253 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5254 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5255 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5256 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00005257 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005258 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5259 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005260 LOG(FATAL) << DebugName() << " is not defined for float values";
5261 UNREACHABLE();
5262 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005263 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5264 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005265 LOG(FATAL) << DebugName() << " is not defined for double values";
5266 UNREACHABLE();
5267 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005268
5269 DECLARE_INSTRUCTION(UShr);
5270
Artem Serovcced8ba2017-07-19 18:18:09 +01005271 protected:
5272 DEFAULT_COPY_CONSTRUCTOR(UShr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005273};
5274
Vladimir Markofcb503c2016-05-18 12:48:17 +01005275class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005276 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005277 HAnd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005278 HInstruction* left,
5279 HInstruction* right,
5280 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305281 : HBinaryOperation(kAnd, result_type, left, right, SideEffects::None(), dex_pc) {
5282 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005283
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005284 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005285
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005286 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005287
5288 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005289 return GetBlock()->GetGraph()->GetIntConstant(
5290 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005291 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005292 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005293 return GetBlock()->GetGraph()->GetLongConstant(
5294 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005295 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005296 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5297 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5298 LOG(FATAL) << DebugName() << " is not defined for float values";
5299 UNREACHABLE();
5300 }
5301 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5302 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5303 LOG(FATAL) << DebugName() << " is not defined for double values";
5304 UNREACHABLE();
5305 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005306
5307 DECLARE_INSTRUCTION(And);
5308
Artem Serovcced8ba2017-07-19 18:18:09 +01005309 protected:
5310 DEFAULT_COPY_CONSTRUCTOR(And);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005311};
5312
Vladimir Markofcb503c2016-05-18 12:48:17 +01005313class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005314 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005315 HOr(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005316 HInstruction* left,
5317 HInstruction* right,
5318 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305319 : HBinaryOperation(kOr, result_type, left, right, SideEffects::None(), dex_pc) {
5320 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005321
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005322 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005323
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005324 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005325
5326 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005327 return GetBlock()->GetGraph()->GetIntConstant(
5328 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005329 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005330 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005331 return GetBlock()->GetGraph()->GetLongConstant(
5332 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005333 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005334 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5335 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5336 LOG(FATAL) << DebugName() << " is not defined for float values";
5337 UNREACHABLE();
5338 }
5339 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5340 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5341 LOG(FATAL) << DebugName() << " is not defined for double values";
5342 UNREACHABLE();
5343 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005344
5345 DECLARE_INSTRUCTION(Or);
5346
Artem Serovcced8ba2017-07-19 18:18:09 +01005347 protected:
5348 DEFAULT_COPY_CONSTRUCTOR(Or);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005349};
5350
Vladimir Markofcb503c2016-05-18 12:48:17 +01005351class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005352 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005353 HXor(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005354 HInstruction* left,
5355 HInstruction* right,
5356 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305357 : HBinaryOperation(kXor, result_type, left, right, SideEffects::None(), dex_pc) {
5358 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005359
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005360 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005361
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005362 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005363
5364 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005365 return GetBlock()->GetGraph()->GetIntConstant(
5366 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005367 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005368 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005369 return GetBlock()->GetGraph()->GetLongConstant(
5370 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005371 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005372 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5373 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5374 LOG(FATAL) << DebugName() << " is not defined for float values";
5375 UNREACHABLE();
5376 }
5377 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5378 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5379 LOG(FATAL) << DebugName() << " is not defined for double values";
5380 UNREACHABLE();
5381 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005382
5383 DECLARE_INSTRUCTION(Xor);
5384
Artem Serovcced8ba2017-07-19 18:18:09 +01005385 protected:
5386 DEFAULT_COPY_CONSTRUCTOR(Xor);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005387};
5388
Vladimir Markofcb503c2016-05-18 12:48:17 +01005389class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005390 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005391 HRor(DataType::Type result_type, HInstruction* value, HInstruction* distance)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305392 : HBinaryOperation(kRor, result_type, value, distance) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005393 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5394 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00005395 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005396
Roland Levillain5b5b9312016-03-22 14:57:31 +00005397 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005398 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005399 typedef typename std::make_unsigned<T>::type V;
5400 V ux = static_cast<V>(value);
5401 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005402 return static_cast<T>(ux);
5403 } else {
5404 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005405 return static_cast<T>(ux >> (distance & max_shift_value)) |
5406 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005407 }
5408 }
5409
Roland Levillain5b5b9312016-03-22 14:57:31 +00005410 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005411 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005412 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005413 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005414 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005415 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005416 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005417 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005418 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5419 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5420 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5421 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005422 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005423 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5424 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005425 LOG(FATAL) << DebugName() << " is not defined for float values";
5426 UNREACHABLE();
5427 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005428 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5429 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005430 LOG(FATAL) << DebugName() << " is not defined for double values";
5431 UNREACHABLE();
5432 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005433
5434 DECLARE_INSTRUCTION(Ror);
5435
Artem Serovcced8ba2017-07-19 18:18:09 +01005436 protected:
5437 DEFAULT_COPY_CONSTRUCTOR(Ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005438};
5439
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005440// The value of a parameter in this method. Its location depends on
5441// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01005442class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005443 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005444 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005445 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005446 uint8_t index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005447 DataType::Type parameter_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005448 bool is_this = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305449 : HExpression(kParameterValue, parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005450 dex_file_(dex_file),
5451 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005452 index_(index) {
5453 SetPackedFlag<kFlagIsThis>(is_this);
5454 SetPackedFlag<kFlagCanBeNull>(!is_this);
5455 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005456
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005457 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005458 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005459 uint8_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07005460 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005461
Vladimir Markoa1de9182016-02-25 11:37:38 +00005462 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
5463 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005464
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005465 DECLARE_INSTRUCTION(ParameterValue);
5466
Artem Serovcced8ba2017-07-19 18:18:09 +01005467 protected:
5468 DEFAULT_COPY_CONSTRUCTOR(ParameterValue);
5469
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005470 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005471 // Whether or not the parameter value corresponds to 'this' argument.
Vladimir Markobd785672018-05-03 17:09:09 +01005472 static constexpr size_t kFlagIsThis = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005473 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5474 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5475 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5476 "Too many packed fields.");
5477
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005478 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005479 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005480 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005481 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005482 const uint8_t index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005483};
5484
Vladimir Markofcb503c2016-05-18 12:48:17 +01005485class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005486 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005487 HNot(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305488 : HUnaryOperation(kNot, result_type, input, dex_pc) {
5489 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005490
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005491 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005492 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005493 return true;
5494 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005495
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005496 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005497
5498 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005499 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005500 }
5501 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005502 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005503 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005504 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5505 LOG(FATAL) << DebugName() << " is not defined for float values";
5506 UNREACHABLE();
5507 }
5508 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5509 LOG(FATAL) << DebugName() << " is not defined for double values";
5510 UNREACHABLE();
5511 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005512
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005513 DECLARE_INSTRUCTION(Not);
5514
Artem Serovcced8ba2017-07-19 18:18:09 +01005515 protected:
5516 DEFAULT_COPY_CONSTRUCTOR(Not);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005517};
5518
Vladimir Markofcb503c2016-05-18 12:48:17 +01005519class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005520 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005521 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305522 : HUnaryOperation(kBooleanNot, DataType::Type::kBool, input, dex_pc) {
5523 }
David Brazdil66d126e2015-04-03 16:02:44 +01005524
5525 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005526 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01005527 return true;
5528 }
5529
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005530 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005531 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005532 return !x;
5533 }
5534
Roland Levillain9867bc72015-08-05 10:21:34 +01005535 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005536 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005537 }
5538 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5539 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005540 UNREACHABLE();
5541 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005542 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5543 LOG(FATAL) << DebugName() << " is not defined for float values";
5544 UNREACHABLE();
5545 }
5546 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5547 LOG(FATAL) << DebugName() << " is not defined for double values";
5548 UNREACHABLE();
5549 }
David Brazdil66d126e2015-04-03 16:02:44 +01005550
5551 DECLARE_INSTRUCTION(BooleanNot);
5552
Artem Serovcced8ba2017-07-19 18:18:09 +01005553 protected:
5554 DEFAULT_COPY_CONSTRUCTOR(BooleanNot);
David Brazdil66d126e2015-04-03 16:02:44 +01005555};
5556
Vladimir Markofcb503c2016-05-18 12:48:17 +01005557class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005558 public:
5559 // Instantiate a type conversion of `input` to `result_type`.
Vladimir Markoc8fb2112017-10-03 11:37:52 +01005560 HTypeConversion(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305561 : HExpression(kTypeConversion, result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005562 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005563 // Invariant: We should never generate a conversion to a Boolean value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005564 DCHECK_NE(DataType::Type::kBool, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005565 }
5566
5567 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005568 DataType::Type GetInputType() const { return GetInput()->GetType(); }
5569 DataType::Type GetResultType() const { return GetType(); }
Roland Levillaindff1f282014-11-05 14:15:05 +00005570
5571 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005572 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5573 return true;
5574 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005575
Mark Mendelle82549b2015-05-06 10:55:34 -04005576 // Try to statically evaluate the conversion and return a HConstant
5577 // containing the result. If the input cannot be converted, return nullptr.
5578 HConstant* TryStaticEvaluation() const;
5579
Roland Levillaindff1f282014-11-05 14:15:05 +00005580 DECLARE_INSTRUCTION(TypeConversion);
5581
Artem Serovcced8ba2017-07-19 18:18:09 +01005582 protected:
5583 DEFAULT_COPY_CONSTRUCTOR(TypeConversion);
Roland Levillaindff1f282014-11-05 14:15:05 +00005584};
5585
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005586static constexpr uint32_t kNoRegNumber = -1;
5587
Vladimir Markofcb503c2016-05-18 12:48:17 +01005588class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005589 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005590 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
5591 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005592 HNullCheck(HInstruction* value, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305593 : HExpression(kNullCheck, value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005594 SetRawInputAt(0, value);
5595 }
5596
Artem Serovcced8ba2017-07-19 18:18:09 +01005597 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00005598 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005599 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005600 return true;
5601 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005602
Calin Juravle10e244f2015-01-26 18:54:32 +00005603 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005604
Calin Juravle10e244f2015-01-26 18:54:32 +00005605 bool CanThrow() const OVERRIDE { return true; }
5606
5607 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005608
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005609 DECLARE_INSTRUCTION(NullCheck);
5610
Artem Serovcced8ba2017-07-19 18:18:09 +01005611 protected:
5612 DEFAULT_COPY_CONSTRUCTOR(NullCheck);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005613};
5614
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005615// Embeds an ArtField and all the information required by the compiler. We cache
5616// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005617class FieldInfo : public ValueObject {
5618 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005619 FieldInfo(ArtField* field,
5620 MemberOffset field_offset,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005621 DataType::Type field_type,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005622 bool is_volatile,
5623 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005624 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005625 const DexFile& dex_file)
5626 : field_(field),
5627 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005628 field_type_(field_type),
5629 is_volatile_(is_volatile),
5630 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005631 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005632 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005633
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005634 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005635 MemberOffset GetFieldOffset() const { return field_offset_; }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005636 DataType::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005637 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005638 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005639 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005640 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005641
5642 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005643 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005644 const MemberOffset field_offset_;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005645 const DataType::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005646 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005647 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005648 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005649 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005650};
5651
Vladimir Markofcb503c2016-05-18 12:48:17 +01005652class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005653 public:
5654 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005655 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005656 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005657 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005658 bool is_volatile,
5659 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005660 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005661 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005662 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305663 : HExpression(kInstanceFieldGet,
5664 field_type,
5665 SideEffects::FieldReadOfType(field_type, is_volatile),
5666 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005667 field_info_(field,
5668 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005669 field_type,
5670 is_volatile,
5671 field_idx,
5672 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005673 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005674 SetRawInputAt(0, value);
5675 }
5676
Artem Serovcced8ba2017-07-19 18:18:09 +01005677 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005678 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005679
Vladimir Marko372f10e2016-05-17 16:30:10 +01005680 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5681 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005682 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005683 }
5684
Calin Juravle641547a2015-04-21 22:08:51 +01005685 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005686 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005687 }
5688
5689 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005690 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5691 }
5692
Calin Juravle52c48962014-12-16 17:02:57 +00005693 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005694 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005695 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005696 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005697
Vladimir Marko61b92282017-10-11 13:23:17 +01005698 void SetType(DataType::Type new_type) {
5699 DCHECK(DataType::IsIntegralType(GetType()));
5700 DCHECK(DataType::IsIntegralType(new_type));
5701 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5702 SetPackedField<TypeField>(new_type);
5703 }
5704
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005705 DECLARE_INSTRUCTION(InstanceFieldGet);
5706
Artem Serovcced8ba2017-07-19 18:18:09 +01005707 protected:
5708 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldGet);
5709
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005710 private:
5711 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005712};
5713
Vladimir Markobd785672018-05-03 17:09:09 +01005714class HInstanceFieldSet FINAL : public HExpression<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005715 public:
5716 HInstanceFieldSet(HInstruction* object,
5717 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005718 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005719 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005720 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005721 bool is_volatile,
5722 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005723 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005724 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005725 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01005726 : HExpression(kInstanceFieldSet,
5727 SideEffects::FieldWriteOfType(field_type, is_volatile),
5728 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005729 field_info_(field,
5730 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005731 field_type,
5732 is_volatile,
5733 field_idx,
5734 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005735 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005736 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005737 SetRawInputAt(0, object);
5738 SetRawInputAt(1, value);
5739 }
5740
Artem Serovcced8ba2017-07-19 18:18:09 +01005741 bool IsClonable() const OVERRIDE { return true; }
5742
Calin Juravle641547a2015-04-21 22:08:51 +01005743 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005744 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005745 }
5746
Calin Juravle52c48962014-12-16 17:02:57 +00005747 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005748 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005749 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005750 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005751 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005752 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5753 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005754
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005755 DECLARE_INSTRUCTION(InstanceFieldSet);
5756
Artem Serovcced8ba2017-07-19 18:18:09 +01005757 protected:
5758 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldSet);
5759
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005760 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005761 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5762 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5763 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5764 "Too many packed fields.");
5765
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005766 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005767};
5768
Vladimir Markofcb503c2016-05-18 12:48:17 +01005769class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005770 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005771 HArrayGet(HInstruction* array,
5772 HInstruction* index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005773 DataType::Type type,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005774 uint32_t dex_pc)
5775 : HArrayGet(array,
5776 index,
5777 type,
5778 SideEffects::ArrayReadOfType(type),
5779 dex_pc,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305780 /* is_string_char_at */ false) {
5781 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005782
5783 HArrayGet(HInstruction* array,
5784 HInstruction* index,
5785 DataType::Type type,
5786 SideEffects side_effects,
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005787 uint32_t dex_pc,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005788 bool is_string_char_at)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305789 : HExpression(kArrayGet, type, side_effects, dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005790 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005791 SetRawInputAt(0, array);
5792 SetRawInputAt(1, index);
5793 }
5794
Artem Serovcced8ba2017-07-19 18:18:09 +01005795 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005796 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005797 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005798 return true;
5799 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005800 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005801 // TODO: We can be smarter here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01005802 // Currently, unless the array is the result of NewArray, the array access is always
5803 // preceded by some form of null NullCheck necessary for the bounds check, usually
5804 // implicit null check on the ArrayLength input to BoundsCheck or Deoptimize for
5805 // dynamic BCE. There are cases when these could be removed to produce better code.
5806 // If we ever add optimizations to do so we should allow an implicit check here
5807 // (as long as the address falls in the first page).
5808 //
5809 // As an example of such fancy optimization, we could eliminate BoundsCheck for
5810 // a = cond ? new int[1] : null;
5811 // a[0]; // The Phi does not need bounds check for either input.
Calin Juravle77520bc2015-01-12 18:45:46 +00005812 return false;
5813 }
5814
David Brazdil4833f5a2015-12-16 10:37:39 +00005815 bool IsEquivalentOf(HArrayGet* other) const {
5816 bool result = (GetDexPc() == other->GetDexPc());
5817 if (kIsDebugBuild && result) {
5818 DCHECK_EQ(GetBlock(), other->GetBlock());
5819 DCHECK_EQ(GetArray(), other->GetArray());
5820 DCHECK_EQ(GetIndex(), other->GetIndex());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005821 if (DataType::IsIntOrLongType(GetType())) {
5822 DCHECK(DataType::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005823 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005824 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
5825 DCHECK(DataType::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005826 }
5827 }
5828 return result;
5829 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005830
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005831 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5832
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005833 HInstruction* GetArray() const { return InputAt(0); }
5834 HInstruction* GetIndex() const { return InputAt(1); }
5835
Vladimir Marko61b92282017-10-11 13:23:17 +01005836 void SetType(DataType::Type new_type) {
5837 DCHECK(DataType::IsIntegralType(GetType()));
5838 DCHECK(DataType::IsIntegralType(new_type));
5839 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5840 SetPackedField<TypeField>(new_type);
5841 }
5842
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005843 DECLARE_INSTRUCTION(ArrayGet);
5844
Artem Serovcced8ba2017-07-19 18:18:09 +01005845 protected:
5846 DEFAULT_COPY_CONSTRUCTOR(ArrayGet);
5847
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005848 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005849 // We treat a String as an array, creating the HArrayGet from String.charAt()
5850 // intrinsic in the instruction simplifier. We can always determine whether
5851 // a particular HArrayGet is actually a String.charAt() by looking at the type
5852 // of the input but that requires holding the mutator lock, so we prefer to use
5853 // a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01005854 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005855 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5856 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5857 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005858};
5859
Vladimir Markobd785672018-05-03 17:09:09 +01005860class HArraySet FINAL : public HExpression<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005861 public:
5862 HArraySet(HInstruction* array,
5863 HInstruction* index,
5864 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005865 DataType::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005866 uint32_t dex_pc)
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005867 : HArraySet(array,
5868 index,
5869 value,
5870 expected_component_type,
5871 // Make a best guess for side effects now, may be refined during SSA building.
5872 ComputeSideEffects(GetComponentType(value->GetType(), expected_component_type)),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305873 dex_pc) {
5874 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005875
5876 HArraySet(HInstruction* array,
5877 HInstruction* index,
5878 HInstruction* value,
5879 DataType::Type expected_component_type,
5880 SideEffects side_effects,
5881 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01005882 : HExpression(kArraySet, side_effects, dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005883 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005884 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005885 SetPackedFlag<kFlagValueCanBeNull>(true);
5886 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005887 SetRawInputAt(0, array);
5888 SetRawInputAt(1, index);
5889 SetRawInputAt(2, value);
5890 }
5891
Artem Serovcced8ba2017-07-19 18:18:09 +01005892 bool IsClonable() const OVERRIDE { return true; }
5893
Calin Juravle77520bc2015-01-12 18:45:46 +00005894 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005895 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005896 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005897 }
5898
Mingyao Yang81014cb2015-06-02 03:16:27 -07005899 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005900 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005901
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005902 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005903 // TODO: Same as for ArrayGet.
5904 return false;
5905 }
5906
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005907 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005908 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005909 }
5910
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005911 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005912 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005913 }
5914
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005915 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005916 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005917 }
5918
Vladimir Markoa1de9182016-02-25 11:37:38 +00005919 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5920 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5921 bool StaticTypeOfArrayIsObjectArray() const {
5922 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5923 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005924
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005925 HInstruction* GetArray() const { return InputAt(0); }
5926 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005927 HInstruction* GetValue() const { return InputAt(2); }
5928
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005929 DataType::Type GetComponentType() const {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005930 return GetComponentType(GetValue()->GetType(), GetRawExpectedComponentType());
5931 }
5932
5933 static DataType::Type GetComponentType(DataType::Type value_type,
5934 DataType::Type expected_component_type) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005935 // The Dex format does not type floating point index operations. Since the
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005936 // `expected_component_type` comes from SSA building and can therefore not
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005937 // be correct, we also check what is the value type. If it is a floating
5938 // point type, we must use that type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005939 return ((value_type == DataType::Type::kFloat32) || (value_type == DataType::Type::kFloat64))
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005940 ? value_type
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005941 : expected_component_type;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005942 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005943
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005944 DataType::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005945 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005946 }
5947
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005948 static SideEffects ComputeSideEffects(DataType::Type type) {
5949 return SideEffects::ArrayWriteOfType(type).Union(SideEffectsForArchRuntimeCalls(type));
Aart Bik18b36ab2016-04-13 16:41:35 -07005950 }
5951
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005952 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type value_type) {
5953 return (value_type == DataType::Type::kReference) ? SideEffects::CanTriggerGC()
5954 : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005955 }
5956
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005957 DECLARE_INSTRUCTION(ArraySet);
5958
Artem Serovcced8ba2017-07-19 18:18:09 +01005959 protected:
5960 DEFAULT_COPY_CONSTRUCTOR(ArraySet);
5961
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005962 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005963 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5964 static constexpr size_t kFieldExpectedComponentTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005965 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00005966 static constexpr size_t kFlagNeedsTypeCheck =
5967 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5968 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005969 // Cached information for the reference_type_info_ so that codegen
5970 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005971 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5972 static constexpr size_t kNumberOfArraySetPackedBits =
5973 kFlagStaticTypeOfArrayIsObjectArray + 1;
5974 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5975 using ExpectedComponentTypeField =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005976 BitField<DataType::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005977};
5978
Vladimir Markofcb503c2016-05-18 12:48:17 +01005979class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005980 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005981 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305982 : HExpression(kArrayLength, DataType::Type::kInt32, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005983 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005984 // Note that arrays do not change length, so the instruction does not
5985 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005986 SetRawInputAt(0, array);
5987 }
5988
Artem Serovcced8ba2017-07-19 18:18:09 +01005989 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle77520bc2015-01-12 18:45:46 +00005990 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005991 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005992 return true;
5993 }
Calin Juravle641547a2015-04-21 22:08:51 +01005994 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5995 return obj == InputAt(0);
5996 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005997
Vladimir Markodce016e2016-04-28 13:10:02 +01005998 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5999
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006000 DECLARE_INSTRUCTION(ArrayLength);
6001
Artem Serovcced8ba2017-07-19 18:18:09 +01006002 protected:
6003 DEFAULT_COPY_CONSTRUCTOR(ArrayLength);
6004
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006005 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01006006 // We treat a String as an array, creating the HArrayLength from String.length()
6007 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
6008 // determine whether a particular HArrayLength is actually a String.length() by
6009 // looking at the type of the input but that requires holding the mutator lock, so
6010 // we prefer to use a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01006011 static constexpr size_t kFlagIsStringLength = kNumberOfGenericPackedBits;
Vladimir Markodce016e2016-04-28 13:10:02 +01006012 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
6013 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6014 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006015};
6016
Vladimir Markofcb503c2016-05-18 12:48:17 +01006017class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006018 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00006019 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
6020 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006021 HBoundsCheck(HInstruction* index,
6022 HInstruction* length,
6023 uint32_t dex_pc,
Vladimir Marko0259c242017-12-04 11:27:47 +00006024 bool is_string_char_at = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306025 : HExpression(kBoundsCheck, index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006026 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(index->GetType()));
Vladimir Marko0259c242017-12-04 11:27:47 +00006027 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006028 SetRawInputAt(0, index);
6029 SetRawInputAt(1, length);
6030 }
6031
Artem Serovcced8ba2017-07-19 18:18:09 +01006032 bool IsClonable() const OVERRIDE { return true; }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006033 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006034 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006035 return true;
6036 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006037
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006038 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006039
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006040 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01006041
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00006042 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006043
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006044 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006045
6046 DECLARE_INSTRUCTION(BoundsCheck);
6047
Artem Serovcced8ba2017-07-19 18:18:09 +01006048 protected:
6049 DEFAULT_COPY_CONSTRUCTOR(BoundsCheck);
6050
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006051 private:
Vladimir Markobd785672018-05-03 17:09:09 +01006052 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006053};
6054
Vladimir Markobd785672018-05-03 17:09:09 +01006055class HSuspendCheck FINAL : public HExpression<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006056 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00006057 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01006058 : HExpression(kSuspendCheck, SideEffects::CanTriggerGC(), dex_pc),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306059 slow_path_(nullptr) {
6060 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006061
Artem Serovcced8ba2017-07-19 18:18:09 +01006062 bool IsClonable() const OVERRIDE { return true; }
6063
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006064 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006065 return true;
6066 }
6067
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006068 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
6069 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006070
6071 DECLARE_INSTRUCTION(SuspendCheck);
6072
Artem Serovcced8ba2017-07-19 18:18:09 +01006073 protected:
6074 DEFAULT_COPY_CONSTRUCTOR(SuspendCheck);
6075
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006076 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006077 // Only used for code generation, in order to share the same slow path between back edges
6078 // of a same loop.
6079 SlowPathCode* slow_path_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006080};
6081
David Srbecky0cf44932015-12-09 14:09:59 +00006082// Pseudo-instruction which provides the native debugger with mapping information.
6083// It ensures that we can generate line number and local variables at this point.
Vladimir Markobd785672018-05-03 17:09:09 +01006084class HNativeDebugInfo : public HExpression<0> {
David Srbecky0cf44932015-12-09 14:09:59 +00006085 public:
6086 explicit HNativeDebugInfo(uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006087 : HExpression<0>(kNativeDebugInfo, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306088 }
David Srbecky0cf44932015-12-09 14:09:59 +00006089
6090 bool NeedsEnvironment() const OVERRIDE {
6091 return true;
6092 }
6093
6094 DECLARE_INSTRUCTION(NativeDebugInfo);
6095
Artem Serovcced8ba2017-07-19 18:18:09 +01006096 protected:
6097 DEFAULT_COPY_CONSTRUCTOR(NativeDebugInfo);
David Srbecky0cf44932015-12-09 14:09:59 +00006098};
6099
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006100/**
6101 * Instruction to load a Class object.
6102 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006103class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006104 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006105 // Determines how to load the Class.
6106 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006107 // We cannot load this class. See HSharpening::SharpenLoadClass.
6108 kInvalid = -1,
6109
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006110 // Use the Class* from the method's own ArtMethod*.
6111 kReferrersClass,
6112
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006113 // Use PC-relative boot image Class* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006114 // Used for boot image classes referenced by boot image code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006115 kBootImageLinkTimePcRelative,
6116
6117 // Use a known boot image Class* address, embedded in the code by the codegen.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006118 // Used for boot image classes referenced by apps in JIT- and AOT-compiled code (non-PIC).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006119 kBootImageAddress,
6120
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006121 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
6122 // Used for boot image classes referenced by apps in AOT-compiled code (PIC).
6123 kBootImageRelRo,
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006124
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006125 // Load from an entry in the .bss section using a PC-relative load.
6126 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
6127 kBssEntry,
6128
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006129 // Load from the root table associated with the JIT compiled method.
6130 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006131
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006132 // Load using a simple runtime call. This is the fall-back load kind when
6133 // the codegen is unable to use another appropriate kind.
6134 kRuntimeCall,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006135
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006136 kLast = kRuntimeCall
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006137 };
6138
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006139 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08006140 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006141 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006142 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006143 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01006144 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00006145 bool needs_access_check)
Vladimir Markobd785672018-05-03 17:09:09 +01006146 : HInstruction(kLoadClass,
6147 DataType::Type::kReference,
6148 SideEffectsForArchRuntimeCalls(),
6149 dex_pc),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006150 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006151 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006152 dex_file_(dex_file),
Vladimir Marko175e7862018-03-27 09:03:13 +00006153 klass_(klass) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01006154 // Referrers class should not need access check. We never inline unverified
6155 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006156 DCHECK(!is_referrers_class || !needs_access_check);
6157
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006158 SetPackedField<LoadKindField>(
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006159 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kRuntimeCall);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006160 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00006161 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006162 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Marko175e7862018-03-27 09:03:13 +00006163 SetPackedFlag<kFlagValidLoadedClassRTI>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006164 }
6165
Artem Serovcced8ba2017-07-19 18:18:09 +01006166 bool IsClonable() const OVERRIDE { return true; }
6167
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006168 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006169
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006170 LoadKind GetLoadKind() const {
6171 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006172 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006173
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006174 bool HasPcRelativeLoadKind() const {
6175 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6176 GetLoadKind() == LoadKind::kBootImageRelRo ||
6177 GetLoadKind() == LoadKind::kBssEntry;
6178 }
6179
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006180 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006181
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006182 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006183
Andreas Gampea5b09a62016-11-17 15:21:22 -08006184 size_t ComputeHashCode() const OVERRIDE { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006185
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01006186 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006187
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006188 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006189 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006190 }
6191
Calin Juravle0ba218d2015-05-19 18:46:01 +01006192 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00006193 // The entrypoint the code generator is going to call does not do
6194 // clinit of the class.
6195 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00006196 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006197 }
6198
6199 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00006200 return NeedsAccessCheck() ||
6201 MustGenerateClinitCheck() ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006202 GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006203 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01006204 }
6205
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006206 bool CanThrow() const OVERRIDE {
Vladimir Marko41559982017-01-06 14:04:23 +00006207 return NeedsAccessCheck() ||
6208 MustGenerateClinitCheck() ||
6209 // If the class is in the boot image, the lookup in the runtime call cannot throw.
6210 // This keeps CanThrow() consistent between non-PIC (using kBootImageAddress) and
6211 // PIC and subsequently avoids a DCE behavior dependency on the PIC option.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006212 ((GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006213 GetLoadKind() == LoadKind::kBssEntry) &&
6214 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006215 }
6216
Calin Juravleacf735c2015-02-12 15:25:22 +00006217 ReferenceTypeInfo GetLoadedClassRTI() {
Vladimir Marko175e7862018-03-27 09:03:13 +00006218 if (GetPackedFlag<kFlagValidLoadedClassRTI>()) {
6219 // Note: The is_exact flag from the return value should not be used.
6220 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact */ true);
6221 } else {
6222 return ReferenceTypeInfo::CreateInvalid();
6223 }
Calin Juravleacf735c2015-02-12 15:25:22 +00006224 }
6225
Vladimir Marko175e7862018-03-27 09:03:13 +00006226 // Loaded class RTI is marked as valid by RTP if the klass_ is admissible.
6227 void SetValidLoadedClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
6228 DCHECK(klass_ != nullptr);
6229 SetPackedFlag<kFlagValidLoadedClassRTI>(true);
Calin Juravleacf735c2015-02-12 15:25:22 +00006230 }
6231
Andreas Gampea5b09a62016-11-17 15:21:22 -08006232 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006233 const DexFile& GetDexFile() const { return dex_file_; }
6234
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006235 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006236 return GetLoadKind() == LoadKind::kRuntimeCall;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006237 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00006238
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006239 static SideEffects SideEffectsForArchRuntimeCalls() {
6240 return SideEffects::CanTriggerGC();
6241 }
6242
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006243 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006244 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006245 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006246 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006247
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006248 void MarkInBootImage() {
6249 SetPackedFlag<kFlagIsInBootImage>(true);
6250 }
6251
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006252 void AddSpecialInput(HInstruction* special_input);
6253
6254 using HInstruction::GetInputRecords; // Keep the const version visible.
6255 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
6256 return ArrayRef<HUserRecord<HInstruction*>>(
6257 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6258 }
6259
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006260 Handle<mirror::Class> GetClass() const {
6261 return klass_;
6262 }
6263
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006264 DECLARE_INSTRUCTION(LoadClass);
6265
Artem Serovcced8ba2017-07-19 18:18:09 +01006266 protected:
6267 DEFAULT_COPY_CONSTRUCTOR(LoadClass);
6268
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006269 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006270 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00006271 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006272 // Whether this instruction must generate the initialization check.
6273 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006274 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006275 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
6276 static constexpr size_t kFieldLoadKindSize =
6277 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
Vladimir Marko175e7862018-03-27 09:03:13 +00006278 static constexpr size_t kFlagValidLoadedClassRTI = kFieldLoadKind + kFieldLoadKindSize;
6279 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagValidLoadedClassRTI + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006280 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006281 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
6282
6283 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006284 return load_kind == LoadKind::kReferrersClass ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006285 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006286 load_kind == LoadKind::kBssEntry ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006287 load_kind == LoadKind::kRuntimeCall;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006288 }
6289
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006290 void SetLoadKindInternal(LoadKind load_kind);
6291
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006292 // The special input is the HCurrentMethod for kRuntimeCall or kReferrersClass.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006293 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006294 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006295 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006296
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006297 // A type index and dex file where the class can be accessed. The dex file can be:
6298 // - The compiling method's dex file if the class is defined there too.
6299 // - The compiling method's dex file if the class is referenced there.
6300 // - The dex file where the class is defined. When the load kind can only be
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006301 // kBssEntry or kRuntimeCall, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08006302 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006303 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006304
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006305 Handle<mirror::Class> klass_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006306};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006307std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
6308
6309// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006310inline void HLoadClass::SetLoadKind(LoadKind load_kind) {
6311 // The load kind should be determined before inserting the instruction to the graph.
6312 DCHECK(GetBlock() == nullptr);
6313 DCHECK(GetEnvironment() == nullptr);
6314 SetPackedField<LoadKindField>(load_kind);
6315 if (load_kind != LoadKind::kRuntimeCall && load_kind != LoadKind::kReferrersClass) {
6316 special_input_ = HUserRecord<HInstruction*>(nullptr);
6317 }
6318 if (!NeedsEnvironment()) {
6319 SetSideEffects(SideEffects::None());
6320 }
6321}
6322
6323// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006324inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006325 // The special input is used for PC-relative loads on some architectures,
6326 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006327 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006328 GetLoadKind() == LoadKind::kBootImageAddress ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006329 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006330 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006331 DCHECK(special_input_.GetInstruction() == nullptr);
6332 special_input_ = HUserRecord<HInstruction*>(special_input);
6333 special_input->AddUseAt(this, 0);
6334}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006335
Vladimir Marko372f10e2016-05-17 16:30:10 +01006336class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006337 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006338 // Determines how to load the String.
6339 enum class LoadKind {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006340 // Use PC-relative boot image String* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006341 // Used for boot image strings referenced by boot image code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006342 kBootImageLinkTimePcRelative,
6343
6344 // Use a known boot image String* address, embedded in the code by the codegen.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006345 // Used for boot image strings referenced by apps in JIT- and AOT-compiled code (non-PIC).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006346 kBootImageAddress,
6347
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006348 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
6349 // Used for boot image strings referenced by apps in AOT-compiled code (PIC).
6350 kBootImageRelRo,
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006351
Vladimir Markoaad75c62016-10-03 08:46:48 +00006352 // Load from an entry in the .bss section using a PC-relative load.
6353 // Used for strings outside boot image when .bss is accessible with a PC-relative load.
6354 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006355
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006356 // Load from the root table associated with the JIT compiled method.
6357 kJitTableAddress,
6358
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006359 // Load using a simple runtime call. This is the fall-back load kind when
6360 // the codegen is unable to use another appropriate kind.
6361 kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006362
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006363 kLast = kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006364 };
6365
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006366 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006367 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006368 const DexFile& dex_file,
6369 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006370 : HInstruction(kLoadString,
6371 DataType::Type::kReference,
6372 SideEffectsForArchRuntimeCalls(),
6373 dex_pc),
Vladimir Marko372f10e2016-05-17 16:30:10 +01006374 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006375 string_index_(string_index),
6376 dex_file_(dex_file) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006377 SetPackedField<LoadKindField>(LoadKind::kRuntimeCall);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006378 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006379
Artem Serovcced8ba2017-07-19 18:18:09 +01006380 bool IsClonable() const OVERRIDE { return true; }
6381
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006382 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006383
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006384 LoadKind GetLoadKind() const {
6385 return GetPackedField<LoadKindField>();
6386 }
6387
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006388 bool HasPcRelativeLoadKind() const {
6389 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6390 GetLoadKind() == LoadKind::kBootImageRelRo ||
6391 GetLoadKind() == LoadKind::kBssEntry;
6392 }
6393
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006394 const DexFile& GetDexFile() const {
6395 return dex_file_;
6396 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006397
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006398 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006399 return string_index_;
6400 }
6401
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006402 Handle<mirror::String> GetString() const {
6403 return string_;
6404 }
6405
6406 void SetString(Handle<mirror::String> str) {
6407 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006408 }
6409
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006410 bool CanBeMoved() const OVERRIDE { return true; }
6411
Vladimir Marko372f10e2016-05-17 16:30:10 +01006412 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006413
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006414 size_t ComputeHashCode() const OVERRIDE { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006415
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006416 // Will call the runtime if we need to load the string through
6417 // the dex cache and the string is not guaranteed to be there yet.
6418 bool NeedsEnvironment() const OVERRIDE {
6419 LoadKind load_kind = GetLoadKind();
Vladimir Marko764d4542017-05-16 10:31:41 +01006420 if (load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006421 load_kind == LoadKind::kBootImageAddress ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006422 load_kind == LoadKind::kBootImageRelRo ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006423 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006424 return false;
6425 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006426 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006427 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006428
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006429 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006430 return GetLoadKind() == LoadKind::kRuntimeCall;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006431 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006432
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07006433 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006434 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006435
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006436 static SideEffects SideEffectsForArchRuntimeCalls() {
6437 return SideEffects::CanTriggerGC();
6438 }
6439
Vladimir Marko372f10e2016-05-17 16:30:10 +01006440 void AddSpecialInput(HInstruction* special_input);
6441
6442 using HInstruction::GetInputRecords; // Keep the const version visible.
6443 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
6444 return ArrayRef<HUserRecord<HInstruction*>>(
6445 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006446 }
6447
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006448 DECLARE_INSTRUCTION(LoadString);
6449
Artem Serovcced8ba2017-07-19 18:18:09 +01006450 protected:
6451 DEFAULT_COPY_CONSTRUCTOR(LoadString);
6452
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006453 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006454 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006455 static constexpr size_t kFieldLoadKindSize =
6456 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
6457 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006458 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006459 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006460
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006461 void SetLoadKindInternal(LoadKind load_kind);
6462
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006463 // The special input is the HCurrentMethod for kRuntimeCall.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006464 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00006465 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01006466 HUserRecord<HInstruction*> special_input_;
6467
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006468 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006469 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006470
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006471 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006472};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006473std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
6474
6475// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006476inline void HLoadString::SetLoadKind(LoadKind load_kind) {
6477 // The load kind should be determined before inserting the instruction to the graph.
6478 DCHECK(GetBlock() == nullptr);
6479 DCHECK(GetEnvironment() == nullptr);
6480 DCHECK_EQ(GetLoadKind(), LoadKind::kRuntimeCall);
6481 SetPackedField<LoadKindField>(load_kind);
6482 if (load_kind != LoadKind::kRuntimeCall) {
6483 special_input_ = HUserRecord<HInstruction*>(nullptr);
6484 }
6485 if (!NeedsEnvironment()) {
6486 SetSideEffects(SideEffects::None());
6487 }
6488}
6489
6490// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006491inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006492 // The special input is used for PC-relative loads on some architectures,
6493 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006494 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006495 GetLoadKind() == LoadKind::kBootImageAddress ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006496 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006497 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01006498 // HLoadString::GetInputRecords() returns an empty array at this point,
6499 // so use the GetInputRecords() from the base class to set the input record.
6500 DCHECK(special_input_.GetInstruction() == nullptr);
6501 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006502 special_input->AddUseAt(this, 0);
6503}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006504
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006505class HLoadMethodHandle FINAL : public HInstruction {
6506 public:
6507 HLoadMethodHandle(HCurrentMethod* current_method,
6508 uint16_t method_handle_idx,
6509 const DexFile& dex_file,
6510 uint32_t dex_pc)
6511 : HInstruction(kLoadMethodHandle,
6512 DataType::Type::kReference,
6513 SideEffectsForArchRuntimeCalls(),
6514 dex_pc),
6515 special_input_(HUserRecord<HInstruction*>(current_method)),
6516 method_handle_idx_(method_handle_idx),
6517 dex_file_(dex_file) {
6518 }
6519
6520 using HInstruction::GetInputRecords; // Keep the const version visible.
6521 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
6522 return ArrayRef<HUserRecord<HInstruction*>>(
6523 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6524 }
6525
6526 bool IsClonable() const OVERRIDE { return true; }
6527
6528 uint16_t GetMethodHandleIndex() const { return method_handle_idx_; }
6529
6530 const DexFile& GetDexFile() const { return dex_file_; }
6531
6532 static SideEffects SideEffectsForArchRuntimeCalls() {
6533 return SideEffects::CanTriggerGC();
6534 }
6535
6536 DECLARE_INSTRUCTION(LoadMethodHandle);
6537
6538 protected:
6539 DEFAULT_COPY_CONSTRUCTOR(LoadMethodHandle);
6540
6541 private:
6542 // The special input is the HCurrentMethod for kRuntimeCall.
6543 HUserRecord<HInstruction*> special_input_;
6544
6545 const uint16_t method_handle_idx_;
6546 const DexFile& dex_file_;
6547};
6548
Orion Hodson18259d72018-04-12 11:18:23 +01006549class HLoadMethodType FINAL : public HInstruction {
6550 public:
6551 HLoadMethodType(HCurrentMethod* current_method,
Orion Hodson06d10a72018-05-14 08:53:38 +01006552 dex::ProtoIndex proto_index,
Orion Hodson18259d72018-04-12 11:18:23 +01006553 const DexFile& dex_file,
6554 uint32_t dex_pc)
6555 : HInstruction(kLoadMethodType,
6556 DataType::Type::kReference,
6557 SideEffectsForArchRuntimeCalls(),
6558 dex_pc),
6559 special_input_(HUserRecord<HInstruction*>(current_method)),
Orion Hodson06d10a72018-05-14 08:53:38 +01006560 proto_index_(proto_index),
Orion Hodson18259d72018-04-12 11:18:23 +01006561 dex_file_(dex_file) {
6562 }
6563
6564 using HInstruction::GetInputRecords; // Keep the const version visible.
6565 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
6566 return ArrayRef<HUserRecord<HInstruction*>>(
6567 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6568 }
6569
6570 bool IsClonable() const OVERRIDE { return true; }
6571
Orion Hodson06d10a72018-05-14 08:53:38 +01006572 dex::ProtoIndex GetProtoIndex() const { return proto_index_; }
Orion Hodson18259d72018-04-12 11:18:23 +01006573
6574 const DexFile& GetDexFile() const { return dex_file_; }
6575
6576 static SideEffects SideEffectsForArchRuntimeCalls() {
6577 return SideEffects::CanTriggerGC();
6578 }
6579
6580 DECLARE_INSTRUCTION(LoadMethodType);
6581
6582 protected:
6583 DEFAULT_COPY_CONSTRUCTOR(LoadMethodType);
6584
6585 private:
6586 // The special input is the HCurrentMethod for kRuntimeCall.
6587 HUserRecord<HInstruction*> special_input_;
6588
Orion Hodson06d10a72018-05-14 08:53:38 +01006589 const dex::ProtoIndex proto_index_;
Orion Hodson18259d72018-04-12 11:18:23 +01006590 const DexFile& dex_file_;
6591};
6592
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006593/**
6594 * Performs an initialization check on its Class object input.
6595 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01006596class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006597 public:
Roland Levillain3887c462015-08-12 18:15:42 +01006598 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07006599 : HExpression(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306600 kClinitCheck,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006601 DataType::Type::kReference,
Mingyao Yang217eb062017-12-11 15:20:07 -08006602 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006603 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006604 SetRawInputAt(0, constant);
6605 }
6606
Artem Serovcced8ba2017-07-19 18:18:09 +01006607 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006608 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006609 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006610 return true;
6611 }
6612
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006613 bool NeedsEnvironment() const OVERRIDE {
6614 // May call runtime to initialize the class.
6615 return true;
6616 }
6617
Nicolas Geoffray729645a2015-11-19 13:29:02 +00006618 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006619
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006620 HLoadClass* GetLoadClass() const {
6621 DCHECK(InputAt(0)->IsLoadClass());
6622 return InputAt(0)->AsLoadClass();
6623 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006624
6625 DECLARE_INSTRUCTION(ClinitCheck);
6626
Artem Serovcced8ba2017-07-19 18:18:09 +01006627
6628 protected:
6629 DEFAULT_COPY_CONSTRUCTOR(ClinitCheck);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006630};
6631
Vladimir Markofcb503c2016-05-18 12:48:17 +01006632class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006633 public:
6634 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006635 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006636 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006637 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006638 bool is_volatile,
6639 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006640 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006641 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006642 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306643 : HExpression(kStaticFieldGet,
6644 field_type,
6645 SideEffects::FieldReadOfType(field_type, is_volatile),
6646 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006647 field_info_(field,
6648 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006649 field_type,
6650 is_volatile,
6651 field_idx,
6652 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006653 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006654 SetRawInputAt(0, cls);
6655 }
6656
Calin Juravle52c48962014-12-16 17:02:57 +00006657
Artem Serovcced8ba2017-07-19 18:18:09 +01006658 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle10c9cbe2014-12-19 10:50:19 +00006659 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006660
Vladimir Marko372f10e2016-05-17 16:30:10 +01006661 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
6662 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006663 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006664 }
6665
6666 size_t ComputeHashCode() const OVERRIDE {
6667 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6668 }
6669
Calin Juravle52c48962014-12-16 17:02:57 +00006670 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006671 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006672 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006673 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006674
Vladimir Marko61b92282017-10-11 13:23:17 +01006675 void SetType(DataType::Type new_type) {
6676 DCHECK(DataType::IsIntegralType(GetType()));
6677 DCHECK(DataType::IsIntegralType(new_type));
6678 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6679 SetPackedField<TypeField>(new_type);
6680 }
6681
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006682 DECLARE_INSTRUCTION(StaticFieldGet);
6683
Artem Serovcced8ba2017-07-19 18:18:09 +01006684 protected:
6685 DEFAULT_COPY_CONSTRUCTOR(StaticFieldGet);
6686
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006687 private:
6688 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006689};
6690
Vladimir Markobd785672018-05-03 17:09:09 +01006691class HStaticFieldSet FINAL : public HExpression<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006692 public:
6693 HStaticFieldSet(HInstruction* cls,
6694 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006695 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006696 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006697 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006698 bool is_volatile,
6699 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006700 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006701 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006702 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006703 : HExpression(kStaticFieldSet,
6704 SideEffects::FieldWriteOfType(field_type, is_volatile),
6705 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006706 field_info_(field,
6707 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006708 field_type,
6709 is_volatile,
6710 field_idx,
6711 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006712 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006713 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006714 SetRawInputAt(0, cls);
6715 SetRawInputAt(1, value);
6716 }
6717
Artem Serovcced8ba2017-07-19 18:18:09 +01006718 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle52c48962014-12-16 17:02:57 +00006719 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006720 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006721 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006722 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006723
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006724 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006725 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6726 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006727
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006728 DECLARE_INSTRUCTION(StaticFieldSet);
6729
Artem Serovcced8ba2017-07-19 18:18:09 +01006730 protected:
6731 DEFAULT_COPY_CONSTRUCTOR(StaticFieldSet);
6732
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006733 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006734 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6735 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6736 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6737 "Too many packed fields.");
6738
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006739 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006740};
6741
Vladimir Markofcb503c2016-05-18 12:48:17 +01006742class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006743 public:
6744 HUnresolvedInstanceFieldGet(HInstruction* obj,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006745 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006746 uint32_t field_index,
6747 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306748 : HExpression(kUnresolvedInstanceFieldGet,
6749 field_type,
6750 SideEffects::AllExceptGCDependency(),
6751 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006752 field_index_(field_index) {
6753 SetRawInputAt(0, obj);
6754 }
6755
Artem Serovcced8ba2017-07-19 18:18:09 +01006756 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006757 bool NeedsEnvironment() const OVERRIDE { return true; }
6758 bool CanThrow() const OVERRIDE { return true; }
6759
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006760 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006761 uint32_t GetFieldIndex() const { return field_index_; }
6762
6763 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6764
Artem Serovcced8ba2017-07-19 18:18:09 +01006765 protected:
6766 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldGet);
6767
Calin Juravlee460d1d2015-09-29 04:52:17 +01006768 private:
6769 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006770};
6771
Vladimir Markobd785672018-05-03 17:09:09 +01006772class HUnresolvedInstanceFieldSet FINAL : public HExpression<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006773 public:
6774 HUnresolvedInstanceFieldSet(HInstruction* obj,
6775 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006776 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006777 uint32_t field_index,
6778 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006779 : HExpression(kUnresolvedInstanceFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006780 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006781 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006782 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006783 SetRawInputAt(0, obj);
6784 SetRawInputAt(1, value);
6785 }
6786
Artem Serovcced8ba2017-07-19 18:18:09 +01006787 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006788 bool NeedsEnvironment() const OVERRIDE { return true; }
6789 bool CanThrow() const OVERRIDE { return true; }
6790
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006791 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006792 uint32_t GetFieldIndex() const { return field_index_; }
6793
6794 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6795
Artem Serovcced8ba2017-07-19 18:18:09 +01006796 protected:
6797 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldSet);
6798
Calin Juravlee460d1d2015-09-29 04:52:17 +01006799 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006800 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6801 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006802 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006803 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6804 kFieldFieldType + kFieldFieldTypeSize;
6805 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6806 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006807 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006808
Calin Juravlee460d1d2015-09-29 04:52:17 +01006809 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006810};
6811
Vladimir Markofcb503c2016-05-18 12:48:17 +01006812class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006813 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006814 HUnresolvedStaticFieldGet(DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006815 uint32_t field_index,
6816 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306817 : HExpression(kUnresolvedStaticFieldGet,
6818 field_type,
6819 SideEffects::AllExceptGCDependency(),
6820 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006821 field_index_(field_index) {
6822 }
6823
Artem Serovcced8ba2017-07-19 18:18:09 +01006824 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006825 bool NeedsEnvironment() const OVERRIDE { return true; }
6826 bool CanThrow() const OVERRIDE { return true; }
6827
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006828 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006829 uint32_t GetFieldIndex() const { return field_index_; }
6830
6831 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6832
Artem Serovcced8ba2017-07-19 18:18:09 +01006833 protected:
6834 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldGet);
6835
Calin Juravlee460d1d2015-09-29 04:52:17 +01006836 private:
6837 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006838};
6839
Vladimir Markobd785672018-05-03 17:09:09 +01006840class HUnresolvedStaticFieldSet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006841 public:
6842 HUnresolvedStaticFieldSet(HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006843 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006844 uint32_t field_index,
6845 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006846 : HExpression(kUnresolvedStaticFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006847 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006848 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006849 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006850 SetRawInputAt(0, value);
6851 }
6852
Artem Serovcced8ba2017-07-19 18:18:09 +01006853 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006854 bool NeedsEnvironment() const OVERRIDE { return true; }
6855 bool CanThrow() const OVERRIDE { return true; }
6856
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006857 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006858 uint32_t GetFieldIndex() const { return field_index_; }
6859
6860 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6861
Artem Serovcced8ba2017-07-19 18:18:09 +01006862 protected:
6863 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldSet);
6864
Calin Juravlee460d1d2015-09-29 04:52:17 +01006865 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006866 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6867 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006868 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006869 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6870 kFieldFieldType + kFieldFieldTypeSize;
6871 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6872 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006873 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006874
Calin Juravlee460d1d2015-09-29 04:52:17 +01006875 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006876};
6877
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006878// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006879class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006880 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006881 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306882 : HExpression(kLoadException, DataType::Type::kReference, SideEffects::None(), dex_pc) {
6883 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006884
David Brazdilbbd733e2015-08-18 17:48:17 +01006885 bool CanBeNull() const OVERRIDE { return false; }
6886
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006887 DECLARE_INSTRUCTION(LoadException);
6888
Artem Serovcced8ba2017-07-19 18:18:09 +01006889 protected:
6890 DEFAULT_COPY_CONSTRUCTOR(LoadException);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006891};
6892
David Brazdilcb1c0552015-08-04 16:22:25 +01006893// Implicit part of move-exception which clears thread-local exception storage.
6894// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markobd785672018-05-03 17:09:09 +01006895class HClearException FINAL : public HExpression<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006896 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006897 explicit HClearException(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01006898 : HExpression(kClearException, SideEffects::AllWrites(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306899 }
David Brazdilcb1c0552015-08-04 16:22:25 +01006900
6901 DECLARE_INSTRUCTION(ClearException);
6902
Artem Serovcced8ba2017-07-19 18:18:09 +01006903 protected:
6904 DEFAULT_COPY_CONSTRUCTOR(ClearException);
David Brazdilcb1c0552015-08-04 16:22:25 +01006905};
6906
Vladimir Markobd785672018-05-03 17:09:09 +01006907class HThrow FINAL : public HExpression<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006908 public:
6909 HThrow(HInstruction* exception, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006910 : HExpression(kThrow, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006911 SetRawInputAt(0, exception);
6912 }
6913
6914 bool IsControlFlow() const OVERRIDE { return true; }
6915
6916 bool NeedsEnvironment() const OVERRIDE { return true; }
6917
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006918 bool CanThrow() const OVERRIDE { return true; }
6919
Aart Bika8b8e9b2018-01-09 11:01:02 -08006920 bool AlwaysThrows() const OVERRIDE { return true; }
6921
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006922 DECLARE_INSTRUCTION(Throw);
6923
Artem Serovcced8ba2017-07-19 18:18:09 +01006924 protected:
6925 DEFAULT_COPY_CONSTRUCTOR(Throw);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006926};
6927
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006928/**
6929 * Implementation strategies for the code generator of a HInstanceOf
6930 * or `HCheckCast`.
6931 */
6932enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006933 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006934 kExactCheck, // Can do a single class compare.
6935 kClassHierarchyCheck, // Can just walk the super class chain.
6936 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6937 kInterfaceCheck, // No optimization yet when checking against an interface.
6938 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006939 kArrayCheck, // No optimization yet when checking against a generic array.
Vladimir Marko175e7862018-03-27 09:03:13 +00006940 kBitstringCheck, // Compare the type check bitstring.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006941 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006942};
6943
Roland Levillain86503782016-02-11 19:07:30 +00006944std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6945
Vladimir Marko175e7862018-03-27 09:03:13 +00006946// Note: HTypeCheckInstruction is just a helper class, not an abstract instruction with an
6947// `IsTypeCheckInstruction()`. (New virtual methods in the HInstruction class have a high cost.)
6948class HTypeCheckInstruction : public HVariableInputSizeInstruction {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006949 public:
Vladimir Marko175e7862018-03-27 09:03:13 +00006950 HTypeCheckInstruction(InstructionKind kind,
Vladimir Markobd785672018-05-03 17:09:09 +01006951 DataType::Type type,
Vladimir Marko175e7862018-03-27 09:03:13 +00006952 HInstruction* object,
6953 HInstruction* target_class_or_null,
6954 TypeCheckKind check_kind,
6955 Handle<mirror::Class> klass,
6956 uint32_t dex_pc,
6957 ArenaAllocator* allocator,
6958 HIntConstant* bitstring_path_to_root,
6959 HIntConstant* bitstring_mask,
6960 SideEffects side_effects)
6961 : HVariableInputSizeInstruction(
6962 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01006963 type,
Vladimir Marko175e7862018-03-27 09:03:13 +00006964 side_effects,
6965 dex_pc,
6966 allocator,
6967 /* number_of_inputs */ check_kind == TypeCheckKind::kBitstringCheck ? 4u : 2u,
6968 kArenaAllocTypeCheckInputs),
6969 klass_(klass) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006970 SetPackedField<TypeCheckKindField>(check_kind);
6971 SetPackedFlag<kFlagMustDoNullCheck>(true);
Vladimir Marko175e7862018-03-27 09:03:13 +00006972 SetPackedFlag<kFlagValidTargetClassRTI>(false);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006973 SetRawInputAt(0, object);
Vladimir Marko175e7862018-03-27 09:03:13 +00006974 SetRawInputAt(1, target_class_or_null);
6975 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_path_to_root != nullptr);
6976 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_mask != nullptr);
6977 if (check_kind == TypeCheckKind::kBitstringCheck) {
6978 DCHECK(target_class_or_null->IsNullConstant());
6979 SetRawInputAt(2, bitstring_path_to_root);
6980 SetRawInputAt(3, bitstring_mask);
6981 } else {
6982 DCHECK(target_class_or_null->IsLoadClass());
6983 }
Vladimir Marko87584542017-12-12 17:47:52 +00006984 }
6985
6986 HLoadClass* GetTargetClass() const {
Vladimir Marko175e7862018-03-27 09:03:13 +00006987 DCHECK_NE(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
Vladimir Marko87584542017-12-12 17:47:52 +00006988 HInstruction* load_class = InputAt(1);
6989 DCHECK(load_class->IsLoadClass());
6990 return load_class->AsLoadClass();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006991 }
6992
Vladimir Marko175e7862018-03-27 09:03:13 +00006993 uint32_t GetBitstringPathToRoot() const {
6994 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
6995 HInstruction* path_to_root = InputAt(2);
6996 DCHECK(path_to_root->IsIntConstant());
6997 return static_cast<uint32_t>(path_to_root->AsIntConstant()->GetValue());
6998 }
6999
7000 uint32_t GetBitstringMask() const {
7001 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7002 HInstruction* mask = InputAt(3);
7003 DCHECK(mask->IsIntConstant());
7004 return static_cast<uint32_t>(mask->AsIntConstant()->GetValue());
7005 }
7006
Artem Serovcced8ba2017-07-19 18:18:09 +01007007 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007008 bool CanBeMoved() const OVERRIDE { return true; }
7009
Vladimir Marko175e7862018-03-27 09:03:13 +00007010 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
7011 DCHECK(other->IsInstanceOf() || other->IsCheckCast()) << other->DebugName();
7012 return GetPackedFields() == down_cast<const HTypeCheckInstruction*>(other)->GetPackedFields();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007013 }
7014
Andreas Gampe3fbd3ad2018-03-26 21:14:46 +00007015 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
7016 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
7017 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
7018 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
7019
Vladimir Marko175e7862018-03-27 09:03:13 +00007020 ReferenceTypeInfo GetTargetClassRTI() {
7021 if (GetPackedFlag<kFlagValidTargetClassRTI>()) {
7022 // Note: The is_exact flag from the return value should not be used.
7023 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact */ true);
7024 } else {
7025 return ReferenceTypeInfo::CreateInvalid();
7026 }
7027 }
7028
7029 // Target class RTI is marked as valid by RTP if the klass_ is admissible.
7030 void SetValidTargetClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
7031 DCHECK(klass_ != nullptr);
7032 SetPackedFlag<kFlagValidTargetClassRTI>(true);
7033 }
7034
7035 Handle<mirror::Class> GetClass() const {
7036 return klass_;
7037 }
7038
7039 protected:
7040 DEFAULT_COPY_CONSTRUCTOR(TypeCheckInstruction);
7041
7042 private:
7043 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
7044 static constexpr size_t kFieldTypeCheckKindSize =
7045 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
7046 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
7047 static constexpr size_t kFlagValidTargetClassRTI = kFlagMustDoNullCheck + 1;
7048 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagValidTargetClassRTI + 1;
7049 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7050 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
7051
7052 Handle<mirror::Class> klass_;
7053};
7054
7055class HInstanceOf FINAL : public HTypeCheckInstruction {
7056 public:
7057 HInstanceOf(HInstruction* object,
7058 HInstruction* target_class_or_null,
7059 TypeCheckKind check_kind,
7060 Handle<mirror::Class> klass,
7061 uint32_t dex_pc,
7062 ArenaAllocator* allocator,
7063 HIntConstant* bitstring_path_to_root,
7064 HIntConstant* bitstring_mask)
7065 : HTypeCheckInstruction(kInstanceOf,
Vladimir Markobd785672018-05-03 17:09:09 +01007066 DataType::Type::kBool,
Vladimir Marko175e7862018-03-27 09:03:13 +00007067 object,
7068 target_class_or_null,
7069 check_kind,
7070 klass,
7071 dex_pc,
7072 allocator,
7073 bitstring_path_to_root,
7074 bitstring_mask,
7075 SideEffectsForArchRuntimeCalls(check_kind)) {}
7076
Vladimir Marko175e7862018-03-27 09:03:13 +00007077 bool NeedsEnvironment() const OVERRIDE {
7078 return CanCallRuntime(GetTypeCheckKind());
7079 }
7080
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007081 static bool CanCallRuntime(TypeCheckKind check_kind) {
7082 // Mips currently does runtime calls for any other checks.
7083 return check_kind != TypeCheckKind::kExactCheck;
7084 }
7085
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007086 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007087 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01007088 }
7089
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007090 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007091
Artem Serovcced8ba2017-07-19 18:18:09 +01007092 protected:
7093 DEFAULT_COPY_CONSTRUCTOR(InstanceOf);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007094};
7095
Vladimir Markofcb503c2016-05-18 12:48:17 +01007096class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00007097 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07007098 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307099 : HExpression(kBoundType, DataType::Type::kReference, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00007100 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
7101 SetPackedFlag<kFlagUpperCanBeNull>(true);
7102 SetPackedFlag<kFlagCanBeNull>(true);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007103 DCHECK_EQ(input->GetType(), DataType::Type::kReference);
Calin Juravleb1498f62015-02-16 13:13:29 +00007104 SetRawInputAt(0, input);
7105 }
7106
Artem Serovcced8ba2017-07-19 18:18:09 +01007107 bool IsClonable() const OVERRIDE { return true; }
7108
David Brazdilf5552582015-12-27 13:36:12 +00007109 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007110 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00007111 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00007112 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007113
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007114 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007115 DCHECK(GetUpperCanBeNull() || !can_be_null);
7116 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007117 }
7118
Vladimir Markoa1de9182016-02-25 11:37:38 +00007119 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007120
Calin Juravleb1498f62015-02-16 13:13:29 +00007121 DECLARE_INSTRUCTION(BoundType);
7122
Artem Serovcced8ba2017-07-19 18:18:09 +01007123 protected:
7124 DEFAULT_COPY_CONSTRUCTOR(BoundType);
7125
Calin Juravleb1498f62015-02-16 13:13:29 +00007126 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007127 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
7128 // is false then CanBeNull() cannot be true).
Vladimir Markobd785672018-05-03 17:09:09 +01007129 static constexpr size_t kFlagUpperCanBeNull = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007130 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
7131 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
7132 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7133
Calin Juravleb1498f62015-02-16 13:13:29 +00007134 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007135 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
7136 // It is used to bound the type in cases like:
7137 // if (x instanceof ClassX) {
7138 // // uper_bound_ will be ClassX
7139 // }
David Brazdilf5552582015-12-27 13:36:12 +00007140 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00007141};
7142
Vladimir Marko175e7862018-03-27 09:03:13 +00007143class HCheckCast FINAL : public HTypeCheckInstruction {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007144 public:
7145 HCheckCast(HInstruction* object,
Vladimir Marko175e7862018-03-27 09:03:13 +00007146 HInstruction* target_class_or_null,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007147 TypeCheckKind check_kind,
Vladimir Marko175e7862018-03-27 09:03:13 +00007148 Handle<mirror::Class> klass,
7149 uint32_t dex_pc,
7150 ArenaAllocator* allocator,
7151 HIntConstant* bitstring_path_to_root,
7152 HIntConstant* bitstring_mask)
7153 : HTypeCheckInstruction(kCheckCast,
Vladimir Markobd785672018-05-03 17:09:09 +01007154 DataType::Type::kVoid,
Vladimir Marko175e7862018-03-27 09:03:13 +00007155 object,
7156 target_class_or_null,
7157 check_kind,
7158 klass,
7159 dex_pc,
7160 allocator,
7161 bitstring_path_to_root,
7162 bitstring_mask,
7163 SideEffects::CanTriggerGC()) {}
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007164
7165 bool NeedsEnvironment() const OVERRIDE {
7166 // Instruction may throw a CheckCastError.
7167 return true;
7168 }
7169
7170 bool CanThrow() const OVERRIDE { return true; }
7171
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007172 DECLARE_INSTRUCTION(CheckCast);
7173
Artem Serovcced8ba2017-07-19 18:18:09 +01007174 protected:
7175 DEFAULT_COPY_CONSTRUCTOR(CheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007176};
7177
Andreas Gampe26de38b2016-07-27 17:53:11 -07007178/**
7179 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
7180 * @details We define the combined barrier types that are actually required
7181 * by the Java Memory Model, rather than using exactly the terminology from
7182 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
7183 * primitives. Note that the JSR-133 cookbook generally does not deal with
7184 * store atomicity issues, and the recipes there are not always entirely sufficient.
7185 * The current recipe is as follows:
7186 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
7187 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
7188 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
7189 * -# Use StoreStore barrier after all stores but before return from any constructor whose
7190 * class has final fields.
7191 * -# Use NTStoreStore to order non-temporal stores with respect to all later
7192 * store-to-memory instructions. Only generated together with non-temporal stores.
7193 */
7194enum MemBarrierKind {
7195 kAnyStore,
7196 kLoadAny,
7197 kStoreStore,
7198 kAnyAny,
7199 kNTStoreStore,
7200 kLastBarrierKind = kNTStoreStore
7201};
7202std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
7203
Vladimir Markobd785672018-05-03 17:09:09 +01007204class HMemoryBarrier FINAL : public HExpression<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01007205 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007206 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007207 : HExpression(kMemoryBarrier,
7208 SideEffects::AllWritesAndReads(), // Assume write/read on all fields/arrays.
7209 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007210 SetPackedField<BarrierKindField>(barrier_kind);
7211 }
Calin Juravle27df7582015-04-17 19:12:31 +01007212
Artem Serovcced8ba2017-07-19 18:18:09 +01007213 bool IsClonable() const OVERRIDE { return true; }
7214
Vladimir Markoa1de9182016-02-25 11:37:38 +00007215 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01007216
7217 DECLARE_INSTRUCTION(MemoryBarrier);
7218
Artem Serovcced8ba2017-07-19 18:18:09 +01007219 protected:
7220 DEFAULT_COPY_CONSTRUCTOR(MemoryBarrier);
7221
Calin Juravle27df7582015-04-17 19:12:31 +01007222 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007223 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
7224 static constexpr size_t kFieldBarrierKindSize =
7225 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
7226 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
7227 kFieldBarrierKind + kFieldBarrierKindSize;
7228 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
7229 "Too many packed fields.");
7230 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01007231};
7232
Igor Murashkind01745e2017-04-05 16:40:31 -07007233// A constructor fence orders all prior stores to fields that could be accessed via a final field of
7234// the specified object(s), with respect to any subsequent store that might "publish"
7235// (i.e. make visible) the specified object to another thread.
7236//
7237// JLS 17.5.1 "Semantics of final fields" states that a freeze action happens
7238// for all final fields (that were set) at the end of the invoked constructor.
7239//
7240// The constructor fence models the freeze actions for the final fields of an object
7241// being constructed (semantically at the end of the constructor). Constructor fences
7242// have a per-object affinity; two separate objects being constructed get two separate
7243// constructor fences.
7244//
7245// (Note: that if calling a super-constructor or forwarding to another constructor,
7246// the freezes would happen at the end of *that* constructor being invoked).
7247//
7248// The memory model guarantees that when the object being constructed is "published" after
7249// constructor completion (i.e. escapes the current thread via a store), then any final field
7250// writes must be observable on other threads (once they observe that publication).
7251//
7252// Further, anything written before the freeze, and read by dereferencing through the final field,
7253// must also be visible (so final object field could itself have an object with non-final fields;
7254// yet the freeze must also extend to them).
7255//
7256// Constructor example:
7257//
7258// class HasFinal {
7259// final int field; Optimizing IR for <init>()V:
7260// HasFinal() {
7261// field = 123; HInstanceFieldSet(this, HasFinal.field, 123)
7262// // freeze(this.field); HConstructorFence(this)
7263// } HReturn
7264// }
7265//
7266// HConstructorFence can serve double duty as a fence for new-instance/new-array allocations of
7267// already-initialized classes; in that case the allocation must act as a "default-initializer"
7268// of the object which effectively writes the class pointer "final field".
7269//
7270// For example, we can model default-initialiation as roughly the equivalent of the following:
7271//
7272// class Object {
7273// private final Class header;
7274// }
7275//
7276// Java code: Optimizing IR:
7277//
7278// T new_instance<T>() {
7279// Object obj = allocate_memory(T.class.size); obj = HInvoke(art_quick_alloc_object, T)
7280// obj.header = T.class; // header write is done by above call.
7281// // freeze(obj.header) HConstructorFence(obj)
7282// return (T)obj;
7283// }
7284//
7285// See also:
7286// * CompilerDriver::RequiresConstructorBarrier
7287// * QuasiAtomic::ThreadFenceForConstructor
7288//
7289class HConstructorFence FINAL : public HVariableInputSizeInstruction {
7290 // A fence has variable inputs because the inputs can be removed
7291 // after prepare_for_register_allocation phase.
7292 // (TODO: In the future a fence could freeze multiple objects
7293 // after merging two fences together.)
7294 public:
7295 // `fence_object` is the reference that needs to be protected for correct publication.
7296 //
7297 // It makes sense in the following situations:
7298 // * <init> constructors, it's the "this" parameter (i.e. HParameterValue, s.t. IsThis() == true).
7299 // * new-instance-like instructions, it's the return value (i.e. HNewInstance).
7300 //
7301 // After construction the `fence_object` becomes the 0th input.
7302 // This is not an input in a real sense, but just a convenient place to stash the information
7303 // about the associated object.
7304 HConstructorFence(HInstruction* fence_object,
7305 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007306 ArenaAllocator* allocator)
Igor Murashkind01745e2017-04-05 16:40:31 -07007307 // We strongly suspect there is not a more accurate way to describe the fine-grained reordering
7308 // constraints described in the class header. We claim that these SideEffects constraints
7309 // enforce a superset of the real constraints.
7310 //
7311 // The ordering described above is conservatively modeled with SideEffects as follows:
7312 //
7313 // * To prevent reordering of the publication stores:
7314 // ----> "Reads of objects" is the initial SideEffect.
7315 // * For every primitive final field store in the constructor:
7316 // ----> Union that field's type as a read (e.g. "Read of T") into the SideEffect.
7317 // * If there are any stores to reference final fields in the constructor:
7318 // ----> Use a more conservative "AllReads" SideEffect because any stores to any references
7319 // that are reachable from `fence_object` also need to be prevented for reordering
7320 // (and we do not want to do alias analysis to figure out what those stores are).
7321 //
7322 // In the implementation, this initially starts out as an "all reads" side effect; this is an
7323 // even more conservative approach than the one described above, and prevents all of the
7324 // above reordering without analyzing any of the instructions in the constructor.
7325 //
7326 // If in a later phase we discover that there are no writes to reference final fields,
7327 // we can refine the side effect to a smaller set of type reads (see above constraints).
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307328 : HVariableInputSizeInstruction(kConstructorFence,
7329 SideEffects::AllReads(),
Igor Murashkind01745e2017-04-05 16:40:31 -07007330 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007331 allocator,
Igor Murashkind01745e2017-04-05 16:40:31 -07007332 /* number_of_inputs */ 1,
7333 kArenaAllocConstructorFenceInputs) {
7334 DCHECK(fence_object != nullptr);
7335 SetRawInputAt(0, fence_object);
7336 }
7337
7338 // The object associated with this constructor fence.
7339 //
7340 // (Note: This will be null after the prepare_for_register_allocation phase,
7341 // as all constructor fence inputs are removed there).
7342 HInstruction* GetFenceObject() const {
7343 return InputAt(0);
7344 }
7345
7346 // Find all the HConstructorFence uses (`fence_use`) for `this` and:
7347 // - Delete `fence_use` from `this`'s use list.
7348 // - Delete `this` from `fence_use`'s inputs list.
7349 // - If the `fence_use` is dead, remove it from the graph.
7350 //
7351 // A fence is considered dead once it no longer has any uses
7352 // and all of the inputs are dead.
7353 //
7354 // This must *not* be called during/after prepare_for_register_allocation,
7355 // because that removes all the inputs to the fences but the fence is actually
7356 // still considered live.
Igor Murashkin6ef45672017-08-08 13:59:55 -07007357 //
7358 // Returns how many HConstructorFence instructions were removed from graph.
7359 static size_t RemoveConstructorFences(HInstruction* instruction);
Igor Murashkind01745e2017-04-05 16:40:31 -07007360
Igor Murashkindd018df2017-08-09 10:38:31 -07007361 // Combine all inputs of `this` and `other` instruction and remove
7362 // `other` from the graph.
7363 //
7364 // Inputs are unique after the merge.
7365 //
7366 // Requirement: `this` must not be the same as `other.
7367 void Merge(HConstructorFence* other);
7368
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007369 // Check if this constructor fence is protecting
7370 // an HNewInstance or HNewArray that is also the immediate
7371 // predecessor of `this`.
7372 //
Igor Murashkindd018df2017-08-09 10:38:31 -07007373 // If `ignore_inputs` is true, then the immediate predecessor doesn't need
7374 // to be one of the inputs of `this`.
7375 //
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007376 // Returns the associated HNewArray or HNewInstance,
7377 // or null otherwise.
Igor Murashkindd018df2017-08-09 10:38:31 -07007378 HInstruction* GetAssociatedAllocation(bool ignore_inputs = false);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007379
Igor Murashkind01745e2017-04-05 16:40:31 -07007380 DECLARE_INSTRUCTION(ConstructorFence);
7381
Artem Serovcced8ba2017-07-19 18:18:09 +01007382 protected:
7383 DEFAULT_COPY_CONSTRUCTOR(ConstructorFence);
Igor Murashkind01745e2017-04-05 16:40:31 -07007384};
7385
Vladimir Markobd785672018-05-03 17:09:09 +01007386class HMonitorOperation FINAL : public HExpression<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007387 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007388 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007389 kEnter,
7390 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00007391 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007392 };
7393
7394 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007395 : HExpression(kMonitorOperation,
7396 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
7397 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007398 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007399 SetRawInputAt(0, object);
7400 }
7401
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007402 // Instruction may go into runtime, so we need an environment.
7403 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00007404
7405 bool CanThrow() const OVERRIDE {
7406 // Verifier guarantees that monitor-exit cannot throw.
7407 // This is important because it allows the HGraphBuilder to remove
7408 // a dead throw-catch loop generated for `synchronized` blocks/methods.
7409 return IsEnter();
7410 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007411
Vladimir Markoa1de9182016-02-25 11:37:38 +00007412 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
7413 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007414
7415 DECLARE_INSTRUCTION(MonitorOperation);
7416
Artem Serovcced8ba2017-07-19 18:18:09 +01007417 protected:
7418 DEFAULT_COPY_CONSTRUCTOR(MonitorOperation);
7419
Calin Juravle52c48962014-12-16 17:02:57 +00007420 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007421 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
7422 static constexpr size_t kFieldOperationKindSize =
7423 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
7424 static constexpr size_t kNumberOfMonitorOperationPackedBits =
7425 kFieldOperationKind + kFieldOperationKindSize;
7426 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7427 "Too many packed fields.");
7428 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007429};
7430
Vladimir Markofcb503c2016-05-18 12:48:17 +01007431class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00007432 public:
7433 HSelect(HInstruction* condition,
7434 HInstruction* true_value,
7435 HInstruction* false_value,
7436 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307437 : HExpression(kSelect, HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
David Brazdil74eb1b22015-12-14 11:44:01 +00007438 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
7439
7440 // First input must be `true_value` or `false_value` to allow codegens to
7441 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
7442 // that architectures which implement HSelect as a conditional move also
7443 // will not need to invert the condition.
7444 SetRawInputAt(0, false_value);
7445 SetRawInputAt(1, true_value);
7446 SetRawInputAt(2, condition);
7447 }
7448
Artem Serovcced8ba2017-07-19 18:18:09 +01007449 bool IsClonable() const OVERRIDE { return true; }
David Brazdil74eb1b22015-12-14 11:44:01 +00007450 HInstruction* GetFalseValue() const { return InputAt(0); }
7451 HInstruction* GetTrueValue() const { return InputAt(1); }
7452 HInstruction* GetCondition() const { return InputAt(2); }
7453
7454 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01007455 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
7456 return true;
7457 }
David Brazdil74eb1b22015-12-14 11:44:01 +00007458
7459 bool CanBeNull() const OVERRIDE {
7460 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
7461 }
7462
7463 DECLARE_INSTRUCTION(Select);
7464
Artem Serovcced8ba2017-07-19 18:18:09 +01007465 protected:
7466 DEFAULT_COPY_CONSTRUCTOR(Select);
David Brazdil74eb1b22015-12-14 11:44:01 +00007467};
7468
Vladimir Markof9f64412015-09-02 14:05:49 +01007469class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007470 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01007471 MoveOperands(Location source,
7472 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007473 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007474 HInstruction* instruction)
7475 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007476
7477 Location GetSource() const { return source_; }
7478 Location GetDestination() const { return destination_; }
7479
7480 void SetSource(Location value) { source_ = value; }
7481 void SetDestination(Location value) { destination_ = value; }
7482
7483 // The parallel move resolver marks moves as "in-progress" by clearing the
7484 // destination (but not the source).
7485 Location MarkPending() {
7486 DCHECK(!IsPending());
7487 Location dest = destination_;
7488 destination_ = Location::NoLocation();
7489 return dest;
7490 }
7491
7492 void ClearPending(Location dest) {
7493 DCHECK(IsPending());
7494 destination_ = dest;
7495 }
7496
7497 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00007498 DCHECK(source_.IsValid() || destination_.IsInvalid());
7499 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007500 }
7501
7502 // True if this blocks a move from the given location.
7503 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08007504 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007505 }
7506
7507 // A move is redundant if it's been eliminated, if its source and
7508 // destination are the same, or if its destination is unneeded.
7509 bool IsRedundant() const {
7510 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
7511 }
7512
7513 // We clear both operands to indicate move that's been eliminated.
7514 void Eliminate() {
7515 source_ = destination_ = Location::NoLocation();
7516 }
7517
7518 bool IsEliminated() const {
7519 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
7520 return source_.IsInvalid();
7521 }
7522
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007523 DataType::Type GetType() const { return type_; }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007524
Nicolas Geoffray90218252015-04-15 11:56:51 +01007525 bool Is64BitMove() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007526 return DataType::Is64BitType(type_);
Nicolas Geoffray90218252015-04-15 11:56:51 +01007527 }
7528
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007529 HInstruction* GetInstruction() const { return instruction_; }
7530
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007531 private:
7532 Location source_;
7533 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01007534 // The type this move is for.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007535 DataType::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007536 // The instruction this move is assocatied with. Null when this move is
7537 // for moving an input in the expected locations of user (including a phi user).
7538 // This is only used in debug mode, to ensure we do not connect interval siblings
7539 // in the same parallel move.
7540 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007541};
7542
Roland Levillainc9285912015-12-18 10:38:42 +00007543std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
7544
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007545static constexpr size_t kDefaultNumberOfMoves = 4;
7546
Vladimir Markobd785672018-05-03 17:09:09 +01007547class HParallelMove FINAL : public HExpression<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007548 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007549 explicit HParallelMove(ArenaAllocator* allocator, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007550 : HExpression(kParallelMove, SideEffects::None(), dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007551 moves_(allocator->Adapter(kArenaAllocMoveOperands)) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007552 moves_.reserve(kDefaultNumberOfMoves);
7553 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007554
Nicolas Geoffray90218252015-04-15 11:56:51 +01007555 void AddMove(Location source,
7556 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007557 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007558 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00007559 DCHECK(source.IsValid());
7560 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007561 if (kIsDebugBuild) {
7562 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007563 for (const MoveOperands& move : moves_) {
7564 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007565 // Special case the situation where the move is for the spill slot
7566 // of the instruction.
7567 if ((GetPrevious() == instruction)
7568 || ((GetPrevious() == nullptr)
7569 && instruction->IsPhi()
7570 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007571 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007572 << "Doing parallel moves for the same instruction.";
7573 } else {
7574 DCHECK(false) << "Doing parallel moves for the same instruction.";
7575 }
7576 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00007577 }
7578 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007579 for (const MoveOperands& move : moves_) {
7580 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007581 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01007582 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007583 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007584 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007585 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007586 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007587 }
7588
Vladimir Marko225b6462015-09-28 12:17:40 +01007589 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007590 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007591 }
7592
Vladimir Marko225b6462015-09-28 12:17:40 +01007593 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007594
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01007595 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007596
Artem Serovcced8ba2017-07-19 18:18:09 +01007597 protected:
7598 DEFAULT_COPY_CONSTRUCTOR(ParallelMove);
7599
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007600 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01007601 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007602};
7603
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007604// This instruction computes an intermediate address pointing in the 'middle' of an object. The
7605// result pointer cannot be handled by GC, so extra care is taken to make sure that this value is
7606// never used across anything that can trigger GC.
7607// The result of this instruction is not a pointer in the sense of `DataType::Type::kreference`.
7608// So we represent it by the type `DataType::Type::kInt`.
7609class HIntermediateAddress FINAL : public HExpression<2> {
7610 public:
7611 HIntermediateAddress(HInstruction* base_address, HInstruction* offset, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307612 : HExpression(kIntermediateAddress,
7613 DataType::Type::kInt32,
7614 SideEffects::DependsOnGC(),
7615 dex_pc) {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007616 DCHECK_EQ(DataType::Size(DataType::Type::kInt32),
7617 DataType::Size(DataType::Type::kReference))
7618 << "kPrimInt and kPrimNot have different sizes.";
7619 SetRawInputAt(0, base_address);
7620 SetRawInputAt(1, offset);
7621 }
7622
Artem Serovcced8ba2017-07-19 18:18:09 +01007623 bool IsClonable() const OVERRIDE { return true; }
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007624 bool CanBeMoved() const OVERRIDE { return true; }
7625 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
7626 return true;
7627 }
7628 bool IsActualObject() const OVERRIDE { return false; }
7629
7630 HInstruction* GetBaseAddress() const { return InputAt(0); }
7631 HInstruction* GetOffset() const { return InputAt(1); }
7632
7633 DECLARE_INSTRUCTION(IntermediateAddress);
7634
Artem Serovcced8ba2017-07-19 18:18:09 +01007635 protected:
7636 DEFAULT_COPY_CONSTRUCTOR(IntermediateAddress);
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007637};
7638
7639
Mark Mendell0616ae02015-04-17 12:49:27 -04007640} // namespace art
7641
Aart Bikf8f5a162017-02-06 15:35:29 -08007642#include "nodes_vector.h"
7643
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007644#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
7645#include "nodes_shared.h"
7646#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007647#ifdef ART_ENABLE_CODEGEN_mips
7648#include "nodes_mips.h"
7649#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04007650#ifdef ART_ENABLE_CODEGEN_x86
7651#include "nodes_x86.h"
7652#endif
7653
7654namespace art {
7655
Igor Murashkin6ef45672017-08-08 13:59:55 -07007656class OptimizingCompilerStats;
7657
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007658class HGraphVisitor : public ValueObject {
7659 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007660 explicit HGraphVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7661 : stats_(stats),
7662 graph_(graph) {}
Dave Allison20dfc792014-06-16 20:44:29 -07007663 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007664
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007665 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007666 virtual void VisitBasicBlock(HBasicBlock* block);
7667
Roland Levillain633021e2014-10-01 14:12:25 +01007668 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007669 void VisitInsertionOrder();
7670
Roland Levillain633021e2014-10-01 14:12:25 +01007671 // Visit the graph following dominator tree reverse post-order.
7672 void VisitReversePostOrder();
7673
Nicolas Geoffray787c3072014-03-17 10:20:19 +00007674 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007675
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007676 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007677#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007678 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
7679
7680 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7681
7682#undef DECLARE_VISIT_INSTRUCTION
7683
Igor Murashkin6ef45672017-08-08 13:59:55 -07007684 protected:
7685 OptimizingCompilerStats* stats_;
7686
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007687 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07007688 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007689
7690 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
7691};
7692
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007693class HGraphDelegateVisitor : public HGraphVisitor {
7694 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007695 explicit HGraphDelegateVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7696 : HGraphVisitor(graph, stats) {}
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007697 virtual ~HGraphDelegateVisitor() {}
7698
7699 // Visit functions that delegate to to super class.
7700#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00007701 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007702
7703 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7704
7705#undef DECLARE_VISIT_INSTRUCTION
7706
7707 private:
7708 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
7709};
7710
Artem Serovcced8ba2017-07-19 18:18:09 +01007711// Create a clone of the instruction, insert it into the graph; replace the old one with a new
7712// and remove the old instruction.
7713HInstruction* ReplaceInstrOrPhiByClone(HInstruction* instr);
7714
7715// Create a clone for each clonable instructions/phis and replace the original with the clone.
7716//
7717// Used for testing individual instruction cloner.
7718class CloneAndReplaceInstructionVisitor : public HGraphDelegateVisitor {
7719 public:
7720 explicit CloneAndReplaceInstructionVisitor(HGraph* graph)
Artem Serov7f4aff62017-06-21 17:02:18 +01007721 : HGraphDelegateVisitor(graph), instr_replaced_by_clones_count_(0) {}
Artem Serovcced8ba2017-07-19 18:18:09 +01007722
7723 void VisitInstruction(HInstruction* instruction) OVERRIDE {
7724 if (instruction->IsClonable()) {
7725 ReplaceInstrOrPhiByClone(instruction);
Artem Serov7f4aff62017-06-21 17:02:18 +01007726 instr_replaced_by_clones_count_++;
Artem Serovcced8ba2017-07-19 18:18:09 +01007727 }
7728 }
7729
Artem Serov7f4aff62017-06-21 17:02:18 +01007730 size_t GetInstrReplacedByClonesCount() const { return instr_replaced_by_clones_count_; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007731
7732 private:
Artem Serov7f4aff62017-06-21 17:02:18 +01007733 size_t instr_replaced_by_clones_count_;
Artem Serovcced8ba2017-07-19 18:18:09 +01007734
7735 DISALLOW_COPY_AND_ASSIGN(CloneAndReplaceInstructionVisitor);
7736};
7737
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007738// Iterator over the blocks that art part of the loop. Includes blocks part
7739// of an inner loop. The order in which the blocks are iterated is on their
7740// block id.
7741class HBlocksInLoopIterator : public ValueObject {
7742 public:
7743 explicit HBlocksInLoopIterator(const HLoopInformation& info)
7744 : blocks_in_loop_(info.GetBlocks()),
7745 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
7746 index_(0) {
7747 if (!blocks_in_loop_.IsBitSet(index_)) {
7748 Advance();
7749 }
7750 }
7751
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007752 bool Done() const { return index_ == blocks_.size(); }
7753 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007754 void Advance() {
7755 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007756 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007757 if (blocks_in_loop_.IsBitSet(index_)) {
7758 break;
7759 }
7760 }
7761 }
7762
7763 private:
7764 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007765 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007766 size_t index_;
7767
7768 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
7769};
7770
Mingyao Yang3584bce2015-05-19 16:01:59 -07007771// Iterator over the blocks that art part of the loop. Includes blocks part
7772// of an inner loop. The order in which the blocks are iterated is reverse
7773// post order.
7774class HBlocksInLoopReversePostOrderIterator : public ValueObject {
7775 public:
7776 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
7777 : blocks_in_loop_(info.GetBlocks()),
7778 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
7779 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007780 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007781 Advance();
7782 }
7783 }
7784
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007785 bool Done() const { return index_ == blocks_.size(); }
7786 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07007787 void Advance() {
7788 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007789 for (size_t e = blocks_.size(); index_ < e; ++index_) {
7790 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007791 break;
7792 }
7793 }
7794 }
7795
7796 private:
7797 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007798 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07007799 size_t index_;
7800
7801 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
7802};
7803
Aart Bikf3e61ee2017-04-12 17:09:20 -07007804// Returns int64_t value of a properly typed constant.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007805inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00007806 if (constant->IsIntConstant()) {
7807 return constant->AsIntConstant()->GetValue();
7808 } else if (constant->IsLongConstant()) {
7809 return constant->AsLongConstant()->GetValue();
7810 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00007811 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00007812 return 0;
7813 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007814}
7815
Aart Bikf3e61ee2017-04-12 17:09:20 -07007816// Returns true iff instruction is an integral constant (and sets value on success).
7817inline bool IsInt64AndGet(HInstruction* instruction, /*out*/ int64_t* value) {
7818 if (instruction->IsIntConstant()) {
7819 *value = instruction->AsIntConstant()->GetValue();
7820 return true;
7821 } else if (instruction->IsLongConstant()) {
7822 *value = instruction->AsLongConstant()->GetValue();
7823 return true;
7824 } else if (instruction->IsNullConstant()) {
7825 *value = 0;
7826 return true;
7827 }
7828 return false;
7829}
7830
Aart Bik0148de42017-09-05 09:25:01 -07007831// Returns true iff instruction is the given integral constant.
7832inline bool IsInt64Value(HInstruction* instruction, int64_t value) {
7833 int64_t val = 0;
7834 return IsInt64AndGet(instruction, &val) && val == value;
7835}
7836
7837// Returns true iff instruction is a zero bit pattern.
7838inline bool IsZeroBitPattern(HInstruction* instruction) {
7839 return instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern();
7840}
7841
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01007842// Implement HInstruction::Is##type() for concrete instructions.
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007843#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01007844 inline bool HInstruction::Is##type() const { return GetKind() == k##type; }
7845 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
7846#undef INSTRUCTION_TYPE_CHECK
7847
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01007848// Implement HInstruction::Is##type() for abstract instructions.
Vladimir Markoa90dd512018-05-04 15:04:45 +01007849#define INSTRUCTION_TYPE_CHECK_RESULT(type, super) \
7850 std::is_base_of<BaseType, H##type>::value,
7851#define INSTRUCTION_TYPE_CHECK(type, super) \
7852 inline bool HInstruction::Is##type() const { \
7853 DCHECK_LT(GetKind(), kLastInstructionKind); \
7854 using BaseType = H##type; \
7855 static constexpr bool results[] = { \
7856 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK_RESULT) \
7857 }; \
7858 return results[static_cast<size_t>(GetKind())]; \
7859 }
7860
7861 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
7862#undef INSTRUCTION_TYPE_CHECK
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01007863#undef INSTRUCTION_TYPE_CHECK_RESULT
Vladimir Markoa90dd512018-05-04 15:04:45 +01007864
7865#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007866 inline const H##type* HInstruction::As##type() const { \
7867 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
7868 } \
7869 inline H##type* HInstruction::As##type() { \
7870 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
7871 }
7872
Vladimir Markoa90dd512018-05-04 15:04:45 +01007873 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
7874#undef INSTRUCTION_TYPE_CAST
7875
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007876
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00007877// Create space in `blocks` for adding `number_of_new_blocks` entries
7878// starting at location `at`. Blocks after `at` are moved accordingly.
7879inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
7880 size_t number_of_new_blocks,
7881 size_t after) {
7882 DCHECK_LT(after, blocks->size());
7883 size_t old_size = blocks->size();
7884 size_t new_size = old_size + number_of_new_blocks;
7885 blocks->resize(new_size);
7886 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
7887}
7888
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00007889/*
7890 * Hunt "under the hood" of array lengths (leading to array references),
7891 * null checks (also leading to array references), and new arrays
7892 * (leading to the actual length). This makes it more likely related
7893 * instructions become actually comparable.
7894 */
7895inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
7896 while (instruction->IsArrayLength() ||
7897 instruction->IsNullCheck() ||
7898 instruction->IsNewArray()) {
7899 instruction = instruction->IsNewArray()
7900 ? instruction->AsNewArray()->GetLength()
7901 : instruction->InputAt(0);
7902 }
7903 return instruction;
7904}
7905
Artem Serov21c7e6f2017-07-27 16:04:42 +01007906void RemoveEnvironmentUses(HInstruction* instruction);
7907bool HasEnvironmentUsedByOthers(HInstruction* instruction);
7908void ResetEnvironmentInputRecords(HInstruction* instruction);
7909
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007910} // namespace art
7911
7912#endif // ART_COMPILER_OPTIMIZING_NODES_H_