blob: 54882ff92bf7e552619546385f954c0c678e8e0a [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;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001288 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001289 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001290 bool HasSinglePhi() const;
1291
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001292 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001293 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001294 ArenaVector<HBasicBlock*> predecessors_;
1295 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001296 HInstructionList instructions_;
1297 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001298 HLoopInformation* loop_information_;
1299 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001300 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001301 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001302 // The dex program counter of the first instruction of this block.
1303 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001304 size_t lifetime_start_;
1305 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001306 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001307
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001308 friend class HGraph;
1309 friend class HInstruction;
1310
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001311 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1312};
1313
David Brazdilb2bd1c52015-03-25 11:17:37 +00001314// Iterates over the LoopInformation of all loops which contain 'block'
1315// from the innermost to the outermost.
1316class HLoopInformationOutwardIterator : public ValueObject {
1317 public:
1318 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1319 : current_(block.GetLoopInformation()) {}
1320
1321 bool Done() const { return current_ == nullptr; }
1322
1323 void Advance() {
1324 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001325 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001326 }
1327
1328 HLoopInformation* Current() const {
1329 DCHECK(!Done());
1330 return current_;
1331 }
1332
1333 private:
1334 HLoopInformation* current_;
1335
1336 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1337};
1338
Alexandre Ramesef20f712015-06-09 10:29:30 +01001339#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001340 M(Above, Condition) \
1341 M(AboveOrEqual, Condition) \
Aart Bik3dad3412018-02-28 12:01:46 -08001342 M(Abs, UnaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001343 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001344 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001345 M(ArrayGet, Instruction) \
1346 M(ArrayLength, Instruction) \
1347 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001348 M(Below, Condition) \
1349 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001350 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001351 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001352 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001353 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001354 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001355 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001356 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001357 M(Compare, BinaryOperation) \
Igor Murashkind01745e2017-04-05 16:40:31 -07001358 M(ConstructorFence, Instruction) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001359 M(CurrentMethod, Instruction) \
Mingyao Yang063fc772016-08-02 11:02:54 -07001360 M(ShouldDeoptimizeFlag, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001361 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001362 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001363 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001364 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001365 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001366 M(Exit, Instruction) \
1367 M(FloatConstant, Constant) \
1368 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001369 M(GreaterThan, Condition) \
1370 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001371 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001372 M(InstanceFieldGet, Instruction) \
1373 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001374 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001375 M(IntConstant, Constant) \
xueliang.zhonge0eb4832017-10-30 13:43:14 +00001376 M(IntermediateAddress, Instruction) \
Calin Juravle175dc732015-08-25 15:42:32 +01001377 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001378 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001379 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001380 M(InvokeVirtual, Invoke) \
Orion Hodsonac141392017-01-13 11:53:47 +00001381 M(InvokePolymorphic, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001382 M(LessThan, Condition) \
1383 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001384 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001385 M(LoadException, Instruction) \
Orion Hodson18259d72018-04-12 11:18:23 +01001386 M(LoadMethodType, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001387 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001388 M(LongConstant, Constant) \
Aart Bik1f8d51b2018-02-15 10:42:37 -08001389 M(Max, Instruction) \
Calin Juravle27df7582015-04-17 19:12:31 +01001390 M(MemoryBarrier, Instruction) \
Aart Bik1f8d51b2018-02-15 10:42:37 -08001391 M(Min, BinaryOperation) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001392 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001393 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001394 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001395 M(Neg, UnaryOperation) \
1396 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001397 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001398 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001399 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001400 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001401 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001402 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001403 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001404 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001405 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001406 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001407 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001408 M(Return, Instruction) \
1409 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001410 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001411 M(Shl, BinaryOperation) \
1412 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001413 M(StaticFieldGet, Instruction) \
1414 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001415 M(UnresolvedInstanceFieldGet, Instruction) \
1416 M(UnresolvedInstanceFieldSet, Instruction) \
1417 M(UnresolvedStaticFieldGet, Instruction) \
1418 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001419 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001420 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001421 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001422 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001423 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001424 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001425 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001426 M(Xor, BinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001427 M(VecReplicateScalar, VecUnaryOperation) \
Aart Bik0148de42017-09-05 09:25:01 -07001428 M(VecExtractScalar, VecUnaryOperation) \
1429 M(VecReduce, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001430 M(VecCnv, VecUnaryOperation) \
1431 M(VecNeg, VecUnaryOperation) \
Aart Bik6daebeb2017-04-03 14:35:41 -07001432 M(VecAbs, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001433 M(VecNot, VecUnaryOperation) \
1434 M(VecAdd, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001435 M(VecHalvingAdd, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001436 M(VecSub, VecBinaryOperation) \
1437 M(VecMul, VecBinaryOperation) \
1438 M(VecDiv, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001439 M(VecMin, VecBinaryOperation) \
1440 M(VecMax, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001441 M(VecAnd, VecBinaryOperation) \
1442 M(VecAndNot, VecBinaryOperation) \
1443 M(VecOr, VecBinaryOperation) \
1444 M(VecXor, VecBinaryOperation) \
Aart Bik29aa0822018-03-08 11:28:00 -08001445 M(VecSaturationAdd, VecBinaryOperation) \
1446 M(VecSaturationSub, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001447 M(VecShl, VecBinaryOperation) \
1448 M(VecShr, VecBinaryOperation) \
1449 M(VecUShr, VecBinaryOperation) \
Aart Bik8de59162017-04-21 09:42:01 -07001450 M(VecSetScalars, VecOperation) \
Artem Serovf34dd202017-04-10 17:41:46 +01001451 M(VecMultiplyAccumulate, VecOperation) \
Aart Bikdbbac8f2017-09-01 13:06:08 -07001452 M(VecSADAccumulate, VecOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001453 M(VecLoad, VecMemoryOperation) \
1454 M(VecStore, VecMemoryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001455
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001456/*
1457 * Instructions, shared across several (not all) architectures.
1458 */
1459#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1460#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1461#else
1462#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001463 M(BitwiseNegatedRight, Instruction) \
Anton Kirilov74234da2017-01-13 14:42:47 +00001464 M(DataProcWithShifterOp, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001465 M(MultiplyAccumulate, Instruction) \
Artem Serove1811ed2017-04-27 16:50:47 +01001466 M(IntermediateAddressIndex, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001467#endif
1468
Alexandre Ramesef20f712015-06-09 10:29:30 +01001469#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1470
1471#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1472
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001473#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001474#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001475#else
1476#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1477 M(MipsComputeBaseMethodAddress, Instruction) \
Lena Djokica2901602017-09-21 13:50:52 +02001478 M(MipsPackedSwitch, Instruction) \
1479 M(IntermediateArrayAddressIndex, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001480#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001481
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001482#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1483
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001484#ifndef ART_ENABLE_CODEGEN_x86
1485#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1486#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001487#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1488 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001489 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001490 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001491 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001492#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001493
1494#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1495
1496#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1497 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001498 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001499 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1500 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001501 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001502 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001503 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1504 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1505
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001506#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1507 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001508 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001509 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001510 M(BinaryOperation, Instruction) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001511 M(Invoke, Instruction) \
1512 M(VecOperation, Instruction) \
1513 M(VecUnaryOperation, VecOperation) \
1514 M(VecBinaryOperation, VecOperation) \
1515 M(VecMemoryOperation, VecOperation)
Dave Allison20dfc792014-06-16 20:44:29 -07001516
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001517#define FOR_EACH_INSTRUCTION(M) \
1518 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1519 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1520
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001521#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001522FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1523#undef FORWARD_DECLARATION
1524
Vladimir Marko372f10e2016-05-17 16:30:10 +01001525#define DECLARE_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001526 private: \
1527 H##type& operator=(const H##type&) = delete; \
1528 public: \
Vladimir Marko372f10e2016-05-17 16:30:10 +01001529 const char* DebugName() const OVERRIDE { return #type; } \
Artem Serovcced8ba2017-07-19 18:18:09 +01001530 HInstruction* Clone(ArenaAllocator* arena) const OVERRIDE { \
1531 DCHECK(IsClonable()); \
1532 return new (arena) H##type(*this->As##type()); \
1533 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001534 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001535
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001536#define DECLARE_ABSTRACT_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001537 private: \
1538 H##type& operator=(const H##type&) = delete; \
Vladimir Markoa90dd512018-05-04 15:04:45 +01001539 public:
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001540
Artem Serovcced8ba2017-07-19 18:18:09 +01001541#define DEFAULT_COPY_CONSTRUCTOR(type) \
1542 explicit H##type(const H##type& other) = default;
1543
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001544template <typename T>
Vladimir Marko82b07402017-03-01 19:02:04 +00001545class HUseListNode : public ArenaObject<kArenaAllocUseListNode>,
1546 public IntrusiveForwardListNode<HUseListNode<T>> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001547 public:
Igor Murashkind01745e2017-04-05 16:40:31 -07001548 // Get the instruction which has this use as one of the inputs.
David Brazdiled596192015-01-23 10:39:45 +00001549 T GetUser() const { return user_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001550 // Get the position of the input record that this use corresponds to.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001551 size_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001552 // Set the position of the input record that this use corresponds to.
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001553 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001554
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001555 private:
David Brazdiled596192015-01-23 10:39:45 +00001556 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001557 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001558
1559 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001560 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001561
Vladimir Marko46817b82016-03-29 12:21:58 +01001562 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001563
1564 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1565};
1566
David Brazdiled596192015-01-23 10:39:45 +00001567template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001568using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001569
David Brazdil1abb4192015-02-17 18:33:36 +00001570// This class is used by HEnvironment and HInstruction classes to record the
1571// instructions they use and pointers to the corresponding HUseListNodes kept
1572// by the used instructions.
1573template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001574class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001575 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001576 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1577 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001578
Vladimir Marko46817b82016-03-29 12:21:58 +01001579 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1580 : HUserRecord(old_record.instruction_, before_use_node) {}
1581 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1582 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001583 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001584 }
1585
1586 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001587 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1588 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001589
1590 private:
1591 // Instruction used by the user.
1592 HInstruction* instruction_;
1593
Vladimir Marko46817b82016-03-29 12:21:58 +01001594 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1595 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001596};
1597
Vladimir Markoe9004912016-06-16 16:50:52 +01001598// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1599// This is used for HInstruction::GetInputs() to return a container wrapper providing
1600// HInstruction* values even though the underlying container has HUserRecord<>s.
1601struct HInputExtractor {
1602 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1603 return record.GetInstruction();
1604 }
1605 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1606 return record.GetInstruction();
1607 }
1608};
1609
1610using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1611using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1612
Aart Bik854a02b2015-07-14 16:07:00 -07001613/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001614 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001615 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001616 * For write/read dependences on fields/arrays, the dependence analysis uses
1617 * type disambiguation (e.g. a float field write cannot modify the value of an
1618 * integer field read) and the access type (e.g. a reference array write cannot
1619 * modify the value of a reference field read [although it may modify the
1620 * reference fetch prior to reading the field, which is represented by its own
1621 * write/read dependence]). The analysis makes conservative points-to
1622 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1623 * the same, and any reference read depends on any reference read without
1624 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001625 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001626 * The internal representation uses 38-bit and is described in the table below.
1627 * The first line indicates the side effect, and for field/array accesses the
1628 * second line indicates the type of the access (in the order of the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001629 * DataType::Type enum).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001630 * The two numbered lines below indicate the bit position in the bitfield (read
1631 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001632 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001633 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1634 * +-------------+---------+---------+--------------+---------+---------+
1635 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1636 * | 3 |333333322|222222221| 1 |111111110|000000000|
1637 * | 7 |654321098|765432109| 8 |765432109|876543210|
1638 *
1639 * Note that, to ease the implementation, 'changes' bits are least significant
1640 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001641 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001642class SideEffects : public ValueObject {
1643 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001644 SideEffects() : flags_(0) {}
1645
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001646 static SideEffects None() {
1647 return SideEffects(0);
1648 }
1649
1650 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001651 return SideEffects(kAllChangeBits | kAllDependOnBits);
1652 }
1653
1654 static SideEffects AllChanges() {
1655 return SideEffects(kAllChangeBits);
1656 }
1657
1658 static SideEffects AllDependencies() {
1659 return SideEffects(kAllDependOnBits);
1660 }
1661
1662 static SideEffects AllExceptGCDependency() {
1663 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1664 }
1665
1666 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001667 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001668 }
1669
Aart Bik34c3ba92015-07-20 14:08:59 -07001670 static SideEffects AllWrites() {
1671 return SideEffects(kAllWrites);
1672 }
1673
1674 static SideEffects AllReads() {
1675 return SideEffects(kAllReads);
1676 }
1677
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001678 static SideEffects FieldWriteOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001679 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001680 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001681 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001682 }
1683
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001684 static SideEffects ArrayWriteOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001685 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001686 }
1687
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001688 static SideEffects FieldReadOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001689 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001690 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001691 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001692 }
1693
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001694 static SideEffects ArrayReadOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001695 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001696 }
1697
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001698 static SideEffects CanTriggerGC() {
1699 return SideEffects(1ULL << kCanTriggerGCBit);
1700 }
1701
1702 static SideEffects DependsOnGC() {
1703 return SideEffects(1ULL << kDependsOnGCBit);
1704 }
1705
Aart Bik854a02b2015-07-14 16:07:00 -07001706 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001707 SideEffects Union(SideEffects other) const {
1708 return SideEffects(flags_ | other.flags_);
1709 }
1710
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001711 SideEffects Exclusion(SideEffects other) const {
1712 return SideEffects(flags_ & ~other.flags_);
1713 }
1714
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001715 void Add(SideEffects other) {
1716 flags_ |= other.flags_;
1717 }
1718
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001719 bool Includes(SideEffects other) const {
1720 return (other.flags_ & flags_) == other.flags_;
1721 }
1722
1723 bool HasSideEffects() const {
1724 return (flags_ & kAllChangeBits);
1725 }
1726
1727 bool HasDependencies() const {
1728 return (flags_ & kAllDependOnBits);
1729 }
1730
1731 // Returns true if there are no side effects or dependencies.
1732 bool DoesNothing() const {
1733 return flags_ == 0;
1734 }
1735
Aart Bik854a02b2015-07-14 16:07:00 -07001736 // Returns true if something is written.
1737 bool DoesAnyWrite() const {
1738 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001739 }
1740
Aart Bik854a02b2015-07-14 16:07:00 -07001741 // Returns true if something is read.
1742 bool DoesAnyRead() const {
1743 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001744 }
1745
Aart Bik854a02b2015-07-14 16:07:00 -07001746 // Returns true if potentially everything is written and read
1747 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001748 bool DoesAllReadWrite() const {
1749 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1750 }
1751
Aart Bik854a02b2015-07-14 16:07:00 -07001752 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001753 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001754 }
1755
Roland Levillain0d5a2812015-11-13 10:07:31 +00001756 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001757 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001758 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1759 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001760 }
1761
1762 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001763 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001764 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001765 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001766 for (int s = kLastBit; s >= 0; s--) {
1767 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1768 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1769 // This is a bit for the GC side effect.
1770 if (current_bit_is_set) {
1771 flags += "GC";
1772 }
Aart Bik854a02b2015-07-14 16:07:00 -07001773 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001774 } else {
1775 // This is a bit for the array/field analysis.
1776 // The underscore character stands for the 'can trigger GC' bit.
1777 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1778 if (current_bit_is_set) {
1779 flags += kDebug[s];
1780 }
1781 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1782 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1783 flags += "|";
1784 }
1785 }
Aart Bik854a02b2015-07-14 16:07:00 -07001786 }
1787 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001788 }
1789
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001790 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001791
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001792 private:
1793 static constexpr int kFieldArrayAnalysisBits = 9;
1794
1795 static constexpr int kFieldWriteOffset = 0;
1796 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1797 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1798 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1799
1800 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1801
1802 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1803 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1804 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1805 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1806
1807 static constexpr int kLastBit = kDependsOnGCBit;
1808 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1809
1810 // Aliases.
1811
1812 static_assert(kChangeBits == kDependOnBits,
1813 "the 'change' bits should match the 'depend on' bits.");
1814
1815 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1816 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1817 static constexpr uint64_t kAllWrites =
1818 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1819 static constexpr uint64_t kAllReads =
1820 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001821
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001822 // Translates type to bit flag. The type must correspond to a Java type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001823 static uint64_t TypeFlag(DataType::Type type, int offset) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001824 int shift;
1825 switch (type) {
1826 case DataType::Type::kReference: shift = 0; break;
1827 case DataType::Type::kBool: shift = 1; break;
1828 case DataType::Type::kInt8: shift = 2; break;
1829 case DataType::Type::kUint16: shift = 3; break;
1830 case DataType::Type::kInt16: shift = 4; break;
1831 case DataType::Type::kInt32: shift = 5; break;
1832 case DataType::Type::kInt64: shift = 6; break;
1833 case DataType::Type::kFloat32: shift = 7; break;
1834 case DataType::Type::kFloat64: shift = 8; break;
1835 default:
1836 LOG(FATAL) << "Unexpected data type " << type;
1837 UNREACHABLE();
1838 }
Aart Bik854a02b2015-07-14 16:07:00 -07001839 DCHECK_LE(kFieldWriteOffset, shift);
1840 DCHECK_LT(shift, kArrayWriteOffset);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001841 return UINT64_C(1) << (shift + offset);
Aart Bik854a02b2015-07-14 16:07:00 -07001842 }
1843
1844 // Private constructor on direct flags value.
1845 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1846
1847 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001848};
1849
David Brazdiled596192015-01-23 10:39:45 +00001850// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001851class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001852 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001853 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
Mingyao Yang01b47b02017-02-03 12:09:57 -08001854 size_t number_of_vregs,
1855 ArtMethod* method,
1856 uint32_t dex_pc,
1857 HInstruction* holder)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001858 : vregs_(number_of_vregs, allocator->Adapter(kArenaAllocEnvironmentVRegs)),
1859 locations_(allocator->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001860 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001861 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001862 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001863 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001864 }
1865
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001866 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
1867 const HEnvironment& to_copy,
1868 HInstruction* holder)
1869 : HEnvironment(allocator,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001870 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001871 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001872 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001873 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001874
Vladimir Markoec32f642017-06-02 10:51:55 +01001875 void AllocateLocations() {
1876 DCHECK(locations_.empty());
1877 locations_.resize(vregs_.size());
1878 }
1879
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001880 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001881 if (parent_ != nullptr) {
1882 parent_->SetAndCopyParentChain(allocator, parent);
1883 } else {
1884 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1885 parent_->CopyFrom(parent);
1886 if (parent->GetParent() != nullptr) {
1887 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1888 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001889 }
David Brazdiled596192015-01-23 10:39:45 +00001890 }
1891
Vladimir Marko69d310e2017-10-09 14:12:23 +01001892 void CopyFrom(ArrayRef<HInstruction* const> locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001893 void CopyFrom(HEnvironment* environment);
1894
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001895 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1896 // input to the loop phi instead. This is for inserting instructions that
1897 // require an environment (like HDeoptimization) in the loop pre-header.
1898 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001899
1900 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001901 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001902 }
1903
1904 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001905 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001906 }
1907
David Brazdil1abb4192015-02-17 18:33:36 +00001908 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001909
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001910 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001911
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001912 HEnvironment* GetParent() const { return parent_; }
1913
1914 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001915 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001916 }
1917
1918 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001919 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001920 }
1921
1922 uint32_t GetDexPc() const {
1923 return dex_pc_;
1924 }
1925
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001926 ArtMethod* GetMethod() const {
1927 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001928 }
1929
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001930 HInstruction* GetHolder() const {
1931 return holder_;
1932 }
1933
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001934
1935 bool IsFromInlinedInvoke() const {
1936 return GetParent() != nullptr;
1937 }
1938
David Brazdiled596192015-01-23 10:39:45 +00001939 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001940 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1941 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001942 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001943 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001944 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00001945
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001946 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001947 HInstruction* const holder_;
1948
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001949 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001950
1951 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1952};
1953
Vladimir Markof9f64412015-09-02 14:05:49 +01001954class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001955 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05301956#define DECLARE_KIND(type, super) k##type,
1957 enum InstructionKind {
Vladimir Markoe3946222018-05-04 14:18:47 +01001958 FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_KIND)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05301959 kLastInstructionKind
1960 };
1961#undef DECLARE_KIND
1962
1963 HInstruction(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01001964 : HInstruction(kind, DataType::Type::kVoid, side_effects, dex_pc) {}
1965
1966 HInstruction(InstructionKind kind, DataType::Type type, SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001967 : previous_(nullptr),
1968 next_(nullptr),
1969 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001970 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001971 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001972 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001973 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001974 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001975 locations_(nullptr),
1976 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001977 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001978 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001979 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05301980 SetPackedField<InstructionKindField>(kind);
Vladimir Markobd785672018-05-03 17:09:09 +01001981 SetPackedField<TypeField>(type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001982 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1983 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001984
Dave Allison20dfc792014-06-16 20:44:29 -07001985 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001986
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001987
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001988 HInstruction* GetNext() const { return next_; }
1989 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001990
Calin Juravle77520bc2015-01-12 18:45:46 +00001991 HInstruction* GetNextDisregardingMoves() const;
1992 HInstruction* GetPreviousDisregardingMoves() const;
1993
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001994 HBasicBlock* GetBlock() const { return block_; }
Vladimir Markoca6fff82017-10-03 14:49:14 +01001995 ArenaAllocator* GetAllocator() const { return block_->GetGraph()->GetAllocator(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001996 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001997 bool IsInBlock() const { return block_ != nullptr; }
1998 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001999 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
2000 bool IsIrreducibleLoopHeaderPhi() const {
2001 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
2002 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002003
Vladimir Marko372f10e2016-05-17 16:30:10 +01002004 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
2005
2006 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
2007 // One virtual method is enough, just const_cast<> and then re-add the const.
2008 return ArrayRef<const HUserRecord<HInstruction*>>(
2009 const_cast<HInstruction*>(this)->GetInputRecords());
2010 }
2011
Vladimir Markoe9004912016-06-16 16:50:52 +01002012 HInputsRef GetInputs() {
2013 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002014 }
2015
Vladimir Markoe9004912016-06-16 16:50:52 +01002016 HConstInputsRef GetInputs() const {
2017 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002018 }
2019
2020 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00002021 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002022
Aart Bik2767f4b2016-10-28 15:03:53 -07002023 bool HasInput(HInstruction* input) const {
2024 for (const HInstruction* i : GetInputs()) {
2025 if (i == input) {
2026 return true;
2027 }
2028 }
2029 return false;
2030 }
2031
Vladimir Marko372f10e2016-05-17 16:30:10 +01002032 void SetRawInputAt(size_t index, HInstruction* input) {
2033 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
2034 }
2035
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002036 virtual void Accept(HGraphVisitor* visitor) = 0;
2037 virtual const char* DebugName() const = 0;
2038
Vladimir Markobd785672018-05-03 17:09:09 +01002039 DataType::Type GetType() const {
2040 return TypeField::Decode(GetPackedFields());
2041 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002042
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002043 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002044
2045 uint32_t GetDexPc() const { return dex_pc_; }
2046
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002047 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01002048
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002049 // Can the instruction throw?
2050 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
2051 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01002052 virtual bool CanThrow() const { return false; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08002053
2054 // Does the instruction always throw an exception unconditionally?
2055 virtual bool AlwaysThrows() const { return false; }
2056
David Brazdilec16f792015-08-19 15:04:01 +01002057 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002058
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002059 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002060 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002061
Calin Juravle10e244f2015-01-26 18:54:32 +00002062 // Does not apply for all instructions, but having this at top level greatly
2063 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00002064 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002065 virtual bool CanBeNull() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002066 DCHECK_EQ(GetType(), DataType::Type::kReference) << "CanBeNull only applies to reference types";
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002067 return true;
2068 }
Calin Juravle10e244f2015-01-26 18:54:32 +00002069
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002070 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01002071 return false;
2072 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002073
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002074 virtual bool IsActualObject() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002075 return GetType() == DataType::Type::kReference;
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002076 }
2077
Calin Juravle2e768302015-07-28 14:41:11 +00002078 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00002079
Calin Juravle61d544b2015-02-23 16:46:57 +00002080 ReferenceTypeInfo GetReferenceTypeInfo() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002081 DCHECK_EQ(GetType(), DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002082 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00002083 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00002084 }
Calin Juravleacf735c2015-02-12 15:25:22 +00002085
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002086 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00002087 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002088 // Note: fixup_end remains valid across push_front().
2089 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
2090 HUseListNode<HInstruction*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002091 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HInstruction*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002092 uses_.push_front(*new_node);
2093 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002094 }
2095
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002096 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01002097 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002098 // Note: env_fixup_end remains valid across push_front().
2099 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
2100 HUseListNode<HEnvironment*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002101 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HEnvironment*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002102 env_uses_.push_front(*new_node);
2103 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002104 }
2105
David Brazdil1abb4192015-02-17 18:33:36 +00002106 void RemoveAsUserOfInput(size_t input) {
2107 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002108 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2109 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2110 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00002111 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002112
Vladimir Marko372f10e2016-05-17 16:30:10 +01002113 void RemoveAsUserOfAllInputs() {
2114 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2115 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2116 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2117 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
2118 }
2119 }
2120
David Brazdil1abb4192015-02-17 18:33:36 +00002121 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2122 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002123
Vladimir Marko46817b82016-03-29 12:21:58 +01002124 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2125 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2126 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002127 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002128 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002129 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002130
Aart Bikcc42be02016-10-20 16:14:16 -07002131 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002132 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002133 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002134 !CanThrow() &&
2135 !IsSuspendCheck() &&
2136 !IsControlFlow() &&
2137 !IsNativeDebugInfo() &&
2138 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002139 // If we added an explicit barrier then we should keep it.
Igor Murashkind01745e2017-04-05 16:40:31 -07002140 !IsMemoryBarrier() &&
2141 !IsConstructorFence();
Aart Bik482095d2016-10-10 15:39:10 -07002142 }
2143
Aart Bikcc42be02016-10-20 16:14:16 -07002144 bool IsDeadAndRemovable() const {
2145 return IsRemovable() && !HasUses();
2146 }
2147
Roland Levillain6c82d402014-10-13 16:10:27 +01002148 // Does this instruction strictly dominate `other_instruction`?
2149 // Returns false if this instruction and `other_instruction` are the same.
2150 // Aborts if this instruction and `other_instruction` are both phis.
2151 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002152
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002153 int GetId() const { return id_; }
2154 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002155
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002156 int GetSsaIndex() const { return ssa_index_; }
2157 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2158 bool HasSsaIndex() const { return ssa_index_ != -1; }
2159
2160 bool HasEnvironment() const { return environment_ != nullptr; }
2161 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002162 // Set the `environment_` field. Raw because this method does not
2163 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002164 void SetRawEnvironment(HEnvironment* environment) {
2165 DCHECK(environment_ == nullptr);
2166 DCHECK_EQ(environment->GetHolder(), this);
2167 environment_ = environment;
2168 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002169
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002170 void InsertRawEnvironment(HEnvironment* environment) {
2171 DCHECK(environment_ != nullptr);
2172 DCHECK_EQ(environment->GetHolder(), this);
2173 DCHECK(environment->GetParent() == nullptr);
2174 environment->parent_ = environment_;
2175 environment_ = environment;
2176 }
2177
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002178 void RemoveEnvironment();
2179
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002180 // Set the environment of this instruction, copying it from `environment`. While
2181 // copying, the uses lists are being updated.
2182 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002183 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002184 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002185 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002186 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002187 if (environment->GetParent() != nullptr) {
2188 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2189 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002190 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002191
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002192 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2193 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002194 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002195 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002196 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002197 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002198 if (environment->GetParent() != nullptr) {
2199 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2200 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002201 }
2202
Nicolas Geoffray39468442014-09-02 15:17:15 +01002203 // Returns the number of entries in the environment. Typically, that is the
2204 // number of dex registers in a method. It could be more in case of inlining.
2205 size_t EnvironmentSize() const;
2206
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002207 LocationSummary* GetLocations() const { return locations_; }
2208 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002209
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002210 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002211 void ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002212 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002213
Alexandre Rames188d4312015-04-09 18:30:21 +01002214 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2215 // uses of this instruction by `other` are *not* updated.
2216 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2217 ReplaceWith(other);
2218 other->ReplaceInput(this, use_index);
2219 }
2220
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002221 // Move `this` instruction before `cursor`
2222 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002223
Vladimir Markofb337ea2015-11-25 15:25:10 +00002224 // Move `this` before its first user and out of any loops. If there is no
2225 // out-of-loop user that dominates all other users, move the instruction
2226 // to the end of the out-of-loop common dominator of the user's blocks.
2227 //
2228 // This can be used only on non-throwing instructions with no side effects that
2229 // have at least one use but no environment uses.
2230 void MoveBeforeFirstUserAndOutOfLoops();
2231
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002232#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01002233 bool Is##type() const;
2234
2235 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2236#undef INSTRUCTION_TYPE_CHECK
2237
2238#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002239 const H##type* As##type() const; \
2240 H##type* As##type();
2241
Vladimir Markoa90dd512018-05-04 15:04:45 +01002242 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
2243#undef INSTRUCTION_TYPE_CAST
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002244
Artem Serovcced8ba2017-07-19 18:18:09 +01002245 // Return a clone of the instruction if it is clonable (shallow copy by default, custom copy
2246 // if a custom copy-constructor is provided for a particular type). If IsClonable() is false for
2247 // the instruction then the behaviour of this function is undefined.
2248 //
2249 // Note: It is semantically valid to create a clone of the instruction only until
2250 // prepare_for_register_allocator phase as lifetime, intervals and codegen info are not
2251 // copied.
2252 //
2253 // Note: HEnvironment and some other fields are not copied and are set to default values, see
2254 // 'explicit HInstruction(const HInstruction& other)' for details.
2255 virtual HInstruction* Clone(ArenaAllocator* arena ATTRIBUTE_UNUSED) const {
2256 LOG(FATAL) << "Cloning is not implemented for the instruction " <<
2257 DebugName() << " " << GetId();
2258 UNREACHABLE();
2259 }
2260
2261 // Return whether instruction can be cloned (copied).
2262 virtual bool IsClonable() const { return false; }
2263
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002264 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002265 // TODO: this method is used by LICM and GVN with possibly different
2266 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002267 virtual bool CanBeMoved() const { return false; }
2268
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002269 // Returns whether any data encoded in the two instructions is equal.
2270 // This method does not look at the inputs. Both instructions must be
2271 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002272 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002273 return false;
2274 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002275
2276 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002277 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002278 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002279 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002280
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002281 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2282 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2283 // the virtual function because the __attribute__((__pure__)) doesn't really
2284 // apply the strong requirement for virtual functions, preventing optimizations.
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302285 InstructionKind GetKind() const { return GetPackedField<InstructionKindField>(); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002286
2287 virtual size_t ComputeHashCode() const {
2288 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002289 for (const HInstruction* input : GetInputs()) {
2290 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002291 }
2292 return result;
2293 }
2294
2295 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002296 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002297 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002298
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002299 size_t GetLifetimePosition() const { return lifetime_position_; }
2300 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2301 LiveInterval* GetLiveInterval() const { return live_interval_; }
2302 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2303 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2304
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002305 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2306
2307 // Returns whether the code generation of the instruction will require to have access
2308 // to the current method. Such instructions are:
2309 // (1): Instructions that require an environment, as calling the runtime requires
2310 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002311 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2312 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002313 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002314 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002315 }
2316
Vladimir Markodc151b22015-10-15 18:02:30 +01002317 // Returns whether the code generation of the instruction will require to have access
2318 // to the dex cache of the current method's declaring class via the current method.
2319 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002320
Mark Mendellc4701932015-04-10 13:18:51 -04002321 // Does this instruction have any use in an environment before
2322 // control flow hits 'other'?
2323 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2324
2325 // Remove all references to environment uses of this instruction.
2326 // The caller must ensure that this is safe to do.
2327 void RemoveEnvironmentUsers();
2328
Vladimir Markoa1de9182016-02-25 11:37:38 +00002329 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2330 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002331
David Brazdil1abb4192015-02-17 18:33:36 +00002332 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002333 // If set, the machine code for this instruction is assumed to be generated by
2334 // its users. Used by liveness analysis to compute use positions accordingly.
2335 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2336 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302337 static constexpr size_t kFieldInstructionKind = kFlagReferenceTypeIsExact + 1;
2338 static constexpr size_t kFieldInstructionKindSize =
2339 MinimumBitsToStore(static_cast<size_t>(InstructionKind::kLastInstructionKind - 1));
Vladimir Markobd785672018-05-03 17:09:09 +01002340 static constexpr size_t kFieldType =
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302341 kFieldInstructionKind + kFieldInstructionKindSize;
Vladimir Markobd785672018-05-03 17:09:09 +01002342 static constexpr size_t kFieldTypeSize =
2343 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
2344 static constexpr size_t kNumberOfGenericPackedBits = kFieldType + kFieldTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00002345 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2346
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302347 static_assert(kNumberOfGenericPackedBits <= kMaxNumberOfPackedBits,
2348 "Too many generic packed fields");
2349
Vladimir Markobd785672018-05-03 17:09:09 +01002350 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
2351
Vladimir Marko372f10e2016-05-17 16:30:10 +01002352 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2353 return GetInputRecords()[i];
2354 }
2355
2356 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2357 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2358 input_records[index] = input;
2359 }
David Brazdil1abb4192015-02-17 18:33:36 +00002360
Vladimir Markoa1de9182016-02-25 11:37:38 +00002361 uint32_t GetPackedFields() const {
2362 return packed_fields_;
2363 }
2364
2365 template <size_t flag>
2366 bool GetPackedFlag() const {
2367 return (packed_fields_ & (1u << flag)) != 0u;
2368 }
2369
2370 template <size_t flag>
2371 void SetPackedFlag(bool value = true) {
2372 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2373 }
2374
2375 template <typename BitFieldType>
2376 typename BitFieldType::value_type GetPackedField() const {
2377 return BitFieldType::Decode(packed_fields_);
2378 }
2379
2380 template <typename BitFieldType>
2381 void SetPackedField(typename BitFieldType::value_type value) {
2382 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2383 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2384 }
2385
Artem Serovcced8ba2017-07-19 18:18:09 +01002386 // Copy construction for the instruction (used for Clone function).
2387 //
2388 // Fields (e.g. lifetime, intervals and codegen info) associated with phases starting from
2389 // prepare_for_register_allocator are not copied (set to default values).
2390 //
2391 // Copy constructors must be provided for every HInstruction type; default copy constructor is
2392 // fine for most of them. However for some of the instructions a custom copy constructor must be
2393 // specified (when instruction has non-trivially copyable fields and must have a special behaviour
2394 // for copying them).
2395 explicit HInstruction(const HInstruction& other)
2396 : previous_(nullptr),
2397 next_(nullptr),
2398 block_(nullptr),
2399 dex_pc_(other.dex_pc_),
2400 id_(-1),
2401 ssa_index_(-1),
2402 packed_fields_(other.packed_fields_),
2403 environment_(nullptr),
2404 locations_(nullptr),
2405 live_interval_(nullptr),
2406 lifetime_position_(kNoLifetime),
2407 side_effects_(other.side_effects_),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302408 reference_type_handle_(other.reference_type_handle_) {
2409 }
Artem Serovcced8ba2017-07-19 18:18:09 +01002410
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002411 private:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302412 using InstructionKindField =
2413 BitField<InstructionKind, kFieldInstructionKind, kFieldInstructionKindSize>;
2414
Vladimir Marko46817b82016-03-29 12:21:58 +01002415 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2416 auto before_use_node = uses_.before_begin();
2417 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2418 HInstruction* user = use_node->GetUser();
2419 size_t input_index = use_node->GetIndex();
2420 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2421 before_use_node = use_node;
2422 }
2423 }
2424
2425 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2426 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2427 if (next != uses_.end()) {
2428 HInstruction* next_user = next->GetUser();
2429 size_t next_index = next->GetIndex();
2430 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2431 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2432 }
2433 }
2434
2435 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2436 auto before_env_use_node = env_uses_.before_begin();
2437 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2438 HEnvironment* user = env_use_node->GetUser();
2439 size_t input_index = env_use_node->GetIndex();
2440 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2441 before_env_use_node = env_use_node;
2442 }
2443 }
2444
2445 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2446 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2447 if (next != env_uses_.end()) {
2448 HEnvironment* next_user = next->GetUser();
2449 size_t next_index = next->GetIndex();
2450 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2451 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2452 }
2453 }
David Brazdil1abb4192015-02-17 18:33:36 +00002454
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002455 HInstruction* previous_;
2456 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002457 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002458 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002459
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002460 // An instruction gets an id when it is added to the graph.
2461 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002462 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002463 int id_;
2464
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002465 // When doing liveness analysis, instructions that have uses get an SSA index.
2466 int ssa_index_;
2467
Vladimir Markoa1de9182016-02-25 11:37:38 +00002468 // Packed fields.
2469 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002470
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002471 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002472 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002473
2474 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002475 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002476
Nicolas Geoffray39468442014-09-02 15:17:15 +01002477 // The environment associated with this instruction. Not null if the instruction
2478 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002479 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002480
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002481 // Set by the code generator.
2482 LocationSummary* locations_;
2483
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002484 // Set by the liveness analysis.
2485 LiveInterval* live_interval_;
2486
2487 // Set by the liveness analysis, this is the position in a linear
2488 // order of blocks where this instruction's live interval start.
2489 size_t lifetime_position_;
2490
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002491 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002492
Vladimir Markoa1de9182016-02-25 11:37:38 +00002493 // The reference handle part of the reference type info.
2494 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002495 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002496 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002497
David Brazdil1abb4192015-02-17 18:33:36 +00002498 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002499 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002500 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002501 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002502 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002503};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002504std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002505
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002506// Iterates over the instructions, while preserving the next instruction
2507// in case the current instruction gets removed from the list by the user
2508// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002509class HInstructionIterator : public ValueObject {
2510 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002511 explicit HInstructionIterator(const HInstructionList& instructions)
2512 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002513 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002514 }
2515
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002516 bool Done() const { return instruction_ == nullptr; }
2517 HInstruction* Current() const { return instruction_; }
2518 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002519 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002520 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002521 }
2522
2523 private:
2524 HInstruction* instruction_;
2525 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002526
2527 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002528};
2529
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002530// Iterates over the instructions without saving the next instruction,
2531// therefore handling changes in the graph potentially made by the user
2532// of this iterator.
2533class HInstructionIteratorHandleChanges : public ValueObject {
2534 public:
2535 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2536 : instruction_(instructions.first_instruction_) {
2537 }
2538
2539 bool Done() const { return instruction_ == nullptr; }
2540 HInstruction* Current() const { return instruction_; }
2541 void Advance() {
2542 instruction_ = instruction_->GetNext();
2543 }
2544
2545 private:
2546 HInstruction* instruction_;
2547
2548 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2549};
2550
2551
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002552class HBackwardInstructionIterator : public ValueObject {
2553 public:
2554 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2555 : instruction_(instructions.last_instruction_) {
2556 next_ = Done() ? nullptr : instruction_->GetPrevious();
2557 }
2558
2559 bool Done() const { return instruction_ == nullptr; }
2560 HInstruction* Current() const { return instruction_; }
2561 void Advance() {
2562 instruction_ = next_;
2563 next_ = Done() ? nullptr : instruction_->GetPrevious();
2564 }
2565
2566 private:
2567 HInstruction* instruction_;
2568 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002569
2570 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002571};
2572
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002573class HVariableInputSizeInstruction : public HInstruction {
2574 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002575 using HInstruction::GetInputRecords; // Keep the const version visible.
2576 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
2577 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2578 }
2579
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002580 void AddInput(HInstruction* input);
2581 void InsertInputAt(size_t index, HInstruction* input);
2582 void RemoveInputAt(size_t index);
2583
Igor Murashkind01745e2017-04-05 16:40:31 -07002584 // Removes all the inputs.
2585 // Also removes this instructions from each input's use list
2586 // (for non-environment uses only).
2587 void RemoveAllInputs();
2588
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002589 protected:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302590 HVariableInputSizeInstruction(InstructionKind inst_kind,
2591 SideEffects side_effects,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002592 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002593 ArenaAllocator* allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002594 size_t number_of_inputs,
2595 ArenaAllocKind kind)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302596 : HInstruction(inst_kind, side_effects, dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002597 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Vladimir Markobd785672018-05-03 17:09:09 +01002598 HVariableInputSizeInstruction(InstructionKind inst_kind,
2599 DataType::Type type,
2600 SideEffects side_effects,
2601 uint32_t dex_pc,
2602 ArenaAllocator* allocator,
2603 size_t number_of_inputs,
2604 ArenaAllocKind kind)
2605 : HInstruction(inst_kind, type, side_effects, dex_pc),
2606 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002607
Artem Serovcced8ba2017-07-19 18:18:09 +01002608 DEFAULT_COPY_CONSTRUCTOR(VariableInputSizeInstruction);
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002609
Artem Serovcced8ba2017-07-19 18:18:09 +01002610 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002611};
2612
Vladimir Markof9f64412015-09-02 14:05:49 +01002613template<size_t N>
Vladimir Markobd785672018-05-03 17:09:09 +01002614class HExpression : public HInstruction {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002615 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002616 HExpression<N>(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302617 : HInstruction(kind, side_effects, dex_pc), inputs_() {}
Vladimir Markobd785672018-05-03 17:09:09 +01002618 HExpression<N>(InstructionKind kind,
2619 DataType::Type type,
2620 SideEffects side_effects,
2621 uint32_t dex_pc)
2622 : HInstruction(kind, type, side_effects, dex_pc), inputs_() {}
2623 virtual ~HExpression() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002624
Vladimir Marko372f10e2016-05-17 16:30:10 +01002625 using HInstruction::GetInputRecords; // Keep the const version visible.
2626 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2627 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002628 }
2629
Artem Serovcced8ba2017-07-19 18:18:09 +01002630 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002631 DEFAULT_COPY_CONSTRUCTOR(Expression<N>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002632
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002633 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002634 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002635
2636 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002637};
2638
Vladimir Markobd785672018-05-03 17:09:09 +01002639// HExpression specialization for N=0.
Vladimir Markof9f64412015-09-02 14:05:49 +01002640template<>
Vladimir Markobd785672018-05-03 17:09:09 +01002641class HExpression<0> : public HInstruction {
Vladimir Markof9f64412015-09-02 14:05:49 +01002642 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002643 using HInstruction::HInstruction;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002644
Vladimir Markobd785672018-05-03 17:09:09 +01002645 virtual ~HExpression() {}
Vladimir Markof9f64412015-09-02 14:05:49 +01002646
Vladimir Marko372f10e2016-05-17 16:30:10 +01002647 using HInstruction::GetInputRecords; // Keep the const version visible.
2648 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2649 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002650 }
2651
Artem Serovcced8ba2017-07-19 18:18:09 +01002652 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002653 DEFAULT_COPY_CONSTRUCTOR(Expression<0>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002654
Vladimir Markof9f64412015-09-02 14:05:49 +01002655 private:
2656 friend class SsaBuilder;
2657};
2658
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002659// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2660// instruction that branches to the exit block.
Vladimir Markobd785672018-05-03 17:09:09 +01002661class HReturnVoid FINAL : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002662 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002663 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002664 : HExpression(kReturnVoid, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302665 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002666
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002667 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002668
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002669 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002670
Artem Serovcced8ba2017-07-19 18:18:09 +01002671 protected:
2672 DEFAULT_COPY_CONSTRUCTOR(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002673};
2674
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002675// Represents dex's RETURN opcodes. A HReturn is a control flow
2676// instruction that branches to the exit block.
Vladimir Markobd785672018-05-03 17:09:09 +01002677class HReturn FINAL : public HExpression<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002678 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002679 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002680 : HExpression(kReturn, SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002681 SetRawInputAt(0, value);
2682 }
2683
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002684 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002685
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002686 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002687
Artem Serovcced8ba2017-07-19 18:18:09 +01002688 protected:
2689 DEFAULT_COPY_CONSTRUCTOR(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002690};
2691
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002692class HPhi FINAL : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002693 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002694 HPhi(ArenaAllocator* allocator,
David Brazdildee58d62016-04-07 09:54:26 +00002695 uint32_t reg_number,
2696 size_t number_of_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002697 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +00002698 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002699 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302700 kPhi,
Vladimir Markobd785672018-05-03 17:09:09 +01002701 ToPhiType(type),
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002702 SideEffects::None(),
2703 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002704 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002705 number_of_inputs,
2706 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002707 reg_number_(reg_number) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002708 DCHECK_NE(GetType(), DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +00002709 // Phis are constructed live and marked dead if conflicting or unused.
2710 // Individual steps of SsaBuilder should assume that if a phi has been
2711 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2712 SetPackedFlag<kFlagIsLive>(true);
2713 SetPackedFlag<kFlagCanBeNull>(true);
2714 }
2715
Artem Serovcced8ba2017-07-19 18:18:09 +01002716 bool IsClonable() const OVERRIDE { return true; }
2717
David Brazdildee58d62016-04-07 09:54:26 +00002718 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002719 static DataType::Type ToPhiType(DataType::Type type) {
2720 return DataType::Kind(type);
David Brazdildee58d62016-04-07 09:54:26 +00002721 }
2722
2723 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2724
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002725 void SetType(DataType::Type new_type) {
David Brazdildee58d62016-04-07 09:54:26 +00002726 // Make sure that only valid type changes occur. The following are allowed:
2727 // (1) int -> float/ref (primitive type propagation),
2728 // (2) long -> double (primitive type propagation).
2729 DCHECK(GetType() == new_type ||
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002730 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kFloat32) ||
2731 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kReference) ||
2732 (GetType() == DataType::Type::kInt64 && new_type == DataType::Type::kFloat64));
David Brazdildee58d62016-04-07 09:54:26 +00002733 SetPackedField<TypeField>(new_type);
2734 }
2735
2736 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2737 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2738
2739 uint32_t GetRegNumber() const { return reg_number_; }
2740
2741 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2742 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2743 bool IsDead() const { return !IsLive(); }
2744 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2745
Vladimir Markoe9004912016-06-16 16:50:52 +01002746 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002747 return other != nullptr
2748 && other->IsPhi()
2749 && other->AsPhi()->GetBlock() == GetBlock()
2750 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2751 }
2752
Nicolas Geoffrayf57c1ae2017-06-28 17:40:18 +01002753 bool HasEquivalentPhi() const {
2754 if (GetPrevious() != nullptr && GetPrevious()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2755 return true;
2756 }
2757 if (GetNext() != nullptr && GetNext()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2758 return true;
2759 }
2760 return false;
2761 }
2762
David Brazdildee58d62016-04-07 09:54:26 +00002763 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2764 // An equivalent phi is a phi having the same dex register and type.
2765 // It assumes that phis with the same dex register are adjacent.
2766 HPhi* GetNextEquivalentPhiWithSameType() {
2767 HInstruction* next = GetNext();
2768 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2769 if (next->GetType() == GetType()) {
2770 return next->AsPhi();
2771 }
2772 next = next->GetNext();
2773 }
2774 return nullptr;
2775 }
2776
2777 DECLARE_INSTRUCTION(Phi);
2778
Artem Serovcced8ba2017-07-19 18:18:09 +01002779 protected:
2780 DEFAULT_COPY_CONSTRUCTOR(Phi);
2781
David Brazdildee58d62016-04-07 09:54:26 +00002782 private:
Vladimir Markobd785672018-05-03 17:09:09 +01002783 static constexpr size_t kFlagIsLive = HInstruction::kNumberOfGenericPackedBits;
David Brazdildee58d62016-04-07 09:54:26 +00002784 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2785 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2786 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
David Brazdildee58d62016-04-07 09:54:26 +00002787
David Brazdildee58d62016-04-07 09:54:26 +00002788 const uint32_t reg_number_;
David Brazdildee58d62016-04-07 09:54:26 +00002789};
2790
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002791// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002792// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002793// exit block.
Vladimir Markobd785672018-05-03 17:09:09 +01002794class HExit FINAL : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002795 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302796 explicit HExit(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002797 : HExpression(kExit, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302798 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002799
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002800 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002801
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002802 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002803
Artem Serovcced8ba2017-07-19 18:18:09 +01002804 protected:
2805 DEFAULT_COPY_CONSTRUCTOR(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002806};
2807
2808// Jumps from one block to another.
Vladimir Markobd785672018-05-03 17:09:09 +01002809class HGoto FINAL : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002810 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302811 explicit HGoto(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002812 : HExpression(kGoto, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302813 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002814
Artem Serovcced8ba2017-07-19 18:18:09 +01002815 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002816 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002817
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002818 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002819 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002820 }
2821
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002822 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002823
Artem Serovcced8ba2017-07-19 18:18:09 +01002824 protected:
2825 DEFAULT_COPY_CONSTRUCTOR(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002826};
2827
Roland Levillain9867bc72015-08-05 10:21:34 +01002828class HConstant : public HExpression<0> {
2829 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302830 explicit HConstant(InstructionKind kind, DataType::Type type, uint32_t dex_pc = kNoDexPc)
2831 : HExpression(kind, type, SideEffects::None(), dex_pc) {
2832 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002833
2834 bool CanBeMoved() const OVERRIDE { return true; }
2835
Roland Levillain1a653882016-03-18 18:05:57 +00002836 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002837 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002838 // Is this constant 0 in the arithmetic sense?
2839 virtual bool IsArithmeticZero() const { return false; }
2840 // Is this constant a 0-bit pattern?
2841 virtual bool IsZeroBitPattern() const { return false; }
2842 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002843 virtual bool IsOne() const { return false; }
2844
David Brazdil77a48ae2015-09-15 12:34:04 +00002845 virtual uint64_t GetValueAsUint64() const = 0;
2846
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002847 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002848
Artem Serovcced8ba2017-07-19 18:18:09 +01002849 protected:
2850 DEFAULT_COPY_CONSTRUCTOR(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002851};
2852
Vladimir Markofcb503c2016-05-18 12:48:17 +01002853class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002854 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002855 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002856 return true;
2857 }
2858
David Brazdil77a48ae2015-09-15 12:34:04 +00002859 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2860
Roland Levillain9867bc72015-08-05 10:21:34 +01002861 size_t ComputeHashCode() const OVERRIDE { return 0; }
2862
Roland Levillain1a653882016-03-18 18:05:57 +00002863 // The null constant representation is a 0-bit pattern.
2864 virtual bool IsZeroBitPattern() const { return true; }
2865
Roland Levillain9867bc72015-08-05 10:21:34 +01002866 DECLARE_INSTRUCTION(NullConstant);
2867
Artem Serovcced8ba2017-07-19 18:18:09 +01002868 protected:
2869 DEFAULT_COPY_CONSTRUCTOR(NullConstant);
2870
Roland Levillain9867bc72015-08-05 10:21:34 +01002871 private:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002872 explicit HNullConstant(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302873 : HConstant(kNullConstant, DataType::Type::kReference, dex_pc) {
2874 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002875
2876 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01002877};
2878
2879// Constants of the type int. Those can be from Dex instructions, or
2880// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002881class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002882 public:
2883 int32_t GetValue() const { return value_; }
2884
David Brazdil9f389d42015-10-01 14:32:56 +01002885 uint64_t GetValueAsUint64() const OVERRIDE {
2886 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2887 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002888
Vladimir Marko372f10e2016-05-17 16:30:10 +01002889 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002890 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002891 return other->AsIntConstant()->value_ == value_;
2892 }
2893
2894 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2895
2896 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002897 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2898 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002899 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2900
Roland Levillain1a653882016-03-18 18:05:57 +00002901 // Integer constants are used to encode Boolean values as well,
2902 // where 1 means true and 0 means false.
2903 bool IsTrue() const { return GetValue() == 1; }
2904 bool IsFalse() const { return GetValue() == 0; }
2905
Roland Levillain9867bc72015-08-05 10:21:34 +01002906 DECLARE_INSTRUCTION(IntConstant);
2907
Artem Serovcced8ba2017-07-19 18:18:09 +01002908 protected:
2909 DEFAULT_COPY_CONSTRUCTOR(IntConstant);
2910
Roland Levillain9867bc72015-08-05 10:21:34 +01002911 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002912 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302913 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc), value_(value) {
2914 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002915 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302916 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc),
2917 value_(value ? 1 : 0) {
2918 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002919
2920 const int32_t value_;
2921
2922 friend class HGraph;
2923 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2924 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Roland Levillain9867bc72015-08-05 10:21:34 +01002925};
2926
Vladimir Markofcb503c2016-05-18 12:48:17 +01002927class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002928 public:
2929 int64_t GetValue() const { return value_; }
2930
David Brazdil77a48ae2015-09-15 12:34:04 +00002931 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2932
Vladimir Marko372f10e2016-05-17 16:30:10 +01002933 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002934 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002935 return other->AsLongConstant()->value_ == value_;
2936 }
2937
2938 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2939
2940 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002941 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2942 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002943 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2944
2945 DECLARE_INSTRUCTION(LongConstant);
2946
Artem Serovcced8ba2017-07-19 18:18:09 +01002947 protected:
2948 DEFAULT_COPY_CONSTRUCTOR(LongConstant);
2949
Roland Levillain9867bc72015-08-05 10:21:34 +01002950 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002951 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302952 : HConstant(kLongConstant, DataType::Type::kInt64, dex_pc),
2953 value_(value) {
2954 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002955
2956 const int64_t value_;
2957
2958 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01002959};
Dave Allison20dfc792014-06-16 20:44:29 -07002960
Vladimir Markofcb503c2016-05-18 12:48:17 +01002961class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002962 public:
2963 float GetValue() const { return value_; }
2964
2965 uint64_t GetValueAsUint64() const OVERRIDE {
2966 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2967 }
2968
Vladimir Marko372f10e2016-05-17 16:30:10 +01002969 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002970 DCHECK(other->IsFloatConstant()) << other->DebugName();
2971 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2972 }
2973
2974 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2975
2976 bool IsMinusOne() const OVERRIDE {
2977 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2978 }
Roland Levillain1a653882016-03-18 18:05:57 +00002979 bool IsArithmeticZero() const OVERRIDE {
2980 return std::fpclassify(value_) == FP_ZERO;
2981 }
2982 bool IsArithmeticPositiveZero() const {
2983 return IsArithmeticZero() && !std::signbit(value_);
2984 }
2985 bool IsArithmeticNegativeZero() const {
2986 return IsArithmeticZero() && std::signbit(value_);
2987 }
2988 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002989 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002990 }
2991 bool IsOne() const OVERRIDE {
2992 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2993 }
2994 bool IsNaN() const {
2995 return std::isnan(value_);
2996 }
2997
2998 DECLARE_INSTRUCTION(FloatConstant);
2999
Artem Serovcced8ba2017-07-19 18:18:09 +01003000 protected:
3001 DEFAULT_COPY_CONSTRUCTOR(FloatConstant);
3002
Roland Levillain31dd3d62016-02-16 12:21:02 +00003003 private:
3004 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303005 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3006 value_(value) {
3007 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003008 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303009 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3010 value_(bit_cast<float, int32_t>(value)) {
3011 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003012
3013 const float value_;
3014
3015 // Only the SsaBuilder and HGraph can create floating-point constants.
3016 friend class SsaBuilder;
3017 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003018};
3019
Vladimir Markofcb503c2016-05-18 12:48:17 +01003020class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003021 public:
3022 double GetValue() const { return value_; }
3023
3024 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
3025
Vladimir Marko372f10e2016-05-17 16:30:10 +01003026 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003027 DCHECK(other->IsDoubleConstant()) << other->DebugName();
3028 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
3029 }
3030
3031 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
3032
3033 bool IsMinusOne() const OVERRIDE {
3034 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
3035 }
Roland Levillain1a653882016-03-18 18:05:57 +00003036 bool IsArithmeticZero() const OVERRIDE {
3037 return std::fpclassify(value_) == FP_ZERO;
3038 }
3039 bool IsArithmeticPositiveZero() const {
3040 return IsArithmeticZero() && !std::signbit(value_);
3041 }
3042 bool IsArithmeticNegativeZero() const {
3043 return IsArithmeticZero() && std::signbit(value_);
3044 }
3045 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003046 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00003047 }
3048 bool IsOne() const OVERRIDE {
3049 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3050 }
3051 bool IsNaN() const {
3052 return std::isnan(value_);
3053 }
3054
3055 DECLARE_INSTRUCTION(DoubleConstant);
3056
Artem Serovcced8ba2017-07-19 18:18:09 +01003057 protected:
3058 DEFAULT_COPY_CONSTRUCTOR(DoubleConstant);
3059
Roland Levillain31dd3d62016-02-16 12:21:02 +00003060 private:
3061 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303062 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3063 value_(value) {
3064 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003065 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303066 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3067 value_(bit_cast<double, int64_t>(value)) {
3068 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003069
3070 const double value_;
3071
3072 // Only the SsaBuilder and HGraph can create floating-point constants.
3073 friend class SsaBuilder;
3074 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003075};
3076
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003077// Conditional branch. A block ending with an HIf instruction must have
3078// two successors.
Vladimir Markobd785672018-05-03 17:09:09 +01003079class HIf FINAL : public HExpression<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003080 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003081 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003082 : HExpression(kIf, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003083 SetRawInputAt(0, input);
3084 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003085
Artem Serovcced8ba2017-07-19 18:18:09 +01003086 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003087 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003088
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003089 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003090 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003091 }
3092
3093 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003094 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003095 }
3096
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003097 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003098
Artem Serovcced8ba2017-07-19 18:18:09 +01003099 protected:
3100 DEFAULT_COPY_CONSTRUCTOR(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003101};
3102
David Brazdilfc6a86a2015-06-26 10:33:45 +00003103
3104// Abstract instruction which marks the beginning and/or end of a try block and
3105// links it to the respective exception handlers. Behaves the same as a Goto in
3106// non-exceptional control flow.
3107// Normal-flow successor is stored at index zero, exception handlers under
3108// higher indices in no particular order.
Vladimir Markobd785672018-05-03 17:09:09 +01003109class HTryBoundary FINAL : public HExpression<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003110 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003111 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01003112 kEntry,
3113 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003114 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01003115 };
3116
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003117 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003118 : HExpression(kTryBoundary, SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003119 SetPackedField<BoundaryKindField>(kind);
3120 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003121
3122 bool IsControlFlow() const OVERRIDE { return true; }
3123
3124 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01003125 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003126
David Brazdild26a4112015-11-10 11:07:31 +00003127 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
3128 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
3129 }
3130
David Brazdilfc6a86a2015-06-26 10:33:45 +00003131 // Returns whether `handler` is among its exception handlers (non-zero index
3132 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01003133 bool HasExceptionHandler(const HBasicBlock& handler) const {
3134 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00003135 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00003136 }
3137
3138 // If not present already, adds `handler` to its block's list of exception
3139 // handlers.
3140 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01003141 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003142 GetBlock()->AddSuccessor(handler);
3143 }
3144 }
3145
Vladimir Markoa1de9182016-02-25 11:37:38 +00003146 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
3147 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003148
David Brazdilffee3d32015-07-06 11:48:53 +01003149 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
3150
David Brazdilfc6a86a2015-06-26 10:33:45 +00003151 DECLARE_INSTRUCTION(TryBoundary);
3152
Artem Serovcced8ba2017-07-19 18:18:09 +01003153 protected:
3154 DEFAULT_COPY_CONSTRUCTOR(TryBoundary);
3155
David Brazdilfc6a86a2015-06-26 10:33:45 +00003156 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003157 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
3158 static constexpr size_t kFieldBoundaryKindSize =
3159 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
3160 static constexpr size_t kNumberOfTryBoundaryPackedBits =
3161 kFieldBoundaryKind + kFieldBoundaryKindSize;
3162 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
3163 "Too many packed fields.");
3164 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00003165};
3166
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003167// Deoptimize to interpreter, upon checking a condition.
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003168class HDeoptimize FINAL : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003169 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003170 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
3171 // across.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003172 HDeoptimize(ArenaAllocator* allocator,
3173 HInstruction* cond,
3174 DeoptimizationKind kind,
3175 uint32_t dex_pc)
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003176 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303177 kDeoptimize,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003178 SideEffects::All(),
3179 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003180 allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003181 /* number_of_inputs */ 1,
3182 kArenaAllocMisc) {
3183 SetPackedFlag<kFieldCanBeMoved>(false);
3184 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003185 SetRawInputAt(0, cond);
3186 }
3187
Artem Serovcced8ba2017-07-19 18:18:09 +01003188 bool IsClonable() const OVERRIDE { return true; }
3189
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003190 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
3191 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
3192 // instead of `guard`.
3193 // We set CanTriggerGC to prevent any intermediate address to be live
3194 // at the point of the `HDeoptimize`.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003195 HDeoptimize(ArenaAllocator* allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003196 HInstruction* cond,
3197 HInstruction* guard,
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003198 DeoptimizationKind kind,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003199 uint32_t dex_pc)
3200 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303201 kDeoptimize,
Vladimir Markobd785672018-05-03 17:09:09 +01003202 guard->GetType(),
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003203 SideEffects::CanTriggerGC(),
3204 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003205 allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003206 /* number_of_inputs */ 2,
3207 kArenaAllocMisc) {
3208 SetPackedFlag<kFieldCanBeMoved>(true);
3209 SetPackedField<DeoptimizeKindField>(kind);
3210 SetRawInputAt(0, cond);
3211 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003212 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003213
3214 bool CanBeMoved() const OVERRIDE { return GetPackedFlag<kFieldCanBeMoved>(); }
3215
3216 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
3217 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
3218 }
3219
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003220 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003221
Aart Bik75ff2c92018-04-21 01:28:11 +00003222 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003223
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003224 DeoptimizationKind GetDeoptimizationKind() const { return GetPackedField<DeoptimizeKindField>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003225
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003226 bool GuardsAnInput() const {
3227 return InputCount() == 2;
3228 }
3229
3230 HInstruction* GuardedInput() const {
3231 DCHECK(GuardsAnInput());
3232 return InputAt(1);
3233 }
3234
3235 void RemoveGuard() {
3236 RemoveInputAt(1);
3237 }
3238
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003239 DECLARE_INSTRUCTION(Deoptimize);
3240
Artem Serovcced8ba2017-07-19 18:18:09 +01003241 protected:
3242 DEFAULT_COPY_CONSTRUCTOR(Deoptimize);
3243
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003244 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003245 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3246 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3247 static constexpr size_t kFieldDeoptimizeKindSize =
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003248 MinimumBitsToStore(static_cast<size_t>(DeoptimizationKind::kLast));
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003249 static constexpr size_t kNumberOfDeoptimizePackedBits =
3250 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3251 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3252 "Too many packed fields.");
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003253 using DeoptimizeKindField =
3254 BitField<DeoptimizationKind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003255};
3256
Mingyao Yang063fc772016-08-02 11:02:54 -07003257// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3258// The compiled code checks this flag value in a guard before devirtualized call and
3259// if it's true, starts to do deoptimization.
3260// It has a 4-byte slot on stack.
3261// TODO: allocate a register for this flag.
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003262class HShouldDeoptimizeFlag FINAL : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003263 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003264 // CHA guards are only optimized in a separate pass and it has no side effects
3265 // with regard to other passes.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003266 HShouldDeoptimizeFlag(ArenaAllocator* allocator, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303267 : HVariableInputSizeInstruction(kShouldDeoptimizeFlag,
Vladimir Markobd785672018-05-03 17:09:09 +01003268 DataType::Type::kInt32,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303269 SideEffects::None(),
3270 dex_pc,
3271 allocator,
3272 0,
3273 kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003274 }
3275
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003276 // We do all CHA guard elimination/motion in a single pass, after which there is no
3277 // further guard elimination/motion since a guard might have been used for justification
3278 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3279 // to avoid other optimizations trying to move it.
Mingyao Yang063fc772016-08-02 11:02:54 -07003280 bool CanBeMoved() const OVERRIDE { return false; }
3281
3282 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3283
Artem Serovcced8ba2017-07-19 18:18:09 +01003284 protected:
3285 DEFAULT_COPY_CONSTRUCTOR(ShouldDeoptimizeFlag);
Mingyao Yang063fc772016-08-02 11:02:54 -07003286};
3287
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003288// Represents the ArtMethod that was passed as a first argument to
3289// the method. It is used by instructions that depend on it, like
3290// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003291class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003292 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003293 explicit HCurrentMethod(DataType::Type type, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303294 : HExpression(kCurrentMethod, type, SideEffects::None(), dex_pc) {
3295 }
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003296
3297 DECLARE_INSTRUCTION(CurrentMethod);
3298
Artem Serovcced8ba2017-07-19 18:18:09 +01003299 protected:
3300 DEFAULT_COPY_CONSTRUCTOR(CurrentMethod);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003301};
3302
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003303// Fetches an ArtMethod from the virtual table or the interface method table
3304// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003305class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003306 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003307 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003308 kVTable,
3309 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003310 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003311 };
3312 HClassTableGet(HInstruction* cls,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003313 DataType::Type type,
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003314 TableKind kind,
3315 size_t index,
3316 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303317 : HExpression(kClassTableGet, type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003318 index_(index) {
3319 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003320 SetRawInputAt(0, cls);
3321 }
3322
Artem Serovcced8ba2017-07-19 18:18:09 +01003323 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003324 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003325 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003326 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003327 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003328 }
3329
Vladimir Markoa1de9182016-02-25 11:37:38 +00003330 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003331 size_t GetIndex() const { return index_; }
3332
3333 DECLARE_INSTRUCTION(ClassTableGet);
3334
Artem Serovcced8ba2017-07-19 18:18:09 +01003335 protected:
3336 DEFAULT_COPY_CONSTRUCTOR(ClassTableGet);
3337
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003338 private:
Vladimir Markobd785672018-05-03 17:09:09 +01003339 static constexpr size_t kFieldTableKind = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003340 static constexpr size_t kFieldTableKindSize =
3341 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3342 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3343 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3344 "Too many packed fields.");
3345 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3346
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003347 // The index of the ArtMethod in the table.
3348 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003349};
3350
Mark Mendellfe57faa2015-09-18 09:26:15 -04003351// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3352// have one successor for each entry in the switch table, and the final successor
3353// will be the block containing the next Dex opcode.
Vladimir Markobd785672018-05-03 17:09:09 +01003354class HPackedSwitch FINAL : public HExpression<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003355 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003356 HPackedSwitch(int32_t start_value,
3357 uint32_t num_entries,
3358 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003359 uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003360 : HExpression(kPackedSwitch, SideEffects::None(), dex_pc),
Mark Mendellfe57faa2015-09-18 09:26:15 -04003361 start_value_(start_value),
3362 num_entries_(num_entries) {
3363 SetRawInputAt(0, input);
3364 }
3365
Artem Serovcced8ba2017-07-19 18:18:09 +01003366 bool IsClonable() const OVERRIDE { return true; }
3367
Mark Mendellfe57faa2015-09-18 09:26:15 -04003368 bool IsControlFlow() const OVERRIDE { return true; }
3369
3370 int32_t GetStartValue() const { return start_value_; }
3371
Vladimir Marko211c2112015-09-24 16:52:33 +01003372 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003373
3374 HBasicBlock* GetDefaultBlock() const {
3375 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003376 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003377 }
3378 DECLARE_INSTRUCTION(PackedSwitch);
3379
Artem Serovcced8ba2017-07-19 18:18:09 +01003380 protected:
3381 DEFAULT_COPY_CONSTRUCTOR(PackedSwitch);
3382
Mark Mendellfe57faa2015-09-18 09:26:15 -04003383 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003384 const int32_t start_value_;
3385 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003386};
3387
Roland Levillain88cb1752014-10-20 16:36:47 +01003388class HUnaryOperation : public HExpression<1> {
3389 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303390 HUnaryOperation(InstructionKind kind,
3391 DataType::Type result_type,
3392 HInstruction* input,
3393 uint32_t dex_pc = kNoDexPc)
3394 : HExpression(kind, result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003395 SetRawInputAt(0, input);
3396 }
3397
Artem Serovcced8ba2017-07-19 18:18:09 +01003398 // All of the UnaryOperation instructions are clonable.
3399 bool IsClonable() const OVERRIDE { return true; }
3400
Roland Levillain88cb1752014-10-20 16:36:47 +01003401 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003402 DataType::Type GetResultType() const { return GetType(); }
Roland Levillain88cb1752014-10-20 16:36:47 +01003403
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003404 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003405 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003406 return true;
3407 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003408
Roland Levillain31dd3d62016-02-16 12:21:02 +00003409 // Try to statically evaluate `this` and return a HConstant
3410 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003411 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003412 HConstant* TryStaticEvaluation() const;
3413
3414 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003415 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3416 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003417 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3418 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003419
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003420 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003421
Artem Serovcced8ba2017-07-19 18:18:09 +01003422 protected:
3423 DEFAULT_COPY_CONSTRUCTOR(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003424};
3425
Dave Allison20dfc792014-06-16 20:44:29 -07003426class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003427 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303428 HBinaryOperation(InstructionKind kind,
3429 DataType::Type result_type,
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003430 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003431 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003432 SideEffects side_effects = SideEffects::None(),
3433 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303434 : HExpression(kind, result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003435 SetRawInputAt(0, left);
3436 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003437 }
3438
Artem Serovcced8ba2017-07-19 18:18:09 +01003439 // All of the BinaryOperation instructions are clonable.
3440 bool IsClonable() const OVERRIDE { return true; }
3441
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003442 HInstruction* GetLeft() const { return InputAt(0); }
3443 HInstruction* GetRight() const { return InputAt(1); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003444 DataType::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003445
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003446 virtual bool IsCommutative() const { return false; }
3447
3448 // Put constant on the right.
3449 // Returns whether order is changed.
3450 bool OrderInputsWithConstantOnTheRight() {
3451 HInstruction* left = InputAt(0);
3452 HInstruction* right = InputAt(1);
3453 if (left->IsConstant() && !right->IsConstant()) {
3454 ReplaceInput(right, 0);
3455 ReplaceInput(left, 1);
3456 return true;
3457 }
3458 return false;
3459 }
3460
3461 // Order inputs by instruction id, but favor constant on the right side.
3462 // This helps GVN for commutative ops.
3463 void OrderInputs() {
3464 DCHECK(IsCommutative());
3465 HInstruction* left = InputAt(0);
3466 HInstruction* right = InputAt(1);
3467 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3468 return;
3469 }
3470 if (OrderInputsWithConstantOnTheRight()) {
3471 return;
3472 }
3473 // Order according to instruction id.
3474 if (left->GetId() > right->GetId()) {
3475 ReplaceInput(right, 0);
3476 ReplaceInput(left, 1);
3477 }
3478 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003479
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003480 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003481 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003482 return true;
3483 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003484
Roland Levillain31dd3d62016-02-16 12:21:02 +00003485 // Try to statically evaluate `this` and return a HConstant
3486 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003487 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003488 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003489
3490 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003491 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3492 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003493 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3494 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003495 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003496 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3497 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003498 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3499 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003500 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3501 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003502 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003503 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3504 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003505
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003506 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003507 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003508 HConstant* GetConstantRight() const;
3509
3510 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003511 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003512 HInstruction* GetLeastConstantLeft() const;
3513
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003514 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003515
Artem Serovcced8ba2017-07-19 18:18:09 +01003516 protected:
3517 DEFAULT_COPY_CONSTRUCTOR(BinaryOperation);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003518};
3519
Mark Mendellc4701932015-04-10 13:18:51 -04003520// The comparison bias applies for floating point operations and indicates how NaN
3521// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003522enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003523 kNoBias, // bias is not applicable (i.e. for long operation)
3524 kGtBias, // return 1 for NaN comparisons
3525 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003526 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003527};
3528
Roland Levillain31dd3d62016-02-16 12:21:02 +00003529std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3530
Dave Allison20dfc792014-06-16 20:44:29 -07003531class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003532 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303533 HCondition(InstructionKind kind,
3534 HInstruction* first,
3535 HInstruction* second,
3536 uint32_t dex_pc = kNoDexPc)
3537 : HBinaryOperation(kind,
3538 DataType::Type::kBool,
3539 first,
3540 second,
3541 SideEffects::None(),
3542 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003543 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3544 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003545
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003546 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003547 // `instruction`, and disregard moves in between.
3548 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003549
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003550 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003551
3552 virtual IfCondition GetCondition() const = 0;
3553
Mark Mendellc4701932015-04-10 13:18:51 -04003554 virtual IfCondition GetOppositeCondition() const = 0;
3555
Vladimir Markoa1de9182016-02-25 11:37:38 +00003556 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003557 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3558
Vladimir Markoa1de9182016-02-25 11:37:38 +00003559 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3560 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003561
Vladimir Marko372f10e2016-05-17 16:30:10 +01003562 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003563 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003564 }
3565
Roland Levillain4fa13f62015-07-06 18:11:54 +01003566 bool IsFPConditionTrueIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003567 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003568 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003569 if (if_cond == kCondNE) {
3570 return true;
3571 } else if (if_cond == kCondEQ) {
3572 return false;
3573 }
3574 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003575 }
3576
3577 bool IsFPConditionFalseIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003578 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003579 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003580 if (if_cond == kCondEQ) {
3581 return true;
3582 } else if (if_cond == kCondNE) {
3583 return false;
3584 }
3585 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003586 }
3587
Roland Levillain31dd3d62016-02-16 12:21:02 +00003588 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003589 // Needed if we merge a HCompare into a HCondition.
Vladimir Markobd785672018-05-03 17:09:09 +01003590 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003591 static constexpr size_t kFieldComparisonBiasSize =
3592 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3593 static constexpr size_t kNumberOfConditionPackedBits =
3594 kFieldComparisonBias + kFieldComparisonBiasSize;
3595 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3596 using ComparisonBiasField =
3597 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3598
Roland Levillain31dd3d62016-02-16 12:21:02 +00003599 template <typename T>
3600 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3601
3602 template <typename T>
3603 int32_t CompareFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003604 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003605 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3606 // Handle the bias.
3607 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3608 }
3609
3610 // Return an integer constant containing the result of a condition evaluated at compile time.
3611 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3612 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3613 }
3614
Artem Serovcced8ba2017-07-19 18:18:09 +01003615 DEFAULT_COPY_CONSTRUCTOR(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003616};
3617
3618// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003619class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003620 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003621 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303622 : HCondition(kEqual, first, second, dex_pc) {
3623 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003624
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003625 bool IsCommutative() const OVERRIDE { return true; }
3626
Vladimir Marko9e23df52015-11-10 17:14:35 +00003627 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3628 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003629 return MakeConstantCondition(true, GetDexPc());
3630 }
3631 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3632 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3633 }
3634 // In the following Evaluate methods, a HCompare instruction has
3635 // been merged into this HEqual instruction; evaluate it as
3636 // `Compare(x, y) == 0`.
3637 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3638 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3639 GetDexPc());
3640 }
3641 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3642 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3643 }
3644 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3645 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003646 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003647
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003648 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003649
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003650 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003651 return kCondEQ;
3652 }
3653
Mark Mendellc4701932015-04-10 13:18:51 -04003654 IfCondition GetOppositeCondition() const OVERRIDE {
3655 return kCondNE;
3656 }
3657
Artem Serovcced8ba2017-07-19 18:18:09 +01003658 protected:
3659 DEFAULT_COPY_CONSTRUCTOR(Equal);
3660
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003661 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003662 template <typename T> static bool Compute(T x, T y) { return x == y; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003663};
3664
Vladimir Markofcb503c2016-05-18 12:48:17 +01003665class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003666 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303667 HNotEqual(HInstruction* first, HInstruction* second,
3668 uint32_t dex_pc = kNoDexPc)
3669 : HCondition(kNotEqual, first, second, dex_pc) {
3670 }
Dave Allison20dfc792014-06-16 20:44:29 -07003671
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003672 bool IsCommutative() const OVERRIDE { return true; }
3673
Vladimir Marko9e23df52015-11-10 17:14:35 +00003674 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3675 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003676 return MakeConstantCondition(false, GetDexPc());
3677 }
3678 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3679 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3680 }
3681 // In the following Evaluate methods, a HCompare instruction has
3682 // been merged into this HNotEqual instruction; evaluate it as
3683 // `Compare(x, y) != 0`.
3684 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3685 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3686 }
3687 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3688 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3689 }
3690 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3691 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003692 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003693
Dave Allison20dfc792014-06-16 20:44:29 -07003694 DECLARE_INSTRUCTION(NotEqual);
3695
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003696 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003697 return kCondNE;
3698 }
3699
Mark Mendellc4701932015-04-10 13:18:51 -04003700 IfCondition GetOppositeCondition() const OVERRIDE {
3701 return kCondEQ;
3702 }
3703
Artem Serovcced8ba2017-07-19 18:18:09 +01003704 protected:
3705 DEFAULT_COPY_CONSTRUCTOR(NotEqual);
3706
Dave Allison20dfc792014-06-16 20:44:29 -07003707 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003708 template <typename T> static bool Compute(T x, T y) { return x != y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003709};
3710
Vladimir Markofcb503c2016-05-18 12:48:17 +01003711class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003712 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303713 HLessThan(HInstruction* first, HInstruction* second,
3714 uint32_t dex_pc = kNoDexPc)
3715 : HCondition(kLessThan, first, second, dex_pc) {
3716 }
Dave Allison20dfc792014-06-16 20:44:29 -07003717
Roland Levillain9867bc72015-08-05 10:21:34 +01003718 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003719 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003720 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003721 // In the following Evaluate methods, a HCompare instruction has
3722 // been merged into this HLessThan instruction; evaluate it as
3723 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003724 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003725 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3726 }
3727 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3728 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3729 }
3730 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3731 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003732 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003733
Dave Allison20dfc792014-06-16 20:44:29 -07003734 DECLARE_INSTRUCTION(LessThan);
3735
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003736 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003737 return kCondLT;
3738 }
3739
Mark Mendellc4701932015-04-10 13:18:51 -04003740 IfCondition GetOppositeCondition() const OVERRIDE {
3741 return kCondGE;
3742 }
3743
Artem Serovcced8ba2017-07-19 18:18:09 +01003744 protected:
3745 DEFAULT_COPY_CONSTRUCTOR(LessThan);
3746
Dave Allison20dfc792014-06-16 20:44:29 -07003747 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003748 template <typename T> static bool Compute(T x, T y) { return x < y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003749};
3750
Vladimir Markofcb503c2016-05-18 12:48:17 +01003751class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003752 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303753 HLessThanOrEqual(HInstruction* first, HInstruction* second,
3754 uint32_t dex_pc = kNoDexPc)
3755 : HCondition(kLessThanOrEqual, first, second, dex_pc) {
3756 }
Dave Allison20dfc792014-06-16 20:44:29 -07003757
Roland Levillain9867bc72015-08-05 10:21:34 +01003758 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003759 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003760 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003761 // In the following Evaluate methods, a HCompare instruction has
3762 // been merged into this HLessThanOrEqual instruction; evaluate it as
3763 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003764 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003765 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3766 }
3767 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3768 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3769 }
3770 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3771 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003772 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003773
Dave Allison20dfc792014-06-16 20:44:29 -07003774 DECLARE_INSTRUCTION(LessThanOrEqual);
3775
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003776 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003777 return kCondLE;
3778 }
3779
Mark Mendellc4701932015-04-10 13:18:51 -04003780 IfCondition GetOppositeCondition() const OVERRIDE {
3781 return kCondGT;
3782 }
3783
Artem Serovcced8ba2017-07-19 18:18:09 +01003784 protected:
3785 DEFAULT_COPY_CONSTRUCTOR(LessThanOrEqual);
3786
Dave Allison20dfc792014-06-16 20:44:29 -07003787 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003788 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003789};
3790
Vladimir Markofcb503c2016-05-18 12:48:17 +01003791class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003792 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003793 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303794 : HCondition(kGreaterThan, first, second, dex_pc) {
3795 }
Dave Allison20dfc792014-06-16 20:44:29 -07003796
Roland Levillain9867bc72015-08-05 10:21:34 +01003797 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003798 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003799 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003800 // In the following Evaluate methods, a HCompare instruction has
3801 // been merged into this HGreaterThan instruction; evaluate it as
3802 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003803 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003804 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3805 }
3806 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3807 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3808 }
3809 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3810 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003811 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003812
Dave Allison20dfc792014-06-16 20:44:29 -07003813 DECLARE_INSTRUCTION(GreaterThan);
3814
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003815 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003816 return kCondGT;
3817 }
3818
Mark Mendellc4701932015-04-10 13:18:51 -04003819 IfCondition GetOppositeCondition() const OVERRIDE {
3820 return kCondLE;
3821 }
3822
Artem Serovcced8ba2017-07-19 18:18:09 +01003823 protected:
3824 DEFAULT_COPY_CONSTRUCTOR(GreaterThan);
3825
Dave Allison20dfc792014-06-16 20:44:29 -07003826 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003827 template <typename T> static bool Compute(T x, T y) { return x > y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003828};
3829
Vladimir Markofcb503c2016-05-18 12:48:17 +01003830class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003831 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003832 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303833 : HCondition(kGreaterThanOrEqual, first, second, dex_pc) {
3834 }
Dave Allison20dfc792014-06-16 20:44:29 -07003835
Roland Levillain9867bc72015-08-05 10:21:34 +01003836 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003837 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003838 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003839 // In the following Evaluate methods, a HCompare instruction has
3840 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3841 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003842 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003843 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3844 }
3845 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3846 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3847 }
3848 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3849 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003850 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003851
Dave Allison20dfc792014-06-16 20:44:29 -07003852 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3853
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003854 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003855 return kCondGE;
3856 }
3857
Mark Mendellc4701932015-04-10 13:18:51 -04003858 IfCondition GetOppositeCondition() const OVERRIDE {
3859 return kCondLT;
3860 }
3861
Artem Serovcced8ba2017-07-19 18:18:09 +01003862 protected:
3863 DEFAULT_COPY_CONSTRUCTOR(GreaterThanOrEqual);
3864
Dave Allison20dfc792014-06-16 20:44:29 -07003865 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003866 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003867};
3868
Vladimir Markofcb503c2016-05-18 12:48:17 +01003869class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003870 public:
3871 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303872 : HCondition(kBelow, first, second, dex_pc) {
3873 }
Aart Bike9f37602015-10-09 11:15:55 -07003874
3875 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003876 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003877 }
3878 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003879 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3880 }
3881 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3882 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3883 LOG(FATAL) << DebugName() << " is not defined for float values";
3884 UNREACHABLE();
3885 }
3886 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3887 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3888 LOG(FATAL) << DebugName() << " is not defined for double values";
3889 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003890 }
3891
3892 DECLARE_INSTRUCTION(Below);
3893
3894 IfCondition GetCondition() const OVERRIDE {
3895 return kCondB;
3896 }
3897
3898 IfCondition GetOppositeCondition() const OVERRIDE {
3899 return kCondAE;
3900 }
3901
Artem Serovcced8ba2017-07-19 18:18:09 +01003902 protected:
3903 DEFAULT_COPY_CONSTRUCTOR(Below);
3904
Aart Bike9f37602015-10-09 11:15:55 -07003905 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003906 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003907 return MakeUnsigned(x) < MakeUnsigned(y);
3908 }
Aart Bike9f37602015-10-09 11:15:55 -07003909};
3910
Vladimir Markofcb503c2016-05-18 12:48:17 +01003911class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003912 public:
3913 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303914 : HCondition(kBelowOrEqual, first, second, dex_pc) {
3915 }
Aart Bike9f37602015-10-09 11:15:55 -07003916
3917 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003918 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003919 }
3920 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003921 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3922 }
3923 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3924 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3925 LOG(FATAL) << DebugName() << " is not defined for float values";
3926 UNREACHABLE();
3927 }
3928 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3929 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3930 LOG(FATAL) << DebugName() << " is not defined for double values";
3931 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003932 }
3933
3934 DECLARE_INSTRUCTION(BelowOrEqual);
3935
3936 IfCondition GetCondition() const OVERRIDE {
3937 return kCondBE;
3938 }
3939
3940 IfCondition GetOppositeCondition() const OVERRIDE {
3941 return kCondA;
3942 }
3943
Artem Serovcced8ba2017-07-19 18:18:09 +01003944 protected:
3945 DEFAULT_COPY_CONSTRUCTOR(BelowOrEqual);
3946
Aart Bike9f37602015-10-09 11:15:55 -07003947 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003948 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003949 return MakeUnsigned(x) <= MakeUnsigned(y);
3950 }
Aart Bike9f37602015-10-09 11:15:55 -07003951};
3952
Vladimir Markofcb503c2016-05-18 12:48:17 +01003953class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003954 public:
3955 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303956 : HCondition(kAbove, first, second, dex_pc) {
3957 }
Aart Bike9f37602015-10-09 11:15:55 -07003958
3959 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003960 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003961 }
3962 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003963 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3964 }
3965 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3966 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3967 LOG(FATAL) << DebugName() << " is not defined for float values";
3968 UNREACHABLE();
3969 }
3970 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3971 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3972 LOG(FATAL) << DebugName() << " is not defined for double values";
3973 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003974 }
3975
3976 DECLARE_INSTRUCTION(Above);
3977
3978 IfCondition GetCondition() const OVERRIDE {
3979 return kCondA;
3980 }
3981
3982 IfCondition GetOppositeCondition() const OVERRIDE {
3983 return kCondBE;
3984 }
3985
Artem Serovcced8ba2017-07-19 18:18:09 +01003986 protected:
3987 DEFAULT_COPY_CONSTRUCTOR(Above);
3988
Aart Bike9f37602015-10-09 11:15:55 -07003989 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003990 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003991 return MakeUnsigned(x) > MakeUnsigned(y);
3992 }
Aart Bike9f37602015-10-09 11:15:55 -07003993};
3994
Vladimir Markofcb503c2016-05-18 12:48:17 +01003995class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003996 public:
3997 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303998 : HCondition(kAboveOrEqual, first, second, dex_pc) {
3999 }
Aart Bike9f37602015-10-09 11:15:55 -07004000
4001 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004002 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004003 }
4004 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004005 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4006 }
4007 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4008 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4009 LOG(FATAL) << DebugName() << " is not defined for float values";
4010 UNREACHABLE();
4011 }
4012 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4013 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4014 LOG(FATAL) << DebugName() << " is not defined for double values";
4015 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004016 }
4017
4018 DECLARE_INSTRUCTION(AboveOrEqual);
4019
4020 IfCondition GetCondition() const OVERRIDE {
4021 return kCondAE;
4022 }
4023
4024 IfCondition GetOppositeCondition() const OVERRIDE {
4025 return kCondB;
4026 }
4027
Artem Serovcced8ba2017-07-19 18:18:09 +01004028 protected:
4029 DEFAULT_COPY_CONSTRUCTOR(AboveOrEqual);
4030
Aart Bike9f37602015-10-09 11:15:55 -07004031 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004032 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004033 return MakeUnsigned(x) >= MakeUnsigned(y);
4034 }
Aart Bike9f37602015-10-09 11:15:55 -07004035};
Dave Allison20dfc792014-06-16 20:44:29 -07004036
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004037// Instruction to check how two inputs compare to each other.
4038// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01004039class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004040 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00004041 // Note that `comparison_type` is the type of comparison performed
4042 // between the comparison's inputs, not the type of the instantiated
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004043 // HCompare instruction (which is always DataType::Type::kInt).
4044 HCompare(DataType::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004045 HInstruction* first,
4046 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04004047 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004048 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304049 : HBinaryOperation(kCompare,
4050 DataType::Type::kInt32,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004051 first,
4052 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00004053 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004054 dex_pc) {
4055 SetPackedField<ComparisonBiasField>(bias);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004056 DCHECK_EQ(comparison_type, DataType::Kind(first->GetType()));
4057 DCHECK_EQ(comparison_type, DataType::Kind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004058 }
4059
Roland Levillain9867bc72015-08-05 10:21:34 +01004060 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004061 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
4062
4063 template <typename T>
4064 int32_t ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004065 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004066 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
4067 // Handle the bias.
4068 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
4069 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004070
Roland Levillain9867bc72015-08-05 10:21:34 +01004071 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004072 // Note that there is no "cmp-int" Dex instruction so we shouldn't
4073 // reach this code path when processing a freshly built HIR
4074 // graph. However HCompare integer instructions can be synthesized
4075 // by the instruction simplifier to implement IntegerCompare and
4076 // IntegerSignum intrinsics, so we have to handle this case.
4077 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004078 }
4079 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004080 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4081 }
4082 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4083 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4084 }
4085 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4086 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01004087 }
4088
Vladimir Marko372f10e2016-05-17 16:30:10 +01004089 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004090 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00004091 }
4092
Vladimir Markoa1de9182016-02-25 11:37:38 +00004093 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04004094
Roland Levillain31dd3d62016-02-16 12:21:02 +00004095 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00004096 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00004097 bool IsGtBias() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004098 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004099 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00004100 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004101
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004102 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type type ATTRIBUTE_UNUSED) {
Roland Levillain1693a1f2016-03-15 14:57:31 +00004103 // Comparisons do not require a runtime call in any back end.
4104 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004105 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004106
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004107 DECLARE_INSTRUCTION(Compare);
4108
Roland Levillain31dd3d62016-02-16 12:21:02 +00004109 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01004110 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004111 static constexpr size_t kFieldComparisonBiasSize =
4112 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
4113 static constexpr size_t kNumberOfComparePackedBits =
4114 kFieldComparisonBias + kFieldComparisonBiasSize;
4115 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4116 using ComparisonBiasField =
4117 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
4118
Roland Levillain31dd3d62016-02-16 12:21:02 +00004119 // Return an integer constant containing the result of a comparison evaluated at compile time.
4120 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
4121 DCHECK(value == -1 || value == 0 || value == 1) << value;
4122 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
4123 }
4124
Artem Serovcced8ba2017-07-19 18:18:09 +01004125 DEFAULT_COPY_CONSTRUCTOR(Compare);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004126};
4127
Nicolas Geoffray2b615ba2017-01-06 14:40:07 +00004128class HNewInstance FINAL : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00004129 public:
4130 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00004131 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004132 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00004133 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00004134 bool finalizable,
4135 QuickEntrypointEnum entrypoint)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304136 : HExpression(kNewInstance,
4137 DataType::Type::kReference,
4138 SideEffects::CanTriggerGC(),
4139 dex_pc),
David Brazdil6de19382016-01-08 17:37:10 +00004140 type_index_(type_index),
4141 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00004142 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004143 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00004144 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00004145 }
4146
Artem Serovcced8ba2017-07-19 18:18:09 +01004147 bool IsClonable() const OVERRIDE { return true; }
4148
Andreas Gampea5b09a62016-11-17 15:21:22 -08004149 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00004150 const DexFile& GetDexFile() const { return dex_file_; }
4151
4152 // Calls runtime so needs an environment.
4153 bool NeedsEnvironment() const OVERRIDE { return true; }
4154
Mingyao Yang062157f2016-03-02 10:15:36 -08004155 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
4156 bool CanThrow() const OVERRIDE { return true; }
4157
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004158 bool NeedsChecks() const {
4159 return entrypoint_ == kQuickAllocObjectWithChecks;
4160 }
David Brazdil6de19382016-01-08 17:37:10 +00004161
Vladimir Markoa1de9182016-02-25 11:37:38 +00004162 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00004163
4164 bool CanBeNull() const OVERRIDE { return false; }
4165
4166 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4167
4168 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
4169 entrypoint_ = entrypoint;
4170 }
4171
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004172 HLoadClass* GetLoadClass() const {
4173 HInstruction* input = InputAt(0);
4174 if (input->IsClinitCheck()) {
4175 input = input->InputAt(0);
4176 }
4177 DCHECK(input->IsLoadClass());
4178 return input->AsLoadClass();
4179 }
4180
David Brazdil6de19382016-01-08 17:37:10 +00004181 bool IsStringAlloc() const;
4182
4183 DECLARE_INSTRUCTION(NewInstance);
4184
Artem Serovcced8ba2017-07-19 18:18:09 +01004185 protected:
4186 DEFAULT_COPY_CONSTRUCTOR(NewInstance);
4187
David Brazdil6de19382016-01-08 17:37:10 +00004188 private:
Vladimir Markobd785672018-05-03 17:09:09 +01004189 static constexpr size_t kFlagFinalizable = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004190 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
4191 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
4192 "Too many packed fields.");
4193
Andreas Gampea5b09a62016-11-17 15:21:22 -08004194 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00004195 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00004196 QuickEntrypointEnum entrypoint_;
David Brazdil6de19382016-01-08 17:37:10 +00004197};
4198
Agi Csaki05f20562015-08-19 14:58:14 -07004199enum IntrinsicNeedsEnvironmentOrCache {
4200 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
4201 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07004202};
4203
Aart Bik5d75afe2015-12-14 11:57:01 -08004204enum IntrinsicSideEffects {
4205 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
4206 kReadSideEffects, // Intrinsic may read heap memory.
4207 kWriteSideEffects, // Intrinsic may write heap memory.
4208 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
4209};
4210
4211enum IntrinsicExceptions {
4212 kNoThrow, // Intrinsic does not throw any exceptions.
4213 kCanThrow // Intrinsic may throw exceptions.
4214};
4215
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004216class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004217 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004218 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004219
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01004220 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004221 SetRawInputAt(index, argument);
4222 }
4223
Roland Levillain3e3d7332015-04-28 11:00:54 +01004224 // Return the number of arguments. This number can be lower than
4225 // the number of inputs returned by InputCount(), as some invoke
4226 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
4227 // inputs at the end of their list of inputs.
4228 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
4229
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004230 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4231
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004232 InvokeType GetInvokeType() const {
4233 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004234 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004235
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01004236 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004237 return intrinsic_;
4238 }
4239
Aart Bik5d75afe2015-12-14 11:57:01 -08004240 void SetIntrinsic(Intrinsics intrinsic,
4241 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
4242 IntrinsicSideEffects side_effects,
4243 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004244
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004245 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00004246 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004247 }
4248
Aart Bikff7d89c2016-11-07 08:49:28 -08004249 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4250
Vladimir Markoa1de9182016-02-25 11:37:38 +00004251 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004252
Aart Bika8b8e9b2018-01-09 11:01:02 -08004253 void SetAlwaysThrows(bool always_throws) { SetPackedFlag<kFlagAlwaysThrows>(always_throws); }
4254
4255 bool AlwaysThrows() const OVERRIDE { return GetPackedFlag<kFlagAlwaysThrows>(); }
4256
Aart Bik71bf7b42016-11-16 10:17:46 -08004257 bool CanBeMoved() const OVERRIDE { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004258
Vladimir Marko372f10e2016-05-17 16:30:10 +01004259 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08004260 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4261 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004262
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004263 uint32_t* GetIntrinsicOptimizations() {
4264 return &intrinsic_optimizations_;
4265 }
4266
4267 const uint32_t* GetIntrinsicOptimizations() const {
4268 return &intrinsic_optimizations_;
4269 }
4270
4271 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4272
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004273 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004274 void SetResolvedMethod(ArtMethod* method) { resolved_method_ = method; }
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004275
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004276 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004277
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004278 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004279 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4280 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004281 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
Vladimir Markobd785672018-05-03 17:09:09 +01004282 static constexpr size_t kFlagCanThrow = kFieldInvokeType + kFieldInvokeTypeSize;
Aart Bika8b8e9b2018-01-09 11:01:02 -08004283 static constexpr size_t kFlagAlwaysThrows = kFlagCanThrow + 1;
4284 static constexpr size_t kNumberOfInvokePackedBits = kFlagAlwaysThrows + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004285 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004286 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004287
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304288 HInvoke(InstructionKind kind,
4289 ArenaAllocator* allocator,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004290 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004291 uint32_t number_of_other_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004292 DataType::Type return_type,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004293 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004294 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004295 ArtMethod* resolved_method,
4296 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004297 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304298 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01004299 return_type,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004300 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4301 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004302 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004303 number_of_arguments + number_of_other_inputs,
4304 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004305 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004306 resolved_method_(resolved_method),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004307 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07004308 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004309 intrinsic_optimizations_(0) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004310 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004311 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004312 }
4313
Artem Serovcced8ba2017-07-19 18:18:09 +01004314 DEFAULT_COPY_CONSTRUCTOR(Invoke);
4315
Roland Levillain3e3d7332015-04-28 11:00:54 +01004316 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004317 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004318 const uint32_t dex_method_index_;
4319 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004320
4321 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4322 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004323};
4324
Vladimir Markofcb503c2016-05-18 12:48:17 +01004325class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004326 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004327 HInvokeUnresolved(ArenaAllocator* allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004328 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004329 DataType::Type return_type,
Calin Juravle175dc732015-08-25 15:42:32 +01004330 uint32_t dex_pc,
4331 uint32_t dex_method_index,
4332 InvokeType invoke_type)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304333 : HInvoke(kInvokeUnresolved,
4334 allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004335 number_of_arguments,
4336 0u /* number_of_other_inputs */,
4337 return_type,
4338 dex_pc,
4339 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004340 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01004341 invoke_type) {
4342 }
4343
Artem Serovcced8ba2017-07-19 18:18:09 +01004344 bool IsClonable() const OVERRIDE { return true; }
4345
Calin Juravle175dc732015-08-25 15:42:32 +01004346 DECLARE_INSTRUCTION(InvokeUnresolved);
4347
Artem Serovcced8ba2017-07-19 18:18:09 +01004348 protected:
4349 DEFAULT_COPY_CONSTRUCTOR(InvokeUnresolved);
Calin Juravle175dc732015-08-25 15:42:32 +01004350};
4351
Orion Hodsonac141392017-01-13 11:53:47 +00004352class HInvokePolymorphic FINAL : public HInvoke {
4353 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004354 HInvokePolymorphic(ArenaAllocator* allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004355 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004356 DataType::Type return_type,
Orion Hodsonac141392017-01-13 11:53:47 +00004357 uint32_t dex_pc,
4358 uint32_t dex_method_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304359 : HInvoke(kInvokePolymorphic,
4360 allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004361 number_of_arguments,
4362 0u /* number_of_other_inputs */,
4363 return_type,
4364 dex_pc,
4365 dex_method_index,
4366 nullptr,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304367 kVirtual) {
4368 }
Orion Hodsonac141392017-01-13 11:53:47 +00004369
Artem Serovcced8ba2017-07-19 18:18:09 +01004370 bool IsClonable() const OVERRIDE { return true; }
4371
Orion Hodsonac141392017-01-13 11:53:47 +00004372 DECLARE_INSTRUCTION(InvokePolymorphic);
4373
Artem Serovcced8ba2017-07-19 18:18:09 +01004374 protected:
4375 DEFAULT_COPY_CONSTRUCTOR(InvokePolymorphic);
Orion Hodsonac141392017-01-13 11:53:47 +00004376};
4377
Vladimir Markofcb503c2016-05-18 12:48:17 +01004378class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004379 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004380 // Requirements of this method call regarding the class
4381 // initialization (clinit) check of its declaring class.
4382 enum class ClinitCheckRequirement {
4383 kNone, // Class already initialized.
4384 kExplicit, // Static call having explicit clinit check as last input.
4385 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004386 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004387 };
4388
Vladimir Marko58155012015-08-19 12:49:41 +00004389 // Determines how to load the target ArtMethod*.
4390 enum class MethodLoadKind {
4391 // Use a String init ArtMethod* loaded from Thread entrypoints.
4392 kStringInit,
4393
4394 // Use the method's own ArtMethod* loaded by the register allocator.
4395 kRecursive,
4396
Vladimir Marko65979462017-05-19 17:25:12 +01004397 // Use PC-relative boot image ArtMethod* address that will be known at link time.
4398 // Used for boot image methods referenced by boot image code.
4399 kBootImageLinkTimePcRelative,
4400
Vladimir Marko58155012015-08-19 12:49:41 +00004401 // Use ArtMethod* at a known address, embed the direct address in the code.
4402 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
4403 kDirectAddress,
4404
Vladimir Markob066d432018-01-03 13:14:37 +00004405 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
4406 // Used for app->boot calls with relocatable image.
4407 kBootImageRelRo,
4408
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004409 // Load from an entry in the .bss section using a PC-relative load.
4410 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
4411 kBssEntry,
Vladimir Marko58155012015-08-19 12:49:41 +00004412
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004413 // Make a runtime call to resolve and call the method. This is the last-resort-kind
4414 // used when other kinds are unimplemented on a particular architecture.
4415 kRuntimeCall,
Vladimir Marko58155012015-08-19 12:49:41 +00004416 };
4417
4418 // Determines the location of the code pointer.
4419 enum class CodePtrLocation {
4420 // Recursive call, use local PC-relative call instruction.
4421 kCallSelf,
4422
Vladimir Marko58155012015-08-19 12:49:41 +00004423 // Use code pointer from the ArtMethod*.
4424 // Used when we don't know the target code. This is also the last-resort-kind used when
4425 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4426 kCallArtMethod,
4427 };
4428
4429 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004430 MethodLoadKind method_load_kind;
4431 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004432 // The method load data holds
4433 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4434 // Note that there are multiple string init methods, each having its own offset.
4435 // - the method address for kDirectAddress
Vladimir Markodc151b22015-10-15 18:02:30 +01004436 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004437 };
4438
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004439 HInvokeStaticOrDirect(ArenaAllocator* allocator,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004440 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004441 DataType::Type return_type,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004442 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004443 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004444 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004445 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004446 InvokeType invoke_type,
4447 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004448 ClinitCheckRequirement clinit_check_requirement)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304449 : HInvoke(kInvokeStaticOrDirect,
4450 allocator,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004451 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004452 // There is potentially one extra argument for the HCurrentMethod node, and
4453 // potentially one other if the clinit check is explicit, and potentially
4454 // one other if the method is a string factory.
4455 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004456 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004457 return_type,
4458 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004459 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004460 resolved_method,
4461 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004462 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004463 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004464 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4465 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004466
Artem Serovcced8ba2017-07-19 18:18:09 +01004467 bool IsClonable() const OVERRIDE { return true; }
4468
Vladimir Markodc151b22015-10-15 18:02:30 +01004469 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004470 bool had_current_method_input = HasCurrentMethodInput();
4471 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4472
4473 // Using the current method is the default and once we find a better
4474 // method load kind, we should not go back to using the current method.
4475 DCHECK(had_current_method_input || !needs_current_method_input);
4476
4477 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004478 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4479 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004480 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004481 dispatch_info_ = dispatch_info;
4482 }
4483
Aart Bik6daebeb2017-04-03 14:35:41 -07004484 DispatchInfo GetDispatchInfo() const {
4485 return dispatch_info_;
4486 }
4487
Vladimir Markoc53c0792015-11-19 15:48:33 +00004488 void AddSpecialInput(HInstruction* input) {
4489 // We allow only one special input.
4490 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4491 DCHECK(InputCount() == GetSpecialInputIndex() ||
4492 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4493 InsertInputAt(GetSpecialInputIndex(), input);
4494 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004495
Vladimir Marko372f10e2016-05-17 16:30:10 +01004496 using HInstruction::GetInputRecords; // Keep the const version visible.
4497 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
4498 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4499 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4500 DCHECK(!input_records.empty());
4501 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4502 HInstruction* last_input = input_records.back().GetInstruction();
4503 // Note: `last_input` may be null during arguments setup.
4504 if (last_input != nullptr) {
4505 // `last_input` is the last input of a static invoke marked as having
4506 // an explicit clinit check. It must either be:
4507 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4508 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4509 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4510 }
4511 }
4512 return input_records;
4513 }
4514
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004515 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004516 // We access the method via the dex cache so we can't do an implicit null check.
4517 // TODO: for intrinsics we can generate implicit null checks.
4518 return false;
4519 }
4520
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004521 bool CanBeNull() const OVERRIDE {
Vladimir Markobd785672018-05-03 17:09:09 +01004522 return GetType() == DataType::Type::kReference && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004523 }
4524
Vladimir Markoc53c0792015-11-19 15:48:33 +00004525 // Get the index of the special input, if any.
4526 //
David Brazdil6de19382016-01-08 17:37:10 +00004527 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4528 // method pointer; otherwise there may be one platform-specific special input,
4529 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004530 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004531 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004532
Vladimir Marko58155012015-08-19 12:49:41 +00004533 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4534 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4535 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004536 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004537 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004538 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko65979462017-05-19 17:25:12 +01004539 bool HasPcRelativeMethodLoadKind() const {
4540 return GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markob066d432018-01-03 13:14:37 +00004541 GetMethodLoadKind() == MethodLoadKind::kBootImageRelRo ||
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004542 GetMethodLoadKind() == MethodLoadKind::kBssEntry;
Vladimir Marko65979462017-05-19 17:25:12 +01004543 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004544 bool HasCurrentMethodInput() const {
4545 // This function can be called only after the invoke has been fully initialized by the builder.
4546 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004547 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004548 return true;
4549 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004550 DCHECK(InputCount() == GetSpecialInputIndex() ||
4551 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004552 return false;
4553 }
4554 }
Vladimir Marko58155012015-08-19 12:49:41 +00004555
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004556 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004557 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004558 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004559 }
4560
4561 uint64_t GetMethodAddress() const {
4562 DCHECK(HasMethodAddress());
4563 return dispatch_info_.method_load_data;
4564 }
4565
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004566 const DexFile& GetDexFileForPcRelativeDexCache() const;
4567
Vladimir Markoa1de9182016-02-25 11:37:38 +00004568 ClinitCheckRequirement GetClinitCheckRequirement() const {
4569 return GetPackedField<ClinitCheckRequirementField>();
4570 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004571
Roland Levillain4c0eb422015-04-24 16:43:49 +01004572 // Is this instruction a call to a static method?
4573 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004574 return GetInvokeType() == kStatic;
4575 }
4576
4577 MethodReference GetTargetMethod() const {
4578 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004579 }
4580
Vladimir Markofbb184a2015-11-13 14:47:00 +00004581 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4582 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4583 // instruction; only relevant for static calls with explicit clinit check.
4584 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004585 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004586 size_t last_input_index = inputs_.size() - 1u;
4587 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004588 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004589 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004590 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004591 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004592 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004593 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004594 }
4595
4596 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004597 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004598 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004599 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004600 }
4601
4602 // Is this a call to a static method whose declaring class has an
4603 // implicit intialization check requirement?
4604 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004605 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004606 }
4607
Vladimir Markob554b5a2015-11-06 12:57:55 +00004608 // Does this method load kind need the current method as an input?
4609 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004610 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kRuntimeCall;
Vladimir Markob554b5a2015-11-06 12:57:55 +00004611 }
4612
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004613 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004614
Artem Serovcced8ba2017-07-19 18:18:09 +01004615 protected:
4616 DEFAULT_COPY_CONSTRUCTOR(InvokeStaticOrDirect);
4617
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004618 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004619 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004620 static constexpr size_t kFieldClinitCheckRequirementSize =
4621 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4622 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4623 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4624 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4625 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004626 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4627 kFieldClinitCheckRequirement,
4628 kFieldClinitCheckRequirementSize>;
4629
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004630 // Cached values of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffrayea179f42018-02-08 22:30:18 +00004631 const MethodReference target_method_;
Vladimir Marko58155012015-08-19 12:49:41 +00004632 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004633};
Vladimir Markof64242a2015-12-01 14:58:23 +00004634std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004635std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004636
Vladimir Markofcb503c2016-05-18 12:48:17 +01004637class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004638 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004639 HInvokeVirtual(ArenaAllocator* allocator,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004640 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004641 DataType::Type return_type,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004642 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004643 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004644 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004645 uint32_t vtable_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304646 : HInvoke(kInvokeVirtual,
4647 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004648 number_of_arguments,
4649 0u,
4650 return_type,
4651 dex_pc,
4652 dex_method_index,
4653 resolved_method,
4654 kVirtual),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304655 vtable_index_(vtable_index) {
4656 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004657
Artem Serovcced8ba2017-07-19 18:18:09 +01004658 bool IsClonable() const OVERRIDE { return true; }
4659
Aart Bik71bf7b42016-11-16 10:17:46 -08004660 bool CanBeNull() const OVERRIDE {
4661 switch (GetIntrinsic()) {
4662 case Intrinsics::kThreadCurrentThread:
4663 case Intrinsics::kStringBufferAppend:
4664 case Intrinsics::kStringBufferToString:
4665 case Intrinsics::kStringBuilderAppend:
4666 case Intrinsics::kStringBuilderToString:
4667 return false;
4668 default:
4669 return HInvoke::CanBeNull();
4670 }
4671 }
4672
Calin Juravle641547a2015-04-21 22:08:51 +01004673 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004674 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004675 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004676 }
4677
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004678 uint32_t GetVTableIndex() const { return vtable_index_; }
4679
4680 DECLARE_INSTRUCTION(InvokeVirtual);
4681
Artem Serovcced8ba2017-07-19 18:18:09 +01004682 protected:
4683 DEFAULT_COPY_CONSTRUCTOR(InvokeVirtual);
4684
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004685 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004686 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004687 const uint32_t vtable_index_;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004688};
4689
Vladimir Markofcb503c2016-05-18 12:48:17 +01004690class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004691 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004692 HInvokeInterface(ArenaAllocator* allocator,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004693 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004694 DataType::Type return_type,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004695 uint32_t dex_pc,
4696 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004697 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004698 uint32_t imt_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304699 : HInvoke(kInvokeInterface,
4700 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004701 number_of_arguments,
4702 0u,
4703 return_type,
4704 dex_pc,
4705 dex_method_index,
4706 resolved_method,
4707 kInterface),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304708 imt_index_(imt_index) {
4709 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004710
Artem Serovcced8ba2017-07-19 18:18:09 +01004711 bool IsClonable() const OVERRIDE { return true; }
4712
Calin Juravle641547a2015-04-21 22:08:51 +01004713 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004714 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004715 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004716 }
4717
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004718 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
4719 // The assembly stub currently needs it.
4720 return true;
4721 }
4722
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004723 uint32_t GetImtIndex() const { return imt_index_; }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004724
4725 DECLARE_INSTRUCTION(InvokeInterface);
4726
Artem Serovcced8ba2017-07-19 18:18:09 +01004727 protected:
4728 DEFAULT_COPY_CONSTRUCTOR(InvokeInterface);
4729
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004730 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004731 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004732 const uint32_t imt_index_;
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004733};
4734
Vladimir Markofcb503c2016-05-18 12:48:17 +01004735class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004736 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004737 HNeg(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304738 : HUnaryOperation(kNeg, result_type, input, dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004739 DCHECK_EQ(result_type, DataType::Kind(input->GetType()));
Roland Levillain937e6cd2016-03-22 11:54:37 +00004740 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004741
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004742 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004743
4744 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004745 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004746 }
4747 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004748 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004749 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004750 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4751 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4752 }
4753 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4754 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4755 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004756
Roland Levillain88cb1752014-10-20 16:36:47 +01004757 DECLARE_INSTRUCTION(Neg);
4758
Artem Serovcced8ba2017-07-19 18:18:09 +01004759 protected:
4760 DEFAULT_COPY_CONSTRUCTOR(Neg);
Roland Levillain88cb1752014-10-20 16:36:47 +01004761};
4762
Vladimir Markofcb503c2016-05-18 12:48:17 +01004763class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004764 public:
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004765 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304766 : HExpression(kNewArray, DataType::Type::kReference, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004767 SetRawInputAt(0, cls);
4768 SetRawInputAt(1, length);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004769 }
4770
Artem Serovcced8ba2017-07-19 18:18:09 +01004771 bool IsClonable() const OVERRIDE { return true; }
4772
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004773 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004774 bool NeedsEnvironment() const OVERRIDE { return true; }
4775
Mingyao Yang0c365e62015-03-31 15:09:29 -07004776 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4777 bool CanThrow() const OVERRIDE { return true; }
4778
Calin Juravle10e244f2015-01-26 18:54:32 +00004779 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004780
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004781 HLoadClass* GetLoadClass() const {
4782 DCHECK(InputAt(0)->IsLoadClass());
4783 return InputAt(0)->AsLoadClass();
4784 }
4785
4786 HInstruction* GetLength() const {
4787 return InputAt(1);
4788 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004789
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004790 DECLARE_INSTRUCTION(NewArray);
4791
Artem Serovcced8ba2017-07-19 18:18:09 +01004792 protected:
4793 DEFAULT_COPY_CONSTRUCTOR(NewArray);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004794};
4795
Vladimir Markofcb503c2016-05-18 12:48:17 +01004796class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004797 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004798 HAdd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004799 HInstruction* left,
4800 HInstruction* right,
4801 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304802 : HBinaryOperation(kAdd, result_type, left, right, SideEffects::None(), dex_pc) {
4803 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004804
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004805 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004806
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004807 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004808
4809 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004810 return GetBlock()->GetGraph()->GetIntConstant(
4811 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004812 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004813 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004814 return GetBlock()->GetGraph()->GetLongConstant(
4815 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004816 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004817 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4818 return GetBlock()->GetGraph()->GetFloatConstant(
4819 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4820 }
4821 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4822 return GetBlock()->GetGraph()->GetDoubleConstant(
4823 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4824 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004825
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004826 DECLARE_INSTRUCTION(Add);
4827
Artem Serovcced8ba2017-07-19 18:18:09 +01004828 protected:
4829 DEFAULT_COPY_CONSTRUCTOR(Add);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004830};
4831
Vladimir Markofcb503c2016-05-18 12:48:17 +01004832class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004833 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004834 HSub(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004835 HInstruction* left,
4836 HInstruction* right,
4837 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304838 : HBinaryOperation(kSub, result_type, left, right, SideEffects::None(), dex_pc) {
4839 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004840
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004841 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004842
4843 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004844 return GetBlock()->GetGraph()->GetIntConstant(
4845 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004846 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004847 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004848 return GetBlock()->GetGraph()->GetLongConstant(
4849 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004850 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004851 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4852 return GetBlock()->GetGraph()->GetFloatConstant(
4853 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4854 }
4855 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4856 return GetBlock()->GetGraph()->GetDoubleConstant(
4857 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4858 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004859
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004860 DECLARE_INSTRUCTION(Sub);
4861
Artem Serovcced8ba2017-07-19 18:18:09 +01004862 protected:
4863 DEFAULT_COPY_CONSTRUCTOR(Sub);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004864};
4865
Vladimir Markofcb503c2016-05-18 12:48:17 +01004866class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004867 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004868 HMul(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004869 HInstruction* left,
4870 HInstruction* right,
4871 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304872 : HBinaryOperation(kMul, result_type, left, right, SideEffects::None(), dex_pc) {
4873 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004874
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004875 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004876
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004877 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004878
4879 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004880 return GetBlock()->GetGraph()->GetIntConstant(
4881 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004882 }
4883 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004884 return GetBlock()->GetGraph()->GetLongConstant(
4885 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004886 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004887 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4888 return GetBlock()->GetGraph()->GetFloatConstant(
4889 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4890 }
4891 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4892 return GetBlock()->GetGraph()->GetDoubleConstant(
4893 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4894 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004895
4896 DECLARE_INSTRUCTION(Mul);
4897
Artem Serovcced8ba2017-07-19 18:18:09 +01004898 protected:
4899 DEFAULT_COPY_CONSTRUCTOR(Mul);
Calin Juravle34bacdf2014-10-07 20:23:36 +01004900};
4901
Vladimir Markofcb503c2016-05-18 12:48:17 +01004902class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004903 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004904 HDiv(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004905 HInstruction* left,
4906 HInstruction* right,
4907 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304908 : HBinaryOperation(kDiv, result_type, left, right, SideEffects::None(), dex_pc) {
4909 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004910
Roland Levillain9867bc72015-08-05 10:21:34 +01004911 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004912 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004913 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004914 // Our graph structure ensures we never have 0 for `y` during
4915 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004916 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004917 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004918 return (y == -1) ? -x : x / y;
4919 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004920
Roland Levillain31dd3d62016-02-16 12:21:02 +00004921 template <typename T>
4922 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004923 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004924 return x / y;
4925 }
4926
Roland Levillain9867bc72015-08-05 10:21:34 +01004927 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004928 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004929 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004930 }
4931 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004932 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004933 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4934 }
4935 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4936 return GetBlock()->GetGraph()->GetFloatConstant(
4937 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4938 }
4939 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4940 return GetBlock()->GetGraph()->GetDoubleConstant(
4941 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004942 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004943
4944 DECLARE_INSTRUCTION(Div);
4945
Artem Serovcced8ba2017-07-19 18:18:09 +01004946 protected:
4947 DEFAULT_COPY_CONSTRUCTOR(Div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00004948};
4949
Vladimir Markofcb503c2016-05-18 12:48:17 +01004950class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004951 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004952 HRem(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004953 HInstruction* left,
4954 HInstruction* right,
4955 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304956 : HBinaryOperation(kRem, result_type, left, right, SideEffects::None(), dex_pc) {
4957 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004958
Roland Levillain9867bc72015-08-05 10:21:34 +01004959 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004960 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004961 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004962 // Our graph structure ensures we never have 0 for `y` during
4963 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004964 DCHECK_NE(y, 0);
4965 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4966 return (y == -1) ? 0 : x % y;
4967 }
4968
Roland Levillain31dd3d62016-02-16 12:21:02 +00004969 template <typename T>
4970 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004971 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004972 return std::fmod(x, y);
4973 }
4974
Roland Levillain9867bc72015-08-05 10:21:34 +01004975 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004976 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004977 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004978 }
4979 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004980 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004981 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004982 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004983 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4984 return GetBlock()->GetGraph()->GetFloatConstant(
4985 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4986 }
4987 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4988 return GetBlock()->GetGraph()->GetDoubleConstant(
4989 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4990 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004991
4992 DECLARE_INSTRUCTION(Rem);
4993
Artem Serovcced8ba2017-07-19 18:18:09 +01004994 protected:
4995 DEFAULT_COPY_CONSTRUCTOR(Rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00004996};
4997
Aart Bik1f8d51b2018-02-15 10:42:37 -08004998class HMin FINAL : public HBinaryOperation {
4999 public:
5000 HMin(DataType::Type result_type,
5001 HInstruction* left,
5002 HInstruction* right,
5003 uint32_t dex_pc)
5004 : HBinaryOperation(kMin, result_type, left, right, SideEffects::None(), dex_pc) {}
5005
5006 bool IsCommutative() const OVERRIDE { return true; }
5007
5008 // Evaluation for integral values.
5009 template <typename T> static T ComputeIntegral(T x, T y) {
5010 return (x <= y) ? x : y;
5011 }
5012
5013 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
5014 return GetBlock()->GetGraph()->GetIntConstant(
5015 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5016 }
5017 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
5018 return GetBlock()->GetGraph()->GetLongConstant(
5019 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5020 }
5021 // TODO: Evaluation for floating-point values.
5022 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5023 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { return nullptr; }
5024 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5025 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { return nullptr; }
5026
5027 DECLARE_INSTRUCTION(Min);
5028
5029 protected:
5030 DEFAULT_COPY_CONSTRUCTOR(Min);
5031};
5032
5033class HMax FINAL : public HBinaryOperation {
5034 public:
5035 HMax(DataType::Type result_type,
5036 HInstruction* left,
5037 HInstruction* right,
5038 uint32_t dex_pc)
5039 : HBinaryOperation(kMax, result_type, left, right, SideEffects::None(), dex_pc) {}
5040
5041 bool IsCommutative() const OVERRIDE { return true; }
5042
5043 // Evaluation for integral values.
5044 template <typename T> static T ComputeIntegral(T x, T y) {
5045 return (x >= y) ? x : y;
5046 }
5047
5048 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
5049 return GetBlock()->GetGraph()->GetIntConstant(
5050 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5051 }
5052 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
5053 return GetBlock()->GetGraph()->GetLongConstant(
5054 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5055 }
5056 // TODO: Evaluation for floating-point values.
5057 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5058 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { return nullptr; }
5059 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5060 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { return nullptr; }
5061
5062 DECLARE_INSTRUCTION(Max);
5063
5064 protected:
5065 DEFAULT_COPY_CONSTRUCTOR(Max);
5066};
5067
Aart Bik3dad3412018-02-28 12:01:46 -08005068class HAbs FINAL : public HUnaryOperation {
5069 public:
5070 HAbs(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
5071 : HUnaryOperation(kAbs, result_type, input, dex_pc) {}
5072
5073 // Evaluation for integral values.
5074 template <typename T> static T ComputeIntegral(T x) {
5075 return x < 0 ? -x : x;
5076 }
5077
5078 // Evaluation for floating-point values.
5079 // Note, as a "quality of implementation", rather than pure "spec compliance",
5080 // we require that Math.abs() clears the sign bit (but changes nothing else)
5081 // for all floating-point numbers, including NaN (signaling NaN may become quiet though).
5082 // http://b/30758343
5083 template <typename T, typename S> static T ComputeFP(T x) {
5084 S bits = bit_cast<S, T>(x);
5085 return bit_cast<T, S>(bits & std::numeric_limits<S>::max());
5086 }
5087
5088 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
5089 return GetBlock()->GetGraph()->GetIntConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5090 }
5091 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
5092 return GetBlock()->GetGraph()->GetLongConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5093 }
5094 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
5095 return GetBlock()->GetGraph()->GetFloatConstant(
5096 ComputeFP<float, int32_t>(x->GetValue()), GetDexPc());
5097 }
5098 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
5099 return GetBlock()->GetGraph()->GetDoubleConstant(
5100 ComputeFP<double, int64_t>(x->GetValue()), GetDexPc());
5101 }
5102
5103 DECLARE_INSTRUCTION(Abs);
5104
5105 protected:
5106 DEFAULT_COPY_CONSTRUCTOR(Abs);
5107};
5108
Vladimir Markofcb503c2016-05-18 12:48:17 +01005109class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00005110 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00005111 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
5112 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00005113 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305114 : HExpression(kDivZeroCheck, value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00005115 SetRawInputAt(0, value);
5116 }
5117
5118 bool CanBeMoved() const OVERRIDE { return true; }
5119
Vladimir Marko372f10e2016-05-17 16:30:10 +01005120 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00005121 return true;
5122 }
5123
5124 bool NeedsEnvironment() const OVERRIDE { return true; }
5125 bool CanThrow() const OVERRIDE { return true; }
5126
Calin Juravled0d48522014-11-04 16:40:20 +00005127 DECLARE_INSTRUCTION(DivZeroCheck);
5128
Artem Serovcced8ba2017-07-19 18:18:09 +01005129 protected:
5130 DEFAULT_COPY_CONSTRUCTOR(DivZeroCheck);
Calin Juravled0d48522014-11-04 16:40:20 +00005131};
5132
Vladimir Markofcb503c2016-05-18 12:48:17 +01005133class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005134 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005135 HShl(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005136 HInstruction* value,
5137 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005138 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305139 : HBinaryOperation(kShl, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005140 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5141 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005142 }
5143
Roland Levillain5b5b9312016-03-22 14:57:31 +00005144 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005145 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005146 return value << (distance & max_shift_distance);
5147 }
5148
5149 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005150 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005151 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005152 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005153 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005154 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005155 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005156 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005157 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5158 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5159 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5160 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005161 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005162 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5163 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005164 LOG(FATAL) << DebugName() << " is not defined for float values";
5165 UNREACHABLE();
5166 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005167 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5168 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005169 LOG(FATAL) << DebugName() << " is not defined for double values";
5170 UNREACHABLE();
5171 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005172
5173 DECLARE_INSTRUCTION(Shl);
5174
Artem Serovcced8ba2017-07-19 18:18:09 +01005175 protected:
5176 DEFAULT_COPY_CONSTRUCTOR(Shl);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005177};
5178
Vladimir Markofcb503c2016-05-18 12:48:17 +01005179class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005180 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005181 HShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005182 HInstruction* value,
5183 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005184 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305185 : HBinaryOperation(kShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005186 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5187 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005188 }
5189
Roland Levillain5b5b9312016-03-22 14:57:31 +00005190 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005191 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005192 return value >> (distance & max_shift_distance);
5193 }
5194
5195 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005196 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005197 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005198 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005199 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005200 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005201 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005202 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005203 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5204 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5205 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5206 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005207 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005208 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5209 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005210 LOG(FATAL) << DebugName() << " is not defined for float values";
5211 UNREACHABLE();
5212 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005213 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5214 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005215 LOG(FATAL) << DebugName() << " is not defined for double values";
5216 UNREACHABLE();
5217 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005218
5219 DECLARE_INSTRUCTION(Shr);
5220
Artem Serovcced8ba2017-07-19 18:18:09 +01005221 protected:
5222 DEFAULT_COPY_CONSTRUCTOR(Shr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005223};
5224
Vladimir Markofcb503c2016-05-18 12:48:17 +01005225class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005226 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005227 HUShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005228 HInstruction* value,
5229 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005230 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305231 : HBinaryOperation(kUShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005232 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5233 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00005234 }
5235
Roland Levillain5b5b9312016-03-22 14:57:31 +00005236 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005237 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005238 typedef typename std::make_unsigned<T>::type V;
5239 V ux = static_cast<V>(value);
5240 return static_cast<T>(ux >> (distance & max_shift_distance));
5241 }
5242
5243 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005244 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005245 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005246 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005247 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005248 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005249 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005250 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005251 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5252 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5253 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5254 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00005255 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005256 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5257 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005258 LOG(FATAL) << DebugName() << " is not defined for float values";
5259 UNREACHABLE();
5260 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005261 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5262 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005263 LOG(FATAL) << DebugName() << " is not defined for double values";
5264 UNREACHABLE();
5265 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005266
5267 DECLARE_INSTRUCTION(UShr);
5268
Artem Serovcced8ba2017-07-19 18:18:09 +01005269 protected:
5270 DEFAULT_COPY_CONSTRUCTOR(UShr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005271};
5272
Vladimir Markofcb503c2016-05-18 12:48:17 +01005273class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005274 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005275 HAnd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005276 HInstruction* left,
5277 HInstruction* right,
5278 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305279 : HBinaryOperation(kAnd, result_type, left, right, SideEffects::None(), dex_pc) {
5280 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005281
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005282 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005283
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005284 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005285
5286 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005287 return GetBlock()->GetGraph()->GetIntConstant(
5288 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005289 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005290 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005291 return GetBlock()->GetGraph()->GetLongConstant(
5292 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005293 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005294 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5295 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5296 LOG(FATAL) << DebugName() << " is not defined for float values";
5297 UNREACHABLE();
5298 }
5299 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5300 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5301 LOG(FATAL) << DebugName() << " is not defined for double values";
5302 UNREACHABLE();
5303 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005304
5305 DECLARE_INSTRUCTION(And);
5306
Artem Serovcced8ba2017-07-19 18:18:09 +01005307 protected:
5308 DEFAULT_COPY_CONSTRUCTOR(And);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005309};
5310
Vladimir Markofcb503c2016-05-18 12:48:17 +01005311class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005312 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005313 HOr(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005314 HInstruction* left,
5315 HInstruction* right,
5316 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305317 : HBinaryOperation(kOr, result_type, left, right, SideEffects::None(), dex_pc) {
5318 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005319
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005320 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005321
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005322 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005323
5324 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005325 return GetBlock()->GetGraph()->GetIntConstant(
5326 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005327 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005328 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005329 return GetBlock()->GetGraph()->GetLongConstant(
5330 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005331 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005332 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5333 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5334 LOG(FATAL) << DebugName() << " is not defined for float values";
5335 UNREACHABLE();
5336 }
5337 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5338 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5339 LOG(FATAL) << DebugName() << " is not defined for double values";
5340 UNREACHABLE();
5341 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005342
5343 DECLARE_INSTRUCTION(Or);
5344
Artem Serovcced8ba2017-07-19 18:18:09 +01005345 protected:
5346 DEFAULT_COPY_CONSTRUCTOR(Or);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005347};
5348
Vladimir Markofcb503c2016-05-18 12:48:17 +01005349class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005350 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005351 HXor(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005352 HInstruction* left,
5353 HInstruction* right,
5354 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305355 : HBinaryOperation(kXor, result_type, left, right, SideEffects::None(), dex_pc) {
5356 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005357
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005358 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005359
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005360 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005361
5362 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005363 return GetBlock()->GetGraph()->GetIntConstant(
5364 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005365 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005366 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005367 return GetBlock()->GetGraph()->GetLongConstant(
5368 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005369 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005370 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5371 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5372 LOG(FATAL) << DebugName() << " is not defined for float values";
5373 UNREACHABLE();
5374 }
5375 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5376 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5377 LOG(FATAL) << DebugName() << " is not defined for double values";
5378 UNREACHABLE();
5379 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005380
5381 DECLARE_INSTRUCTION(Xor);
5382
Artem Serovcced8ba2017-07-19 18:18:09 +01005383 protected:
5384 DEFAULT_COPY_CONSTRUCTOR(Xor);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005385};
5386
Vladimir Markofcb503c2016-05-18 12:48:17 +01005387class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005388 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005389 HRor(DataType::Type result_type, HInstruction* value, HInstruction* distance)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305390 : HBinaryOperation(kRor, result_type, value, distance) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005391 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5392 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00005393 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005394
Roland Levillain5b5b9312016-03-22 14:57:31 +00005395 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005396 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005397 typedef typename std::make_unsigned<T>::type V;
5398 V ux = static_cast<V>(value);
5399 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005400 return static_cast<T>(ux);
5401 } else {
5402 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005403 return static_cast<T>(ux >> (distance & max_shift_value)) |
5404 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005405 }
5406 }
5407
Roland Levillain5b5b9312016-03-22 14:57:31 +00005408 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005409 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005410 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005411 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005412 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005413 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005414 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005415 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005416 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5417 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5418 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5419 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005420 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005421 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5422 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005423 LOG(FATAL) << DebugName() << " is not defined for float values";
5424 UNREACHABLE();
5425 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005426 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5427 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005428 LOG(FATAL) << DebugName() << " is not defined for double values";
5429 UNREACHABLE();
5430 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005431
5432 DECLARE_INSTRUCTION(Ror);
5433
Artem Serovcced8ba2017-07-19 18:18:09 +01005434 protected:
5435 DEFAULT_COPY_CONSTRUCTOR(Ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005436};
5437
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005438// The value of a parameter in this method. Its location depends on
5439// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01005440class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005441 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005442 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005443 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005444 uint8_t index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005445 DataType::Type parameter_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005446 bool is_this = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305447 : HExpression(kParameterValue, parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005448 dex_file_(dex_file),
5449 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005450 index_(index) {
5451 SetPackedFlag<kFlagIsThis>(is_this);
5452 SetPackedFlag<kFlagCanBeNull>(!is_this);
5453 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005454
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005455 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005456 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005457 uint8_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07005458 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005459
Vladimir Markoa1de9182016-02-25 11:37:38 +00005460 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
5461 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005462
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005463 DECLARE_INSTRUCTION(ParameterValue);
5464
Artem Serovcced8ba2017-07-19 18:18:09 +01005465 protected:
5466 DEFAULT_COPY_CONSTRUCTOR(ParameterValue);
5467
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005468 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005469 // Whether or not the parameter value corresponds to 'this' argument.
Vladimir Markobd785672018-05-03 17:09:09 +01005470 static constexpr size_t kFlagIsThis = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005471 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5472 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5473 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5474 "Too many packed fields.");
5475
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005476 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005477 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005478 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005479 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005480 const uint8_t index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005481};
5482
Vladimir Markofcb503c2016-05-18 12:48:17 +01005483class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005484 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005485 HNot(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305486 : HUnaryOperation(kNot, result_type, input, dex_pc) {
5487 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005488
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005489 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005490 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005491 return true;
5492 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005493
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005494 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005495
5496 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005497 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005498 }
5499 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005500 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005501 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005502 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5503 LOG(FATAL) << DebugName() << " is not defined for float values";
5504 UNREACHABLE();
5505 }
5506 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5507 LOG(FATAL) << DebugName() << " is not defined for double values";
5508 UNREACHABLE();
5509 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005510
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005511 DECLARE_INSTRUCTION(Not);
5512
Artem Serovcced8ba2017-07-19 18:18:09 +01005513 protected:
5514 DEFAULT_COPY_CONSTRUCTOR(Not);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005515};
5516
Vladimir Markofcb503c2016-05-18 12:48:17 +01005517class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005518 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005519 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305520 : HUnaryOperation(kBooleanNot, DataType::Type::kBool, input, dex_pc) {
5521 }
David Brazdil66d126e2015-04-03 16:02:44 +01005522
5523 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005524 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01005525 return true;
5526 }
5527
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005528 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005529 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005530 return !x;
5531 }
5532
Roland Levillain9867bc72015-08-05 10:21:34 +01005533 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005534 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005535 }
5536 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5537 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005538 UNREACHABLE();
5539 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005540 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5541 LOG(FATAL) << DebugName() << " is not defined for float values";
5542 UNREACHABLE();
5543 }
5544 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5545 LOG(FATAL) << DebugName() << " is not defined for double values";
5546 UNREACHABLE();
5547 }
David Brazdil66d126e2015-04-03 16:02:44 +01005548
5549 DECLARE_INSTRUCTION(BooleanNot);
5550
Artem Serovcced8ba2017-07-19 18:18:09 +01005551 protected:
5552 DEFAULT_COPY_CONSTRUCTOR(BooleanNot);
David Brazdil66d126e2015-04-03 16:02:44 +01005553};
5554
Vladimir Markofcb503c2016-05-18 12:48:17 +01005555class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005556 public:
5557 // Instantiate a type conversion of `input` to `result_type`.
Vladimir Markoc8fb2112017-10-03 11:37:52 +01005558 HTypeConversion(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305559 : HExpression(kTypeConversion, result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005560 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005561 // Invariant: We should never generate a conversion to a Boolean value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005562 DCHECK_NE(DataType::Type::kBool, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005563 }
5564
5565 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005566 DataType::Type GetInputType() const { return GetInput()->GetType(); }
5567 DataType::Type GetResultType() const { return GetType(); }
Roland Levillaindff1f282014-11-05 14:15:05 +00005568
5569 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005570 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5571 return true;
5572 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005573
Mark Mendelle82549b2015-05-06 10:55:34 -04005574 // Try to statically evaluate the conversion and return a HConstant
5575 // containing the result. If the input cannot be converted, return nullptr.
5576 HConstant* TryStaticEvaluation() const;
5577
Roland Levillaindff1f282014-11-05 14:15:05 +00005578 DECLARE_INSTRUCTION(TypeConversion);
5579
Artem Serovcced8ba2017-07-19 18:18:09 +01005580 protected:
5581 DEFAULT_COPY_CONSTRUCTOR(TypeConversion);
Roland Levillaindff1f282014-11-05 14:15:05 +00005582};
5583
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005584static constexpr uint32_t kNoRegNumber = -1;
5585
Vladimir Markofcb503c2016-05-18 12:48:17 +01005586class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005587 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005588 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
5589 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005590 HNullCheck(HInstruction* value, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305591 : HExpression(kNullCheck, value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005592 SetRawInputAt(0, value);
5593 }
5594
Artem Serovcced8ba2017-07-19 18:18:09 +01005595 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00005596 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005597 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005598 return true;
5599 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005600
Calin Juravle10e244f2015-01-26 18:54:32 +00005601 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005602
Calin Juravle10e244f2015-01-26 18:54:32 +00005603 bool CanThrow() const OVERRIDE { return true; }
5604
5605 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005606
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005607 DECLARE_INSTRUCTION(NullCheck);
5608
Artem Serovcced8ba2017-07-19 18:18:09 +01005609 protected:
5610 DEFAULT_COPY_CONSTRUCTOR(NullCheck);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005611};
5612
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005613// Embeds an ArtField and all the information required by the compiler. We cache
5614// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005615class FieldInfo : public ValueObject {
5616 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005617 FieldInfo(ArtField* field,
5618 MemberOffset field_offset,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005619 DataType::Type field_type,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005620 bool is_volatile,
5621 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005622 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005623 const DexFile& dex_file)
5624 : field_(field),
5625 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005626 field_type_(field_type),
5627 is_volatile_(is_volatile),
5628 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005629 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005630 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005631
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005632 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005633 MemberOffset GetFieldOffset() const { return field_offset_; }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005634 DataType::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005635 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005636 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005637 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005638 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005639
5640 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005641 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005642 const MemberOffset field_offset_;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005643 const DataType::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005644 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005645 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005646 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005647 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005648};
5649
Vladimir Markofcb503c2016-05-18 12:48:17 +01005650class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005651 public:
5652 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005653 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005654 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005655 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005656 bool is_volatile,
5657 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005658 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005659 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005660 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305661 : HExpression(kInstanceFieldGet,
5662 field_type,
5663 SideEffects::FieldReadOfType(field_type, is_volatile),
5664 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005665 field_info_(field,
5666 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005667 field_type,
5668 is_volatile,
5669 field_idx,
5670 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005671 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005672 SetRawInputAt(0, value);
5673 }
5674
Artem Serovcced8ba2017-07-19 18:18:09 +01005675 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005676 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005677
Vladimir Marko372f10e2016-05-17 16:30:10 +01005678 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5679 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005680 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005681 }
5682
Calin Juravle641547a2015-04-21 22:08:51 +01005683 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005684 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005685 }
5686
5687 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005688 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5689 }
5690
Calin Juravle52c48962014-12-16 17:02:57 +00005691 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005692 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005693 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005694 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005695
Vladimir Marko61b92282017-10-11 13:23:17 +01005696 void SetType(DataType::Type new_type) {
5697 DCHECK(DataType::IsIntegralType(GetType()));
5698 DCHECK(DataType::IsIntegralType(new_type));
5699 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5700 SetPackedField<TypeField>(new_type);
5701 }
5702
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005703 DECLARE_INSTRUCTION(InstanceFieldGet);
5704
Artem Serovcced8ba2017-07-19 18:18:09 +01005705 protected:
5706 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldGet);
5707
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005708 private:
5709 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005710};
5711
Vladimir Markobd785672018-05-03 17:09:09 +01005712class HInstanceFieldSet FINAL : public HExpression<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005713 public:
5714 HInstanceFieldSet(HInstruction* object,
5715 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005716 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005717 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005718 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005719 bool is_volatile,
5720 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005721 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005722 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005723 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01005724 : HExpression(kInstanceFieldSet,
5725 SideEffects::FieldWriteOfType(field_type, is_volatile),
5726 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005727 field_info_(field,
5728 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005729 field_type,
5730 is_volatile,
5731 field_idx,
5732 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005733 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005734 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005735 SetRawInputAt(0, object);
5736 SetRawInputAt(1, value);
5737 }
5738
Artem Serovcced8ba2017-07-19 18:18:09 +01005739 bool IsClonable() const OVERRIDE { return true; }
5740
Calin Juravle641547a2015-04-21 22:08:51 +01005741 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005742 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005743 }
5744
Calin Juravle52c48962014-12-16 17:02:57 +00005745 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005746 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005747 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005748 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005749 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005750 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5751 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005752
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005753 DECLARE_INSTRUCTION(InstanceFieldSet);
5754
Artem Serovcced8ba2017-07-19 18:18:09 +01005755 protected:
5756 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldSet);
5757
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005758 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005759 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5760 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5761 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5762 "Too many packed fields.");
5763
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005764 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005765};
5766
Vladimir Markofcb503c2016-05-18 12:48:17 +01005767class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005768 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005769 HArrayGet(HInstruction* array,
5770 HInstruction* index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005771 DataType::Type type,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005772 uint32_t dex_pc)
5773 : HArrayGet(array,
5774 index,
5775 type,
5776 SideEffects::ArrayReadOfType(type),
5777 dex_pc,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305778 /* is_string_char_at */ false) {
5779 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005780
5781 HArrayGet(HInstruction* array,
5782 HInstruction* index,
5783 DataType::Type type,
5784 SideEffects side_effects,
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005785 uint32_t dex_pc,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005786 bool is_string_char_at)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305787 : HExpression(kArrayGet, type, side_effects, dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005788 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005789 SetRawInputAt(0, array);
5790 SetRawInputAt(1, index);
5791 }
5792
Artem Serovcced8ba2017-07-19 18:18:09 +01005793 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005794 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005795 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005796 return true;
5797 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005798 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005799 // TODO: We can be smarter here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01005800 // Currently, unless the array is the result of NewArray, the array access is always
5801 // preceded by some form of null NullCheck necessary for the bounds check, usually
5802 // implicit null check on the ArrayLength input to BoundsCheck or Deoptimize for
5803 // dynamic BCE. There are cases when these could be removed to produce better code.
5804 // If we ever add optimizations to do so we should allow an implicit check here
5805 // (as long as the address falls in the first page).
5806 //
5807 // As an example of such fancy optimization, we could eliminate BoundsCheck for
5808 // a = cond ? new int[1] : null;
5809 // a[0]; // The Phi does not need bounds check for either input.
Calin Juravle77520bc2015-01-12 18:45:46 +00005810 return false;
5811 }
5812
David Brazdil4833f5a2015-12-16 10:37:39 +00005813 bool IsEquivalentOf(HArrayGet* other) const {
5814 bool result = (GetDexPc() == other->GetDexPc());
5815 if (kIsDebugBuild && result) {
5816 DCHECK_EQ(GetBlock(), other->GetBlock());
5817 DCHECK_EQ(GetArray(), other->GetArray());
5818 DCHECK_EQ(GetIndex(), other->GetIndex());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005819 if (DataType::IsIntOrLongType(GetType())) {
5820 DCHECK(DataType::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005821 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005822 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
5823 DCHECK(DataType::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005824 }
5825 }
5826 return result;
5827 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005828
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005829 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5830
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005831 HInstruction* GetArray() const { return InputAt(0); }
5832 HInstruction* GetIndex() const { return InputAt(1); }
5833
Vladimir Marko61b92282017-10-11 13:23:17 +01005834 void SetType(DataType::Type new_type) {
5835 DCHECK(DataType::IsIntegralType(GetType()));
5836 DCHECK(DataType::IsIntegralType(new_type));
5837 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5838 SetPackedField<TypeField>(new_type);
5839 }
5840
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005841 DECLARE_INSTRUCTION(ArrayGet);
5842
Artem Serovcced8ba2017-07-19 18:18:09 +01005843 protected:
5844 DEFAULT_COPY_CONSTRUCTOR(ArrayGet);
5845
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005846 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005847 // We treat a String as an array, creating the HArrayGet from String.charAt()
5848 // intrinsic in the instruction simplifier. We can always determine whether
5849 // a particular HArrayGet is actually a String.charAt() by looking at the type
5850 // of the input but that requires holding the mutator lock, so we prefer to use
5851 // a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01005852 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005853 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5854 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5855 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005856};
5857
Vladimir Markobd785672018-05-03 17:09:09 +01005858class HArraySet FINAL : public HExpression<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005859 public:
5860 HArraySet(HInstruction* array,
5861 HInstruction* index,
5862 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005863 DataType::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005864 uint32_t dex_pc)
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005865 : HArraySet(array,
5866 index,
5867 value,
5868 expected_component_type,
5869 // Make a best guess for side effects now, may be refined during SSA building.
5870 ComputeSideEffects(GetComponentType(value->GetType(), expected_component_type)),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305871 dex_pc) {
5872 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005873
5874 HArraySet(HInstruction* array,
5875 HInstruction* index,
5876 HInstruction* value,
5877 DataType::Type expected_component_type,
5878 SideEffects side_effects,
5879 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01005880 : HExpression(kArraySet, side_effects, dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005881 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005882 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005883 SetPackedFlag<kFlagValueCanBeNull>(true);
5884 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005885 SetRawInputAt(0, array);
5886 SetRawInputAt(1, index);
5887 SetRawInputAt(2, value);
5888 }
5889
Artem Serovcced8ba2017-07-19 18:18:09 +01005890 bool IsClonable() const OVERRIDE { return true; }
5891
Calin Juravle77520bc2015-01-12 18:45:46 +00005892 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005893 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005894 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005895 }
5896
Mingyao Yang81014cb2015-06-02 03:16:27 -07005897 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005898 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005899
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005900 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005901 // TODO: Same as for ArrayGet.
5902 return false;
5903 }
5904
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005905 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005906 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005907 }
5908
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005909 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005910 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005911 }
5912
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005913 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005914 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005915 }
5916
Vladimir Markoa1de9182016-02-25 11:37:38 +00005917 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5918 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5919 bool StaticTypeOfArrayIsObjectArray() const {
5920 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5921 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005922
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005923 HInstruction* GetArray() const { return InputAt(0); }
5924 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005925 HInstruction* GetValue() const { return InputAt(2); }
5926
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005927 DataType::Type GetComponentType() const {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005928 return GetComponentType(GetValue()->GetType(), GetRawExpectedComponentType());
5929 }
5930
5931 static DataType::Type GetComponentType(DataType::Type value_type,
5932 DataType::Type expected_component_type) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005933 // The Dex format does not type floating point index operations. Since the
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005934 // `expected_component_type` comes from SSA building and can therefore not
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005935 // be correct, we also check what is the value type. If it is a floating
5936 // point type, we must use that type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005937 return ((value_type == DataType::Type::kFloat32) || (value_type == DataType::Type::kFloat64))
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005938 ? value_type
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005939 : expected_component_type;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005940 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005941
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005942 DataType::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005943 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005944 }
5945
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005946 static SideEffects ComputeSideEffects(DataType::Type type) {
5947 return SideEffects::ArrayWriteOfType(type).Union(SideEffectsForArchRuntimeCalls(type));
Aart Bik18b36ab2016-04-13 16:41:35 -07005948 }
5949
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005950 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type value_type) {
5951 return (value_type == DataType::Type::kReference) ? SideEffects::CanTriggerGC()
5952 : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005953 }
5954
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005955 DECLARE_INSTRUCTION(ArraySet);
5956
Artem Serovcced8ba2017-07-19 18:18:09 +01005957 protected:
5958 DEFAULT_COPY_CONSTRUCTOR(ArraySet);
5959
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005960 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005961 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5962 static constexpr size_t kFieldExpectedComponentTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005963 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00005964 static constexpr size_t kFlagNeedsTypeCheck =
5965 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5966 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005967 // Cached information for the reference_type_info_ so that codegen
5968 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005969 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5970 static constexpr size_t kNumberOfArraySetPackedBits =
5971 kFlagStaticTypeOfArrayIsObjectArray + 1;
5972 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5973 using ExpectedComponentTypeField =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005974 BitField<DataType::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005975};
5976
Vladimir Markofcb503c2016-05-18 12:48:17 +01005977class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005978 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005979 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305980 : HExpression(kArrayLength, DataType::Type::kInt32, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005981 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005982 // Note that arrays do not change length, so the instruction does not
5983 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005984 SetRawInputAt(0, array);
5985 }
5986
Artem Serovcced8ba2017-07-19 18:18:09 +01005987 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle77520bc2015-01-12 18:45:46 +00005988 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005989 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005990 return true;
5991 }
Calin Juravle641547a2015-04-21 22:08:51 +01005992 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5993 return obj == InputAt(0);
5994 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005995
Vladimir Markodce016e2016-04-28 13:10:02 +01005996 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5997
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005998 DECLARE_INSTRUCTION(ArrayLength);
5999
Artem Serovcced8ba2017-07-19 18:18:09 +01006000 protected:
6001 DEFAULT_COPY_CONSTRUCTOR(ArrayLength);
6002
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006003 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01006004 // We treat a String as an array, creating the HArrayLength from String.length()
6005 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
6006 // determine whether a particular HArrayLength is actually a String.length() by
6007 // looking at the type of the input but that requires holding the mutator lock, so
6008 // we prefer to use a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01006009 static constexpr size_t kFlagIsStringLength = kNumberOfGenericPackedBits;
Vladimir Markodce016e2016-04-28 13:10:02 +01006010 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
6011 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6012 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006013};
6014
Vladimir Markofcb503c2016-05-18 12:48:17 +01006015class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006016 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00006017 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
6018 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006019 HBoundsCheck(HInstruction* index,
6020 HInstruction* length,
6021 uint32_t dex_pc,
Vladimir Marko0259c242017-12-04 11:27:47 +00006022 bool is_string_char_at = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306023 : HExpression(kBoundsCheck, index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006024 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(index->GetType()));
Vladimir Marko0259c242017-12-04 11:27:47 +00006025 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006026 SetRawInputAt(0, index);
6027 SetRawInputAt(1, length);
6028 }
6029
Artem Serovcced8ba2017-07-19 18:18:09 +01006030 bool IsClonable() const OVERRIDE { return true; }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006031 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006032 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006033 return true;
6034 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006035
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006036 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006037
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006038 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01006039
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00006040 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006041
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006042 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006043
6044 DECLARE_INSTRUCTION(BoundsCheck);
6045
Artem Serovcced8ba2017-07-19 18:18:09 +01006046 protected:
6047 DEFAULT_COPY_CONSTRUCTOR(BoundsCheck);
6048
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006049 private:
Vladimir Markobd785672018-05-03 17:09:09 +01006050 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006051};
6052
Vladimir Markobd785672018-05-03 17:09:09 +01006053class HSuspendCheck FINAL : public HExpression<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006054 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00006055 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01006056 : HExpression(kSuspendCheck, SideEffects::CanTriggerGC(), dex_pc),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306057 slow_path_(nullptr) {
6058 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006059
Artem Serovcced8ba2017-07-19 18:18:09 +01006060 bool IsClonable() const OVERRIDE { return true; }
6061
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006062 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006063 return true;
6064 }
6065
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006066 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
6067 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006068
6069 DECLARE_INSTRUCTION(SuspendCheck);
6070
Artem Serovcced8ba2017-07-19 18:18:09 +01006071 protected:
6072 DEFAULT_COPY_CONSTRUCTOR(SuspendCheck);
6073
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006074 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006075 // Only used for code generation, in order to share the same slow path between back edges
6076 // of a same loop.
6077 SlowPathCode* slow_path_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006078};
6079
David Srbecky0cf44932015-12-09 14:09:59 +00006080// Pseudo-instruction which provides the native debugger with mapping information.
6081// It ensures that we can generate line number and local variables at this point.
Vladimir Markobd785672018-05-03 17:09:09 +01006082class HNativeDebugInfo : public HExpression<0> {
David Srbecky0cf44932015-12-09 14:09:59 +00006083 public:
6084 explicit HNativeDebugInfo(uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006085 : HExpression<0>(kNativeDebugInfo, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306086 }
David Srbecky0cf44932015-12-09 14:09:59 +00006087
6088 bool NeedsEnvironment() const OVERRIDE {
6089 return true;
6090 }
6091
6092 DECLARE_INSTRUCTION(NativeDebugInfo);
6093
Artem Serovcced8ba2017-07-19 18:18:09 +01006094 protected:
6095 DEFAULT_COPY_CONSTRUCTOR(NativeDebugInfo);
David Srbecky0cf44932015-12-09 14:09:59 +00006096};
6097
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006098/**
6099 * Instruction to load a Class object.
6100 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006101class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006102 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006103 // Determines how to load the Class.
6104 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006105 // We cannot load this class. See HSharpening::SharpenLoadClass.
6106 kInvalid = -1,
6107
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006108 // Use the Class* from the method's own ArtMethod*.
6109 kReferrersClass,
6110
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006111 // Use PC-relative boot image Class* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006112 // Used for boot image classes referenced by boot image code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006113 kBootImageLinkTimePcRelative,
6114
6115 // Use a known boot image Class* address, embedded in the code by the codegen.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006116 // Used for boot image classes referenced by apps in JIT- and AOT-compiled code (non-PIC).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006117 kBootImageAddress,
6118
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006119 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
6120 // Used for boot image classes referenced by apps in AOT-compiled code (PIC).
6121 kBootImageRelRo,
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006122
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006123 // Load from an entry in the .bss section using a PC-relative load.
6124 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
6125 kBssEntry,
6126
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006127 // Load from the root table associated with the JIT compiled method.
6128 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006129
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006130 // Load using a simple runtime call. This is the fall-back load kind when
6131 // the codegen is unable to use another appropriate kind.
6132 kRuntimeCall,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006133
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006134 kLast = kRuntimeCall
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006135 };
6136
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006137 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08006138 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006139 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006140 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006141 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01006142 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00006143 bool needs_access_check)
Vladimir Markobd785672018-05-03 17:09:09 +01006144 : HInstruction(kLoadClass,
6145 DataType::Type::kReference,
6146 SideEffectsForArchRuntimeCalls(),
6147 dex_pc),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006148 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006149 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006150 dex_file_(dex_file),
Vladimir Marko175e7862018-03-27 09:03:13 +00006151 klass_(klass) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01006152 // Referrers class should not need access check. We never inline unverified
6153 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006154 DCHECK(!is_referrers_class || !needs_access_check);
6155
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006156 SetPackedField<LoadKindField>(
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006157 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kRuntimeCall);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006158 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00006159 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006160 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Marko175e7862018-03-27 09:03:13 +00006161 SetPackedFlag<kFlagValidLoadedClassRTI>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006162 }
6163
Artem Serovcced8ba2017-07-19 18:18:09 +01006164 bool IsClonable() const OVERRIDE { return true; }
6165
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006166 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006167
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006168 LoadKind GetLoadKind() const {
6169 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006170 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006171
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006172 bool HasPcRelativeLoadKind() const {
6173 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6174 GetLoadKind() == LoadKind::kBootImageRelRo ||
6175 GetLoadKind() == LoadKind::kBssEntry;
6176 }
6177
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006178 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006179
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006180 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006181
Andreas Gampea5b09a62016-11-17 15:21:22 -08006182 size_t ComputeHashCode() const OVERRIDE { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006183
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01006184 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006185
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006186 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006187 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006188 }
6189
Calin Juravle0ba218d2015-05-19 18:46:01 +01006190 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00006191 // The entrypoint the code generator is going to call does not do
6192 // clinit of the class.
6193 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00006194 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006195 }
6196
6197 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00006198 return NeedsAccessCheck() ||
6199 MustGenerateClinitCheck() ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006200 GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006201 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01006202 }
6203
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006204 bool CanThrow() const OVERRIDE {
Vladimir Marko41559982017-01-06 14:04:23 +00006205 return NeedsAccessCheck() ||
6206 MustGenerateClinitCheck() ||
6207 // If the class is in the boot image, the lookup in the runtime call cannot throw.
6208 // This keeps CanThrow() consistent between non-PIC (using kBootImageAddress) and
6209 // PIC and subsequently avoids a DCE behavior dependency on the PIC option.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006210 ((GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006211 GetLoadKind() == LoadKind::kBssEntry) &&
6212 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006213 }
6214
Calin Juravleacf735c2015-02-12 15:25:22 +00006215 ReferenceTypeInfo GetLoadedClassRTI() {
Vladimir Marko175e7862018-03-27 09:03:13 +00006216 if (GetPackedFlag<kFlagValidLoadedClassRTI>()) {
6217 // Note: The is_exact flag from the return value should not be used.
6218 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact */ true);
6219 } else {
6220 return ReferenceTypeInfo::CreateInvalid();
6221 }
Calin Juravleacf735c2015-02-12 15:25:22 +00006222 }
6223
Vladimir Marko175e7862018-03-27 09:03:13 +00006224 // Loaded class RTI is marked as valid by RTP if the klass_ is admissible.
6225 void SetValidLoadedClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
6226 DCHECK(klass_ != nullptr);
6227 SetPackedFlag<kFlagValidLoadedClassRTI>(true);
Calin Juravleacf735c2015-02-12 15:25:22 +00006228 }
6229
Andreas Gampea5b09a62016-11-17 15:21:22 -08006230 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006231 const DexFile& GetDexFile() const { return dex_file_; }
6232
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006233 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006234 return GetLoadKind() == LoadKind::kRuntimeCall;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006235 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00006236
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006237 static SideEffects SideEffectsForArchRuntimeCalls() {
6238 return SideEffects::CanTriggerGC();
6239 }
6240
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006241 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006242 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006243 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006244 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006245
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006246 void MarkInBootImage() {
6247 SetPackedFlag<kFlagIsInBootImage>(true);
6248 }
6249
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006250 void AddSpecialInput(HInstruction* special_input);
6251
6252 using HInstruction::GetInputRecords; // Keep the const version visible.
6253 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
6254 return ArrayRef<HUserRecord<HInstruction*>>(
6255 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6256 }
6257
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006258 Handle<mirror::Class> GetClass() const {
6259 return klass_;
6260 }
6261
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006262 DECLARE_INSTRUCTION(LoadClass);
6263
Artem Serovcced8ba2017-07-19 18:18:09 +01006264 protected:
6265 DEFAULT_COPY_CONSTRUCTOR(LoadClass);
6266
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006267 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006268 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00006269 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006270 // Whether this instruction must generate the initialization check.
6271 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006272 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006273 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
6274 static constexpr size_t kFieldLoadKindSize =
6275 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
Vladimir Marko175e7862018-03-27 09:03:13 +00006276 static constexpr size_t kFlagValidLoadedClassRTI = kFieldLoadKind + kFieldLoadKindSize;
6277 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagValidLoadedClassRTI + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006278 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006279 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
6280
6281 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006282 return load_kind == LoadKind::kReferrersClass ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006283 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006284 load_kind == LoadKind::kBssEntry ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006285 load_kind == LoadKind::kRuntimeCall;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006286 }
6287
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006288 void SetLoadKindInternal(LoadKind load_kind);
6289
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006290 // The special input is the HCurrentMethod for kRuntimeCall or kReferrersClass.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006291 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006292 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006293 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006294
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006295 // A type index and dex file where the class can be accessed. The dex file can be:
6296 // - The compiling method's dex file if the class is defined there too.
6297 // - The compiling method's dex file if the class is referenced there.
6298 // - The dex file where the class is defined. When the load kind can only be
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006299 // kBssEntry or kRuntimeCall, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08006300 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006301 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006302
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006303 Handle<mirror::Class> klass_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006304};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006305std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
6306
6307// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006308inline void HLoadClass::SetLoadKind(LoadKind load_kind) {
6309 // The load kind should be determined before inserting the instruction to the graph.
6310 DCHECK(GetBlock() == nullptr);
6311 DCHECK(GetEnvironment() == nullptr);
6312 SetPackedField<LoadKindField>(load_kind);
6313 if (load_kind != LoadKind::kRuntimeCall && load_kind != LoadKind::kReferrersClass) {
6314 special_input_ = HUserRecord<HInstruction*>(nullptr);
6315 }
6316 if (!NeedsEnvironment()) {
6317 SetSideEffects(SideEffects::None());
6318 }
6319}
6320
6321// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006322inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006323 // The special input is used for PC-relative loads on some architectures,
6324 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006325 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006326 GetLoadKind() == LoadKind::kBootImageAddress ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006327 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006328 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006329 DCHECK(special_input_.GetInstruction() == nullptr);
6330 special_input_ = HUserRecord<HInstruction*>(special_input);
6331 special_input->AddUseAt(this, 0);
6332}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006333
Vladimir Marko372f10e2016-05-17 16:30:10 +01006334class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006335 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006336 // Determines how to load the String.
6337 enum class LoadKind {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006338 // Use PC-relative boot image String* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006339 // Used for boot image strings referenced by boot image code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006340 kBootImageLinkTimePcRelative,
6341
6342 // Use a known boot image String* address, embedded in the code by the codegen.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006343 // Used for boot image strings referenced by apps in JIT- and AOT-compiled code (non-PIC).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006344 kBootImageAddress,
6345
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006346 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
6347 // Used for boot image strings referenced by apps in AOT-compiled code (PIC).
6348 kBootImageRelRo,
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006349
Vladimir Markoaad75c62016-10-03 08:46:48 +00006350 // Load from an entry in the .bss section using a PC-relative load.
6351 // Used for strings outside boot image when .bss is accessible with a PC-relative load.
6352 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006353
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006354 // Load from the root table associated with the JIT compiled method.
6355 kJitTableAddress,
6356
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006357 // Load using a simple runtime call. This is the fall-back load kind when
6358 // the codegen is unable to use another appropriate kind.
6359 kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006360
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006361 kLast = kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006362 };
6363
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006364 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006365 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006366 const DexFile& dex_file,
6367 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006368 : HInstruction(kLoadString,
6369 DataType::Type::kReference,
6370 SideEffectsForArchRuntimeCalls(),
6371 dex_pc),
Vladimir Marko372f10e2016-05-17 16:30:10 +01006372 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006373 string_index_(string_index),
6374 dex_file_(dex_file) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006375 SetPackedField<LoadKindField>(LoadKind::kRuntimeCall);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006376 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006377
Artem Serovcced8ba2017-07-19 18:18:09 +01006378 bool IsClonable() const OVERRIDE { return true; }
6379
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006380 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006381
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006382 LoadKind GetLoadKind() const {
6383 return GetPackedField<LoadKindField>();
6384 }
6385
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006386 bool HasPcRelativeLoadKind() const {
6387 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6388 GetLoadKind() == LoadKind::kBootImageRelRo ||
6389 GetLoadKind() == LoadKind::kBssEntry;
6390 }
6391
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006392 const DexFile& GetDexFile() const {
6393 return dex_file_;
6394 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006395
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006396 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006397 return string_index_;
6398 }
6399
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006400 Handle<mirror::String> GetString() const {
6401 return string_;
6402 }
6403
6404 void SetString(Handle<mirror::String> str) {
6405 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006406 }
6407
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006408 bool CanBeMoved() const OVERRIDE { return true; }
6409
Vladimir Marko372f10e2016-05-17 16:30:10 +01006410 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006411
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006412 size_t ComputeHashCode() const OVERRIDE { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006413
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006414 // Will call the runtime if we need to load the string through
6415 // the dex cache and the string is not guaranteed to be there yet.
6416 bool NeedsEnvironment() const OVERRIDE {
6417 LoadKind load_kind = GetLoadKind();
Vladimir Marko764d4542017-05-16 10:31:41 +01006418 if (load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006419 load_kind == LoadKind::kBootImageAddress ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006420 load_kind == LoadKind::kBootImageRelRo ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006421 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006422 return false;
6423 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006424 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006425 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006426
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006427 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006428 return GetLoadKind() == LoadKind::kRuntimeCall;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006429 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006430
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07006431 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006432 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006433
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006434 static SideEffects SideEffectsForArchRuntimeCalls() {
6435 return SideEffects::CanTriggerGC();
6436 }
6437
Vladimir Marko372f10e2016-05-17 16:30:10 +01006438 void AddSpecialInput(HInstruction* special_input);
6439
6440 using HInstruction::GetInputRecords; // Keep the const version visible.
6441 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
6442 return ArrayRef<HUserRecord<HInstruction*>>(
6443 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006444 }
6445
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006446 DECLARE_INSTRUCTION(LoadString);
6447
Artem Serovcced8ba2017-07-19 18:18:09 +01006448 protected:
6449 DEFAULT_COPY_CONSTRUCTOR(LoadString);
6450
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006451 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006452 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006453 static constexpr size_t kFieldLoadKindSize =
6454 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
6455 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006456 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006457 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006458
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006459 void SetLoadKindInternal(LoadKind load_kind);
6460
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006461 // The special input is the HCurrentMethod for kRuntimeCall.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006462 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00006463 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01006464 HUserRecord<HInstruction*> special_input_;
6465
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006466 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006467 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006468
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006469 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006470};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006471std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
6472
6473// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006474inline void HLoadString::SetLoadKind(LoadKind load_kind) {
6475 // The load kind should be determined before inserting the instruction to the graph.
6476 DCHECK(GetBlock() == nullptr);
6477 DCHECK(GetEnvironment() == nullptr);
6478 DCHECK_EQ(GetLoadKind(), LoadKind::kRuntimeCall);
6479 SetPackedField<LoadKindField>(load_kind);
6480 if (load_kind != LoadKind::kRuntimeCall) {
6481 special_input_ = HUserRecord<HInstruction*>(nullptr);
6482 }
6483 if (!NeedsEnvironment()) {
6484 SetSideEffects(SideEffects::None());
6485 }
6486}
6487
6488// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006489inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006490 // The special input is used for PC-relative loads on some architectures,
6491 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006492 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006493 GetLoadKind() == LoadKind::kBootImageAddress ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006494 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006495 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01006496 // HLoadString::GetInputRecords() returns an empty array at this point,
6497 // so use the GetInputRecords() from the base class to set the input record.
6498 DCHECK(special_input_.GetInstruction() == nullptr);
6499 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006500 special_input->AddUseAt(this, 0);
6501}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006502
Orion Hodson18259d72018-04-12 11:18:23 +01006503class HLoadMethodType FINAL : public HInstruction {
6504 public:
6505 HLoadMethodType(HCurrentMethod* current_method,
6506 uint16_t proto_idx,
6507 const DexFile& dex_file,
6508 uint32_t dex_pc)
6509 : HInstruction(kLoadMethodType,
6510 DataType::Type::kReference,
6511 SideEffectsForArchRuntimeCalls(),
6512 dex_pc),
6513 special_input_(HUserRecord<HInstruction*>(current_method)),
6514 proto_idx_(proto_idx),
6515 dex_file_(dex_file) {
6516 }
6517
6518 using HInstruction::GetInputRecords; // Keep the const version visible.
6519 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
6520 return ArrayRef<HUserRecord<HInstruction*>>(
6521 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6522 }
6523
6524 bool IsClonable() const OVERRIDE { return true; }
6525
6526 uint16_t GetProtoIndex() const { return proto_idx_; }
6527
6528 const DexFile& GetDexFile() const { return dex_file_; }
6529
6530 static SideEffects SideEffectsForArchRuntimeCalls() {
6531 return SideEffects::CanTriggerGC();
6532 }
6533
6534 DECLARE_INSTRUCTION(LoadMethodType);
6535
6536 protected:
6537 DEFAULT_COPY_CONSTRUCTOR(LoadMethodType);
6538
6539 private:
6540 // The special input is the HCurrentMethod for kRuntimeCall.
6541 HUserRecord<HInstruction*> special_input_;
6542
6543 uint16_t proto_idx_;
6544 const DexFile& dex_file_;
6545};
6546
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006547/**
6548 * Performs an initialization check on its Class object input.
6549 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01006550class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006551 public:
Roland Levillain3887c462015-08-12 18:15:42 +01006552 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07006553 : HExpression(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306554 kClinitCheck,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006555 DataType::Type::kReference,
Mingyao Yang217eb062017-12-11 15:20:07 -08006556 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006557 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006558 SetRawInputAt(0, constant);
6559 }
6560
Artem Serovcced8ba2017-07-19 18:18:09 +01006561 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006562 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006563 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006564 return true;
6565 }
6566
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006567 bool NeedsEnvironment() const OVERRIDE {
6568 // May call runtime to initialize the class.
6569 return true;
6570 }
6571
Nicolas Geoffray729645a2015-11-19 13:29:02 +00006572 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006573
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006574 HLoadClass* GetLoadClass() const {
6575 DCHECK(InputAt(0)->IsLoadClass());
6576 return InputAt(0)->AsLoadClass();
6577 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006578
6579 DECLARE_INSTRUCTION(ClinitCheck);
6580
Artem Serovcced8ba2017-07-19 18:18:09 +01006581
6582 protected:
6583 DEFAULT_COPY_CONSTRUCTOR(ClinitCheck);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006584};
6585
Vladimir Markofcb503c2016-05-18 12:48:17 +01006586class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006587 public:
6588 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006589 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006590 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006591 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006592 bool is_volatile,
6593 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006594 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006595 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006596 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306597 : HExpression(kStaticFieldGet,
6598 field_type,
6599 SideEffects::FieldReadOfType(field_type, is_volatile),
6600 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006601 field_info_(field,
6602 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006603 field_type,
6604 is_volatile,
6605 field_idx,
6606 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006607 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006608 SetRawInputAt(0, cls);
6609 }
6610
Calin Juravle52c48962014-12-16 17:02:57 +00006611
Artem Serovcced8ba2017-07-19 18:18:09 +01006612 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle10c9cbe2014-12-19 10:50:19 +00006613 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006614
Vladimir Marko372f10e2016-05-17 16:30:10 +01006615 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
6616 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006617 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006618 }
6619
6620 size_t ComputeHashCode() const OVERRIDE {
6621 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6622 }
6623
Calin Juravle52c48962014-12-16 17:02:57 +00006624 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006625 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006626 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006627 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006628
Vladimir Marko61b92282017-10-11 13:23:17 +01006629 void SetType(DataType::Type new_type) {
6630 DCHECK(DataType::IsIntegralType(GetType()));
6631 DCHECK(DataType::IsIntegralType(new_type));
6632 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6633 SetPackedField<TypeField>(new_type);
6634 }
6635
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006636 DECLARE_INSTRUCTION(StaticFieldGet);
6637
Artem Serovcced8ba2017-07-19 18:18:09 +01006638 protected:
6639 DEFAULT_COPY_CONSTRUCTOR(StaticFieldGet);
6640
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006641 private:
6642 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006643};
6644
Vladimir Markobd785672018-05-03 17:09:09 +01006645class HStaticFieldSet FINAL : public HExpression<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006646 public:
6647 HStaticFieldSet(HInstruction* cls,
6648 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006649 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006650 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006651 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006652 bool is_volatile,
6653 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006654 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006655 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006656 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006657 : HExpression(kStaticFieldSet,
6658 SideEffects::FieldWriteOfType(field_type, is_volatile),
6659 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006660 field_info_(field,
6661 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006662 field_type,
6663 is_volatile,
6664 field_idx,
6665 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006666 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006667 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006668 SetRawInputAt(0, cls);
6669 SetRawInputAt(1, value);
6670 }
6671
Artem Serovcced8ba2017-07-19 18:18:09 +01006672 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle52c48962014-12-16 17:02:57 +00006673 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006674 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006675 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006676 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006677
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006678 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006679 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6680 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006681
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006682 DECLARE_INSTRUCTION(StaticFieldSet);
6683
Artem Serovcced8ba2017-07-19 18:18:09 +01006684 protected:
6685 DEFAULT_COPY_CONSTRUCTOR(StaticFieldSet);
6686
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006687 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006688 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6689 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6690 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6691 "Too many packed fields.");
6692
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006693 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006694};
6695
Vladimir Markofcb503c2016-05-18 12:48:17 +01006696class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006697 public:
6698 HUnresolvedInstanceFieldGet(HInstruction* obj,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006699 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006700 uint32_t field_index,
6701 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306702 : HExpression(kUnresolvedInstanceFieldGet,
6703 field_type,
6704 SideEffects::AllExceptGCDependency(),
6705 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006706 field_index_(field_index) {
6707 SetRawInputAt(0, obj);
6708 }
6709
Artem Serovcced8ba2017-07-19 18:18:09 +01006710 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006711 bool NeedsEnvironment() const OVERRIDE { return true; }
6712 bool CanThrow() const OVERRIDE { return true; }
6713
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006714 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006715 uint32_t GetFieldIndex() const { return field_index_; }
6716
6717 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6718
Artem Serovcced8ba2017-07-19 18:18:09 +01006719 protected:
6720 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldGet);
6721
Calin Juravlee460d1d2015-09-29 04:52:17 +01006722 private:
6723 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006724};
6725
Vladimir Markobd785672018-05-03 17:09:09 +01006726class HUnresolvedInstanceFieldSet FINAL : public HExpression<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006727 public:
6728 HUnresolvedInstanceFieldSet(HInstruction* obj,
6729 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006730 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006731 uint32_t field_index,
6732 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006733 : HExpression(kUnresolvedInstanceFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006734 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006735 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006736 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006737 SetRawInputAt(0, obj);
6738 SetRawInputAt(1, value);
6739 }
6740
Artem Serovcced8ba2017-07-19 18:18:09 +01006741 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006742 bool NeedsEnvironment() const OVERRIDE { return true; }
6743 bool CanThrow() const OVERRIDE { return true; }
6744
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006745 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006746 uint32_t GetFieldIndex() const { return field_index_; }
6747
6748 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6749
Artem Serovcced8ba2017-07-19 18:18:09 +01006750 protected:
6751 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldSet);
6752
Calin Juravlee460d1d2015-09-29 04:52:17 +01006753 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006754 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6755 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006756 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006757 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6758 kFieldFieldType + kFieldFieldTypeSize;
6759 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6760 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006761 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006762
Calin Juravlee460d1d2015-09-29 04:52:17 +01006763 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006764};
6765
Vladimir Markofcb503c2016-05-18 12:48:17 +01006766class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006767 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006768 HUnresolvedStaticFieldGet(DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006769 uint32_t field_index,
6770 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306771 : HExpression(kUnresolvedStaticFieldGet,
6772 field_type,
6773 SideEffects::AllExceptGCDependency(),
6774 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006775 field_index_(field_index) {
6776 }
6777
Artem Serovcced8ba2017-07-19 18:18:09 +01006778 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006779 bool NeedsEnvironment() const OVERRIDE { return true; }
6780 bool CanThrow() const OVERRIDE { return true; }
6781
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006782 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006783 uint32_t GetFieldIndex() const { return field_index_; }
6784
6785 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6786
Artem Serovcced8ba2017-07-19 18:18:09 +01006787 protected:
6788 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldGet);
6789
Calin Juravlee460d1d2015-09-29 04:52:17 +01006790 private:
6791 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006792};
6793
Vladimir Markobd785672018-05-03 17:09:09 +01006794class HUnresolvedStaticFieldSet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006795 public:
6796 HUnresolvedStaticFieldSet(HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006797 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006798 uint32_t field_index,
6799 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006800 : HExpression(kUnresolvedStaticFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006801 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006802 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006803 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006804 SetRawInputAt(0, value);
6805 }
6806
Artem Serovcced8ba2017-07-19 18:18:09 +01006807 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006808 bool NeedsEnvironment() const OVERRIDE { return true; }
6809 bool CanThrow() const OVERRIDE { return true; }
6810
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006811 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006812 uint32_t GetFieldIndex() const { return field_index_; }
6813
6814 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6815
Artem Serovcced8ba2017-07-19 18:18:09 +01006816 protected:
6817 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldSet);
6818
Calin Juravlee460d1d2015-09-29 04:52:17 +01006819 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006820 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6821 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006822 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006823 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6824 kFieldFieldType + kFieldFieldTypeSize;
6825 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6826 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006827 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006828
Calin Juravlee460d1d2015-09-29 04:52:17 +01006829 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006830};
6831
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006832// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006833class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006834 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006835 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306836 : HExpression(kLoadException, DataType::Type::kReference, SideEffects::None(), dex_pc) {
6837 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006838
David Brazdilbbd733e2015-08-18 17:48:17 +01006839 bool CanBeNull() const OVERRIDE { return false; }
6840
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006841 DECLARE_INSTRUCTION(LoadException);
6842
Artem Serovcced8ba2017-07-19 18:18:09 +01006843 protected:
6844 DEFAULT_COPY_CONSTRUCTOR(LoadException);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006845};
6846
David Brazdilcb1c0552015-08-04 16:22:25 +01006847// Implicit part of move-exception which clears thread-local exception storage.
6848// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markobd785672018-05-03 17:09:09 +01006849class HClearException FINAL : public HExpression<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006850 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006851 explicit HClearException(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01006852 : HExpression(kClearException, SideEffects::AllWrites(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306853 }
David Brazdilcb1c0552015-08-04 16:22:25 +01006854
6855 DECLARE_INSTRUCTION(ClearException);
6856
Artem Serovcced8ba2017-07-19 18:18:09 +01006857 protected:
6858 DEFAULT_COPY_CONSTRUCTOR(ClearException);
David Brazdilcb1c0552015-08-04 16:22:25 +01006859};
6860
Vladimir Markobd785672018-05-03 17:09:09 +01006861class HThrow FINAL : public HExpression<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006862 public:
6863 HThrow(HInstruction* exception, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006864 : HExpression(kThrow, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006865 SetRawInputAt(0, exception);
6866 }
6867
6868 bool IsControlFlow() const OVERRIDE { return true; }
6869
6870 bool NeedsEnvironment() const OVERRIDE { return true; }
6871
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006872 bool CanThrow() const OVERRIDE { return true; }
6873
Aart Bika8b8e9b2018-01-09 11:01:02 -08006874 bool AlwaysThrows() const OVERRIDE { return true; }
6875
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006876 DECLARE_INSTRUCTION(Throw);
6877
Artem Serovcced8ba2017-07-19 18:18:09 +01006878 protected:
6879 DEFAULT_COPY_CONSTRUCTOR(Throw);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006880};
6881
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006882/**
6883 * Implementation strategies for the code generator of a HInstanceOf
6884 * or `HCheckCast`.
6885 */
6886enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006887 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006888 kExactCheck, // Can do a single class compare.
6889 kClassHierarchyCheck, // Can just walk the super class chain.
6890 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6891 kInterfaceCheck, // No optimization yet when checking against an interface.
6892 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006893 kArrayCheck, // No optimization yet when checking against a generic array.
Vladimir Marko175e7862018-03-27 09:03:13 +00006894 kBitstringCheck, // Compare the type check bitstring.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006895 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006896};
6897
Roland Levillain86503782016-02-11 19:07:30 +00006898std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6899
Vladimir Marko175e7862018-03-27 09:03:13 +00006900// Note: HTypeCheckInstruction is just a helper class, not an abstract instruction with an
6901// `IsTypeCheckInstruction()`. (New virtual methods in the HInstruction class have a high cost.)
6902class HTypeCheckInstruction : public HVariableInputSizeInstruction {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006903 public:
Vladimir Marko175e7862018-03-27 09:03:13 +00006904 HTypeCheckInstruction(InstructionKind kind,
Vladimir Markobd785672018-05-03 17:09:09 +01006905 DataType::Type type,
Vladimir Marko175e7862018-03-27 09:03:13 +00006906 HInstruction* object,
6907 HInstruction* target_class_or_null,
6908 TypeCheckKind check_kind,
6909 Handle<mirror::Class> klass,
6910 uint32_t dex_pc,
6911 ArenaAllocator* allocator,
6912 HIntConstant* bitstring_path_to_root,
6913 HIntConstant* bitstring_mask,
6914 SideEffects side_effects)
6915 : HVariableInputSizeInstruction(
6916 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01006917 type,
Vladimir Marko175e7862018-03-27 09:03:13 +00006918 side_effects,
6919 dex_pc,
6920 allocator,
6921 /* number_of_inputs */ check_kind == TypeCheckKind::kBitstringCheck ? 4u : 2u,
6922 kArenaAllocTypeCheckInputs),
6923 klass_(klass) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006924 SetPackedField<TypeCheckKindField>(check_kind);
6925 SetPackedFlag<kFlagMustDoNullCheck>(true);
Vladimir Marko175e7862018-03-27 09:03:13 +00006926 SetPackedFlag<kFlagValidTargetClassRTI>(false);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006927 SetRawInputAt(0, object);
Vladimir Marko175e7862018-03-27 09:03:13 +00006928 SetRawInputAt(1, target_class_or_null);
6929 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_path_to_root != nullptr);
6930 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_mask != nullptr);
6931 if (check_kind == TypeCheckKind::kBitstringCheck) {
6932 DCHECK(target_class_or_null->IsNullConstant());
6933 SetRawInputAt(2, bitstring_path_to_root);
6934 SetRawInputAt(3, bitstring_mask);
6935 } else {
6936 DCHECK(target_class_or_null->IsLoadClass());
6937 }
Vladimir Marko87584542017-12-12 17:47:52 +00006938 }
6939
6940 HLoadClass* GetTargetClass() const {
Vladimir Marko175e7862018-03-27 09:03:13 +00006941 DCHECK_NE(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
Vladimir Marko87584542017-12-12 17:47:52 +00006942 HInstruction* load_class = InputAt(1);
6943 DCHECK(load_class->IsLoadClass());
6944 return load_class->AsLoadClass();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006945 }
6946
Vladimir Marko175e7862018-03-27 09:03:13 +00006947 uint32_t GetBitstringPathToRoot() const {
6948 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
6949 HInstruction* path_to_root = InputAt(2);
6950 DCHECK(path_to_root->IsIntConstant());
6951 return static_cast<uint32_t>(path_to_root->AsIntConstant()->GetValue());
6952 }
6953
6954 uint32_t GetBitstringMask() const {
6955 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
6956 HInstruction* mask = InputAt(3);
6957 DCHECK(mask->IsIntConstant());
6958 return static_cast<uint32_t>(mask->AsIntConstant()->GetValue());
6959 }
6960
Artem Serovcced8ba2017-07-19 18:18:09 +01006961 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006962 bool CanBeMoved() const OVERRIDE { return true; }
6963
Vladimir Marko175e7862018-03-27 09:03:13 +00006964 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
6965 DCHECK(other->IsInstanceOf() || other->IsCheckCast()) << other->DebugName();
6966 return GetPackedFields() == down_cast<const HTypeCheckInstruction*>(other)->GetPackedFields();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006967 }
6968
Andreas Gampe3fbd3ad2018-03-26 21:14:46 +00006969 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6970 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6971 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6972 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
6973
Vladimir Marko175e7862018-03-27 09:03:13 +00006974 ReferenceTypeInfo GetTargetClassRTI() {
6975 if (GetPackedFlag<kFlagValidTargetClassRTI>()) {
6976 // Note: The is_exact flag from the return value should not be used.
6977 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact */ true);
6978 } else {
6979 return ReferenceTypeInfo::CreateInvalid();
6980 }
6981 }
6982
6983 // Target class RTI is marked as valid by RTP if the klass_ is admissible.
6984 void SetValidTargetClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
6985 DCHECK(klass_ != nullptr);
6986 SetPackedFlag<kFlagValidTargetClassRTI>(true);
6987 }
6988
6989 Handle<mirror::Class> GetClass() const {
6990 return klass_;
6991 }
6992
6993 protected:
6994 DEFAULT_COPY_CONSTRUCTOR(TypeCheckInstruction);
6995
6996 private:
6997 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6998 static constexpr size_t kFieldTypeCheckKindSize =
6999 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
7000 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
7001 static constexpr size_t kFlagValidTargetClassRTI = kFlagMustDoNullCheck + 1;
7002 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagValidTargetClassRTI + 1;
7003 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7004 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
7005
7006 Handle<mirror::Class> klass_;
7007};
7008
7009class HInstanceOf FINAL : public HTypeCheckInstruction {
7010 public:
7011 HInstanceOf(HInstruction* object,
7012 HInstruction* target_class_or_null,
7013 TypeCheckKind check_kind,
7014 Handle<mirror::Class> klass,
7015 uint32_t dex_pc,
7016 ArenaAllocator* allocator,
7017 HIntConstant* bitstring_path_to_root,
7018 HIntConstant* bitstring_mask)
7019 : HTypeCheckInstruction(kInstanceOf,
Vladimir Markobd785672018-05-03 17:09:09 +01007020 DataType::Type::kBool,
Vladimir Marko175e7862018-03-27 09:03:13 +00007021 object,
7022 target_class_or_null,
7023 check_kind,
7024 klass,
7025 dex_pc,
7026 allocator,
7027 bitstring_path_to_root,
7028 bitstring_mask,
7029 SideEffectsForArchRuntimeCalls(check_kind)) {}
7030
Vladimir Marko175e7862018-03-27 09:03:13 +00007031 bool NeedsEnvironment() const OVERRIDE {
7032 return CanCallRuntime(GetTypeCheckKind());
7033 }
7034
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007035 static bool CanCallRuntime(TypeCheckKind check_kind) {
7036 // Mips currently does runtime calls for any other checks.
7037 return check_kind != TypeCheckKind::kExactCheck;
7038 }
7039
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007040 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007041 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01007042 }
7043
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007044 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007045
Artem Serovcced8ba2017-07-19 18:18:09 +01007046 protected:
7047 DEFAULT_COPY_CONSTRUCTOR(InstanceOf);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007048};
7049
Vladimir Markofcb503c2016-05-18 12:48:17 +01007050class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00007051 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07007052 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307053 : HExpression(kBoundType, DataType::Type::kReference, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00007054 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
7055 SetPackedFlag<kFlagUpperCanBeNull>(true);
7056 SetPackedFlag<kFlagCanBeNull>(true);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007057 DCHECK_EQ(input->GetType(), DataType::Type::kReference);
Calin Juravleb1498f62015-02-16 13:13:29 +00007058 SetRawInputAt(0, input);
7059 }
7060
Artem Serovcced8ba2017-07-19 18:18:09 +01007061 bool IsClonable() const OVERRIDE { return true; }
7062
David Brazdilf5552582015-12-27 13:36:12 +00007063 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007064 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00007065 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00007066 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007067
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007068 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007069 DCHECK(GetUpperCanBeNull() || !can_be_null);
7070 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007071 }
7072
Vladimir Markoa1de9182016-02-25 11:37:38 +00007073 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007074
Calin Juravleb1498f62015-02-16 13:13:29 +00007075 DECLARE_INSTRUCTION(BoundType);
7076
Artem Serovcced8ba2017-07-19 18:18:09 +01007077 protected:
7078 DEFAULT_COPY_CONSTRUCTOR(BoundType);
7079
Calin Juravleb1498f62015-02-16 13:13:29 +00007080 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007081 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
7082 // is false then CanBeNull() cannot be true).
Vladimir Markobd785672018-05-03 17:09:09 +01007083 static constexpr size_t kFlagUpperCanBeNull = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007084 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
7085 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
7086 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7087
Calin Juravleb1498f62015-02-16 13:13:29 +00007088 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007089 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
7090 // It is used to bound the type in cases like:
7091 // if (x instanceof ClassX) {
7092 // // uper_bound_ will be ClassX
7093 // }
David Brazdilf5552582015-12-27 13:36:12 +00007094 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00007095};
7096
Vladimir Marko175e7862018-03-27 09:03:13 +00007097class HCheckCast FINAL : public HTypeCheckInstruction {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007098 public:
7099 HCheckCast(HInstruction* object,
Vladimir Marko175e7862018-03-27 09:03:13 +00007100 HInstruction* target_class_or_null,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007101 TypeCheckKind check_kind,
Vladimir Marko175e7862018-03-27 09:03:13 +00007102 Handle<mirror::Class> klass,
7103 uint32_t dex_pc,
7104 ArenaAllocator* allocator,
7105 HIntConstant* bitstring_path_to_root,
7106 HIntConstant* bitstring_mask)
7107 : HTypeCheckInstruction(kCheckCast,
Vladimir Markobd785672018-05-03 17:09:09 +01007108 DataType::Type::kVoid,
Vladimir Marko175e7862018-03-27 09:03:13 +00007109 object,
7110 target_class_or_null,
7111 check_kind,
7112 klass,
7113 dex_pc,
7114 allocator,
7115 bitstring_path_to_root,
7116 bitstring_mask,
7117 SideEffects::CanTriggerGC()) {}
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007118
7119 bool NeedsEnvironment() const OVERRIDE {
7120 // Instruction may throw a CheckCastError.
7121 return true;
7122 }
7123
7124 bool CanThrow() const OVERRIDE { return true; }
7125
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007126 DECLARE_INSTRUCTION(CheckCast);
7127
Artem Serovcced8ba2017-07-19 18:18:09 +01007128 protected:
7129 DEFAULT_COPY_CONSTRUCTOR(CheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007130};
7131
Andreas Gampe26de38b2016-07-27 17:53:11 -07007132/**
7133 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
7134 * @details We define the combined barrier types that are actually required
7135 * by the Java Memory Model, rather than using exactly the terminology from
7136 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
7137 * primitives. Note that the JSR-133 cookbook generally does not deal with
7138 * store atomicity issues, and the recipes there are not always entirely sufficient.
7139 * The current recipe is as follows:
7140 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
7141 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
7142 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
7143 * -# Use StoreStore barrier after all stores but before return from any constructor whose
7144 * class has final fields.
7145 * -# Use NTStoreStore to order non-temporal stores with respect to all later
7146 * store-to-memory instructions. Only generated together with non-temporal stores.
7147 */
7148enum MemBarrierKind {
7149 kAnyStore,
7150 kLoadAny,
7151 kStoreStore,
7152 kAnyAny,
7153 kNTStoreStore,
7154 kLastBarrierKind = kNTStoreStore
7155};
7156std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
7157
Vladimir Markobd785672018-05-03 17:09:09 +01007158class HMemoryBarrier FINAL : public HExpression<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01007159 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007160 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007161 : HExpression(kMemoryBarrier,
7162 SideEffects::AllWritesAndReads(), // Assume write/read on all fields/arrays.
7163 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007164 SetPackedField<BarrierKindField>(barrier_kind);
7165 }
Calin Juravle27df7582015-04-17 19:12:31 +01007166
Artem Serovcced8ba2017-07-19 18:18:09 +01007167 bool IsClonable() const OVERRIDE { return true; }
7168
Vladimir Markoa1de9182016-02-25 11:37:38 +00007169 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01007170
7171 DECLARE_INSTRUCTION(MemoryBarrier);
7172
Artem Serovcced8ba2017-07-19 18:18:09 +01007173 protected:
7174 DEFAULT_COPY_CONSTRUCTOR(MemoryBarrier);
7175
Calin Juravle27df7582015-04-17 19:12:31 +01007176 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007177 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
7178 static constexpr size_t kFieldBarrierKindSize =
7179 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
7180 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
7181 kFieldBarrierKind + kFieldBarrierKindSize;
7182 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
7183 "Too many packed fields.");
7184 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01007185};
7186
Igor Murashkind01745e2017-04-05 16:40:31 -07007187// A constructor fence orders all prior stores to fields that could be accessed via a final field of
7188// the specified object(s), with respect to any subsequent store that might "publish"
7189// (i.e. make visible) the specified object to another thread.
7190//
7191// JLS 17.5.1 "Semantics of final fields" states that a freeze action happens
7192// for all final fields (that were set) at the end of the invoked constructor.
7193//
7194// The constructor fence models the freeze actions for the final fields of an object
7195// being constructed (semantically at the end of the constructor). Constructor fences
7196// have a per-object affinity; two separate objects being constructed get two separate
7197// constructor fences.
7198//
7199// (Note: that if calling a super-constructor or forwarding to another constructor,
7200// the freezes would happen at the end of *that* constructor being invoked).
7201//
7202// The memory model guarantees that when the object being constructed is "published" after
7203// constructor completion (i.e. escapes the current thread via a store), then any final field
7204// writes must be observable on other threads (once they observe that publication).
7205//
7206// Further, anything written before the freeze, and read by dereferencing through the final field,
7207// must also be visible (so final object field could itself have an object with non-final fields;
7208// yet the freeze must also extend to them).
7209//
7210// Constructor example:
7211//
7212// class HasFinal {
7213// final int field; Optimizing IR for <init>()V:
7214// HasFinal() {
7215// field = 123; HInstanceFieldSet(this, HasFinal.field, 123)
7216// // freeze(this.field); HConstructorFence(this)
7217// } HReturn
7218// }
7219//
7220// HConstructorFence can serve double duty as a fence for new-instance/new-array allocations of
7221// already-initialized classes; in that case the allocation must act as a "default-initializer"
7222// of the object which effectively writes the class pointer "final field".
7223//
7224// For example, we can model default-initialiation as roughly the equivalent of the following:
7225//
7226// class Object {
7227// private final Class header;
7228// }
7229//
7230// Java code: Optimizing IR:
7231//
7232// T new_instance<T>() {
7233// Object obj = allocate_memory(T.class.size); obj = HInvoke(art_quick_alloc_object, T)
7234// obj.header = T.class; // header write is done by above call.
7235// // freeze(obj.header) HConstructorFence(obj)
7236// return (T)obj;
7237// }
7238//
7239// See also:
7240// * CompilerDriver::RequiresConstructorBarrier
7241// * QuasiAtomic::ThreadFenceForConstructor
7242//
7243class HConstructorFence FINAL : public HVariableInputSizeInstruction {
7244 // A fence has variable inputs because the inputs can be removed
7245 // after prepare_for_register_allocation phase.
7246 // (TODO: In the future a fence could freeze multiple objects
7247 // after merging two fences together.)
7248 public:
7249 // `fence_object` is the reference that needs to be protected for correct publication.
7250 //
7251 // It makes sense in the following situations:
7252 // * <init> constructors, it's the "this" parameter (i.e. HParameterValue, s.t. IsThis() == true).
7253 // * new-instance-like instructions, it's the return value (i.e. HNewInstance).
7254 //
7255 // After construction the `fence_object` becomes the 0th input.
7256 // This is not an input in a real sense, but just a convenient place to stash the information
7257 // about the associated object.
7258 HConstructorFence(HInstruction* fence_object,
7259 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007260 ArenaAllocator* allocator)
Igor Murashkind01745e2017-04-05 16:40:31 -07007261 // We strongly suspect there is not a more accurate way to describe the fine-grained reordering
7262 // constraints described in the class header. We claim that these SideEffects constraints
7263 // enforce a superset of the real constraints.
7264 //
7265 // The ordering described above is conservatively modeled with SideEffects as follows:
7266 //
7267 // * To prevent reordering of the publication stores:
7268 // ----> "Reads of objects" is the initial SideEffect.
7269 // * For every primitive final field store in the constructor:
7270 // ----> Union that field's type as a read (e.g. "Read of T") into the SideEffect.
7271 // * If there are any stores to reference final fields in the constructor:
7272 // ----> Use a more conservative "AllReads" SideEffect because any stores to any references
7273 // that are reachable from `fence_object` also need to be prevented for reordering
7274 // (and we do not want to do alias analysis to figure out what those stores are).
7275 //
7276 // In the implementation, this initially starts out as an "all reads" side effect; this is an
7277 // even more conservative approach than the one described above, and prevents all of the
7278 // above reordering without analyzing any of the instructions in the constructor.
7279 //
7280 // If in a later phase we discover that there are no writes to reference final fields,
7281 // we can refine the side effect to a smaller set of type reads (see above constraints).
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307282 : HVariableInputSizeInstruction(kConstructorFence,
7283 SideEffects::AllReads(),
Igor Murashkind01745e2017-04-05 16:40:31 -07007284 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007285 allocator,
Igor Murashkind01745e2017-04-05 16:40:31 -07007286 /* number_of_inputs */ 1,
7287 kArenaAllocConstructorFenceInputs) {
7288 DCHECK(fence_object != nullptr);
7289 SetRawInputAt(0, fence_object);
7290 }
7291
7292 // The object associated with this constructor fence.
7293 //
7294 // (Note: This will be null after the prepare_for_register_allocation phase,
7295 // as all constructor fence inputs are removed there).
7296 HInstruction* GetFenceObject() const {
7297 return InputAt(0);
7298 }
7299
7300 // Find all the HConstructorFence uses (`fence_use`) for `this` and:
7301 // - Delete `fence_use` from `this`'s use list.
7302 // - Delete `this` from `fence_use`'s inputs list.
7303 // - If the `fence_use` is dead, remove it from the graph.
7304 //
7305 // A fence is considered dead once it no longer has any uses
7306 // and all of the inputs are dead.
7307 //
7308 // This must *not* be called during/after prepare_for_register_allocation,
7309 // because that removes all the inputs to the fences but the fence is actually
7310 // still considered live.
Igor Murashkin6ef45672017-08-08 13:59:55 -07007311 //
7312 // Returns how many HConstructorFence instructions were removed from graph.
7313 static size_t RemoveConstructorFences(HInstruction* instruction);
Igor Murashkind01745e2017-04-05 16:40:31 -07007314
Igor Murashkindd018df2017-08-09 10:38:31 -07007315 // Combine all inputs of `this` and `other` instruction and remove
7316 // `other` from the graph.
7317 //
7318 // Inputs are unique after the merge.
7319 //
7320 // Requirement: `this` must not be the same as `other.
7321 void Merge(HConstructorFence* other);
7322
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007323 // Check if this constructor fence is protecting
7324 // an HNewInstance or HNewArray that is also the immediate
7325 // predecessor of `this`.
7326 //
Igor Murashkindd018df2017-08-09 10:38:31 -07007327 // If `ignore_inputs` is true, then the immediate predecessor doesn't need
7328 // to be one of the inputs of `this`.
7329 //
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007330 // Returns the associated HNewArray or HNewInstance,
7331 // or null otherwise.
Igor Murashkindd018df2017-08-09 10:38:31 -07007332 HInstruction* GetAssociatedAllocation(bool ignore_inputs = false);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007333
Igor Murashkind01745e2017-04-05 16:40:31 -07007334 DECLARE_INSTRUCTION(ConstructorFence);
7335
Artem Serovcced8ba2017-07-19 18:18:09 +01007336 protected:
7337 DEFAULT_COPY_CONSTRUCTOR(ConstructorFence);
Igor Murashkind01745e2017-04-05 16:40:31 -07007338};
7339
Vladimir Markobd785672018-05-03 17:09:09 +01007340class HMonitorOperation FINAL : public HExpression<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007341 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007342 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007343 kEnter,
7344 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00007345 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007346 };
7347
7348 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007349 : HExpression(kMonitorOperation,
7350 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
7351 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007352 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007353 SetRawInputAt(0, object);
7354 }
7355
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007356 // Instruction may go into runtime, so we need an environment.
7357 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00007358
7359 bool CanThrow() const OVERRIDE {
7360 // Verifier guarantees that monitor-exit cannot throw.
7361 // This is important because it allows the HGraphBuilder to remove
7362 // a dead throw-catch loop generated for `synchronized` blocks/methods.
7363 return IsEnter();
7364 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007365
Vladimir Markoa1de9182016-02-25 11:37:38 +00007366 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
7367 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007368
7369 DECLARE_INSTRUCTION(MonitorOperation);
7370
Artem Serovcced8ba2017-07-19 18:18:09 +01007371 protected:
7372 DEFAULT_COPY_CONSTRUCTOR(MonitorOperation);
7373
Calin Juravle52c48962014-12-16 17:02:57 +00007374 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007375 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
7376 static constexpr size_t kFieldOperationKindSize =
7377 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
7378 static constexpr size_t kNumberOfMonitorOperationPackedBits =
7379 kFieldOperationKind + kFieldOperationKindSize;
7380 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7381 "Too many packed fields.");
7382 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007383};
7384
Vladimir Markofcb503c2016-05-18 12:48:17 +01007385class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00007386 public:
7387 HSelect(HInstruction* condition,
7388 HInstruction* true_value,
7389 HInstruction* false_value,
7390 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307391 : HExpression(kSelect, HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
David Brazdil74eb1b22015-12-14 11:44:01 +00007392 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
7393
7394 // First input must be `true_value` or `false_value` to allow codegens to
7395 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
7396 // that architectures which implement HSelect as a conditional move also
7397 // will not need to invert the condition.
7398 SetRawInputAt(0, false_value);
7399 SetRawInputAt(1, true_value);
7400 SetRawInputAt(2, condition);
7401 }
7402
Artem Serovcced8ba2017-07-19 18:18:09 +01007403 bool IsClonable() const OVERRIDE { return true; }
David Brazdil74eb1b22015-12-14 11:44:01 +00007404 HInstruction* GetFalseValue() const { return InputAt(0); }
7405 HInstruction* GetTrueValue() const { return InputAt(1); }
7406 HInstruction* GetCondition() const { return InputAt(2); }
7407
7408 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01007409 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
7410 return true;
7411 }
David Brazdil74eb1b22015-12-14 11:44:01 +00007412
7413 bool CanBeNull() const OVERRIDE {
7414 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
7415 }
7416
7417 DECLARE_INSTRUCTION(Select);
7418
Artem Serovcced8ba2017-07-19 18:18:09 +01007419 protected:
7420 DEFAULT_COPY_CONSTRUCTOR(Select);
David Brazdil74eb1b22015-12-14 11:44:01 +00007421};
7422
Vladimir Markof9f64412015-09-02 14:05:49 +01007423class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007424 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01007425 MoveOperands(Location source,
7426 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007427 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007428 HInstruction* instruction)
7429 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007430
7431 Location GetSource() const { return source_; }
7432 Location GetDestination() const { return destination_; }
7433
7434 void SetSource(Location value) { source_ = value; }
7435 void SetDestination(Location value) { destination_ = value; }
7436
7437 // The parallel move resolver marks moves as "in-progress" by clearing the
7438 // destination (but not the source).
7439 Location MarkPending() {
7440 DCHECK(!IsPending());
7441 Location dest = destination_;
7442 destination_ = Location::NoLocation();
7443 return dest;
7444 }
7445
7446 void ClearPending(Location dest) {
7447 DCHECK(IsPending());
7448 destination_ = dest;
7449 }
7450
7451 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00007452 DCHECK(source_.IsValid() || destination_.IsInvalid());
7453 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007454 }
7455
7456 // True if this blocks a move from the given location.
7457 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08007458 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007459 }
7460
7461 // A move is redundant if it's been eliminated, if its source and
7462 // destination are the same, or if its destination is unneeded.
7463 bool IsRedundant() const {
7464 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
7465 }
7466
7467 // We clear both operands to indicate move that's been eliminated.
7468 void Eliminate() {
7469 source_ = destination_ = Location::NoLocation();
7470 }
7471
7472 bool IsEliminated() const {
7473 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
7474 return source_.IsInvalid();
7475 }
7476
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007477 DataType::Type GetType() const { return type_; }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007478
Nicolas Geoffray90218252015-04-15 11:56:51 +01007479 bool Is64BitMove() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007480 return DataType::Is64BitType(type_);
Nicolas Geoffray90218252015-04-15 11:56:51 +01007481 }
7482
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007483 HInstruction* GetInstruction() const { return instruction_; }
7484
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007485 private:
7486 Location source_;
7487 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01007488 // The type this move is for.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007489 DataType::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007490 // The instruction this move is assocatied with. Null when this move is
7491 // for moving an input in the expected locations of user (including a phi user).
7492 // This is only used in debug mode, to ensure we do not connect interval siblings
7493 // in the same parallel move.
7494 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007495};
7496
Roland Levillainc9285912015-12-18 10:38:42 +00007497std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
7498
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007499static constexpr size_t kDefaultNumberOfMoves = 4;
7500
Vladimir Markobd785672018-05-03 17:09:09 +01007501class HParallelMove FINAL : public HExpression<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007502 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007503 explicit HParallelMove(ArenaAllocator* allocator, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007504 : HExpression(kParallelMove, SideEffects::None(), dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007505 moves_(allocator->Adapter(kArenaAllocMoveOperands)) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007506 moves_.reserve(kDefaultNumberOfMoves);
7507 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007508
Nicolas Geoffray90218252015-04-15 11:56:51 +01007509 void AddMove(Location source,
7510 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007511 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007512 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00007513 DCHECK(source.IsValid());
7514 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007515 if (kIsDebugBuild) {
7516 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007517 for (const MoveOperands& move : moves_) {
7518 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007519 // Special case the situation where the move is for the spill slot
7520 // of the instruction.
7521 if ((GetPrevious() == instruction)
7522 || ((GetPrevious() == nullptr)
7523 && instruction->IsPhi()
7524 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007525 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007526 << "Doing parallel moves for the same instruction.";
7527 } else {
7528 DCHECK(false) << "Doing parallel moves for the same instruction.";
7529 }
7530 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00007531 }
7532 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007533 for (const MoveOperands& move : moves_) {
7534 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007535 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01007536 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007537 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007538 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007539 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007540 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007541 }
7542
Vladimir Marko225b6462015-09-28 12:17:40 +01007543 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007544 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007545 }
7546
Vladimir Marko225b6462015-09-28 12:17:40 +01007547 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007548
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01007549 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007550
Artem Serovcced8ba2017-07-19 18:18:09 +01007551 protected:
7552 DEFAULT_COPY_CONSTRUCTOR(ParallelMove);
7553
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007554 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01007555 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007556};
7557
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007558// This instruction computes an intermediate address pointing in the 'middle' of an object. The
7559// result pointer cannot be handled by GC, so extra care is taken to make sure that this value is
7560// never used across anything that can trigger GC.
7561// The result of this instruction is not a pointer in the sense of `DataType::Type::kreference`.
7562// So we represent it by the type `DataType::Type::kInt`.
7563class HIntermediateAddress FINAL : public HExpression<2> {
7564 public:
7565 HIntermediateAddress(HInstruction* base_address, HInstruction* offset, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307566 : HExpression(kIntermediateAddress,
7567 DataType::Type::kInt32,
7568 SideEffects::DependsOnGC(),
7569 dex_pc) {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007570 DCHECK_EQ(DataType::Size(DataType::Type::kInt32),
7571 DataType::Size(DataType::Type::kReference))
7572 << "kPrimInt and kPrimNot have different sizes.";
7573 SetRawInputAt(0, base_address);
7574 SetRawInputAt(1, offset);
7575 }
7576
Artem Serovcced8ba2017-07-19 18:18:09 +01007577 bool IsClonable() const OVERRIDE { return true; }
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007578 bool CanBeMoved() const OVERRIDE { return true; }
7579 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
7580 return true;
7581 }
7582 bool IsActualObject() const OVERRIDE { return false; }
7583
7584 HInstruction* GetBaseAddress() const { return InputAt(0); }
7585 HInstruction* GetOffset() const { return InputAt(1); }
7586
7587 DECLARE_INSTRUCTION(IntermediateAddress);
7588
Artem Serovcced8ba2017-07-19 18:18:09 +01007589 protected:
7590 DEFAULT_COPY_CONSTRUCTOR(IntermediateAddress);
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007591};
7592
7593
Mark Mendell0616ae02015-04-17 12:49:27 -04007594} // namespace art
7595
Aart Bikf8f5a162017-02-06 15:35:29 -08007596#include "nodes_vector.h"
7597
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007598#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
7599#include "nodes_shared.h"
7600#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007601#ifdef ART_ENABLE_CODEGEN_mips
7602#include "nodes_mips.h"
7603#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04007604#ifdef ART_ENABLE_CODEGEN_x86
7605#include "nodes_x86.h"
7606#endif
7607
7608namespace art {
7609
Igor Murashkin6ef45672017-08-08 13:59:55 -07007610class OptimizingCompilerStats;
7611
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007612class HGraphVisitor : public ValueObject {
7613 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007614 explicit HGraphVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7615 : stats_(stats),
7616 graph_(graph) {}
Dave Allison20dfc792014-06-16 20:44:29 -07007617 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007618
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007619 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007620 virtual void VisitBasicBlock(HBasicBlock* block);
7621
Roland Levillain633021e2014-10-01 14:12:25 +01007622 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007623 void VisitInsertionOrder();
7624
Roland Levillain633021e2014-10-01 14:12:25 +01007625 // Visit the graph following dominator tree reverse post-order.
7626 void VisitReversePostOrder();
7627
Nicolas Geoffray787c3072014-03-17 10:20:19 +00007628 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007629
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007630 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007631#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007632 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
7633
7634 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7635
7636#undef DECLARE_VISIT_INSTRUCTION
7637
Igor Murashkin6ef45672017-08-08 13:59:55 -07007638 protected:
7639 OptimizingCompilerStats* stats_;
7640
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007641 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07007642 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007643
7644 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
7645};
7646
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007647class HGraphDelegateVisitor : public HGraphVisitor {
7648 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007649 explicit HGraphDelegateVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7650 : HGraphVisitor(graph, stats) {}
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007651 virtual ~HGraphDelegateVisitor() {}
7652
7653 // Visit functions that delegate to to super class.
7654#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00007655 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007656
7657 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7658
7659#undef DECLARE_VISIT_INSTRUCTION
7660
7661 private:
7662 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
7663};
7664
Artem Serovcced8ba2017-07-19 18:18:09 +01007665// Create a clone of the instruction, insert it into the graph; replace the old one with a new
7666// and remove the old instruction.
7667HInstruction* ReplaceInstrOrPhiByClone(HInstruction* instr);
7668
7669// Create a clone for each clonable instructions/phis and replace the original with the clone.
7670//
7671// Used for testing individual instruction cloner.
7672class CloneAndReplaceInstructionVisitor : public HGraphDelegateVisitor {
7673 public:
7674 explicit CloneAndReplaceInstructionVisitor(HGraph* graph)
Artem Serov7f4aff62017-06-21 17:02:18 +01007675 : HGraphDelegateVisitor(graph), instr_replaced_by_clones_count_(0) {}
Artem Serovcced8ba2017-07-19 18:18:09 +01007676
7677 void VisitInstruction(HInstruction* instruction) OVERRIDE {
7678 if (instruction->IsClonable()) {
7679 ReplaceInstrOrPhiByClone(instruction);
Artem Serov7f4aff62017-06-21 17:02:18 +01007680 instr_replaced_by_clones_count_++;
Artem Serovcced8ba2017-07-19 18:18:09 +01007681 }
7682 }
7683
Artem Serov7f4aff62017-06-21 17:02:18 +01007684 size_t GetInstrReplacedByClonesCount() const { return instr_replaced_by_clones_count_; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007685
7686 private:
Artem Serov7f4aff62017-06-21 17:02:18 +01007687 size_t instr_replaced_by_clones_count_;
Artem Serovcced8ba2017-07-19 18:18:09 +01007688
7689 DISALLOW_COPY_AND_ASSIGN(CloneAndReplaceInstructionVisitor);
7690};
7691
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007692// Iterator over the blocks that art part of the loop. Includes blocks part
7693// of an inner loop. The order in which the blocks are iterated is on their
7694// block id.
7695class HBlocksInLoopIterator : public ValueObject {
7696 public:
7697 explicit HBlocksInLoopIterator(const HLoopInformation& info)
7698 : blocks_in_loop_(info.GetBlocks()),
7699 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
7700 index_(0) {
7701 if (!blocks_in_loop_.IsBitSet(index_)) {
7702 Advance();
7703 }
7704 }
7705
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007706 bool Done() const { return index_ == blocks_.size(); }
7707 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007708 void Advance() {
7709 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007710 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007711 if (blocks_in_loop_.IsBitSet(index_)) {
7712 break;
7713 }
7714 }
7715 }
7716
7717 private:
7718 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007719 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007720 size_t index_;
7721
7722 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
7723};
7724
Mingyao Yang3584bce2015-05-19 16:01:59 -07007725// Iterator over the blocks that art part of the loop. Includes blocks part
7726// of an inner loop. The order in which the blocks are iterated is reverse
7727// post order.
7728class HBlocksInLoopReversePostOrderIterator : public ValueObject {
7729 public:
7730 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
7731 : blocks_in_loop_(info.GetBlocks()),
7732 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
7733 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007734 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007735 Advance();
7736 }
7737 }
7738
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007739 bool Done() const { return index_ == blocks_.size(); }
7740 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07007741 void Advance() {
7742 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007743 for (size_t e = blocks_.size(); index_ < e; ++index_) {
7744 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007745 break;
7746 }
7747 }
7748 }
7749
7750 private:
7751 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007752 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07007753 size_t index_;
7754
7755 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
7756};
7757
Aart Bikf3e61ee2017-04-12 17:09:20 -07007758// Returns int64_t value of a properly typed constant.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007759inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00007760 if (constant->IsIntConstant()) {
7761 return constant->AsIntConstant()->GetValue();
7762 } else if (constant->IsLongConstant()) {
7763 return constant->AsLongConstant()->GetValue();
7764 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00007765 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00007766 return 0;
7767 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007768}
7769
Aart Bikf3e61ee2017-04-12 17:09:20 -07007770// Returns true iff instruction is an integral constant (and sets value on success).
7771inline bool IsInt64AndGet(HInstruction* instruction, /*out*/ int64_t* value) {
7772 if (instruction->IsIntConstant()) {
7773 *value = instruction->AsIntConstant()->GetValue();
7774 return true;
7775 } else if (instruction->IsLongConstant()) {
7776 *value = instruction->AsLongConstant()->GetValue();
7777 return true;
7778 } else if (instruction->IsNullConstant()) {
7779 *value = 0;
7780 return true;
7781 }
7782 return false;
7783}
7784
Aart Bik0148de42017-09-05 09:25:01 -07007785// Returns true iff instruction is the given integral constant.
7786inline bool IsInt64Value(HInstruction* instruction, int64_t value) {
7787 int64_t val = 0;
7788 return IsInt64AndGet(instruction, &val) && val == value;
7789}
7790
7791// Returns true iff instruction is a zero bit pattern.
7792inline bool IsZeroBitPattern(HInstruction* instruction) {
7793 return instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern();
7794}
7795
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01007796// Implement HInstruction::Is##type() for concrete instructions.
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007797#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01007798 inline bool HInstruction::Is##type() const { return GetKind() == k##type; }
7799 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
7800#undef INSTRUCTION_TYPE_CHECK
7801
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01007802// Implement HInstruction::Is##type() for abstract instructions.
Vladimir Markoa90dd512018-05-04 15:04:45 +01007803#define INSTRUCTION_TYPE_CHECK_RESULT(type, super) \
7804 std::is_base_of<BaseType, H##type>::value,
7805#define INSTRUCTION_TYPE_CHECK(type, super) \
7806 inline bool HInstruction::Is##type() const { \
7807 DCHECK_LT(GetKind(), kLastInstructionKind); \
7808 using BaseType = H##type; \
7809 static constexpr bool results[] = { \
7810 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK_RESULT) \
7811 }; \
7812 return results[static_cast<size_t>(GetKind())]; \
7813 }
7814
7815 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
7816#undef INSTRUCTION_TYPE_CHECK
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01007817#undef INSTRUCTION_TYPE_CHECK_RESULT
Vladimir Markoa90dd512018-05-04 15:04:45 +01007818
7819#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007820 inline const H##type* HInstruction::As##type() const { \
7821 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
7822 } \
7823 inline H##type* HInstruction::As##type() { \
7824 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
7825 }
7826
Vladimir Markoa90dd512018-05-04 15:04:45 +01007827 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
7828#undef INSTRUCTION_TYPE_CAST
7829
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007830
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00007831// Create space in `blocks` for adding `number_of_new_blocks` entries
7832// starting at location `at`. Blocks after `at` are moved accordingly.
7833inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
7834 size_t number_of_new_blocks,
7835 size_t after) {
7836 DCHECK_LT(after, blocks->size());
7837 size_t old_size = blocks->size();
7838 size_t new_size = old_size + number_of_new_blocks;
7839 blocks->resize(new_size);
7840 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
7841}
7842
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00007843/*
7844 * Hunt "under the hood" of array lengths (leading to array references),
7845 * null checks (also leading to array references), and new arrays
7846 * (leading to the actual length). This makes it more likely related
7847 * instructions become actually comparable.
7848 */
7849inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
7850 while (instruction->IsArrayLength() ||
7851 instruction->IsNullCheck() ||
7852 instruction->IsNewArray()) {
7853 instruction = instruction->IsNewArray()
7854 ? instruction->AsNewArray()->GetLength()
7855 : instruction->InputAt(0);
7856 }
7857 return instruction;
7858}
7859
Artem Serov21c7e6f2017-07-27 16:04:42 +01007860void RemoveEnvironmentUses(HInstruction* instruction);
7861bool HasEnvironmentUsedByOthers(HInstruction* instruction);
7862void ResetEnvironmentInputRecords(HInstruction* instruction);
7863
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007864} // namespace art
7865
7866#endif // ART_COMPILER_OPTIMIZING_NODES_H_