blob: e786502dee86f3b0f70cedaf0242040bf2b7d6c9 [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 Hodsondbaa5c72018-05-10 08:22:46 +01001386 M(LoadMethodHandle, Instruction) \
Orion Hodson18259d72018-04-12 11:18:23 +01001387 M(LoadMethodType, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001388 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001389 M(LongConstant, Constant) \
Aart Bik1f8d51b2018-02-15 10:42:37 -08001390 M(Max, Instruction) \
Calin Juravle27df7582015-04-17 19:12:31 +01001391 M(MemoryBarrier, Instruction) \
Aart Bik1f8d51b2018-02-15 10:42:37 -08001392 M(Min, BinaryOperation) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001393 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001394 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001395 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001396 M(Neg, UnaryOperation) \
1397 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001398 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001399 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001400 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001401 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001402 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001403 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001404 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001405 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001406 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001407 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001408 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001409 M(Return, Instruction) \
1410 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001411 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001412 M(Shl, BinaryOperation) \
1413 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001414 M(StaticFieldGet, Instruction) \
1415 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001416 M(UnresolvedInstanceFieldGet, Instruction) \
1417 M(UnresolvedInstanceFieldSet, Instruction) \
1418 M(UnresolvedStaticFieldGet, Instruction) \
1419 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001420 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001421 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001422 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001423 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001424 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001425 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001426 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001427 M(Xor, BinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001428 M(VecReplicateScalar, VecUnaryOperation) \
Aart Bik0148de42017-09-05 09:25:01 -07001429 M(VecExtractScalar, VecUnaryOperation) \
1430 M(VecReduce, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001431 M(VecCnv, VecUnaryOperation) \
1432 M(VecNeg, VecUnaryOperation) \
Aart Bik6daebeb2017-04-03 14:35:41 -07001433 M(VecAbs, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001434 M(VecNot, VecUnaryOperation) \
1435 M(VecAdd, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001436 M(VecHalvingAdd, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001437 M(VecSub, VecBinaryOperation) \
1438 M(VecMul, VecBinaryOperation) \
1439 M(VecDiv, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001440 M(VecMin, VecBinaryOperation) \
1441 M(VecMax, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001442 M(VecAnd, VecBinaryOperation) \
1443 M(VecAndNot, VecBinaryOperation) \
1444 M(VecOr, VecBinaryOperation) \
1445 M(VecXor, VecBinaryOperation) \
Aart Bik29aa0822018-03-08 11:28:00 -08001446 M(VecSaturationAdd, VecBinaryOperation) \
1447 M(VecSaturationSub, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001448 M(VecShl, VecBinaryOperation) \
1449 M(VecShr, VecBinaryOperation) \
1450 M(VecUShr, VecBinaryOperation) \
Aart Bik8de59162017-04-21 09:42:01 -07001451 M(VecSetScalars, VecOperation) \
Artem Serovf34dd202017-04-10 17:41:46 +01001452 M(VecMultiplyAccumulate, VecOperation) \
Aart Bikdbbac8f2017-09-01 13:06:08 -07001453 M(VecSADAccumulate, VecOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001454 M(VecLoad, VecMemoryOperation) \
1455 M(VecStore, VecMemoryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001456
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001457/*
1458 * Instructions, shared across several (not all) architectures.
1459 */
1460#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1461#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1462#else
1463#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001464 M(BitwiseNegatedRight, Instruction) \
Anton Kirilov74234da2017-01-13 14:42:47 +00001465 M(DataProcWithShifterOp, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001466 M(MultiplyAccumulate, Instruction) \
Artem Serove1811ed2017-04-27 16:50:47 +01001467 M(IntermediateAddressIndex, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001468#endif
1469
Alexandre Ramesef20f712015-06-09 10:29:30 +01001470#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1471
1472#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1473
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001474#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001475#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001476#else
1477#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1478 M(MipsComputeBaseMethodAddress, Instruction) \
Lena Djokica2901602017-09-21 13:50:52 +02001479 M(MipsPackedSwitch, Instruction) \
1480 M(IntermediateArrayAddressIndex, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001481#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001482
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001483#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1484
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001485#ifndef ART_ENABLE_CODEGEN_x86
1486#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1487#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001488#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1489 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001490 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001491 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001492 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001493#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001494
1495#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1496
1497#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1498 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001499 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001500 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1501 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001502 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001503 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001504 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1505 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1506
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001507#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1508 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001509 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001510 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001511 M(BinaryOperation, Instruction) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001512 M(Invoke, Instruction) \
1513 M(VecOperation, Instruction) \
1514 M(VecUnaryOperation, VecOperation) \
1515 M(VecBinaryOperation, VecOperation) \
1516 M(VecMemoryOperation, VecOperation)
Dave Allison20dfc792014-06-16 20:44:29 -07001517
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001518#define FOR_EACH_INSTRUCTION(M) \
1519 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1520 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1521
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001522#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001523FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1524#undef FORWARD_DECLARATION
1525
Vladimir Marko372f10e2016-05-17 16:30:10 +01001526#define DECLARE_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001527 private: \
1528 H##type& operator=(const H##type&) = delete; \
1529 public: \
Vladimir Marko372f10e2016-05-17 16:30:10 +01001530 const char* DebugName() const OVERRIDE { return #type; } \
Artem Serovcced8ba2017-07-19 18:18:09 +01001531 HInstruction* Clone(ArenaAllocator* arena) const OVERRIDE { \
1532 DCHECK(IsClonable()); \
1533 return new (arena) H##type(*this->As##type()); \
1534 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001535 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001536
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001537#define DECLARE_ABSTRACT_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001538 private: \
1539 H##type& operator=(const H##type&) = delete; \
Vladimir Markoa90dd512018-05-04 15:04:45 +01001540 public:
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001541
Artem Serovcced8ba2017-07-19 18:18:09 +01001542#define DEFAULT_COPY_CONSTRUCTOR(type) \
1543 explicit H##type(const H##type& other) = default;
1544
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001545template <typename T>
Vladimir Marko82b07402017-03-01 19:02:04 +00001546class HUseListNode : public ArenaObject<kArenaAllocUseListNode>,
1547 public IntrusiveForwardListNode<HUseListNode<T>> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001548 public:
Igor Murashkind01745e2017-04-05 16:40:31 -07001549 // Get the instruction which has this use as one of the inputs.
David Brazdiled596192015-01-23 10:39:45 +00001550 T GetUser() const { return user_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001551 // Get the position of the input record that this use corresponds to.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001552 size_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001553 // Set the position of the input record that this use corresponds to.
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001554 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001555
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001556 private:
David Brazdiled596192015-01-23 10:39:45 +00001557 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001558 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001559
1560 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001561 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001562
Vladimir Marko46817b82016-03-29 12:21:58 +01001563 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001564
1565 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1566};
1567
David Brazdiled596192015-01-23 10:39:45 +00001568template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001569using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001570
David Brazdil1abb4192015-02-17 18:33:36 +00001571// This class is used by HEnvironment and HInstruction classes to record the
1572// instructions they use and pointers to the corresponding HUseListNodes kept
1573// by the used instructions.
1574template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001575class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001576 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001577 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1578 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001579
Vladimir Marko46817b82016-03-29 12:21:58 +01001580 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1581 : HUserRecord(old_record.instruction_, before_use_node) {}
1582 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1583 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001584 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001585 }
1586
1587 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001588 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1589 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001590
1591 private:
1592 // Instruction used by the user.
1593 HInstruction* instruction_;
1594
Vladimir Marko46817b82016-03-29 12:21:58 +01001595 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1596 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001597};
1598
Vladimir Markoe9004912016-06-16 16:50:52 +01001599// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1600// This is used for HInstruction::GetInputs() to return a container wrapper providing
1601// HInstruction* values even though the underlying container has HUserRecord<>s.
1602struct HInputExtractor {
1603 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1604 return record.GetInstruction();
1605 }
1606 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1607 return record.GetInstruction();
1608 }
1609};
1610
1611using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1612using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1613
Aart Bik854a02b2015-07-14 16:07:00 -07001614/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001615 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001616 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001617 * For write/read dependences on fields/arrays, the dependence analysis uses
1618 * type disambiguation (e.g. a float field write cannot modify the value of an
1619 * integer field read) and the access type (e.g. a reference array write cannot
1620 * modify the value of a reference field read [although it may modify the
1621 * reference fetch prior to reading the field, which is represented by its own
1622 * write/read dependence]). The analysis makes conservative points-to
1623 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1624 * the same, and any reference read depends on any reference read without
1625 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001626 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001627 * The internal representation uses 38-bit and is described in the table below.
1628 * The first line indicates the side effect, and for field/array accesses the
1629 * second line indicates the type of the access (in the order of the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001630 * DataType::Type enum).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001631 * The two numbered lines below indicate the bit position in the bitfield (read
1632 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001633 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001634 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1635 * +-------------+---------+---------+--------------+---------+---------+
1636 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1637 * | 3 |333333322|222222221| 1 |111111110|000000000|
1638 * | 7 |654321098|765432109| 8 |765432109|876543210|
1639 *
1640 * Note that, to ease the implementation, 'changes' bits are least significant
1641 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001642 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001643class SideEffects : public ValueObject {
1644 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001645 SideEffects() : flags_(0) {}
1646
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001647 static SideEffects None() {
1648 return SideEffects(0);
1649 }
1650
1651 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001652 return SideEffects(kAllChangeBits | kAllDependOnBits);
1653 }
1654
1655 static SideEffects AllChanges() {
1656 return SideEffects(kAllChangeBits);
1657 }
1658
1659 static SideEffects AllDependencies() {
1660 return SideEffects(kAllDependOnBits);
1661 }
1662
1663 static SideEffects AllExceptGCDependency() {
1664 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1665 }
1666
1667 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001668 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001669 }
1670
Aart Bik34c3ba92015-07-20 14:08:59 -07001671 static SideEffects AllWrites() {
1672 return SideEffects(kAllWrites);
1673 }
1674
1675 static SideEffects AllReads() {
1676 return SideEffects(kAllReads);
1677 }
1678
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001679 static SideEffects FieldWriteOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001680 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001681 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001682 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001683 }
1684
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001685 static SideEffects ArrayWriteOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001686 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001687 }
1688
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001689 static SideEffects FieldReadOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001690 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001691 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001692 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001693 }
1694
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001695 static SideEffects ArrayReadOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001696 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001697 }
1698
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001699 static SideEffects CanTriggerGC() {
1700 return SideEffects(1ULL << kCanTriggerGCBit);
1701 }
1702
1703 static SideEffects DependsOnGC() {
1704 return SideEffects(1ULL << kDependsOnGCBit);
1705 }
1706
Aart Bik854a02b2015-07-14 16:07:00 -07001707 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001708 SideEffects Union(SideEffects other) const {
1709 return SideEffects(flags_ | other.flags_);
1710 }
1711
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001712 SideEffects Exclusion(SideEffects other) const {
1713 return SideEffects(flags_ & ~other.flags_);
1714 }
1715
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001716 void Add(SideEffects other) {
1717 flags_ |= other.flags_;
1718 }
1719
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001720 bool Includes(SideEffects other) const {
1721 return (other.flags_ & flags_) == other.flags_;
1722 }
1723
1724 bool HasSideEffects() const {
1725 return (flags_ & kAllChangeBits);
1726 }
1727
1728 bool HasDependencies() const {
1729 return (flags_ & kAllDependOnBits);
1730 }
1731
1732 // Returns true if there are no side effects or dependencies.
1733 bool DoesNothing() const {
1734 return flags_ == 0;
1735 }
1736
Aart Bik854a02b2015-07-14 16:07:00 -07001737 // Returns true if something is written.
1738 bool DoesAnyWrite() const {
1739 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001740 }
1741
Aart Bik854a02b2015-07-14 16:07:00 -07001742 // Returns true if something is read.
1743 bool DoesAnyRead() const {
1744 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001745 }
1746
Aart Bik854a02b2015-07-14 16:07:00 -07001747 // Returns true if potentially everything is written and read
1748 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001749 bool DoesAllReadWrite() const {
1750 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1751 }
1752
Aart Bik854a02b2015-07-14 16:07:00 -07001753 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001754 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001755 }
1756
Roland Levillain0d5a2812015-11-13 10:07:31 +00001757 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001758 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001759 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1760 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001761 }
1762
1763 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001764 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001765 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001766 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001767 for (int s = kLastBit; s >= 0; s--) {
1768 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1769 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1770 // This is a bit for the GC side effect.
1771 if (current_bit_is_set) {
1772 flags += "GC";
1773 }
Aart Bik854a02b2015-07-14 16:07:00 -07001774 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001775 } else {
1776 // This is a bit for the array/field analysis.
1777 // The underscore character stands for the 'can trigger GC' bit.
1778 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1779 if (current_bit_is_set) {
1780 flags += kDebug[s];
1781 }
1782 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1783 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1784 flags += "|";
1785 }
1786 }
Aart Bik854a02b2015-07-14 16:07:00 -07001787 }
1788 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001789 }
1790
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001791 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001792
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001793 private:
1794 static constexpr int kFieldArrayAnalysisBits = 9;
1795
1796 static constexpr int kFieldWriteOffset = 0;
1797 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1798 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1799 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1800
1801 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1802
1803 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1804 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1805 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1806 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1807
1808 static constexpr int kLastBit = kDependsOnGCBit;
1809 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1810
1811 // Aliases.
1812
1813 static_assert(kChangeBits == kDependOnBits,
1814 "the 'change' bits should match the 'depend on' bits.");
1815
1816 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1817 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1818 static constexpr uint64_t kAllWrites =
1819 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1820 static constexpr uint64_t kAllReads =
1821 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001822
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001823 // Translates type to bit flag. The type must correspond to a Java type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001824 static uint64_t TypeFlag(DataType::Type type, int offset) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001825 int shift;
1826 switch (type) {
1827 case DataType::Type::kReference: shift = 0; break;
1828 case DataType::Type::kBool: shift = 1; break;
1829 case DataType::Type::kInt8: shift = 2; break;
1830 case DataType::Type::kUint16: shift = 3; break;
1831 case DataType::Type::kInt16: shift = 4; break;
1832 case DataType::Type::kInt32: shift = 5; break;
1833 case DataType::Type::kInt64: shift = 6; break;
1834 case DataType::Type::kFloat32: shift = 7; break;
1835 case DataType::Type::kFloat64: shift = 8; break;
1836 default:
1837 LOG(FATAL) << "Unexpected data type " << type;
1838 UNREACHABLE();
1839 }
Aart Bik854a02b2015-07-14 16:07:00 -07001840 DCHECK_LE(kFieldWriteOffset, shift);
1841 DCHECK_LT(shift, kArrayWriteOffset);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001842 return UINT64_C(1) << (shift + offset);
Aart Bik854a02b2015-07-14 16:07:00 -07001843 }
1844
1845 // Private constructor on direct flags value.
1846 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1847
1848 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001849};
1850
David Brazdiled596192015-01-23 10:39:45 +00001851// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001852class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001853 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001854 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
Mingyao Yang01b47b02017-02-03 12:09:57 -08001855 size_t number_of_vregs,
1856 ArtMethod* method,
1857 uint32_t dex_pc,
1858 HInstruction* holder)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001859 : vregs_(number_of_vregs, allocator->Adapter(kArenaAllocEnvironmentVRegs)),
1860 locations_(allocator->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001861 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001862 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001863 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001864 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001865 }
1866
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001867 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
1868 const HEnvironment& to_copy,
1869 HInstruction* holder)
1870 : HEnvironment(allocator,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001871 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001872 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001873 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001874 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001875
Vladimir Markoec32f642017-06-02 10:51:55 +01001876 void AllocateLocations() {
1877 DCHECK(locations_.empty());
1878 locations_.resize(vregs_.size());
1879 }
1880
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001881 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001882 if (parent_ != nullptr) {
1883 parent_->SetAndCopyParentChain(allocator, parent);
1884 } else {
1885 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1886 parent_->CopyFrom(parent);
1887 if (parent->GetParent() != nullptr) {
1888 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1889 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001890 }
David Brazdiled596192015-01-23 10:39:45 +00001891 }
1892
Vladimir Marko69d310e2017-10-09 14:12:23 +01001893 void CopyFrom(ArrayRef<HInstruction* const> locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001894 void CopyFrom(HEnvironment* environment);
1895
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001896 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1897 // input to the loop phi instead. This is for inserting instructions that
1898 // require an environment (like HDeoptimization) in the loop pre-header.
1899 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001900
1901 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001902 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001903 }
1904
1905 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001906 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001907 }
1908
David Brazdil1abb4192015-02-17 18:33:36 +00001909 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001910
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001911 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001912
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001913 HEnvironment* GetParent() const { return parent_; }
1914
1915 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001916 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001917 }
1918
1919 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001920 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001921 }
1922
1923 uint32_t GetDexPc() const {
1924 return dex_pc_;
1925 }
1926
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001927 ArtMethod* GetMethod() const {
1928 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001929 }
1930
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001931 HInstruction* GetHolder() const {
1932 return holder_;
1933 }
1934
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001935
1936 bool IsFromInlinedInvoke() const {
1937 return GetParent() != nullptr;
1938 }
1939
David Brazdiled596192015-01-23 10:39:45 +00001940 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001941 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1942 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001943 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001944 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001945 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00001946
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001947 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001948 HInstruction* const holder_;
1949
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001950 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001951
1952 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1953};
1954
Vladimir Markof9f64412015-09-02 14:05:49 +01001955class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001956 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05301957#define DECLARE_KIND(type, super) k##type,
1958 enum InstructionKind {
Vladimir Markoe3946222018-05-04 14:18:47 +01001959 FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_KIND)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05301960 kLastInstructionKind
1961 };
1962#undef DECLARE_KIND
1963
1964 HInstruction(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01001965 : HInstruction(kind, DataType::Type::kVoid, side_effects, dex_pc) {}
1966
1967 HInstruction(InstructionKind kind, DataType::Type type, SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001968 : previous_(nullptr),
1969 next_(nullptr),
1970 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001971 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001972 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001973 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001974 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001975 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001976 locations_(nullptr),
1977 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001978 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001979 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001980 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05301981 SetPackedField<InstructionKindField>(kind);
Vladimir Markobd785672018-05-03 17:09:09 +01001982 SetPackedField<TypeField>(type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001983 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1984 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001985
Dave Allison20dfc792014-06-16 20:44:29 -07001986 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001987
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001988
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001989 HInstruction* GetNext() const { return next_; }
1990 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001991
Calin Juravle77520bc2015-01-12 18:45:46 +00001992 HInstruction* GetNextDisregardingMoves() const;
1993 HInstruction* GetPreviousDisregardingMoves() const;
1994
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001995 HBasicBlock* GetBlock() const { return block_; }
Vladimir Markoca6fff82017-10-03 14:49:14 +01001996 ArenaAllocator* GetAllocator() const { return block_->GetGraph()->GetAllocator(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001997 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001998 bool IsInBlock() const { return block_ != nullptr; }
1999 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00002000 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
2001 bool IsIrreducibleLoopHeaderPhi() const {
2002 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
2003 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002004
Vladimir Marko372f10e2016-05-17 16:30:10 +01002005 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
2006
2007 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
2008 // One virtual method is enough, just const_cast<> and then re-add the const.
2009 return ArrayRef<const HUserRecord<HInstruction*>>(
2010 const_cast<HInstruction*>(this)->GetInputRecords());
2011 }
2012
Vladimir Markoe9004912016-06-16 16:50:52 +01002013 HInputsRef GetInputs() {
2014 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002015 }
2016
Vladimir Markoe9004912016-06-16 16:50:52 +01002017 HConstInputsRef GetInputs() const {
2018 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002019 }
2020
2021 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00002022 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002023
Aart Bik2767f4b2016-10-28 15:03:53 -07002024 bool HasInput(HInstruction* input) const {
2025 for (const HInstruction* i : GetInputs()) {
2026 if (i == input) {
2027 return true;
2028 }
2029 }
2030 return false;
2031 }
2032
Vladimir Marko372f10e2016-05-17 16:30:10 +01002033 void SetRawInputAt(size_t index, HInstruction* input) {
2034 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
2035 }
2036
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002037 virtual void Accept(HGraphVisitor* visitor) = 0;
2038 virtual const char* DebugName() const = 0;
2039
Vladimir Markobd785672018-05-03 17:09:09 +01002040 DataType::Type GetType() const {
2041 return TypeField::Decode(GetPackedFields());
2042 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002043
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002044 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002045
2046 uint32_t GetDexPc() const { return dex_pc_; }
2047
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002048 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01002049
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002050 // Can the instruction throw?
2051 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
2052 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01002053 virtual bool CanThrow() const { return false; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08002054
2055 // Does the instruction always throw an exception unconditionally?
2056 virtual bool AlwaysThrows() const { return false; }
2057
David Brazdilec16f792015-08-19 15:04:01 +01002058 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002059
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002060 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002061 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002062
Calin Juravle10e244f2015-01-26 18:54:32 +00002063 // Does not apply for all instructions, but having this at top level greatly
2064 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00002065 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002066 virtual bool CanBeNull() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002067 DCHECK_EQ(GetType(), DataType::Type::kReference) << "CanBeNull only applies to reference types";
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002068 return true;
2069 }
Calin Juravle10e244f2015-01-26 18:54:32 +00002070
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002071 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01002072 return false;
2073 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002074
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002075 virtual bool IsActualObject() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002076 return GetType() == DataType::Type::kReference;
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002077 }
2078
Calin Juravle2e768302015-07-28 14:41:11 +00002079 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00002080
Calin Juravle61d544b2015-02-23 16:46:57 +00002081 ReferenceTypeInfo GetReferenceTypeInfo() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002082 DCHECK_EQ(GetType(), DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002083 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00002084 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00002085 }
Calin Juravleacf735c2015-02-12 15:25:22 +00002086
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002087 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00002088 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002089 // Note: fixup_end remains valid across push_front().
2090 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
2091 HUseListNode<HInstruction*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002092 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HInstruction*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002093 uses_.push_front(*new_node);
2094 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002095 }
2096
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002097 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01002098 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002099 // Note: env_fixup_end remains valid across push_front().
2100 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
2101 HUseListNode<HEnvironment*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002102 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HEnvironment*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002103 env_uses_.push_front(*new_node);
2104 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002105 }
2106
David Brazdil1abb4192015-02-17 18:33:36 +00002107 void RemoveAsUserOfInput(size_t input) {
2108 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002109 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2110 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2111 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00002112 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002113
Vladimir Marko372f10e2016-05-17 16:30:10 +01002114 void RemoveAsUserOfAllInputs() {
2115 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2116 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2117 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2118 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
2119 }
2120 }
2121
David Brazdil1abb4192015-02-17 18:33:36 +00002122 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2123 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002124
Vladimir Marko46817b82016-03-29 12:21:58 +01002125 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2126 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2127 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002128 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002129 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002130 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002131
Aart Bikcc42be02016-10-20 16:14:16 -07002132 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002133 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002134 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002135 !CanThrow() &&
2136 !IsSuspendCheck() &&
2137 !IsControlFlow() &&
2138 !IsNativeDebugInfo() &&
2139 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002140 // If we added an explicit barrier then we should keep it.
Igor Murashkind01745e2017-04-05 16:40:31 -07002141 !IsMemoryBarrier() &&
2142 !IsConstructorFence();
Aart Bik482095d2016-10-10 15:39:10 -07002143 }
2144
Aart Bikcc42be02016-10-20 16:14:16 -07002145 bool IsDeadAndRemovable() const {
2146 return IsRemovable() && !HasUses();
2147 }
2148
Roland Levillain6c82d402014-10-13 16:10:27 +01002149 // Does this instruction strictly dominate `other_instruction`?
2150 // Returns false if this instruction and `other_instruction` are the same.
2151 // Aborts if this instruction and `other_instruction` are both phis.
2152 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002153
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002154 int GetId() const { return id_; }
2155 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002156
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002157 int GetSsaIndex() const { return ssa_index_; }
2158 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2159 bool HasSsaIndex() const { return ssa_index_ != -1; }
2160
2161 bool HasEnvironment() const { return environment_ != nullptr; }
2162 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002163 // Set the `environment_` field. Raw because this method does not
2164 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002165 void SetRawEnvironment(HEnvironment* environment) {
2166 DCHECK(environment_ == nullptr);
2167 DCHECK_EQ(environment->GetHolder(), this);
2168 environment_ = environment;
2169 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002170
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002171 void InsertRawEnvironment(HEnvironment* environment) {
2172 DCHECK(environment_ != nullptr);
2173 DCHECK_EQ(environment->GetHolder(), this);
2174 DCHECK(environment->GetParent() == nullptr);
2175 environment->parent_ = environment_;
2176 environment_ = environment;
2177 }
2178
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002179 void RemoveEnvironment();
2180
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002181 // Set the environment of this instruction, copying it from `environment`. While
2182 // copying, the uses lists are being updated.
2183 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002184 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002185 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002186 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002187 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002188 if (environment->GetParent() != nullptr) {
2189 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2190 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002191 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002192
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002193 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2194 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002195 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002196 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002197 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002198 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002199 if (environment->GetParent() != nullptr) {
2200 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2201 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002202 }
2203
Nicolas Geoffray39468442014-09-02 15:17:15 +01002204 // Returns the number of entries in the environment. Typically, that is the
2205 // number of dex registers in a method. It could be more in case of inlining.
2206 size_t EnvironmentSize() const;
2207
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002208 LocationSummary* GetLocations() const { return locations_; }
2209 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002210
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002211 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002212 void ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002213 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002214
Alexandre Rames188d4312015-04-09 18:30:21 +01002215 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2216 // uses of this instruction by `other` are *not* updated.
2217 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2218 ReplaceWith(other);
2219 other->ReplaceInput(this, use_index);
2220 }
2221
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002222 // Move `this` instruction before `cursor`
2223 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002224
Vladimir Markofb337ea2015-11-25 15:25:10 +00002225 // Move `this` before its first user and out of any loops. If there is no
2226 // out-of-loop user that dominates all other users, move the instruction
2227 // to the end of the out-of-loop common dominator of the user's blocks.
2228 //
2229 // This can be used only on non-throwing instructions with no side effects that
2230 // have at least one use but no environment uses.
2231 void MoveBeforeFirstUserAndOutOfLoops();
2232
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002233#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01002234 bool Is##type() const;
2235
2236 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2237#undef INSTRUCTION_TYPE_CHECK
2238
2239#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002240 const H##type* As##type() const; \
2241 H##type* As##type();
2242
Vladimir Markoa90dd512018-05-04 15:04:45 +01002243 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
2244#undef INSTRUCTION_TYPE_CAST
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002245
Artem Serovcced8ba2017-07-19 18:18:09 +01002246 // Return a clone of the instruction if it is clonable (shallow copy by default, custom copy
2247 // if a custom copy-constructor is provided for a particular type). If IsClonable() is false for
2248 // the instruction then the behaviour of this function is undefined.
2249 //
2250 // Note: It is semantically valid to create a clone of the instruction only until
2251 // prepare_for_register_allocator phase as lifetime, intervals and codegen info are not
2252 // copied.
2253 //
2254 // Note: HEnvironment and some other fields are not copied and are set to default values, see
2255 // 'explicit HInstruction(const HInstruction& other)' for details.
2256 virtual HInstruction* Clone(ArenaAllocator* arena ATTRIBUTE_UNUSED) const {
2257 LOG(FATAL) << "Cloning is not implemented for the instruction " <<
2258 DebugName() << " " << GetId();
2259 UNREACHABLE();
2260 }
2261
2262 // Return whether instruction can be cloned (copied).
2263 virtual bool IsClonable() const { return false; }
2264
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002265 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002266 // TODO: this method is used by LICM and GVN with possibly different
2267 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002268 virtual bool CanBeMoved() const { return false; }
2269
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002270 // Returns whether any data encoded in the two instructions is equal.
2271 // This method does not look at the inputs. Both instructions must be
2272 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002273 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002274 return false;
2275 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002276
2277 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002278 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002279 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002280 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002281
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002282 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2283 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2284 // the virtual function because the __attribute__((__pure__)) doesn't really
2285 // apply the strong requirement for virtual functions, preventing optimizations.
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302286 InstructionKind GetKind() const { return GetPackedField<InstructionKindField>(); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002287
2288 virtual size_t ComputeHashCode() const {
2289 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002290 for (const HInstruction* input : GetInputs()) {
2291 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002292 }
2293 return result;
2294 }
2295
2296 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002297 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002298 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002299
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002300 size_t GetLifetimePosition() const { return lifetime_position_; }
2301 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2302 LiveInterval* GetLiveInterval() const { return live_interval_; }
2303 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2304 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2305
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002306 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2307
2308 // Returns whether the code generation of the instruction will require to have access
2309 // to the current method. Such instructions are:
2310 // (1): Instructions that require an environment, as calling the runtime requires
2311 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002312 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2313 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002314 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002315 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002316 }
2317
Vladimir Markodc151b22015-10-15 18:02:30 +01002318 // Returns whether the code generation of the instruction will require to have access
2319 // to the dex cache of the current method's declaring class via the current method.
2320 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002321
Mark Mendellc4701932015-04-10 13:18:51 -04002322 // Does this instruction have any use in an environment before
2323 // control flow hits 'other'?
2324 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2325
2326 // Remove all references to environment uses of this instruction.
2327 // The caller must ensure that this is safe to do.
2328 void RemoveEnvironmentUsers();
2329
Vladimir Markoa1de9182016-02-25 11:37:38 +00002330 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2331 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002332
David Brazdil1abb4192015-02-17 18:33:36 +00002333 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002334 // If set, the machine code for this instruction is assumed to be generated by
2335 // its users. Used by liveness analysis to compute use positions accordingly.
2336 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2337 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302338 static constexpr size_t kFieldInstructionKind = kFlagReferenceTypeIsExact + 1;
2339 static constexpr size_t kFieldInstructionKindSize =
2340 MinimumBitsToStore(static_cast<size_t>(InstructionKind::kLastInstructionKind - 1));
Vladimir Markobd785672018-05-03 17:09:09 +01002341 static constexpr size_t kFieldType =
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302342 kFieldInstructionKind + kFieldInstructionKindSize;
Vladimir Markobd785672018-05-03 17:09:09 +01002343 static constexpr size_t kFieldTypeSize =
2344 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
2345 static constexpr size_t kNumberOfGenericPackedBits = kFieldType + kFieldTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00002346 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2347
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302348 static_assert(kNumberOfGenericPackedBits <= kMaxNumberOfPackedBits,
2349 "Too many generic packed fields");
2350
Vladimir Markobd785672018-05-03 17:09:09 +01002351 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
2352
Vladimir Marko372f10e2016-05-17 16:30:10 +01002353 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2354 return GetInputRecords()[i];
2355 }
2356
2357 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2358 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2359 input_records[index] = input;
2360 }
David Brazdil1abb4192015-02-17 18:33:36 +00002361
Vladimir Markoa1de9182016-02-25 11:37:38 +00002362 uint32_t GetPackedFields() const {
2363 return packed_fields_;
2364 }
2365
2366 template <size_t flag>
2367 bool GetPackedFlag() const {
2368 return (packed_fields_ & (1u << flag)) != 0u;
2369 }
2370
2371 template <size_t flag>
2372 void SetPackedFlag(bool value = true) {
2373 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2374 }
2375
2376 template <typename BitFieldType>
2377 typename BitFieldType::value_type GetPackedField() const {
2378 return BitFieldType::Decode(packed_fields_);
2379 }
2380
2381 template <typename BitFieldType>
2382 void SetPackedField(typename BitFieldType::value_type value) {
2383 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2384 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2385 }
2386
Artem Serovcced8ba2017-07-19 18:18:09 +01002387 // Copy construction for the instruction (used for Clone function).
2388 //
2389 // Fields (e.g. lifetime, intervals and codegen info) associated with phases starting from
2390 // prepare_for_register_allocator are not copied (set to default values).
2391 //
2392 // Copy constructors must be provided for every HInstruction type; default copy constructor is
2393 // fine for most of them. However for some of the instructions a custom copy constructor must be
2394 // specified (when instruction has non-trivially copyable fields and must have a special behaviour
2395 // for copying them).
2396 explicit HInstruction(const HInstruction& other)
2397 : previous_(nullptr),
2398 next_(nullptr),
2399 block_(nullptr),
2400 dex_pc_(other.dex_pc_),
2401 id_(-1),
2402 ssa_index_(-1),
2403 packed_fields_(other.packed_fields_),
2404 environment_(nullptr),
2405 locations_(nullptr),
2406 live_interval_(nullptr),
2407 lifetime_position_(kNoLifetime),
2408 side_effects_(other.side_effects_),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302409 reference_type_handle_(other.reference_type_handle_) {
2410 }
Artem Serovcced8ba2017-07-19 18:18:09 +01002411
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002412 private:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302413 using InstructionKindField =
2414 BitField<InstructionKind, kFieldInstructionKind, kFieldInstructionKindSize>;
2415
Vladimir Marko46817b82016-03-29 12:21:58 +01002416 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2417 auto before_use_node = uses_.before_begin();
2418 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2419 HInstruction* user = use_node->GetUser();
2420 size_t input_index = use_node->GetIndex();
2421 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2422 before_use_node = use_node;
2423 }
2424 }
2425
2426 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2427 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2428 if (next != uses_.end()) {
2429 HInstruction* next_user = next->GetUser();
2430 size_t next_index = next->GetIndex();
2431 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2432 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2433 }
2434 }
2435
2436 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2437 auto before_env_use_node = env_uses_.before_begin();
2438 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2439 HEnvironment* user = env_use_node->GetUser();
2440 size_t input_index = env_use_node->GetIndex();
2441 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2442 before_env_use_node = env_use_node;
2443 }
2444 }
2445
2446 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2447 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2448 if (next != env_uses_.end()) {
2449 HEnvironment* next_user = next->GetUser();
2450 size_t next_index = next->GetIndex();
2451 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2452 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2453 }
2454 }
David Brazdil1abb4192015-02-17 18:33:36 +00002455
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002456 HInstruction* previous_;
2457 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002458 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002459 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002460
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002461 // An instruction gets an id when it is added to the graph.
2462 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002463 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002464 int id_;
2465
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002466 // When doing liveness analysis, instructions that have uses get an SSA index.
2467 int ssa_index_;
2468
Vladimir Markoa1de9182016-02-25 11:37:38 +00002469 // Packed fields.
2470 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002471
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002472 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002473 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002474
2475 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002476 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002477
Nicolas Geoffray39468442014-09-02 15:17:15 +01002478 // The environment associated with this instruction. Not null if the instruction
2479 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002480 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002481
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002482 // Set by the code generator.
2483 LocationSummary* locations_;
2484
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002485 // Set by the liveness analysis.
2486 LiveInterval* live_interval_;
2487
2488 // Set by the liveness analysis, this is the position in a linear
2489 // order of blocks where this instruction's live interval start.
2490 size_t lifetime_position_;
2491
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002492 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002493
Vladimir Markoa1de9182016-02-25 11:37:38 +00002494 // The reference handle part of the reference type info.
2495 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002496 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002497 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002498
David Brazdil1abb4192015-02-17 18:33:36 +00002499 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002500 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002501 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002502 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002503 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002504};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002505std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002506
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002507// Iterates over the instructions, while preserving the next instruction
2508// in case the current instruction gets removed from the list by the user
2509// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002510class HInstructionIterator : public ValueObject {
2511 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002512 explicit HInstructionIterator(const HInstructionList& instructions)
2513 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002514 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002515 }
2516
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002517 bool Done() const { return instruction_ == nullptr; }
2518 HInstruction* Current() const { return instruction_; }
2519 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002520 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002521 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002522 }
2523
2524 private:
2525 HInstruction* instruction_;
2526 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002527
2528 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002529};
2530
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002531// Iterates over the instructions without saving the next instruction,
2532// therefore handling changes in the graph potentially made by the user
2533// of this iterator.
2534class HInstructionIteratorHandleChanges : public ValueObject {
2535 public:
2536 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2537 : instruction_(instructions.first_instruction_) {
2538 }
2539
2540 bool Done() const { return instruction_ == nullptr; }
2541 HInstruction* Current() const { return instruction_; }
2542 void Advance() {
2543 instruction_ = instruction_->GetNext();
2544 }
2545
2546 private:
2547 HInstruction* instruction_;
2548
2549 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2550};
2551
2552
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002553class HBackwardInstructionIterator : public ValueObject {
2554 public:
2555 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2556 : instruction_(instructions.last_instruction_) {
2557 next_ = Done() ? nullptr : instruction_->GetPrevious();
2558 }
2559
2560 bool Done() const { return instruction_ == nullptr; }
2561 HInstruction* Current() const { return instruction_; }
2562 void Advance() {
2563 instruction_ = next_;
2564 next_ = Done() ? nullptr : instruction_->GetPrevious();
2565 }
2566
2567 private:
2568 HInstruction* instruction_;
2569 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002570
2571 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002572};
2573
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002574class HVariableInputSizeInstruction : public HInstruction {
2575 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002576 using HInstruction::GetInputRecords; // Keep the const version visible.
2577 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
2578 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2579 }
2580
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002581 void AddInput(HInstruction* input);
2582 void InsertInputAt(size_t index, HInstruction* input);
2583 void RemoveInputAt(size_t index);
2584
Igor Murashkind01745e2017-04-05 16:40:31 -07002585 // Removes all the inputs.
2586 // Also removes this instructions from each input's use list
2587 // (for non-environment uses only).
2588 void RemoveAllInputs();
2589
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002590 protected:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302591 HVariableInputSizeInstruction(InstructionKind inst_kind,
2592 SideEffects side_effects,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002593 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002594 ArenaAllocator* allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002595 size_t number_of_inputs,
2596 ArenaAllocKind kind)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302597 : HInstruction(inst_kind, side_effects, dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002598 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Vladimir Markobd785672018-05-03 17:09:09 +01002599 HVariableInputSizeInstruction(InstructionKind inst_kind,
2600 DataType::Type type,
2601 SideEffects side_effects,
2602 uint32_t dex_pc,
2603 ArenaAllocator* allocator,
2604 size_t number_of_inputs,
2605 ArenaAllocKind kind)
2606 : HInstruction(inst_kind, type, side_effects, dex_pc),
2607 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002608
Artem Serovcced8ba2017-07-19 18:18:09 +01002609 DEFAULT_COPY_CONSTRUCTOR(VariableInputSizeInstruction);
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002610
Artem Serovcced8ba2017-07-19 18:18:09 +01002611 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002612};
2613
Vladimir Markof9f64412015-09-02 14:05:49 +01002614template<size_t N>
Vladimir Markobd785672018-05-03 17:09:09 +01002615class HExpression : public HInstruction {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002616 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002617 HExpression<N>(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302618 : HInstruction(kind, side_effects, dex_pc), inputs_() {}
Vladimir Markobd785672018-05-03 17:09:09 +01002619 HExpression<N>(InstructionKind kind,
2620 DataType::Type type,
2621 SideEffects side_effects,
2622 uint32_t dex_pc)
2623 : HInstruction(kind, type, side_effects, dex_pc), inputs_() {}
2624 virtual ~HExpression() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002625
Vladimir Marko372f10e2016-05-17 16:30:10 +01002626 using HInstruction::GetInputRecords; // Keep the const version visible.
2627 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2628 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002629 }
2630
Artem Serovcced8ba2017-07-19 18:18:09 +01002631 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002632 DEFAULT_COPY_CONSTRUCTOR(Expression<N>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002633
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002634 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002635 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002636
2637 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002638};
2639
Vladimir Markobd785672018-05-03 17:09:09 +01002640// HExpression specialization for N=0.
Vladimir Markof9f64412015-09-02 14:05:49 +01002641template<>
Vladimir Markobd785672018-05-03 17:09:09 +01002642class HExpression<0> : public HInstruction {
Vladimir Markof9f64412015-09-02 14:05:49 +01002643 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002644 using HInstruction::HInstruction;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002645
Vladimir Markobd785672018-05-03 17:09:09 +01002646 virtual ~HExpression() {}
Vladimir Markof9f64412015-09-02 14:05:49 +01002647
Vladimir Marko372f10e2016-05-17 16:30:10 +01002648 using HInstruction::GetInputRecords; // Keep the const version visible.
2649 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2650 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002651 }
2652
Artem Serovcced8ba2017-07-19 18:18:09 +01002653 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002654 DEFAULT_COPY_CONSTRUCTOR(Expression<0>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002655
Vladimir Markof9f64412015-09-02 14:05:49 +01002656 private:
2657 friend class SsaBuilder;
2658};
2659
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002660// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2661// instruction that branches to the exit block.
Vladimir Markobd785672018-05-03 17:09:09 +01002662class HReturnVoid FINAL : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002663 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002664 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002665 : HExpression(kReturnVoid, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302666 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002667
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002668 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002669
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002670 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002671
Artem Serovcced8ba2017-07-19 18:18:09 +01002672 protected:
2673 DEFAULT_COPY_CONSTRUCTOR(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002674};
2675
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002676// Represents dex's RETURN opcodes. A HReturn is a control flow
2677// instruction that branches to the exit block.
Vladimir Markobd785672018-05-03 17:09:09 +01002678class HReturn FINAL : public HExpression<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002679 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002680 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002681 : HExpression(kReturn, SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002682 SetRawInputAt(0, value);
2683 }
2684
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002685 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002686
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002687 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002688
Artem Serovcced8ba2017-07-19 18:18:09 +01002689 protected:
2690 DEFAULT_COPY_CONSTRUCTOR(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002691};
2692
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002693class HPhi FINAL : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002694 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002695 HPhi(ArenaAllocator* allocator,
David Brazdildee58d62016-04-07 09:54:26 +00002696 uint32_t reg_number,
2697 size_t number_of_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002698 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +00002699 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002700 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302701 kPhi,
Vladimir Markobd785672018-05-03 17:09:09 +01002702 ToPhiType(type),
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002703 SideEffects::None(),
2704 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002705 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002706 number_of_inputs,
2707 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002708 reg_number_(reg_number) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002709 DCHECK_NE(GetType(), DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +00002710 // Phis are constructed live and marked dead if conflicting or unused.
2711 // Individual steps of SsaBuilder should assume that if a phi has been
2712 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2713 SetPackedFlag<kFlagIsLive>(true);
2714 SetPackedFlag<kFlagCanBeNull>(true);
2715 }
2716
Artem Serovcced8ba2017-07-19 18:18:09 +01002717 bool IsClonable() const OVERRIDE { return true; }
2718
David Brazdildee58d62016-04-07 09:54:26 +00002719 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002720 static DataType::Type ToPhiType(DataType::Type type) {
2721 return DataType::Kind(type);
David Brazdildee58d62016-04-07 09:54:26 +00002722 }
2723
2724 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2725
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002726 void SetType(DataType::Type new_type) {
David Brazdildee58d62016-04-07 09:54:26 +00002727 // Make sure that only valid type changes occur. The following are allowed:
2728 // (1) int -> float/ref (primitive type propagation),
2729 // (2) long -> double (primitive type propagation).
2730 DCHECK(GetType() == new_type ||
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002731 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kFloat32) ||
2732 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kReference) ||
2733 (GetType() == DataType::Type::kInt64 && new_type == DataType::Type::kFloat64));
David Brazdildee58d62016-04-07 09:54:26 +00002734 SetPackedField<TypeField>(new_type);
2735 }
2736
2737 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2738 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2739
2740 uint32_t GetRegNumber() const { return reg_number_; }
2741
2742 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2743 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2744 bool IsDead() const { return !IsLive(); }
2745 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2746
Vladimir Markoe9004912016-06-16 16:50:52 +01002747 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002748 return other != nullptr
2749 && other->IsPhi()
2750 && other->AsPhi()->GetBlock() == GetBlock()
2751 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2752 }
2753
Nicolas Geoffrayf57c1ae2017-06-28 17:40:18 +01002754 bool HasEquivalentPhi() const {
2755 if (GetPrevious() != nullptr && GetPrevious()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2756 return true;
2757 }
2758 if (GetNext() != nullptr && GetNext()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2759 return true;
2760 }
2761 return false;
2762 }
2763
David Brazdildee58d62016-04-07 09:54:26 +00002764 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2765 // An equivalent phi is a phi having the same dex register and type.
2766 // It assumes that phis with the same dex register are adjacent.
2767 HPhi* GetNextEquivalentPhiWithSameType() {
2768 HInstruction* next = GetNext();
2769 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2770 if (next->GetType() == GetType()) {
2771 return next->AsPhi();
2772 }
2773 next = next->GetNext();
2774 }
2775 return nullptr;
2776 }
2777
2778 DECLARE_INSTRUCTION(Phi);
2779
Artem Serovcced8ba2017-07-19 18:18:09 +01002780 protected:
2781 DEFAULT_COPY_CONSTRUCTOR(Phi);
2782
David Brazdildee58d62016-04-07 09:54:26 +00002783 private:
Vladimir Markobd785672018-05-03 17:09:09 +01002784 static constexpr size_t kFlagIsLive = HInstruction::kNumberOfGenericPackedBits;
David Brazdildee58d62016-04-07 09:54:26 +00002785 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2786 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2787 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
David Brazdildee58d62016-04-07 09:54:26 +00002788
David Brazdildee58d62016-04-07 09:54:26 +00002789 const uint32_t reg_number_;
David Brazdildee58d62016-04-07 09:54:26 +00002790};
2791
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002792// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002793// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002794// exit block.
Vladimir Markobd785672018-05-03 17:09:09 +01002795class HExit FINAL : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002796 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302797 explicit HExit(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002798 : HExpression(kExit, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302799 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002800
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002801 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002802
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002803 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002804
Artem Serovcced8ba2017-07-19 18:18:09 +01002805 protected:
2806 DEFAULT_COPY_CONSTRUCTOR(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002807};
2808
2809// Jumps from one block to another.
Vladimir Markobd785672018-05-03 17:09:09 +01002810class HGoto FINAL : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002811 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302812 explicit HGoto(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002813 : HExpression(kGoto, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302814 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002815
Artem Serovcced8ba2017-07-19 18:18:09 +01002816 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002817 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002818
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002819 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002820 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002821 }
2822
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002823 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002824
Artem Serovcced8ba2017-07-19 18:18:09 +01002825 protected:
2826 DEFAULT_COPY_CONSTRUCTOR(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002827};
2828
Roland Levillain9867bc72015-08-05 10:21:34 +01002829class HConstant : public HExpression<0> {
2830 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302831 explicit HConstant(InstructionKind kind, DataType::Type type, uint32_t dex_pc = kNoDexPc)
2832 : HExpression(kind, type, SideEffects::None(), dex_pc) {
2833 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002834
2835 bool CanBeMoved() const OVERRIDE { return true; }
2836
Roland Levillain1a653882016-03-18 18:05:57 +00002837 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002838 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002839 // Is this constant 0 in the arithmetic sense?
2840 virtual bool IsArithmeticZero() const { return false; }
2841 // Is this constant a 0-bit pattern?
2842 virtual bool IsZeroBitPattern() const { return false; }
2843 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002844 virtual bool IsOne() const { return false; }
2845
David Brazdil77a48ae2015-09-15 12:34:04 +00002846 virtual uint64_t GetValueAsUint64() const = 0;
2847
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002848 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002849
Artem Serovcced8ba2017-07-19 18:18:09 +01002850 protected:
2851 DEFAULT_COPY_CONSTRUCTOR(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002852};
2853
Vladimir Markofcb503c2016-05-18 12:48:17 +01002854class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002855 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002856 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002857 return true;
2858 }
2859
David Brazdil77a48ae2015-09-15 12:34:04 +00002860 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2861
Roland Levillain9867bc72015-08-05 10:21:34 +01002862 size_t ComputeHashCode() const OVERRIDE { return 0; }
2863
Roland Levillain1a653882016-03-18 18:05:57 +00002864 // The null constant representation is a 0-bit pattern.
2865 virtual bool IsZeroBitPattern() const { return true; }
2866
Roland Levillain9867bc72015-08-05 10:21:34 +01002867 DECLARE_INSTRUCTION(NullConstant);
2868
Artem Serovcced8ba2017-07-19 18:18:09 +01002869 protected:
2870 DEFAULT_COPY_CONSTRUCTOR(NullConstant);
2871
Roland Levillain9867bc72015-08-05 10:21:34 +01002872 private:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002873 explicit HNullConstant(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302874 : HConstant(kNullConstant, DataType::Type::kReference, dex_pc) {
2875 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002876
2877 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01002878};
2879
2880// Constants of the type int. Those can be from Dex instructions, or
2881// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002882class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002883 public:
2884 int32_t GetValue() const { return value_; }
2885
David Brazdil9f389d42015-10-01 14:32:56 +01002886 uint64_t GetValueAsUint64() const OVERRIDE {
2887 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2888 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002889
Vladimir Marko372f10e2016-05-17 16:30:10 +01002890 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002891 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002892 return other->AsIntConstant()->value_ == value_;
2893 }
2894
2895 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2896
2897 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002898 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2899 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002900 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2901
Roland Levillain1a653882016-03-18 18:05:57 +00002902 // Integer constants are used to encode Boolean values as well,
2903 // where 1 means true and 0 means false.
2904 bool IsTrue() const { return GetValue() == 1; }
2905 bool IsFalse() const { return GetValue() == 0; }
2906
Roland Levillain9867bc72015-08-05 10:21:34 +01002907 DECLARE_INSTRUCTION(IntConstant);
2908
Artem Serovcced8ba2017-07-19 18:18:09 +01002909 protected:
2910 DEFAULT_COPY_CONSTRUCTOR(IntConstant);
2911
Roland Levillain9867bc72015-08-05 10:21:34 +01002912 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002913 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302914 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc), value_(value) {
2915 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002916 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302917 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc),
2918 value_(value ? 1 : 0) {
2919 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002920
2921 const int32_t value_;
2922
2923 friend class HGraph;
2924 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2925 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Roland Levillain9867bc72015-08-05 10:21:34 +01002926};
2927
Vladimir Markofcb503c2016-05-18 12:48:17 +01002928class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002929 public:
2930 int64_t GetValue() const { return value_; }
2931
David Brazdil77a48ae2015-09-15 12:34:04 +00002932 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2933
Vladimir Marko372f10e2016-05-17 16:30:10 +01002934 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002935 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002936 return other->AsLongConstant()->value_ == value_;
2937 }
2938
2939 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2940
2941 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002942 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2943 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002944 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2945
2946 DECLARE_INSTRUCTION(LongConstant);
2947
Artem Serovcced8ba2017-07-19 18:18:09 +01002948 protected:
2949 DEFAULT_COPY_CONSTRUCTOR(LongConstant);
2950
Roland Levillain9867bc72015-08-05 10:21:34 +01002951 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002952 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302953 : HConstant(kLongConstant, DataType::Type::kInt64, dex_pc),
2954 value_(value) {
2955 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002956
2957 const int64_t value_;
2958
2959 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01002960};
Dave Allison20dfc792014-06-16 20:44:29 -07002961
Vladimir Markofcb503c2016-05-18 12:48:17 +01002962class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002963 public:
2964 float GetValue() const { return value_; }
2965
2966 uint64_t GetValueAsUint64() const OVERRIDE {
2967 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2968 }
2969
Vladimir Marko372f10e2016-05-17 16:30:10 +01002970 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002971 DCHECK(other->IsFloatConstant()) << other->DebugName();
2972 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2973 }
2974
2975 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2976
2977 bool IsMinusOne() const OVERRIDE {
2978 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2979 }
Roland Levillain1a653882016-03-18 18:05:57 +00002980 bool IsArithmeticZero() const OVERRIDE {
2981 return std::fpclassify(value_) == FP_ZERO;
2982 }
2983 bool IsArithmeticPositiveZero() const {
2984 return IsArithmeticZero() && !std::signbit(value_);
2985 }
2986 bool IsArithmeticNegativeZero() const {
2987 return IsArithmeticZero() && std::signbit(value_);
2988 }
2989 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002990 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002991 }
2992 bool IsOne() const OVERRIDE {
2993 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2994 }
2995 bool IsNaN() const {
2996 return std::isnan(value_);
2997 }
2998
2999 DECLARE_INSTRUCTION(FloatConstant);
3000
Artem Serovcced8ba2017-07-19 18:18:09 +01003001 protected:
3002 DEFAULT_COPY_CONSTRUCTOR(FloatConstant);
3003
Roland Levillain31dd3d62016-02-16 12:21:02 +00003004 private:
3005 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303006 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3007 value_(value) {
3008 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003009 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303010 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3011 value_(bit_cast<float, int32_t>(value)) {
3012 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003013
3014 const float value_;
3015
3016 // Only the SsaBuilder and HGraph can create floating-point constants.
3017 friend class SsaBuilder;
3018 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003019};
3020
Vladimir Markofcb503c2016-05-18 12:48:17 +01003021class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003022 public:
3023 double GetValue() const { return value_; }
3024
3025 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
3026
Vladimir Marko372f10e2016-05-17 16:30:10 +01003027 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003028 DCHECK(other->IsDoubleConstant()) << other->DebugName();
3029 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
3030 }
3031
3032 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
3033
3034 bool IsMinusOne() const OVERRIDE {
3035 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
3036 }
Roland Levillain1a653882016-03-18 18:05:57 +00003037 bool IsArithmeticZero() const OVERRIDE {
3038 return std::fpclassify(value_) == FP_ZERO;
3039 }
3040 bool IsArithmeticPositiveZero() const {
3041 return IsArithmeticZero() && !std::signbit(value_);
3042 }
3043 bool IsArithmeticNegativeZero() const {
3044 return IsArithmeticZero() && std::signbit(value_);
3045 }
3046 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003047 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00003048 }
3049 bool IsOne() const OVERRIDE {
3050 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3051 }
3052 bool IsNaN() const {
3053 return std::isnan(value_);
3054 }
3055
3056 DECLARE_INSTRUCTION(DoubleConstant);
3057
Artem Serovcced8ba2017-07-19 18:18:09 +01003058 protected:
3059 DEFAULT_COPY_CONSTRUCTOR(DoubleConstant);
3060
Roland Levillain31dd3d62016-02-16 12:21:02 +00003061 private:
3062 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303063 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3064 value_(value) {
3065 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003066 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303067 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3068 value_(bit_cast<double, int64_t>(value)) {
3069 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003070
3071 const double value_;
3072
3073 // Only the SsaBuilder and HGraph can create floating-point constants.
3074 friend class SsaBuilder;
3075 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003076};
3077
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003078// Conditional branch. A block ending with an HIf instruction must have
3079// two successors.
Vladimir Markobd785672018-05-03 17:09:09 +01003080class HIf FINAL : public HExpression<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003081 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003082 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003083 : HExpression(kIf, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003084 SetRawInputAt(0, input);
3085 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003086
Artem Serovcced8ba2017-07-19 18:18:09 +01003087 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003088 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003089
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003090 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003091 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003092 }
3093
3094 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003095 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003096 }
3097
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003098 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003099
Artem Serovcced8ba2017-07-19 18:18:09 +01003100 protected:
3101 DEFAULT_COPY_CONSTRUCTOR(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003102};
3103
David Brazdilfc6a86a2015-06-26 10:33:45 +00003104
3105// Abstract instruction which marks the beginning and/or end of a try block and
3106// links it to the respective exception handlers. Behaves the same as a Goto in
3107// non-exceptional control flow.
3108// Normal-flow successor is stored at index zero, exception handlers under
3109// higher indices in no particular order.
Vladimir Markobd785672018-05-03 17:09:09 +01003110class HTryBoundary FINAL : public HExpression<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003111 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003112 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01003113 kEntry,
3114 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003115 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01003116 };
3117
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003118 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003119 : HExpression(kTryBoundary, SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003120 SetPackedField<BoundaryKindField>(kind);
3121 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003122
3123 bool IsControlFlow() const OVERRIDE { return true; }
3124
3125 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01003126 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003127
David Brazdild26a4112015-11-10 11:07:31 +00003128 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
3129 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
3130 }
3131
David Brazdilfc6a86a2015-06-26 10:33:45 +00003132 // Returns whether `handler` is among its exception handlers (non-zero index
3133 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01003134 bool HasExceptionHandler(const HBasicBlock& handler) const {
3135 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00003136 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00003137 }
3138
3139 // If not present already, adds `handler` to its block's list of exception
3140 // handlers.
3141 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01003142 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003143 GetBlock()->AddSuccessor(handler);
3144 }
3145 }
3146
Vladimir Markoa1de9182016-02-25 11:37:38 +00003147 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
3148 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003149
David Brazdilffee3d32015-07-06 11:48:53 +01003150 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
3151
David Brazdilfc6a86a2015-06-26 10:33:45 +00003152 DECLARE_INSTRUCTION(TryBoundary);
3153
Artem Serovcced8ba2017-07-19 18:18:09 +01003154 protected:
3155 DEFAULT_COPY_CONSTRUCTOR(TryBoundary);
3156
David Brazdilfc6a86a2015-06-26 10:33:45 +00003157 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003158 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
3159 static constexpr size_t kFieldBoundaryKindSize =
3160 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
3161 static constexpr size_t kNumberOfTryBoundaryPackedBits =
3162 kFieldBoundaryKind + kFieldBoundaryKindSize;
3163 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
3164 "Too many packed fields.");
3165 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00003166};
3167
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003168// Deoptimize to interpreter, upon checking a condition.
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003169class HDeoptimize FINAL : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003170 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003171 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
3172 // across.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003173 HDeoptimize(ArenaAllocator* allocator,
3174 HInstruction* cond,
3175 DeoptimizationKind kind,
3176 uint32_t dex_pc)
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003177 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303178 kDeoptimize,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003179 SideEffects::All(),
3180 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003181 allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003182 /* number_of_inputs */ 1,
3183 kArenaAllocMisc) {
3184 SetPackedFlag<kFieldCanBeMoved>(false);
3185 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003186 SetRawInputAt(0, cond);
3187 }
3188
Artem Serovcced8ba2017-07-19 18:18:09 +01003189 bool IsClonable() const OVERRIDE { return true; }
3190
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003191 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
3192 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
3193 // instead of `guard`.
3194 // We set CanTriggerGC to prevent any intermediate address to be live
3195 // at the point of the `HDeoptimize`.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003196 HDeoptimize(ArenaAllocator* allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003197 HInstruction* cond,
3198 HInstruction* guard,
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003199 DeoptimizationKind kind,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003200 uint32_t dex_pc)
3201 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303202 kDeoptimize,
Vladimir Markobd785672018-05-03 17:09:09 +01003203 guard->GetType(),
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003204 SideEffects::CanTriggerGC(),
3205 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003206 allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003207 /* number_of_inputs */ 2,
3208 kArenaAllocMisc) {
3209 SetPackedFlag<kFieldCanBeMoved>(true);
3210 SetPackedField<DeoptimizeKindField>(kind);
3211 SetRawInputAt(0, cond);
3212 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003213 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003214
3215 bool CanBeMoved() const OVERRIDE { return GetPackedFlag<kFieldCanBeMoved>(); }
3216
3217 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
3218 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
3219 }
3220
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003221 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003222
Aart Bik75ff2c92018-04-21 01:28:11 +00003223 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003224
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003225 DeoptimizationKind GetDeoptimizationKind() const { return GetPackedField<DeoptimizeKindField>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003226
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003227 bool GuardsAnInput() const {
3228 return InputCount() == 2;
3229 }
3230
3231 HInstruction* GuardedInput() const {
3232 DCHECK(GuardsAnInput());
3233 return InputAt(1);
3234 }
3235
3236 void RemoveGuard() {
3237 RemoveInputAt(1);
3238 }
3239
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003240 DECLARE_INSTRUCTION(Deoptimize);
3241
Artem Serovcced8ba2017-07-19 18:18:09 +01003242 protected:
3243 DEFAULT_COPY_CONSTRUCTOR(Deoptimize);
3244
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003245 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003246 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3247 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3248 static constexpr size_t kFieldDeoptimizeKindSize =
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003249 MinimumBitsToStore(static_cast<size_t>(DeoptimizationKind::kLast));
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003250 static constexpr size_t kNumberOfDeoptimizePackedBits =
3251 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3252 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3253 "Too many packed fields.");
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003254 using DeoptimizeKindField =
3255 BitField<DeoptimizationKind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003256};
3257
Mingyao Yang063fc772016-08-02 11:02:54 -07003258// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3259// The compiled code checks this flag value in a guard before devirtualized call and
3260// if it's true, starts to do deoptimization.
3261// It has a 4-byte slot on stack.
3262// TODO: allocate a register for this flag.
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003263class HShouldDeoptimizeFlag FINAL : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003264 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003265 // CHA guards are only optimized in a separate pass and it has no side effects
3266 // with regard to other passes.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003267 HShouldDeoptimizeFlag(ArenaAllocator* allocator, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303268 : HVariableInputSizeInstruction(kShouldDeoptimizeFlag,
Vladimir Markobd785672018-05-03 17:09:09 +01003269 DataType::Type::kInt32,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303270 SideEffects::None(),
3271 dex_pc,
3272 allocator,
3273 0,
3274 kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003275 }
3276
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003277 // We do all CHA guard elimination/motion in a single pass, after which there is no
3278 // further guard elimination/motion since a guard might have been used for justification
3279 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3280 // to avoid other optimizations trying to move it.
Mingyao Yang063fc772016-08-02 11:02:54 -07003281 bool CanBeMoved() const OVERRIDE { return false; }
3282
3283 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3284
Artem Serovcced8ba2017-07-19 18:18:09 +01003285 protected:
3286 DEFAULT_COPY_CONSTRUCTOR(ShouldDeoptimizeFlag);
Mingyao Yang063fc772016-08-02 11:02:54 -07003287};
3288
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003289// Represents the ArtMethod that was passed as a first argument to
3290// the method. It is used by instructions that depend on it, like
3291// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003292class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003293 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003294 explicit HCurrentMethod(DataType::Type type, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303295 : HExpression(kCurrentMethod, type, SideEffects::None(), dex_pc) {
3296 }
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003297
3298 DECLARE_INSTRUCTION(CurrentMethod);
3299
Artem Serovcced8ba2017-07-19 18:18:09 +01003300 protected:
3301 DEFAULT_COPY_CONSTRUCTOR(CurrentMethod);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003302};
3303
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003304// Fetches an ArtMethod from the virtual table or the interface method table
3305// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003306class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003307 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003308 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003309 kVTable,
3310 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003311 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003312 };
3313 HClassTableGet(HInstruction* cls,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003314 DataType::Type type,
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003315 TableKind kind,
3316 size_t index,
3317 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303318 : HExpression(kClassTableGet, type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003319 index_(index) {
3320 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003321 SetRawInputAt(0, cls);
3322 }
3323
Artem Serovcced8ba2017-07-19 18:18:09 +01003324 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003325 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003326 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003327 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003328 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003329 }
3330
Vladimir Markoa1de9182016-02-25 11:37:38 +00003331 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003332 size_t GetIndex() const { return index_; }
3333
3334 DECLARE_INSTRUCTION(ClassTableGet);
3335
Artem Serovcced8ba2017-07-19 18:18:09 +01003336 protected:
3337 DEFAULT_COPY_CONSTRUCTOR(ClassTableGet);
3338
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003339 private:
Vladimir Markobd785672018-05-03 17:09:09 +01003340 static constexpr size_t kFieldTableKind = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003341 static constexpr size_t kFieldTableKindSize =
3342 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3343 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3344 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3345 "Too many packed fields.");
3346 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3347
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003348 // The index of the ArtMethod in the table.
3349 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003350};
3351
Mark Mendellfe57faa2015-09-18 09:26:15 -04003352// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3353// have one successor for each entry in the switch table, and the final successor
3354// will be the block containing the next Dex opcode.
Vladimir Markobd785672018-05-03 17:09:09 +01003355class HPackedSwitch FINAL : public HExpression<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003356 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003357 HPackedSwitch(int32_t start_value,
3358 uint32_t num_entries,
3359 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003360 uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003361 : HExpression(kPackedSwitch, SideEffects::None(), dex_pc),
Mark Mendellfe57faa2015-09-18 09:26:15 -04003362 start_value_(start_value),
3363 num_entries_(num_entries) {
3364 SetRawInputAt(0, input);
3365 }
3366
Artem Serovcced8ba2017-07-19 18:18:09 +01003367 bool IsClonable() const OVERRIDE { return true; }
3368
Mark Mendellfe57faa2015-09-18 09:26:15 -04003369 bool IsControlFlow() const OVERRIDE { return true; }
3370
3371 int32_t GetStartValue() const { return start_value_; }
3372
Vladimir Marko211c2112015-09-24 16:52:33 +01003373 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003374
3375 HBasicBlock* GetDefaultBlock() const {
3376 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003377 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003378 }
3379 DECLARE_INSTRUCTION(PackedSwitch);
3380
Artem Serovcced8ba2017-07-19 18:18:09 +01003381 protected:
3382 DEFAULT_COPY_CONSTRUCTOR(PackedSwitch);
3383
Mark Mendellfe57faa2015-09-18 09:26:15 -04003384 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003385 const int32_t start_value_;
3386 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003387};
3388
Roland Levillain88cb1752014-10-20 16:36:47 +01003389class HUnaryOperation : public HExpression<1> {
3390 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303391 HUnaryOperation(InstructionKind kind,
3392 DataType::Type result_type,
3393 HInstruction* input,
3394 uint32_t dex_pc = kNoDexPc)
3395 : HExpression(kind, result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003396 SetRawInputAt(0, input);
3397 }
3398
Artem Serovcced8ba2017-07-19 18:18:09 +01003399 // All of the UnaryOperation instructions are clonable.
3400 bool IsClonable() const OVERRIDE { return true; }
3401
Roland Levillain88cb1752014-10-20 16:36:47 +01003402 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003403 DataType::Type GetResultType() const { return GetType(); }
Roland Levillain88cb1752014-10-20 16:36:47 +01003404
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003405 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003406 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003407 return true;
3408 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003409
Roland Levillain31dd3d62016-02-16 12:21:02 +00003410 // Try to statically evaluate `this` and return a HConstant
3411 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003412 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003413 HConstant* TryStaticEvaluation() const;
3414
3415 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003416 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3417 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003418 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3419 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003420
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003421 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003422
Artem Serovcced8ba2017-07-19 18:18:09 +01003423 protected:
3424 DEFAULT_COPY_CONSTRUCTOR(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003425};
3426
Dave Allison20dfc792014-06-16 20:44:29 -07003427class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003428 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303429 HBinaryOperation(InstructionKind kind,
3430 DataType::Type result_type,
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003431 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003432 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003433 SideEffects side_effects = SideEffects::None(),
3434 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303435 : HExpression(kind, result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003436 SetRawInputAt(0, left);
3437 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003438 }
3439
Artem Serovcced8ba2017-07-19 18:18:09 +01003440 // All of the BinaryOperation instructions are clonable.
3441 bool IsClonable() const OVERRIDE { return true; }
3442
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003443 HInstruction* GetLeft() const { return InputAt(0); }
3444 HInstruction* GetRight() const { return InputAt(1); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003445 DataType::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003446
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003447 virtual bool IsCommutative() const { return false; }
3448
3449 // Put constant on the right.
3450 // Returns whether order is changed.
3451 bool OrderInputsWithConstantOnTheRight() {
3452 HInstruction* left = InputAt(0);
3453 HInstruction* right = InputAt(1);
3454 if (left->IsConstant() && !right->IsConstant()) {
3455 ReplaceInput(right, 0);
3456 ReplaceInput(left, 1);
3457 return true;
3458 }
3459 return false;
3460 }
3461
3462 // Order inputs by instruction id, but favor constant on the right side.
3463 // This helps GVN for commutative ops.
3464 void OrderInputs() {
3465 DCHECK(IsCommutative());
3466 HInstruction* left = InputAt(0);
3467 HInstruction* right = InputAt(1);
3468 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3469 return;
3470 }
3471 if (OrderInputsWithConstantOnTheRight()) {
3472 return;
3473 }
3474 // Order according to instruction id.
3475 if (left->GetId() > right->GetId()) {
3476 ReplaceInput(right, 0);
3477 ReplaceInput(left, 1);
3478 }
3479 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003480
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003481 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003482 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003483 return true;
3484 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003485
Roland Levillain31dd3d62016-02-16 12:21:02 +00003486 // Try to statically evaluate `this` and return a HConstant
3487 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003488 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003489 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003490
3491 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003492 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3493 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003494 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3495 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003496 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003497 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3498 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003499 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3500 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003501 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3502 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003503 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003504 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3505 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003506
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003507 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003508 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003509 HConstant* GetConstantRight() const;
3510
3511 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003512 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003513 HInstruction* GetLeastConstantLeft() const;
3514
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003515 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003516
Artem Serovcced8ba2017-07-19 18:18:09 +01003517 protected:
3518 DEFAULT_COPY_CONSTRUCTOR(BinaryOperation);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003519};
3520
Mark Mendellc4701932015-04-10 13:18:51 -04003521// The comparison bias applies for floating point operations and indicates how NaN
3522// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003523enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003524 kNoBias, // bias is not applicable (i.e. for long operation)
3525 kGtBias, // return 1 for NaN comparisons
3526 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003527 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003528};
3529
Roland Levillain31dd3d62016-02-16 12:21:02 +00003530std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3531
Dave Allison20dfc792014-06-16 20:44:29 -07003532class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003533 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303534 HCondition(InstructionKind kind,
3535 HInstruction* first,
3536 HInstruction* second,
3537 uint32_t dex_pc = kNoDexPc)
3538 : HBinaryOperation(kind,
3539 DataType::Type::kBool,
3540 first,
3541 second,
3542 SideEffects::None(),
3543 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003544 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3545 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003546
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003547 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003548 // `instruction`, and disregard moves in between.
3549 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003550
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003551 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003552
3553 virtual IfCondition GetCondition() const = 0;
3554
Mark Mendellc4701932015-04-10 13:18:51 -04003555 virtual IfCondition GetOppositeCondition() const = 0;
3556
Vladimir Markoa1de9182016-02-25 11:37:38 +00003557 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003558 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3559
Vladimir Markoa1de9182016-02-25 11:37:38 +00003560 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3561 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003562
Vladimir Marko372f10e2016-05-17 16:30:10 +01003563 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003564 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003565 }
3566
Roland Levillain4fa13f62015-07-06 18:11:54 +01003567 bool IsFPConditionTrueIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003568 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003569 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003570 if (if_cond == kCondNE) {
3571 return true;
3572 } else if (if_cond == kCondEQ) {
3573 return false;
3574 }
3575 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003576 }
3577
3578 bool IsFPConditionFalseIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003579 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003580 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003581 if (if_cond == kCondEQ) {
3582 return true;
3583 } else if (if_cond == kCondNE) {
3584 return false;
3585 }
3586 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003587 }
3588
Roland Levillain31dd3d62016-02-16 12:21:02 +00003589 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003590 // Needed if we merge a HCompare into a HCondition.
Vladimir Markobd785672018-05-03 17:09:09 +01003591 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003592 static constexpr size_t kFieldComparisonBiasSize =
3593 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3594 static constexpr size_t kNumberOfConditionPackedBits =
3595 kFieldComparisonBias + kFieldComparisonBiasSize;
3596 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3597 using ComparisonBiasField =
3598 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3599
Roland Levillain31dd3d62016-02-16 12:21:02 +00003600 template <typename T>
3601 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3602
3603 template <typename T>
3604 int32_t CompareFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003605 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003606 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3607 // Handle the bias.
3608 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3609 }
3610
3611 // Return an integer constant containing the result of a condition evaluated at compile time.
3612 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3613 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3614 }
3615
Artem Serovcced8ba2017-07-19 18:18:09 +01003616 DEFAULT_COPY_CONSTRUCTOR(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003617};
3618
3619// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003620class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003621 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003622 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303623 : HCondition(kEqual, first, second, dex_pc) {
3624 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003625
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003626 bool IsCommutative() const OVERRIDE { return true; }
3627
Vladimir Marko9e23df52015-11-10 17:14:35 +00003628 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3629 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003630 return MakeConstantCondition(true, GetDexPc());
3631 }
3632 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3633 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3634 }
3635 // In the following Evaluate methods, a HCompare instruction has
3636 // been merged into this HEqual instruction; evaluate it as
3637 // `Compare(x, y) == 0`.
3638 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3639 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3640 GetDexPc());
3641 }
3642 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3643 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3644 }
3645 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3646 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003647 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003648
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003649 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003650
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003651 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003652 return kCondEQ;
3653 }
3654
Mark Mendellc4701932015-04-10 13:18:51 -04003655 IfCondition GetOppositeCondition() const OVERRIDE {
3656 return kCondNE;
3657 }
3658
Artem Serovcced8ba2017-07-19 18:18:09 +01003659 protected:
3660 DEFAULT_COPY_CONSTRUCTOR(Equal);
3661
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003662 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003663 template <typename T> static bool Compute(T x, T y) { return x == y; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003664};
3665
Vladimir Markofcb503c2016-05-18 12:48:17 +01003666class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003667 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303668 HNotEqual(HInstruction* first, HInstruction* second,
3669 uint32_t dex_pc = kNoDexPc)
3670 : HCondition(kNotEqual, first, second, dex_pc) {
3671 }
Dave Allison20dfc792014-06-16 20:44:29 -07003672
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003673 bool IsCommutative() const OVERRIDE { return true; }
3674
Vladimir Marko9e23df52015-11-10 17:14:35 +00003675 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3676 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003677 return MakeConstantCondition(false, GetDexPc());
3678 }
3679 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3680 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3681 }
3682 // In the following Evaluate methods, a HCompare instruction has
3683 // been merged into this HNotEqual instruction; evaluate it as
3684 // `Compare(x, y) != 0`.
3685 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3686 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3687 }
3688 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3689 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3690 }
3691 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3692 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003693 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003694
Dave Allison20dfc792014-06-16 20:44:29 -07003695 DECLARE_INSTRUCTION(NotEqual);
3696
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003697 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003698 return kCondNE;
3699 }
3700
Mark Mendellc4701932015-04-10 13:18:51 -04003701 IfCondition GetOppositeCondition() const OVERRIDE {
3702 return kCondEQ;
3703 }
3704
Artem Serovcced8ba2017-07-19 18:18:09 +01003705 protected:
3706 DEFAULT_COPY_CONSTRUCTOR(NotEqual);
3707
Dave Allison20dfc792014-06-16 20:44:29 -07003708 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003709 template <typename T> static bool Compute(T x, T y) { return x != y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003710};
3711
Vladimir Markofcb503c2016-05-18 12:48:17 +01003712class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003713 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303714 HLessThan(HInstruction* first, HInstruction* second,
3715 uint32_t dex_pc = kNoDexPc)
3716 : HCondition(kLessThan, first, second, dex_pc) {
3717 }
Dave Allison20dfc792014-06-16 20:44:29 -07003718
Roland Levillain9867bc72015-08-05 10:21:34 +01003719 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003720 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003721 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003722 // In the following Evaluate methods, a HCompare instruction has
3723 // been merged into this HLessThan instruction; evaluate it as
3724 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003725 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003726 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3727 }
3728 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3729 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3730 }
3731 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3732 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003733 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003734
Dave Allison20dfc792014-06-16 20:44:29 -07003735 DECLARE_INSTRUCTION(LessThan);
3736
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003737 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003738 return kCondLT;
3739 }
3740
Mark Mendellc4701932015-04-10 13:18:51 -04003741 IfCondition GetOppositeCondition() const OVERRIDE {
3742 return kCondGE;
3743 }
3744
Artem Serovcced8ba2017-07-19 18:18:09 +01003745 protected:
3746 DEFAULT_COPY_CONSTRUCTOR(LessThan);
3747
Dave Allison20dfc792014-06-16 20:44:29 -07003748 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003749 template <typename T> static bool Compute(T x, T y) { return x < y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003750};
3751
Vladimir Markofcb503c2016-05-18 12:48:17 +01003752class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003753 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303754 HLessThanOrEqual(HInstruction* first, HInstruction* second,
3755 uint32_t dex_pc = kNoDexPc)
3756 : HCondition(kLessThanOrEqual, first, second, dex_pc) {
3757 }
Dave Allison20dfc792014-06-16 20:44:29 -07003758
Roland Levillain9867bc72015-08-05 10:21:34 +01003759 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003760 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003761 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003762 // In the following Evaluate methods, a HCompare instruction has
3763 // been merged into this HLessThanOrEqual instruction; evaluate it as
3764 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003765 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003766 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3767 }
3768 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3769 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3770 }
3771 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3772 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003773 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003774
Dave Allison20dfc792014-06-16 20:44:29 -07003775 DECLARE_INSTRUCTION(LessThanOrEqual);
3776
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003777 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003778 return kCondLE;
3779 }
3780
Mark Mendellc4701932015-04-10 13:18:51 -04003781 IfCondition GetOppositeCondition() const OVERRIDE {
3782 return kCondGT;
3783 }
3784
Artem Serovcced8ba2017-07-19 18:18:09 +01003785 protected:
3786 DEFAULT_COPY_CONSTRUCTOR(LessThanOrEqual);
3787
Dave Allison20dfc792014-06-16 20:44:29 -07003788 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003789 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003790};
3791
Vladimir Markofcb503c2016-05-18 12:48:17 +01003792class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003793 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003794 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303795 : HCondition(kGreaterThan, first, second, dex_pc) {
3796 }
Dave Allison20dfc792014-06-16 20:44:29 -07003797
Roland Levillain9867bc72015-08-05 10:21:34 +01003798 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003799 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003800 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003801 // In the following Evaluate methods, a HCompare instruction has
3802 // been merged into this HGreaterThan instruction; evaluate it as
3803 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003804 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003805 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3806 }
3807 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3808 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3809 }
3810 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3811 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003812 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003813
Dave Allison20dfc792014-06-16 20:44:29 -07003814 DECLARE_INSTRUCTION(GreaterThan);
3815
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003816 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003817 return kCondGT;
3818 }
3819
Mark Mendellc4701932015-04-10 13:18:51 -04003820 IfCondition GetOppositeCondition() const OVERRIDE {
3821 return kCondLE;
3822 }
3823
Artem Serovcced8ba2017-07-19 18:18:09 +01003824 protected:
3825 DEFAULT_COPY_CONSTRUCTOR(GreaterThan);
3826
Dave Allison20dfc792014-06-16 20:44:29 -07003827 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003828 template <typename T> static bool Compute(T x, T y) { return x > y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003829};
3830
Vladimir Markofcb503c2016-05-18 12:48:17 +01003831class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003832 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003833 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303834 : HCondition(kGreaterThanOrEqual, first, second, dex_pc) {
3835 }
Dave Allison20dfc792014-06-16 20:44:29 -07003836
Roland Levillain9867bc72015-08-05 10:21:34 +01003837 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003838 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003839 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003840 // In the following Evaluate methods, a HCompare instruction has
3841 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3842 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003843 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003844 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3845 }
3846 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3847 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3848 }
3849 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3850 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003851 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003852
Dave Allison20dfc792014-06-16 20:44:29 -07003853 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3854
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003855 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003856 return kCondGE;
3857 }
3858
Mark Mendellc4701932015-04-10 13:18:51 -04003859 IfCondition GetOppositeCondition() const OVERRIDE {
3860 return kCondLT;
3861 }
3862
Artem Serovcced8ba2017-07-19 18:18:09 +01003863 protected:
3864 DEFAULT_COPY_CONSTRUCTOR(GreaterThanOrEqual);
3865
Dave Allison20dfc792014-06-16 20:44:29 -07003866 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003867 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003868};
3869
Vladimir Markofcb503c2016-05-18 12:48:17 +01003870class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003871 public:
3872 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303873 : HCondition(kBelow, first, second, dex_pc) {
3874 }
Aart Bike9f37602015-10-09 11:15:55 -07003875
3876 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003877 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003878 }
3879 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003880 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3881 }
3882 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3883 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3884 LOG(FATAL) << DebugName() << " is not defined for float values";
3885 UNREACHABLE();
3886 }
3887 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3888 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3889 LOG(FATAL) << DebugName() << " is not defined for double values";
3890 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003891 }
3892
3893 DECLARE_INSTRUCTION(Below);
3894
3895 IfCondition GetCondition() const OVERRIDE {
3896 return kCondB;
3897 }
3898
3899 IfCondition GetOppositeCondition() const OVERRIDE {
3900 return kCondAE;
3901 }
3902
Artem Serovcced8ba2017-07-19 18:18:09 +01003903 protected:
3904 DEFAULT_COPY_CONSTRUCTOR(Below);
3905
Aart Bike9f37602015-10-09 11:15:55 -07003906 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003907 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003908 return MakeUnsigned(x) < MakeUnsigned(y);
3909 }
Aart Bike9f37602015-10-09 11:15:55 -07003910};
3911
Vladimir Markofcb503c2016-05-18 12:48:17 +01003912class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003913 public:
3914 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303915 : HCondition(kBelowOrEqual, first, second, dex_pc) {
3916 }
Aart Bike9f37602015-10-09 11:15:55 -07003917
3918 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003919 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003920 }
3921 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003922 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3923 }
3924 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3925 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3926 LOG(FATAL) << DebugName() << " is not defined for float values";
3927 UNREACHABLE();
3928 }
3929 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3930 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3931 LOG(FATAL) << DebugName() << " is not defined for double values";
3932 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003933 }
3934
3935 DECLARE_INSTRUCTION(BelowOrEqual);
3936
3937 IfCondition GetCondition() const OVERRIDE {
3938 return kCondBE;
3939 }
3940
3941 IfCondition GetOppositeCondition() const OVERRIDE {
3942 return kCondA;
3943 }
3944
Artem Serovcced8ba2017-07-19 18:18:09 +01003945 protected:
3946 DEFAULT_COPY_CONSTRUCTOR(BelowOrEqual);
3947
Aart Bike9f37602015-10-09 11:15:55 -07003948 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003949 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003950 return MakeUnsigned(x) <= MakeUnsigned(y);
3951 }
Aart Bike9f37602015-10-09 11:15:55 -07003952};
3953
Vladimir Markofcb503c2016-05-18 12:48:17 +01003954class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003955 public:
3956 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303957 : HCondition(kAbove, first, second, dex_pc) {
3958 }
Aart Bike9f37602015-10-09 11:15:55 -07003959
3960 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003961 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003962 }
3963 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003964 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3965 }
3966 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3967 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3968 LOG(FATAL) << DebugName() << " is not defined for float values";
3969 UNREACHABLE();
3970 }
3971 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3972 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3973 LOG(FATAL) << DebugName() << " is not defined for double values";
3974 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003975 }
3976
3977 DECLARE_INSTRUCTION(Above);
3978
3979 IfCondition GetCondition() const OVERRIDE {
3980 return kCondA;
3981 }
3982
3983 IfCondition GetOppositeCondition() const OVERRIDE {
3984 return kCondBE;
3985 }
3986
Artem Serovcced8ba2017-07-19 18:18:09 +01003987 protected:
3988 DEFAULT_COPY_CONSTRUCTOR(Above);
3989
Aart Bike9f37602015-10-09 11:15:55 -07003990 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003991 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003992 return MakeUnsigned(x) > MakeUnsigned(y);
3993 }
Aart Bike9f37602015-10-09 11:15:55 -07003994};
3995
Vladimir Markofcb503c2016-05-18 12:48:17 +01003996class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003997 public:
3998 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303999 : HCondition(kAboveOrEqual, first, second, dex_pc) {
4000 }
Aart Bike9f37602015-10-09 11:15:55 -07004001
4002 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004003 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004004 }
4005 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004006 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4007 }
4008 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4009 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4010 LOG(FATAL) << DebugName() << " is not defined for float values";
4011 UNREACHABLE();
4012 }
4013 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4014 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4015 LOG(FATAL) << DebugName() << " is not defined for double values";
4016 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004017 }
4018
4019 DECLARE_INSTRUCTION(AboveOrEqual);
4020
4021 IfCondition GetCondition() const OVERRIDE {
4022 return kCondAE;
4023 }
4024
4025 IfCondition GetOppositeCondition() const OVERRIDE {
4026 return kCondB;
4027 }
4028
Artem Serovcced8ba2017-07-19 18:18:09 +01004029 protected:
4030 DEFAULT_COPY_CONSTRUCTOR(AboveOrEqual);
4031
Aart Bike9f37602015-10-09 11:15:55 -07004032 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004033 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004034 return MakeUnsigned(x) >= MakeUnsigned(y);
4035 }
Aart Bike9f37602015-10-09 11:15:55 -07004036};
Dave Allison20dfc792014-06-16 20:44:29 -07004037
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004038// Instruction to check how two inputs compare to each other.
4039// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01004040class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004041 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00004042 // Note that `comparison_type` is the type of comparison performed
4043 // between the comparison's inputs, not the type of the instantiated
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004044 // HCompare instruction (which is always DataType::Type::kInt).
4045 HCompare(DataType::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004046 HInstruction* first,
4047 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04004048 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004049 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304050 : HBinaryOperation(kCompare,
4051 DataType::Type::kInt32,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004052 first,
4053 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00004054 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004055 dex_pc) {
4056 SetPackedField<ComparisonBiasField>(bias);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004057 DCHECK_EQ(comparison_type, DataType::Kind(first->GetType()));
4058 DCHECK_EQ(comparison_type, DataType::Kind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004059 }
4060
Roland Levillain9867bc72015-08-05 10:21:34 +01004061 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004062 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
4063
4064 template <typename T>
4065 int32_t ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004066 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004067 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
4068 // Handle the bias.
4069 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
4070 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004071
Roland Levillain9867bc72015-08-05 10:21:34 +01004072 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004073 // Note that there is no "cmp-int" Dex instruction so we shouldn't
4074 // reach this code path when processing a freshly built HIR
4075 // graph. However HCompare integer instructions can be synthesized
4076 // by the instruction simplifier to implement IntegerCompare and
4077 // IntegerSignum intrinsics, so we have to handle this case.
4078 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004079 }
4080 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004081 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4082 }
4083 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4084 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4085 }
4086 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4087 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01004088 }
4089
Vladimir Marko372f10e2016-05-17 16:30:10 +01004090 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004091 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00004092 }
4093
Vladimir Markoa1de9182016-02-25 11:37:38 +00004094 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04004095
Roland Levillain31dd3d62016-02-16 12:21:02 +00004096 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00004097 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00004098 bool IsGtBias() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004099 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004100 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00004101 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004102
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004103 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type type ATTRIBUTE_UNUSED) {
Roland Levillain1693a1f2016-03-15 14:57:31 +00004104 // Comparisons do not require a runtime call in any back end.
4105 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004106 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004107
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004108 DECLARE_INSTRUCTION(Compare);
4109
Roland Levillain31dd3d62016-02-16 12:21:02 +00004110 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01004111 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004112 static constexpr size_t kFieldComparisonBiasSize =
4113 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
4114 static constexpr size_t kNumberOfComparePackedBits =
4115 kFieldComparisonBias + kFieldComparisonBiasSize;
4116 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4117 using ComparisonBiasField =
4118 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
4119
Roland Levillain31dd3d62016-02-16 12:21:02 +00004120 // Return an integer constant containing the result of a comparison evaluated at compile time.
4121 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
4122 DCHECK(value == -1 || value == 0 || value == 1) << value;
4123 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
4124 }
4125
Artem Serovcced8ba2017-07-19 18:18:09 +01004126 DEFAULT_COPY_CONSTRUCTOR(Compare);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004127};
4128
Nicolas Geoffray2b615ba2017-01-06 14:40:07 +00004129class HNewInstance FINAL : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00004130 public:
4131 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00004132 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004133 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00004134 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00004135 bool finalizable,
4136 QuickEntrypointEnum entrypoint)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304137 : HExpression(kNewInstance,
4138 DataType::Type::kReference,
4139 SideEffects::CanTriggerGC(),
4140 dex_pc),
David Brazdil6de19382016-01-08 17:37:10 +00004141 type_index_(type_index),
4142 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00004143 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004144 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00004145 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00004146 }
4147
Artem Serovcced8ba2017-07-19 18:18:09 +01004148 bool IsClonable() const OVERRIDE { return true; }
4149
Andreas Gampea5b09a62016-11-17 15:21:22 -08004150 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00004151 const DexFile& GetDexFile() const { return dex_file_; }
4152
4153 // Calls runtime so needs an environment.
4154 bool NeedsEnvironment() const OVERRIDE { return true; }
4155
Mingyao Yang062157f2016-03-02 10:15:36 -08004156 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
4157 bool CanThrow() const OVERRIDE { return true; }
4158
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004159 bool NeedsChecks() const {
4160 return entrypoint_ == kQuickAllocObjectWithChecks;
4161 }
David Brazdil6de19382016-01-08 17:37:10 +00004162
Vladimir Markoa1de9182016-02-25 11:37:38 +00004163 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00004164
4165 bool CanBeNull() const OVERRIDE { return false; }
4166
4167 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4168
4169 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
4170 entrypoint_ = entrypoint;
4171 }
4172
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004173 HLoadClass* GetLoadClass() const {
4174 HInstruction* input = InputAt(0);
4175 if (input->IsClinitCheck()) {
4176 input = input->InputAt(0);
4177 }
4178 DCHECK(input->IsLoadClass());
4179 return input->AsLoadClass();
4180 }
4181
David Brazdil6de19382016-01-08 17:37:10 +00004182 bool IsStringAlloc() const;
4183
4184 DECLARE_INSTRUCTION(NewInstance);
4185
Artem Serovcced8ba2017-07-19 18:18:09 +01004186 protected:
4187 DEFAULT_COPY_CONSTRUCTOR(NewInstance);
4188
David Brazdil6de19382016-01-08 17:37:10 +00004189 private:
Vladimir Markobd785672018-05-03 17:09:09 +01004190 static constexpr size_t kFlagFinalizable = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004191 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
4192 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
4193 "Too many packed fields.");
4194
Andreas Gampea5b09a62016-11-17 15:21:22 -08004195 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00004196 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00004197 QuickEntrypointEnum entrypoint_;
David Brazdil6de19382016-01-08 17:37:10 +00004198};
4199
Agi Csaki05f20562015-08-19 14:58:14 -07004200enum IntrinsicNeedsEnvironmentOrCache {
4201 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
4202 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07004203};
4204
Aart Bik5d75afe2015-12-14 11:57:01 -08004205enum IntrinsicSideEffects {
4206 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
4207 kReadSideEffects, // Intrinsic may read heap memory.
4208 kWriteSideEffects, // Intrinsic may write heap memory.
4209 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
4210};
4211
4212enum IntrinsicExceptions {
4213 kNoThrow, // Intrinsic does not throw any exceptions.
4214 kCanThrow // Intrinsic may throw exceptions.
4215};
4216
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004217class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004218 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004219 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004220
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01004221 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004222 SetRawInputAt(index, argument);
4223 }
4224
Roland Levillain3e3d7332015-04-28 11:00:54 +01004225 // Return the number of arguments. This number can be lower than
4226 // the number of inputs returned by InputCount(), as some invoke
4227 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
4228 // inputs at the end of their list of inputs.
4229 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
4230
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004231 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4232
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004233 InvokeType GetInvokeType() const {
4234 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004235 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004236
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01004237 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004238 return intrinsic_;
4239 }
4240
Aart Bik5d75afe2015-12-14 11:57:01 -08004241 void SetIntrinsic(Intrinsics intrinsic,
4242 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
4243 IntrinsicSideEffects side_effects,
4244 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004245
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004246 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00004247 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004248 }
4249
Aart Bikff7d89c2016-11-07 08:49:28 -08004250 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4251
Vladimir Markoa1de9182016-02-25 11:37:38 +00004252 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004253
Aart Bika8b8e9b2018-01-09 11:01:02 -08004254 void SetAlwaysThrows(bool always_throws) { SetPackedFlag<kFlagAlwaysThrows>(always_throws); }
4255
4256 bool AlwaysThrows() const OVERRIDE { return GetPackedFlag<kFlagAlwaysThrows>(); }
4257
Aart Bik71bf7b42016-11-16 10:17:46 -08004258 bool CanBeMoved() const OVERRIDE { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004259
Vladimir Marko372f10e2016-05-17 16:30:10 +01004260 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08004261 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4262 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004263
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004264 uint32_t* GetIntrinsicOptimizations() {
4265 return &intrinsic_optimizations_;
4266 }
4267
4268 const uint32_t* GetIntrinsicOptimizations() const {
4269 return &intrinsic_optimizations_;
4270 }
4271
4272 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4273
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004274 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004275 void SetResolvedMethod(ArtMethod* method) { resolved_method_ = method; }
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004276
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004277 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004278
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004279 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004280 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4281 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004282 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
Vladimir Markobd785672018-05-03 17:09:09 +01004283 static constexpr size_t kFlagCanThrow = kFieldInvokeType + kFieldInvokeTypeSize;
Aart Bika8b8e9b2018-01-09 11:01:02 -08004284 static constexpr size_t kFlagAlwaysThrows = kFlagCanThrow + 1;
4285 static constexpr size_t kNumberOfInvokePackedBits = kFlagAlwaysThrows + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004286 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004287 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004288
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304289 HInvoke(InstructionKind kind,
4290 ArenaAllocator* allocator,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004291 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004292 uint32_t number_of_other_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004293 DataType::Type return_type,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004294 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004295 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004296 ArtMethod* resolved_method,
4297 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004298 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304299 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01004300 return_type,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004301 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4302 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004303 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004304 number_of_arguments + number_of_other_inputs,
4305 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004306 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004307 resolved_method_(resolved_method),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004308 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07004309 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004310 intrinsic_optimizations_(0) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004311 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004312 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004313 }
4314
Artem Serovcced8ba2017-07-19 18:18:09 +01004315 DEFAULT_COPY_CONSTRUCTOR(Invoke);
4316
Roland Levillain3e3d7332015-04-28 11:00:54 +01004317 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004318 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004319 const uint32_t dex_method_index_;
4320 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004321
4322 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4323 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004324};
4325
Vladimir Markofcb503c2016-05-18 12:48:17 +01004326class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004327 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004328 HInvokeUnresolved(ArenaAllocator* allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004329 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004330 DataType::Type return_type,
Calin Juravle175dc732015-08-25 15:42:32 +01004331 uint32_t dex_pc,
4332 uint32_t dex_method_index,
4333 InvokeType invoke_type)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304334 : HInvoke(kInvokeUnresolved,
4335 allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004336 number_of_arguments,
4337 0u /* number_of_other_inputs */,
4338 return_type,
4339 dex_pc,
4340 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004341 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01004342 invoke_type) {
4343 }
4344
Artem Serovcced8ba2017-07-19 18:18:09 +01004345 bool IsClonable() const OVERRIDE { return true; }
4346
Calin Juravle175dc732015-08-25 15:42:32 +01004347 DECLARE_INSTRUCTION(InvokeUnresolved);
4348
Artem Serovcced8ba2017-07-19 18:18:09 +01004349 protected:
4350 DEFAULT_COPY_CONSTRUCTOR(InvokeUnresolved);
Calin Juravle175dc732015-08-25 15:42:32 +01004351};
4352
Orion Hodsonac141392017-01-13 11:53:47 +00004353class HInvokePolymorphic FINAL : public HInvoke {
4354 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004355 HInvokePolymorphic(ArenaAllocator* allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004356 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004357 DataType::Type return_type,
Orion Hodsonac141392017-01-13 11:53:47 +00004358 uint32_t dex_pc,
4359 uint32_t dex_method_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304360 : HInvoke(kInvokePolymorphic,
4361 allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004362 number_of_arguments,
4363 0u /* number_of_other_inputs */,
4364 return_type,
4365 dex_pc,
4366 dex_method_index,
4367 nullptr,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304368 kVirtual) {
4369 }
Orion Hodsonac141392017-01-13 11:53:47 +00004370
Artem Serovcced8ba2017-07-19 18:18:09 +01004371 bool IsClonable() const OVERRIDE { return true; }
4372
Orion Hodsonac141392017-01-13 11:53:47 +00004373 DECLARE_INSTRUCTION(InvokePolymorphic);
4374
Artem Serovcced8ba2017-07-19 18:18:09 +01004375 protected:
4376 DEFAULT_COPY_CONSTRUCTOR(InvokePolymorphic);
Orion Hodsonac141392017-01-13 11:53:47 +00004377};
4378
Vladimir Markofcb503c2016-05-18 12:48:17 +01004379class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004380 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004381 // Requirements of this method call regarding the class
4382 // initialization (clinit) check of its declaring class.
4383 enum class ClinitCheckRequirement {
4384 kNone, // Class already initialized.
4385 kExplicit, // Static call having explicit clinit check as last input.
4386 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004387 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004388 };
4389
Vladimir Marko58155012015-08-19 12:49:41 +00004390 // Determines how to load the target ArtMethod*.
4391 enum class MethodLoadKind {
4392 // Use a String init ArtMethod* loaded from Thread entrypoints.
4393 kStringInit,
4394
4395 // Use the method's own ArtMethod* loaded by the register allocator.
4396 kRecursive,
4397
Vladimir Marko65979462017-05-19 17:25:12 +01004398 // Use PC-relative boot image ArtMethod* address that will be known at link time.
4399 // Used for boot image methods referenced by boot image code.
4400 kBootImageLinkTimePcRelative,
4401
Vladimir Marko58155012015-08-19 12:49:41 +00004402 // Use ArtMethod* at a known address, embed the direct address in the code.
4403 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
4404 kDirectAddress,
4405
Vladimir Markob066d432018-01-03 13:14:37 +00004406 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
4407 // Used for app->boot calls with relocatable image.
4408 kBootImageRelRo,
4409
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004410 // Load from an entry in the .bss section using a PC-relative load.
4411 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
4412 kBssEntry,
Vladimir Marko58155012015-08-19 12:49:41 +00004413
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004414 // Make a runtime call to resolve and call the method. This is the last-resort-kind
4415 // used when other kinds are unimplemented on a particular architecture.
4416 kRuntimeCall,
Vladimir Marko58155012015-08-19 12:49:41 +00004417 };
4418
4419 // Determines the location of the code pointer.
4420 enum class CodePtrLocation {
4421 // Recursive call, use local PC-relative call instruction.
4422 kCallSelf,
4423
Vladimir Marko58155012015-08-19 12:49:41 +00004424 // Use code pointer from the ArtMethod*.
4425 // Used when we don't know the target code. This is also the last-resort-kind used when
4426 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4427 kCallArtMethod,
4428 };
4429
4430 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004431 MethodLoadKind method_load_kind;
4432 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004433 // The method load data holds
4434 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4435 // Note that there are multiple string init methods, each having its own offset.
4436 // - the method address for kDirectAddress
Vladimir Markodc151b22015-10-15 18:02:30 +01004437 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004438 };
4439
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004440 HInvokeStaticOrDirect(ArenaAllocator* allocator,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004441 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004442 DataType::Type return_type,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004443 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004444 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004445 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004446 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004447 InvokeType invoke_type,
4448 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004449 ClinitCheckRequirement clinit_check_requirement)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304450 : HInvoke(kInvokeStaticOrDirect,
4451 allocator,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004452 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004453 // There is potentially one extra argument for the HCurrentMethod node, and
4454 // potentially one other if the clinit check is explicit, and potentially
4455 // one other if the method is a string factory.
4456 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004457 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004458 return_type,
4459 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004460 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004461 resolved_method,
4462 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004463 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004464 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004465 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4466 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004467
Artem Serovcced8ba2017-07-19 18:18:09 +01004468 bool IsClonable() const OVERRIDE { return true; }
4469
Vladimir Markodc151b22015-10-15 18:02:30 +01004470 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004471 bool had_current_method_input = HasCurrentMethodInput();
4472 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4473
4474 // Using the current method is the default and once we find a better
4475 // method load kind, we should not go back to using the current method.
4476 DCHECK(had_current_method_input || !needs_current_method_input);
4477
4478 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004479 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4480 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004481 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004482 dispatch_info_ = dispatch_info;
4483 }
4484
Aart Bik6daebeb2017-04-03 14:35:41 -07004485 DispatchInfo GetDispatchInfo() const {
4486 return dispatch_info_;
4487 }
4488
Vladimir Markoc53c0792015-11-19 15:48:33 +00004489 void AddSpecialInput(HInstruction* input) {
4490 // We allow only one special input.
4491 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4492 DCHECK(InputCount() == GetSpecialInputIndex() ||
4493 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4494 InsertInputAt(GetSpecialInputIndex(), input);
4495 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004496
Vladimir Marko372f10e2016-05-17 16:30:10 +01004497 using HInstruction::GetInputRecords; // Keep the const version visible.
4498 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
4499 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4500 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4501 DCHECK(!input_records.empty());
4502 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4503 HInstruction* last_input = input_records.back().GetInstruction();
4504 // Note: `last_input` may be null during arguments setup.
4505 if (last_input != nullptr) {
4506 // `last_input` is the last input of a static invoke marked as having
4507 // an explicit clinit check. It must either be:
4508 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4509 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4510 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4511 }
4512 }
4513 return input_records;
4514 }
4515
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004516 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004517 // We access the method via the dex cache so we can't do an implicit null check.
4518 // TODO: for intrinsics we can generate implicit null checks.
4519 return false;
4520 }
4521
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004522 bool CanBeNull() const OVERRIDE {
Vladimir Markobd785672018-05-03 17:09:09 +01004523 return GetType() == DataType::Type::kReference && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004524 }
4525
Vladimir Markoc53c0792015-11-19 15:48:33 +00004526 // Get the index of the special input, if any.
4527 //
David Brazdil6de19382016-01-08 17:37:10 +00004528 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4529 // method pointer; otherwise there may be one platform-specific special input,
4530 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004531 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004532 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004533
Vladimir Marko58155012015-08-19 12:49:41 +00004534 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4535 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4536 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004537 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004538 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004539 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko65979462017-05-19 17:25:12 +01004540 bool HasPcRelativeMethodLoadKind() const {
4541 return GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markob066d432018-01-03 13:14:37 +00004542 GetMethodLoadKind() == MethodLoadKind::kBootImageRelRo ||
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004543 GetMethodLoadKind() == MethodLoadKind::kBssEntry;
Vladimir Marko65979462017-05-19 17:25:12 +01004544 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004545 bool HasCurrentMethodInput() const {
4546 // This function can be called only after the invoke has been fully initialized by the builder.
4547 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004548 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004549 return true;
4550 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004551 DCHECK(InputCount() == GetSpecialInputIndex() ||
4552 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004553 return false;
4554 }
4555 }
Vladimir Marko58155012015-08-19 12:49:41 +00004556
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004557 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004558 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004559 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004560 }
4561
4562 uint64_t GetMethodAddress() const {
4563 DCHECK(HasMethodAddress());
4564 return dispatch_info_.method_load_data;
4565 }
4566
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004567 const DexFile& GetDexFileForPcRelativeDexCache() const;
4568
Vladimir Markoa1de9182016-02-25 11:37:38 +00004569 ClinitCheckRequirement GetClinitCheckRequirement() const {
4570 return GetPackedField<ClinitCheckRequirementField>();
4571 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004572
Roland Levillain4c0eb422015-04-24 16:43:49 +01004573 // Is this instruction a call to a static method?
4574 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004575 return GetInvokeType() == kStatic;
4576 }
4577
4578 MethodReference GetTargetMethod() const {
4579 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004580 }
4581
Vladimir Markofbb184a2015-11-13 14:47:00 +00004582 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4583 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4584 // instruction; only relevant for static calls with explicit clinit check.
4585 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004586 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004587 size_t last_input_index = inputs_.size() - 1u;
4588 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004589 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004590 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004591 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004592 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004593 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004594 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004595 }
4596
4597 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004598 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004599 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004600 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004601 }
4602
4603 // Is this a call to a static method whose declaring class has an
4604 // implicit intialization check requirement?
4605 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004606 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004607 }
4608
Vladimir Markob554b5a2015-11-06 12:57:55 +00004609 // Does this method load kind need the current method as an input?
4610 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004611 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kRuntimeCall;
Vladimir Markob554b5a2015-11-06 12:57:55 +00004612 }
4613
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004614 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004615
Artem Serovcced8ba2017-07-19 18:18:09 +01004616 protected:
4617 DEFAULT_COPY_CONSTRUCTOR(InvokeStaticOrDirect);
4618
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004619 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004620 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004621 static constexpr size_t kFieldClinitCheckRequirementSize =
4622 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4623 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4624 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4625 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4626 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004627 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4628 kFieldClinitCheckRequirement,
4629 kFieldClinitCheckRequirementSize>;
4630
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004631 // Cached values of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffrayea179f42018-02-08 22:30:18 +00004632 const MethodReference target_method_;
Vladimir Marko58155012015-08-19 12:49:41 +00004633 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004634};
Vladimir Markof64242a2015-12-01 14:58:23 +00004635std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004636std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004637
Vladimir Markofcb503c2016-05-18 12:48:17 +01004638class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004639 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004640 HInvokeVirtual(ArenaAllocator* allocator,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004641 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004642 DataType::Type return_type,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004643 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004644 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004645 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004646 uint32_t vtable_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304647 : HInvoke(kInvokeVirtual,
4648 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004649 number_of_arguments,
4650 0u,
4651 return_type,
4652 dex_pc,
4653 dex_method_index,
4654 resolved_method,
4655 kVirtual),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304656 vtable_index_(vtable_index) {
4657 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004658
Artem Serovcced8ba2017-07-19 18:18:09 +01004659 bool IsClonable() const OVERRIDE { return true; }
4660
Aart Bik71bf7b42016-11-16 10:17:46 -08004661 bool CanBeNull() const OVERRIDE {
4662 switch (GetIntrinsic()) {
4663 case Intrinsics::kThreadCurrentThread:
4664 case Intrinsics::kStringBufferAppend:
4665 case Intrinsics::kStringBufferToString:
4666 case Intrinsics::kStringBuilderAppend:
4667 case Intrinsics::kStringBuilderToString:
4668 return false;
4669 default:
4670 return HInvoke::CanBeNull();
4671 }
4672 }
4673
Calin Juravle641547a2015-04-21 22:08:51 +01004674 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004675 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004676 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004677 }
4678
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004679 uint32_t GetVTableIndex() const { return vtable_index_; }
4680
4681 DECLARE_INSTRUCTION(InvokeVirtual);
4682
Artem Serovcced8ba2017-07-19 18:18:09 +01004683 protected:
4684 DEFAULT_COPY_CONSTRUCTOR(InvokeVirtual);
4685
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004686 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004687 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004688 const uint32_t vtable_index_;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004689};
4690
Vladimir Markofcb503c2016-05-18 12:48:17 +01004691class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004692 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004693 HInvokeInterface(ArenaAllocator* allocator,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004694 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004695 DataType::Type return_type,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004696 uint32_t dex_pc,
4697 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004698 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004699 uint32_t imt_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304700 : HInvoke(kInvokeInterface,
4701 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004702 number_of_arguments,
4703 0u,
4704 return_type,
4705 dex_pc,
4706 dex_method_index,
4707 resolved_method,
4708 kInterface),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304709 imt_index_(imt_index) {
4710 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004711
Artem Serovcced8ba2017-07-19 18:18:09 +01004712 bool IsClonable() const OVERRIDE { return true; }
4713
Calin Juravle641547a2015-04-21 22:08:51 +01004714 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004715 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004716 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004717 }
4718
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004719 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
4720 // The assembly stub currently needs it.
4721 return true;
4722 }
4723
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004724 uint32_t GetImtIndex() const { return imt_index_; }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004725
4726 DECLARE_INSTRUCTION(InvokeInterface);
4727
Artem Serovcced8ba2017-07-19 18:18:09 +01004728 protected:
4729 DEFAULT_COPY_CONSTRUCTOR(InvokeInterface);
4730
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004731 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004732 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004733 const uint32_t imt_index_;
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004734};
4735
Vladimir Markofcb503c2016-05-18 12:48:17 +01004736class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004737 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004738 HNeg(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304739 : HUnaryOperation(kNeg, result_type, input, dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004740 DCHECK_EQ(result_type, DataType::Kind(input->GetType()));
Roland Levillain937e6cd2016-03-22 11:54:37 +00004741 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004742
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004743 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004744
4745 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004746 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004747 }
4748 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004749 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004750 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004751 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4752 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4753 }
4754 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4755 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4756 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004757
Roland Levillain88cb1752014-10-20 16:36:47 +01004758 DECLARE_INSTRUCTION(Neg);
4759
Artem Serovcced8ba2017-07-19 18:18:09 +01004760 protected:
4761 DEFAULT_COPY_CONSTRUCTOR(Neg);
Roland Levillain88cb1752014-10-20 16:36:47 +01004762};
4763
Vladimir Markofcb503c2016-05-18 12:48:17 +01004764class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004765 public:
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004766 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304767 : HExpression(kNewArray, DataType::Type::kReference, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004768 SetRawInputAt(0, cls);
4769 SetRawInputAt(1, length);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004770 }
4771
Artem Serovcced8ba2017-07-19 18:18:09 +01004772 bool IsClonable() const OVERRIDE { return true; }
4773
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004774 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004775 bool NeedsEnvironment() const OVERRIDE { return true; }
4776
Mingyao Yang0c365e62015-03-31 15:09:29 -07004777 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4778 bool CanThrow() const OVERRIDE { return true; }
4779
Calin Juravle10e244f2015-01-26 18:54:32 +00004780 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004781
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004782 HLoadClass* GetLoadClass() const {
4783 DCHECK(InputAt(0)->IsLoadClass());
4784 return InputAt(0)->AsLoadClass();
4785 }
4786
4787 HInstruction* GetLength() const {
4788 return InputAt(1);
4789 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004790
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004791 DECLARE_INSTRUCTION(NewArray);
4792
Artem Serovcced8ba2017-07-19 18:18:09 +01004793 protected:
4794 DEFAULT_COPY_CONSTRUCTOR(NewArray);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004795};
4796
Vladimir Markofcb503c2016-05-18 12:48:17 +01004797class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004798 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004799 HAdd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004800 HInstruction* left,
4801 HInstruction* right,
4802 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304803 : HBinaryOperation(kAdd, result_type, left, right, SideEffects::None(), dex_pc) {
4804 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004805
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004806 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004807
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004808 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004809
4810 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004811 return GetBlock()->GetGraph()->GetIntConstant(
4812 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004813 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004814 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004815 return GetBlock()->GetGraph()->GetLongConstant(
4816 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004817 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004818 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4819 return GetBlock()->GetGraph()->GetFloatConstant(
4820 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4821 }
4822 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4823 return GetBlock()->GetGraph()->GetDoubleConstant(
4824 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4825 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004826
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004827 DECLARE_INSTRUCTION(Add);
4828
Artem Serovcced8ba2017-07-19 18:18:09 +01004829 protected:
4830 DEFAULT_COPY_CONSTRUCTOR(Add);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004831};
4832
Vladimir Markofcb503c2016-05-18 12:48:17 +01004833class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004834 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004835 HSub(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004836 HInstruction* left,
4837 HInstruction* right,
4838 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304839 : HBinaryOperation(kSub, result_type, left, right, SideEffects::None(), dex_pc) {
4840 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004841
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004842 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004843
4844 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004845 return GetBlock()->GetGraph()->GetIntConstant(
4846 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004847 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004848 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004849 return GetBlock()->GetGraph()->GetLongConstant(
4850 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004851 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004852 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4853 return GetBlock()->GetGraph()->GetFloatConstant(
4854 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4855 }
4856 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4857 return GetBlock()->GetGraph()->GetDoubleConstant(
4858 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4859 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004860
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004861 DECLARE_INSTRUCTION(Sub);
4862
Artem Serovcced8ba2017-07-19 18:18:09 +01004863 protected:
4864 DEFAULT_COPY_CONSTRUCTOR(Sub);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004865};
4866
Vladimir Markofcb503c2016-05-18 12:48:17 +01004867class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004868 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004869 HMul(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004870 HInstruction* left,
4871 HInstruction* right,
4872 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304873 : HBinaryOperation(kMul, result_type, left, right, SideEffects::None(), dex_pc) {
4874 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004875
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004876 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004877
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004878 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004879
4880 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004881 return GetBlock()->GetGraph()->GetIntConstant(
4882 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004883 }
4884 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004885 return GetBlock()->GetGraph()->GetLongConstant(
4886 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004887 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004888 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4889 return GetBlock()->GetGraph()->GetFloatConstant(
4890 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4891 }
4892 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4893 return GetBlock()->GetGraph()->GetDoubleConstant(
4894 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4895 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004896
4897 DECLARE_INSTRUCTION(Mul);
4898
Artem Serovcced8ba2017-07-19 18:18:09 +01004899 protected:
4900 DEFAULT_COPY_CONSTRUCTOR(Mul);
Calin Juravle34bacdf2014-10-07 20:23:36 +01004901};
4902
Vladimir Markofcb503c2016-05-18 12:48:17 +01004903class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004904 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004905 HDiv(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004906 HInstruction* left,
4907 HInstruction* right,
4908 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304909 : HBinaryOperation(kDiv, result_type, left, right, SideEffects::None(), dex_pc) {
4910 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004911
Roland Levillain9867bc72015-08-05 10:21:34 +01004912 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004913 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004914 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004915 // Our graph structure ensures we never have 0 for `y` during
4916 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004917 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004918 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004919 return (y == -1) ? -x : x / y;
4920 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004921
Roland Levillain31dd3d62016-02-16 12:21:02 +00004922 template <typename T>
4923 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004924 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004925 return x / y;
4926 }
4927
Roland Levillain9867bc72015-08-05 10:21:34 +01004928 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004929 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004930 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004931 }
4932 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004933 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004934 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4935 }
4936 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4937 return GetBlock()->GetGraph()->GetFloatConstant(
4938 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4939 }
4940 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4941 return GetBlock()->GetGraph()->GetDoubleConstant(
4942 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004943 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004944
4945 DECLARE_INSTRUCTION(Div);
4946
Artem Serovcced8ba2017-07-19 18:18:09 +01004947 protected:
4948 DEFAULT_COPY_CONSTRUCTOR(Div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00004949};
4950
Vladimir Markofcb503c2016-05-18 12:48:17 +01004951class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004952 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004953 HRem(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004954 HInstruction* left,
4955 HInstruction* right,
4956 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304957 : HBinaryOperation(kRem, result_type, left, right, SideEffects::None(), dex_pc) {
4958 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004959
Roland Levillain9867bc72015-08-05 10:21:34 +01004960 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004961 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004962 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004963 // Our graph structure ensures we never have 0 for `y` during
4964 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004965 DCHECK_NE(y, 0);
4966 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4967 return (y == -1) ? 0 : x % y;
4968 }
4969
Roland Levillain31dd3d62016-02-16 12:21:02 +00004970 template <typename T>
4971 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004972 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004973 return std::fmod(x, y);
4974 }
4975
Roland Levillain9867bc72015-08-05 10:21:34 +01004976 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004977 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004978 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004979 }
4980 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004981 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004982 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004983 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004984 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4985 return GetBlock()->GetGraph()->GetFloatConstant(
4986 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4987 }
4988 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4989 return GetBlock()->GetGraph()->GetDoubleConstant(
4990 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4991 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004992
4993 DECLARE_INSTRUCTION(Rem);
4994
Artem Serovcced8ba2017-07-19 18:18:09 +01004995 protected:
4996 DEFAULT_COPY_CONSTRUCTOR(Rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00004997};
4998
Aart Bik1f8d51b2018-02-15 10:42:37 -08004999class HMin FINAL : public HBinaryOperation {
5000 public:
5001 HMin(DataType::Type result_type,
5002 HInstruction* left,
5003 HInstruction* right,
5004 uint32_t dex_pc)
5005 : HBinaryOperation(kMin, result_type, left, right, SideEffects::None(), dex_pc) {}
5006
5007 bool IsCommutative() const OVERRIDE { return true; }
5008
5009 // Evaluation for integral values.
5010 template <typename T> static T ComputeIntegral(T x, T y) {
5011 return (x <= y) ? x : y;
5012 }
5013
5014 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
5015 return GetBlock()->GetGraph()->GetIntConstant(
5016 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5017 }
5018 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
5019 return GetBlock()->GetGraph()->GetLongConstant(
5020 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5021 }
5022 // TODO: Evaluation for floating-point values.
5023 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5024 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { return nullptr; }
5025 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5026 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { return nullptr; }
5027
5028 DECLARE_INSTRUCTION(Min);
5029
5030 protected:
5031 DEFAULT_COPY_CONSTRUCTOR(Min);
5032};
5033
5034class HMax FINAL : public HBinaryOperation {
5035 public:
5036 HMax(DataType::Type result_type,
5037 HInstruction* left,
5038 HInstruction* right,
5039 uint32_t dex_pc)
5040 : HBinaryOperation(kMax, result_type, left, right, SideEffects::None(), dex_pc) {}
5041
5042 bool IsCommutative() const OVERRIDE { return true; }
5043
5044 // Evaluation for integral values.
5045 template <typename T> static T ComputeIntegral(T x, T y) {
5046 return (x >= y) ? x : y;
5047 }
5048
5049 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
5050 return GetBlock()->GetGraph()->GetIntConstant(
5051 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5052 }
5053 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
5054 return GetBlock()->GetGraph()->GetLongConstant(
5055 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5056 }
5057 // TODO: Evaluation for floating-point values.
5058 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5059 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { return nullptr; }
5060 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5061 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { return nullptr; }
5062
5063 DECLARE_INSTRUCTION(Max);
5064
5065 protected:
5066 DEFAULT_COPY_CONSTRUCTOR(Max);
5067};
5068
Aart Bik3dad3412018-02-28 12:01:46 -08005069class HAbs FINAL : public HUnaryOperation {
5070 public:
5071 HAbs(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
5072 : HUnaryOperation(kAbs, result_type, input, dex_pc) {}
5073
5074 // Evaluation for integral values.
5075 template <typename T> static T ComputeIntegral(T x) {
5076 return x < 0 ? -x : x;
5077 }
5078
5079 // Evaluation for floating-point values.
5080 // Note, as a "quality of implementation", rather than pure "spec compliance",
5081 // we require that Math.abs() clears the sign bit (but changes nothing else)
5082 // for all floating-point numbers, including NaN (signaling NaN may become quiet though).
5083 // http://b/30758343
5084 template <typename T, typename S> static T ComputeFP(T x) {
5085 S bits = bit_cast<S, T>(x);
5086 return bit_cast<T, S>(bits & std::numeric_limits<S>::max());
5087 }
5088
5089 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
5090 return GetBlock()->GetGraph()->GetIntConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5091 }
5092 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
5093 return GetBlock()->GetGraph()->GetLongConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5094 }
5095 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
5096 return GetBlock()->GetGraph()->GetFloatConstant(
5097 ComputeFP<float, int32_t>(x->GetValue()), GetDexPc());
5098 }
5099 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
5100 return GetBlock()->GetGraph()->GetDoubleConstant(
5101 ComputeFP<double, int64_t>(x->GetValue()), GetDexPc());
5102 }
5103
5104 DECLARE_INSTRUCTION(Abs);
5105
5106 protected:
5107 DEFAULT_COPY_CONSTRUCTOR(Abs);
5108};
5109
Vladimir Markofcb503c2016-05-18 12:48:17 +01005110class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00005111 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00005112 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
5113 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00005114 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305115 : HExpression(kDivZeroCheck, value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00005116 SetRawInputAt(0, value);
5117 }
5118
5119 bool CanBeMoved() const OVERRIDE { return true; }
5120
Vladimir Marko372f10e2016-05-17 16:30:10 +01005121 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00005122 return true;
5123 }
5124
5125 bool NeedsEnvironment() const OVERRIDE { return true; }
5126 bool CanThrow() const OVERRIDE { return true; }
5127
Calin Juravled0d48522014-11-04 16:40:20 +00005128 DECLARE_INSTRUCTION(DivZeroCheck);
5129
Artem Serovcced8ba2017-07-19 18:18:09 +01005130 protected:
5131 DEFAULT_COPY_CONSTRUCTOR(DivZeroCheck);
Calin Juravled0d48522014-11-04 16:40:20 +00005132};
5133
Vladimir Markofcb503c2016-05-18 12:48:17 +01005134class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005135 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005136 HShl(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005137 HInstruction* value,
5138 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005139 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305140 : HBinaryOperation(kShl, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005141 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5142 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005143 }
5144
Roland Levillain5b5b9312016-03-22 14:57:31 +00005145 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005146 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005147 return value << (distance & max_shift_distance);
5148 }
5149
5150 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005151 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005152 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005153 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005154 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005155 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005156 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005157 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005158 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5159 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5160 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5161 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005162 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005163 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5164 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005165 LOG(FATAL) << DebugName() << " is not defined for float values";
5166 UNREACHABLE();
5167 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005168 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5169 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005170 LOG(FATAL) << DebugName() << " is not defined for double values";
5171 UNREACHABLE();
5172 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005173
5174 DECLARE_INSTRUCTION(Shl);
5175
Artem Serovcced8ba2017-07-19 18:18:09 +01005176 protected:
5177 DEFAULT_COPY_CONSTRUCTOR(Shl);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005178};
5179
Vladimir Markofcb503c2016-05-18 12:48:17 +01005180class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005181 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005182 HShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005183 HInstruction* value,
5184 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005185 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305186 : HBinaryOperation(kShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005187 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5188 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005189 }
5190
Roland Levillain5b5b9312016-03-22 14:57:31 +00005191 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005192 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005193 return value >> (distance & max_shift_distance);
5194 }
5195
5196 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005197 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005198 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005199 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005200 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005201 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005202 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005203 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005204 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5205 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5206 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5207 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005208 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005209 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5210 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005211 LOG(FATAL) << DebugName() << " is not defined for float values";
5212 UNREACHABLE();
5213 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005214 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5215 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005216 LOG(FATAL) << DebugName() << " is not defined for double values";
5217 UNREACHABLE();
5218 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005219
5220 DECLARE_INSTRUCTION(Shr);
5221
Artem Serovcced8ba2017-07-19 18:18:09 +01005222 protected:
5223 DEFAULT_COPY_CONSTRUCTOR(Shr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005224};
5225
Vladimir Markofcb503c2016-05-18 12:48:17 +01005226class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005227 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005228 HUShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005229 HInstruction* value,
5230 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005231 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305232 : HBinaryOperation(kUShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005233 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5234 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00005235 }
5236
Roland Levillain5b5b9312016-03-22 14:57:31 +00005237 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005238 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005239 typedef typename std::make_unsigned<T>::type V;
5240 V ux = static_cast<V>(value);
5241 return static_cast<T>(ux >> (distance & max_shift_distance));
5242 }
5243
5244 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005245 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005246 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005247 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005248 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005249 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005250 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005251 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005252 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5253 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5254 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5255 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00005256 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005257 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5258 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005259 LOG(FATAL) << DebugName() << " is not defined for float values";
5260 UNREACHABLE();
5261 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005262 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5263 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005264 LOG(FATAL) << DebugName() << " is not defined for double values";
5265 UNREACHABLE();
5266 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005267
5268 DECLARE_INSTRUCTION(UShr);
5269
Artem Serovcced8ba2017-07-19 18:18:09 +01005270 protected:
5271 DEFAULT_COPY_CONSTRUCTOR(UShr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005272};
5273
Vladimir Markofcb503c2016-05-18 12:48:17 +01005274class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005275 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005276 HAnd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005277 HInstruction* left,
5278 HInstruction* right,
5279 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305280 : HBinaryOperation(kAnd, result_type, left, right, SideEffects::None(), dex_pc) {
5281 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005282
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005283 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005284
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005285 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005286
5287 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005288 return GetBlock()->GetGraph()->GetIntConstant(
5289 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005290 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005291 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005292 return GetBlock()->GetGraph()->GetLongConstant(
5293 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005294 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005295 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5296 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5297 LOG(FATAL) << DebugName() << " is not defined for float values";
5298 UNREACHABLE();
5299 }
5300 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5301 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5302 LOG(FATAL) << DebugName() << " is not defined for double values";
5303 UNREACHABLE();
5304 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005305
5306 DECLARE_INSTRUCTION(And);
5307
Artem Serovcced8ba2017-07-19 18:18:09 +01005308 protected:
5309 DEFAULT_COPY_CONSTRUCTOR(And);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005310};
5311
Vladimir Markofcb503c2016-05-18 12:48:17 +01005312class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005313 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005314 HOr(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005315 HInstruction* left,
5316 HInstruction* right,
5317 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305318 : HBinaryOperation(kOr, result_type, left, right, SideEffects::None(), dex_pc) {
5319 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005320
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005321 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005322
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005323 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005324
5325 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005326 return GetBlock()->GetGraph()->GetIntConstant(
5327 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005328 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005329 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005330 return GetBlock()->GetGraph()->GetLongConstant(
5331 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005332 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005333 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5334 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5335 LOG(FATAL) << DebugName() << " is not defined for float values";
5336 UNREACHABLE();
5337 }
5338 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5339 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5340 LOG(FATAL) << DebugName() << " is not defined for double values";
5341 UNREACHABLE();
5342 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005343
5344 DECLARE_INSTRUCTION(Or);
5345
Artem Serovcced8ba2017-07-19 18:18:09 +01005346 protected:
5347 DEFAULT_COPY_CONSTRUCTOR(Or);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005348};
5349
Vladimir Markofcb503c2016-05-18 12:48:17 +01005350class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005351 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005352 HXor(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005353 HInstruction* left,
5354 HInstruction* right,
5355 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305356 : HBinaryOperation(kXor, result_type, left, right, SideEffects::None(), dex_pc) {
5357 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005358
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005359 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005360
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005361 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005362
5363 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005364 return GetBlock()->GetGraph()->GetIntConstant(
5365 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005366 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005367 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005368 return GetBlock()->GetGraph()->GetLongConstant(
5369 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005370 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005371 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5372 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5373 LOG(FATAL) << DebugName() << " is not defined for float values";
5374 UNREACHABLE();
5375 }
5376 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5377 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5378 LOG(FATAL) << DebugName() << " is not defined for double values";
5379 UNREACHABLE();
5380 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005381
5382 DECLARE_INSTRUCTION(Xor);
5383
Artem Serovcced8ba2017-07-19 18:18:09 +01005384 protected:
5385 DEFAULT_COPY_CONSTRUCTOR(Xor);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005386};
5387
Vladimir Markofcb503c2016-05-18 12:48:17 +01005388class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005389 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005390 HRor(DataType::Type result_type, HInstruction* value, HInstruction* distance)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305391 : HBinaryOperation(kRor, result_type, value, distance) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005392 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5393 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00005394 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005395
Roland Levillain5b5b9312016-03-22 14:57:31 +00005396 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005397 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005398 typedef typename std::make_unsigned<T>::type V;
5399 V ux = static_cast<V>(value);
5400 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005401 return static_cast<T>(ux);
5402 } else {
5403 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005404 return static_cast<T>(ux >> (distance & max_shift_value)) |
5405 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005406 }
5407 }
5408
Roland Levillain5b5b9312016-03-22 14:57:31 +00005409 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005410 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005411 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005412 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005413 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005414 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005415 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005416 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005417 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5418 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5419 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5420 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005421 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005422 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5423 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005424 LOG(FATAL) << DebugName() << " is not defined for float values";
5425 UNREACHABLE();
5426 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005427 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5428 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005429 LOG(FATAL) << DebugName() << " is not defined for double values";
5430 UNREACHABLE();
5431 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005432
5433 DECLARE_INSTRUCTION(Ror);
5434
Artem Serovcced8ba2017-07-19 18:18:09 +01005435 protected:
5436 DEFAULT_COPY_CONSTRUCTOR(Ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005437};
5438
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005439// The value of a parameter in this method. Its location depends on
5440// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01005441class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005442 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005443 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005444 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005445 uint8_t index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005446 DataType::Type parameter_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005447 bool is_this = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305448 : HExpression(kParameterValue, parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005449 dex_file_(dex_file),
5450 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005451 index_(index) {
5452 SetPackedFlag<kFlagIsThis>(is_this);
5453 SetPackedFlag<kFlagCanBeNull>(!is_this);
5454 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005455
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005456 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005457 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005458 uint8_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07005459 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005460
Vladimir Markoa1de9182016-02-25 11:37:38 +00005461 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
5462 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005463
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005464 DECLARE_INSTRUCTION(ParameterValue);
5465
Artem Serovcced8ba2017-07-19 18:18:09 +01005466 protected:
5467 DEFAULT_COPY_CONSTRUCTOR(ParameterValue);
5468
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005469 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005470 // Whether or not the parameter value corresponds to 'this' argument.
Vladimir Markobd785672018-05-03 17:09:09 +01005471 static constexpr size_t kFlagIsThis = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005472 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5473 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5474 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5475 "Too many packed fields.");
5476
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005477 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005478 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005479 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005480 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005481 const uint8_t index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005482};
5483
Vladimir Markofcb503c2016-05-18 12:48:17 +01005484class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005485 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005486 HNot(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305487 : HUnaryOperation(kNot, result_type, input, dex_pc) {
5488 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005489
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005490 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005491 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005492 return true;
5493 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005494
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005495 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005496
5497 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005498 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005499 }
5500 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005501 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005502 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005503 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5504 LOG(FATAL) << DebugName() << " is not defined for float values";
5505 UNREACHABLE();
5506 }
5507 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5508 LOG(FATAL) << DebugName() << " is not defined for double values";
5509 UNREACHABLE();
5510 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005511
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005512 DECLARE_INSTRUCTION(Not);
5513
Artem Serovcced8ba2017-07-19 18:18:09 +01005514 protected:
5515 DEFAULT_COPY_CONSTRUCTOR(Not);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005516};
5517
Vladimir Markofcb503c2016-05-18 12:48:17 +01005518class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005519 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005520 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305521 : HUnaryOperation(kBooleanNot, DataType::Type::kBool, input, dex_pc) {
5522 }
David Brazdil66d126e2015-04-03 16:02:44 +01005523
5524 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005525 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01005526 return true;
5527 }
5528
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005529 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005530 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005531 return !x;
5532 }
5533
Roland Levillain9867bc72015-08-05 10:21:34 +01005534 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005535 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005536 }
5537 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5538 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005539 UNREACHABLE();
5540 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005541 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5542 LOG(FATAL) << DebugName() << " is not defined for float values";
5543 UNREACHABLE();
5544 }
5545 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5546 LOG(FATAL) << DebugName() << " is not defined for double values";
5547 UNREACHABLE();
5548 }
David Brazdil66d126e2015-04-03 16:02:44 +01005549
5550 DECLARE_INSTRUCTION(BooleanNot);
5551
Artem Serovcced8ba2017-07-19 18:18:09 +01005552 protected:
5553 DEFAULT_COPY_CONSTRUCTOR(BooleanNot);
David Brazdil66d126e2015-04-03 16:02:44 +01005554};
5555
Vladimir Markofcb503c2016-05-18 12:48:17 +01005556class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005557 public:
5558 // Instantiate a type conversion of `input` to `result_type`.
Vladimir Markoc8fb2112017-10-03 11:37:52 +01005559 HTypeConversion(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305560 : HExpression(kTypeConversion, result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005561 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005562 // Invariant: We should never generate a conversion to a Boolean value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005563 DCHECK_NE(DataType::Type::kBool, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005564 }
5565
5566 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005567 DataType::Type GetInputType() const { return GetInput()->GetType(); }
5568 DataType::Type GetResultType() const { return GetType(); }
Roland Levillaindff1f282014-11-05 14:15:05 +00005569
5570 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005571 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5572 return true;
5573 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005574
Mark Mendelle82549b2015-05-06 10:55:34 -04005575 // Try to statically evaluate the conversion and return a HConstant
5576 // containing the result. If the input cannot be converted, return nullptr.
5577 HConstant* TryStaticEvaluation() const;
5578
Roland Levillaindff1f282014-11-05 14:15:05 +00005579 DECLARE_INSTRUCTION(TypeConversion);
5580
Artem Serovcced8ba2017-07-19 18:18:09 +01005581 protected:
5582 DEFAULT_COPY_CONSTRUCTOR(TypeConversion);
Roland Levillaindff1f282014-11-05 14:15:05 +00005583};
5584
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005585static constexpr uint32_t kNoRegNumber = -1;
5586
Vladimir Markofcb503c2016-05-18 12:48:17 +01005587class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005588 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005589 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
5590 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005591 HNullCheck(HInstruction* value, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305592 : HExpression(kNullCheck, value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005593 SetRawInputAt(0, value);
5594 }
5595
Artem Serovcced8ba2017-07-19 18:18:09 +01005596 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00005597 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005598 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005599 return true;
5600 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005601
Calin Juravle10e244f2015-01-26 18:54:32 +00005602 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005603
Calin Juravle10e244f2015-01-26 18:54:32 +00005604 bool CanThrow() const OVERRIDE { return true; }
5605
5606 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005607
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005608 DECLARE_INSTRUCTION(NullCheck);
5609
Artem Serovcced8ba2017-07-19 18:18:09 +01005610 protected:
5611 DEFAULT_COPY_CONSTRUCTOR(NullCheck);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005612};
5613
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005614// Embeds an ArtField and all the information required by the compiler. We cache
5615// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005616class FieldInfo : public ValueObject {
5617 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005618 FieldInfo(ArtField* field,
5619 MemberOffset field_offset,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005620 DataType::Type field_type,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005621 bool is_volatile,
5622 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005623 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005624 const DexFile& dex_file)
5625 : field_(field),
5626 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005627 field_type_(field_type),
5628 is_volatile_(is_volatile),
5629 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005630 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005631 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005632
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005633 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005634 MemberOffset GetFieldOffset() const { return field_offset_; }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005635 DataType::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005636 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005637 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005638 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005639 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005640
5641 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005642 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005643 const MemberOffset field_offset_;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005644 const DataType::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005645 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005646 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005647 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005648 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005649};
5650
Vladimir Markofcb503c2016-05-18 12:48:17 +01005651class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005652 public:
5653 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005654 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005655 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005656 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005657 bool is_volatile,
5658 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005659 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005660 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005661 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305662 : HExpression(kInstanceFieldGet,
5663 field_type,
5664 SideEffects::FieldReadOfType(field_type, is_volatile),
5665 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005666 field_info_(field,
5667 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005668 field_type,
5669 is_volatile,
5670 field_idx,
5671 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005672 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005673 SetRawInputAt(0, value);
5674 }
5675
Artem Serovcced8ba2017-07-19 18:18:09 +01005676 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005677 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005678
Vladimir Marko372f10e2016-05-17 16:30:10 +01005679 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5680 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005681 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005682 }
5683
Calin Juravle641547a2015-04-21 22:08:51 +01005684 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005685 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005686 }
5687
5688 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005689 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5690 }
5691
Calin Juravle52c48962014-12-16 17:02:57 +00005692 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005693 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005694 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005695 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005696
Vladimir Marko61b92282017-10-11 13:23:17 +01005697 void SetType(DataType::Type new_type) {
5698 DCHECK(DataType::IsIntegralType(GetType()));
5699 DCHECK(DataType::IsIntegralType(new_type));
5700 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5701 SetPackedField<TypeField>(new_type);
5702 }
5703
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005704 DECLARE_INSTRUCTION(InstanceFieldGet);
5705
Artem Serovcced8ba2017-07-19 18:18:09 +01005706 protected:
5707 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldGet);
5708
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005709 private:
5710 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005711};
5712
Vladimir Markobd785672018-05-03 17:09:09 +01005713class HInstanceFieldSet FINAL : public HExpression<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005714 public:
5715 HInstanceFieldSet(HInstruction* object,
5716 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005717 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005718 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005719 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005720 bool is_volatile,
5721 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005722 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005723 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005724 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01005725 : HExpression(kInstanceFieldSet,
5726 SideEffects::FieldWriteOfType(field_type, is_volatile),
5727 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005728 field_info_(field,
5729 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005730 field_type,
5731 is_volatile,
5732 field_idx,
5733 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005734 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005735 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005736 SetRawInputAt(0, object);
5737 SetRawInputAt(1, value);
5738 }
5739
Artem Serovcced8ba2017-07-19 18:18:09 +01005740 bool IsClonable() const OVERRIDE { return true; }
5741
Calin Juravle641547a2015-04-21 22:08:51 +01005742 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005743 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005744 }
5745
Calin Juravle52c48962014-12-16 17:02:57 +00005746 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005747 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005748 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005749 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005750 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005751 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5752 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005753
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005754 DECLARE_INSTRUCTION(InstanceFieldSet);
5755
Artem Serovcced8ba2017-07-19 18:18:09 +01005756 protected:
5757 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldSet);
5758
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005759 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005760 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5761 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5762 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5763 "Too many packed fields.");
5764
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005765 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005766};
5767
Vladimir Markofcb503c2016-05-18 12:48:17 +01005768class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005769 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005770 HArrayGet(HInstruction* array,
5771 HInstruction* index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005772 DataType::Type type,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005773 uint32_t dex_pc)
5774 : HArrayGet(array,
5775 index,
5776 type,
5777 SideEffects::ArrayReadOfType(type),
5778 dex_pc,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305779 /* is_string_char_at */ false) {
5780 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005781
5782 HArrayGet(HInstruction* array,
5783 HInstruction* index,
5784 DataType::Type type,
5785 SideEffects side_effects,
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005786 uint32_t dex_pc,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005787 bool is_string_char_at)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305788 : HExpression(kArrayGet, type, side_effects, dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005789 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005790 SetRawInputAt(0, array);
5791 SetRawInputAt(1, index);
5792 }
5793
Artem Serovcced8ba2017-07-19 18:18:09 +01005794 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005795 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005796 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005797 return true;
5798 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005799 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005800 // TODO: We can be smarter here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01005801 // Currently, unless the array is the result of NewArray, the array access is always
5802 // preceded by some form of null NullCheck necessary for the bounds check, usually
5803 // implicit null check on the ArrayLength input to BoundsCheck or Deoptimize for
5804 // dynamic BCE. There are cases when these could be removed to produce better code.
5805 // If we ever add optimizations to do so we should allow an implicit check here
5806 // (as long as the address falls in the first page).
5807 //
5808 // As an example of such fancy optimization, we could eliminate BoundsCheck for
5809 // a = cond ? new int[1] : null;
5810 // a[0]; // The Phi does not need bounds check for either input.
Calin Juravle77520bc2015-01-12 18:45:46 +00005811 return false;
5812 }
5813
David Brazdil4833f5a2015-12-16 10:37:39 +00005814 bool IsEquivalentOf(HArrayGet* other) const {
5815 bool result = (GetDexPc() == other->GetDexPc());
5816 if (kIsDebugBuild && result) {
5817 DCHECK_EQ(GetBlock(), other->GetBlock());
5818 DCHECK_EQ(GetArray(), other->GetArray());
5819 DCHECK_EQ(GetIndex(), other->GetIndex());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005820 if (DataType::IsIntOrLongType(GetType())) {
5821 DCHECK(DataType::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005822 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005823 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
5824 DCHECK(DataType::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005825 }
5826 }
5827 return result;
5828 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005829
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005830 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5831
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005832 HInstruction* GetArray() const { return InputAt(0); }
5833 HInstruction* GetIndex() const { return InputAt(1); }
5834
Vladimir Marko61b92282017-10-11 13:23:17 +01005835 void SetType(DataType::Type new_type) {
5836 DCHECK(DataType::IsIntegralType(GetType()));
5837 DCHECK(DataType::IsIntegralType(new_type));
5838 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5839 SetPackedField<TypeField>(new_type);
5840 }
5841
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005842 DECLARE_INSTRUCTION(ArrayGet);
5843
Artem Serovcced8ba2017-07-19 18:18:09 +01005844 protected:
5845 DEFAULT_COPY_CONSTRUCTOR(ArrayGet);
5846
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005847 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005848 // We treat a String as an array, creating the HArrayGet from String.charAt()
5849 // intrinsic in the instruction simplifier. We can always determine whether
5850 // a particular HArrayGet is actually a String.charAt() by looking at the type
5851 // of the input but that requires holding the mutator lock, so we prefer to use
5852 // a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01005853 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005854 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5855 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5856 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005857};
5858
Vladimir Markobd785672018-05-03 17:09:09 +01005859class HArraySet FINAL : public HExpression<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005860 public:
5861 HArraySet(HInstruction* array,
5862 HInstruction* index,
5863 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005864 DataType::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005865 uint32_t dex_pc)
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005866 : HArraySet(array,
5867 index,
5868 value,
5869 expected_component_type,
5870 // Make a best guess for side effects now, may be refined during SSA building.
5871 ComputeSideEffects(GetComponentType(value->GetType(), expected_component_type)),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305872 dex_pc) {
5873 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005874
5875 HArraySet(HInstruction* array,
5876 HInstruction* index,
5877 HInstruction* value,
5878 DataType::Type expected_component_type,
5879 SideEffects side_effects,
5880 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01005881 : HExpression(kArraySet, side_effects, dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005882 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005883 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005884 SetPackedFlag<kFlagValueCanBeNull>(true);
5885 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005886 SetRawInputAt(0, array);
5887 SetRawInputAt(1, index);
5888 SetRawInputAt(2, value);
5889 }
5890
Artem Serovcced8ba2017-07-19 18:18:09 +01005891 bool IsClonable() const OVERRIDE { return true; }
5892
Calin Juravle77520bc2015-01-12 18:45:46 +00005893 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005894 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005895 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005896 }
5897
Mingyao Yang81014cb2015-06-02 03:16:27 -07005898 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005899 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005900
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005901 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005902 // TODO: Same as for ArrayGet.
5903 return false;
5904 }
5905
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005906 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005907 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005908 }
5909
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005910 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005911 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005912 }
5913
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005914 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005915 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005916 }
5917
Vladimir Markoa1de9182016-02-25 11:37:38 +00005918 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5919 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5920 bool StaticTypeOfArrayIsObjectArray() const {
5921 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5922 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005923
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005924 HInstruction* GetArray() const { return InputAt(0); }
5925 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005926 HInstruction* GetValue() const { return InputAt(2); }
5927
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005928 DataType::Type GetComponentType() const {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005929 return GetComponentType(GetValue()->GetType(), GetRawExpectedComponentType());
5930 }
5931
5932 static DataType::Type GetComponentType(DataType::Type value_type,
5933 DataType::Type expected_component_type) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005934 // The Dex format does not type floating point index operations. Since the
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005935 // `expected_component_type` comes from SSA building and can therefore not
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005936 // be correct, we also check what is the value type. If it is a floating
5937 // point type, we must use that type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005938 return ((value_type == DataType::Type::kFloat32) || (value_type == DataType::Type::kFloat64))
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005939 ? value_type
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005940 : expected_component_type;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005941 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005942
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005943 DataType::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005944 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005945 }
5946
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005947 static SideEffects ComputeSideEffects(DataType::Type type) {
5948 return SideEffects::ArrayWriteOfType(type).Union(SideEffectsForArchRuntimeCalls(type));
Aart Bik18b36ab2016-04-13 16:41:35 -07005949 }
5950
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005951 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type value_type) {
5952 return (value_type == DataType::Type::kReference) ? SideEffects::CanTriggerGC()
5953 : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005954 }
5955
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005956 DECLARE_INSTRUCTION(ArraySet);
5957
Artem Serovcced8ba2017-07-19 18:18:09 +01005958 protected:
5959 DEFAULT_COPY_CONSTRUCTOR(ArraySet);
5960
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005961 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005962 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5963 static constexpr size_t kFieldExpectedComponentTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005964 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00005965 static constexpr size_t kFlagNeedsTypeCheck =
5966 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5967 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005968 // Cached information for the reference_type_info_ so that codegen
5969 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005970 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5971 static constexpr size_t kNumberOfArraySetPackedBits =
5972 kFlagStaticTypeOfArrayIsObjectArray + 1;
5973 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5974 using ExpectedComponentTypeField =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005975 BitField<DataType::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005976};
5977
Vladimir Markofcb503c2016-05-18 12:48:17 +01005978class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005979 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005980 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305981 : HExpression(kArrayLength, DataType::Type::kInt32, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005982 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005983 // Note that arrays do not change length, so the instruction does not
5984 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005985 SetRawInputAt(0, array);
5986 }
5987
Artem Serovcced8ba2017-07-19 18:18:09 +01005988 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle77520bc2015-01-12 18:45:46 +00005989 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005990 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005991 return true;
5992 }
Calin Juravle641547a2015-04-21 22:08:51 +01005993 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5994 return obj == InputAt(0);
5995 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005996
Vladimir Markodce016e2016-04-28 13:10:02 +01005997 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5998
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005999 DECLARE_INSTRUCTION(ArrayLength);
6000
Artem Serovcced8ba2017-07-19 18:18:09 +01006001 protected:
6002 DEFAULT_COPY_CONSTRUCTOR(ArrayLength);
6003
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006004 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01006005 // We treat a String as an array, creating the HArrayLength from String.length()
6006 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
6007 // determine whether a particular HArrayLength is actually a String.length() by
6008 // looking at the type of the input but that requires holding the mutator lock, so
6009 // we prefer to use a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01006010 static constexpr size_t kFlagIsStringLength = kNumberOfGenericPackedBits;
Vladimir Markodce016e2016-04-28 13:10:02 +01006011 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
6012 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6013 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006014};
6015
Vladimir Markofcb503c2016-05-18 12:48:17 +01006016class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006017 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00006018 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
6019 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006020 HBoundsCheck(HInstruction* index,
6021 HInstruction* length,
6022 uint32_t dex_pc,
Vladimir Marko0259c242017-12-04 11:27:47 +00006023 bool is_string_char_at = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306024 : HExpression(kBoundsCheck, index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006025 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(index->GetType()));
Vladimir Marko0259c242017-12-04 11:27:47 +00006026 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006027 SetRawInputAt(0, index);
6028 SetRawInputAt(1, length);
6029 }
6030
Artem Serovcced8ba2017-07-19 18:18:09 +01006031 bool IsClonable() const OVERRIDE { return true; }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006032 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006033 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006034 return true;
6035 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006036
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006037 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006038
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006039 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01006040
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00006041 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006042
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006043 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006044
6045 DECLARE_INSTRUCTION(BoundsCheck);
6046
Artem Serovcced8ba2017-07-19 18:18:09 +01006047 protected:
6048 DEFAULT_COPY_CONSTRUCTOR(BoundsCheck);
6049
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006050 private:
Vladimir Markobd785672018-05-03 17:09:09 +01006051 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006052};
6053
Vladimir Markobd785672018-05-03 17:09:09 +01006054class HSuspendCheck FINAL : public HExpression<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006055 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00006056 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01006057 : HExpression(kSuspendCheck, SideEffects::CanTriggerGC(), dex_pc),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306058 slow_path_(nullptr) {
6059 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006060
Artem Serovcced8ba2017-07-19 18:18:09 +01006061 bool IsClonable() const OVERRIDE { return true; }
6062
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006063 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006064 return true;
6065 }
6066
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006067 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
6068 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006069
6070 DECLARE_INSTRUCTION(SuspendCheck);
6071
Artem Serovcced8ba2017-07-19 18:18:09 +01006072 protected:
6073 DEFAULT_COPY_CONSTRUCTOR(SuspendCheck);
6074
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006075 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006076 // Only used for code generation, in order to share the same slow path between back edges
6077 // of a same loop.
6078 SlowPathCode* slow_path_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006079};
6080
David Srbecky0cf44932015-12-09 14:09:59 +00006081// Pseudo-instruction which provides the native debugger with mapping information.
6082// It ensures that we can generate line number and local variables at this point.
Vladimir Markobd785672018-05-03 17:09:09 +01006083class HNativeDebugInfo : public HExpression<0> {
David Srbecky0cf44932015-12-09 14:09:59 +00006084 public:
6085 explicit HNativeDebugInfo(uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006086 : HExpression<0>(kNativeDebugInfo, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306087 }
David Srbecky0cf44932015-12-09 14:09:59 +00006088
6089 bool NeedsEnvironment() const OVERRIDE {
6090 return true;
6091 }
6092
6093 DECLARE_INSTRUCTION(NativeDebugInfo);
6094
Artem Serovcced8ba2017-07-19 18:18:09 +01006095 protected:
6096 DEFAULT_COPY_CONSTRUCTOR(NativeDebugInfo);
David Srbecky0cf44932015-12-09 14:09:59 +00006097};
6098
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006099/**
6100 * Instruction to load a Class object.
6101 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006102class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006103 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006104 // Determines how to load the Class.
6105 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006106 // We cannot load this class. See HSharpening::SharpenLoadClass.
6107 kInvalid = -1,
6108
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006109 // Use the Class* from the method's own ArtMethod*.
6110 kReferrersClass,
6111
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006112 // Use PC-relative boot image Class* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006113 // Used for boot image classes referenced by boot image code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006114 kBootImageLinkTimePcRelative,
6115
6116 // Use a known boot image Class* address, embedded in the code by the codegen.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006117 // Used for boot image classes referenced by apps in JIT- and AOT-compiled code (non-PIC).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006118 kBootImageAddress,
6119
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006120 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
6121 // Used for boot image classes referenced by apps in AOT-compiled code (PIC).
6122 kBootImageRelRo,
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006123
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006124 // Load from an entry in the .bss section using a PC-relative load.
6125 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
6126 kBssEntry,
6127
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006128 // Load from the root table associated with the JIT compiled method.
6129 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006130
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006131 // Load using a simple runtime call. This is the fall-back load kind when
6132 // the codegen is unable to use another appropriate kind.
6133 kRuntimeCall,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006134
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006135 kLast = kRuntimeCall
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006136 };
6137
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006138 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08006139 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006140 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006141 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006142 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01006143 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00006144 bool needs_access_check)
Vladimir Markobd785672018-05-03 17:09:09 +01006145 : HInstruction(kLoadClass,
6146 DataType::Type::kReference,
6147 SideEffectsForArchRuntimeCalls(),
6148 dex_pc),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006149 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006150 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006151 dex_file_(dex_file),
Vladimir Marko175e7862018-03-27 09:03:13 +00006152 klass_(klass) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01006153 // Referrers class should not need access check. We never inline unverified
6154 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006155 DCHECK(!is_referrers_class || !needs_access_check);
6156
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006157 SetPackedField<LoadKindField>(
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006158 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kRuntimeCall);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006159 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00006160 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006161 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Marko175e7862018-03-27 09:03:13 +00006162 SetPackedFlag<kFlagValidLoadedClassRTI>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006163 }
6164
Artem Serovcced8ba2017-07-19 18:18:09 +01006165 bool IsClonable() const OVERRIDE { return true; }
6166
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006167 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006168
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006169 LoadKind GetLoadKind() const {
6170 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006171 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006172
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006173 bool HasPcRelativeLoadKind() const {
6174 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6175 GetLoadKind() == LoadKind::kBootImageRelRo ||
6176 GetLoadKind() == LoadKind::kBssEntry;
6177 }
6178
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006179 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006180
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006181 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006182
Andreas Gampea5b09a62016-11-17 15:21:22 -08006183 size_t ComputeHashCode() const OVERRIDE { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006184
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01006185 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006186
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006187 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006188 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006189 }
6190
Calin Juravle0ba218d2015-05-19 18:46:01 +01006191 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00006192 // The entrypoint the code generator is going to call does not do
6193 // clinit of the class.
6194 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00006195 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006196 }
6197
6198 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00006199 return NeedsAccessCheck() ||
6200 MustGenerateClinitCheck() ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006201 GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006202 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01006203 }
6204
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006205 bool CanThrow() const OVERRIDE {
Vladimir Marko41559982017-01-06 14:04:23 +00006206 return NeedsAccessCheck() ||
6207 MustGenerateClinitCheck() ||
6208 // If the class is in the boot image, the lookup in the runtime call cannot throw.
6209 // This keeps CanThrow() consistent between non-PIC (using kBootImageAddress) and
6210 // PIC and subsequently avoids a DCE behavior dependency on the PIC option.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006211 ((GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006212 GetLoadKind() == LoadKind::kBssEntry) &&
6213 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006214 }
6215
Calin Juravleacf735c2015-02-12 15:25:22 +00006216 ReferenceTypeInfo GetLoadedClassRTI() {
Vladimir Marko175e7862018-03-27 09:03:13 +00006217 if (GetPackedFlag<kFlagValidLoadedClassRTI>()) {
6218 // Note: The is_exact flag from the return value should not be used.
6219 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact */ true);
6220 } else {
6221 return ReferenceTypeInfo::CreateInvalid();
6222 }
Calin Juravleacf735c2015-02-12 15:25:22 +00006223 }
6224
Vladimir Marko175e7862018-03-27 09:03:13 +00006225 // Loaded class RTI is marked as valid by RTP if the klass_ is admissible.
6226 void SetValidLoadedClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
6227 DCHECK(klass_ != nullptr);
6228 SetPackedFlag<kFlagValidLoadedClassRTI>(true);
Calin Juravleacf735c2015-02-12 15:25:22 +00006229 }
6230
Andreas Gampea5b09a62016-11-17 15:21:22 -08006231 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006232 const DexFile& GetDexFile() const { return dex_file_; }
6233
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006234 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006235 return GetLoadKind() == LoadKind::kRuntimeCall;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006236 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00006237
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006238 static SideEffects SideEffectsForArchRuntimeCalls() {
6239 return SideEffects::CanTriggerGC();
6240 }
6241
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006242 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006243 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006244 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006245 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006246
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006247 void MarkInBootImage() {
6248 SetPackedFlag<kFlagIsInBootImage>(true);
6249 }
6250
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006251 void AddSpecialInput(HInstruction* special_input);
6252
6253 using HInstruction::GetInputRecords; // Keep the const version visible.
6254 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
6255 return ArrayRef<HUserRecord<HInstruction*>>(
6256 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6257 }
6258
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006259 Handle<mirror::Class> GetClass() const {
6260 return klass_;
6261 }
6262
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006263 DECLARE_INSTRUCTION(LoadClass);
6264
Artem Serovcced8ba2017-07-19 18:18:09 +01006265 protected:
6266 DEFAULT_COPY_CONSTRUCTOR(LoadClass);
6267
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006268 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006269 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00006270 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006271 // Whether this instruction must generate the initialization check.
6272 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006273 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006274 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
6275 static constexpr size_t kFieldLoadKindSize =
6276 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
Vladimir Marko175e7862018-03-27 09:03:13 +00006277 static constexpr size_t kFlagValidLoadedClassRTI = kFieldLoadKind + kFieldLoadKindSize;
6278 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagValidLoadedClassRTI + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006279 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006280 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
6281
6282 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006283 return load_kind == LoadKind::kReferrersClass ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006284 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006285 load_kind == LoadKind::kBssEntry ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006286 load_kind == LoadKind::kRuntimeCall;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006287 }
6288
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006289 void SetLoadKindInternal(LoadKind load_kind);
6290
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006291 // The special input is the HCurrentMethod for kRuntimeCall or kReferrersClass.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006292 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006293 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006294 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006295
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006296 // A type index and dex file where the class can be accessed. The dex file can be:
6297 // - The compiling method's dex file if the class is defined there too.
6298 // - The compiling method's dex file if the class is referenced there.
6299 // - The dex file where the class is defined. When the load kind can only be
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006300 // kBssEntry or kRuntimeCall, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08006301 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006302 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006303
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006304 Handle<mirror::Class> klass_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006305};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006306std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
6307
6308// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006309inline void HLoadClass::SetLoadKind(LoadKind load_kind) {
6310 // The load kind should be determined before inserting the instruction to the graph.
6311 DCHECK(GetBlock() == nullptr);
6312 DCHECK(GetEnvironment() == nullptr);
6313 SetPackedField<LoadKindField>(load_kind);
6314 if (load_kind != LoadKind::kRuntimeCall && load_kind != LoadKind::kReferrersClass) {
6315 special_input_ = HUserRecord<HInstruction*>(nullptr);
6316 }
6317 if (!NeedsEnvironment()) {
6318 SetSideEffects(SideEffects::None());
6319 }
6320}
6321
6322// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006323inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006324 // The special input is used for PC-relative loads on some architectures,
6325 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006326 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006327 GetLoadKind() == LoadKind::kBootImageAddress ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006328 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006329 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006330 DCHECK(special_input_.GetInstruction() == nullptr);
6331 special_input_ = HUserRecord<HInstruction*>(special_input);
6332 special_input->AddUseAt(this, 0);
6333}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006334
Vladimir Marko372f10e2016-05-17 16:30:10 +01006335class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006336 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006337 // Determines how to load the String.
6338 enum class LoadKind {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006339 // Use PC-relative boot image String* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006340 // Used for boot image strings referenced by boot image code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006341 kBootImageLinkTimePcRelative,
6342
6343 // Use a known boot image String* address, embedded in the code by the codegen.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006344 // Used for boot image strings referenced by apps in JIT- and AOT-compiled code (non-PIC).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006345 kBootImageAddress,
6346
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006347 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
6348 // Used for boot image strings referenced by apps in AOT-compiled code (PIC).
6349 kBootImageRelRo,
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006350
Vladimir Markoaad75c62016-10-03 08:46:48 +00006351 // Load from an entry in the .bss section using a PC-relative load.
6352 // Used for strings outside boot image when .bss is accessible with a PC-relative load.
6353 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006354
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006355 // Load from the root table associated with the JIT compiled method.
6356 kJitTableAddress,
6357
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006358 // Load using a simple runtime call. This is the fall-back load kind when
6359 // the codegen is unable to use another appropriate kind.
6360 kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006361
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006362 kLast = kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006363 };
6364
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006365 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006366 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006367 const DexFile& dex_file,
6368 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006369 : HInstruction(kLoadString,
6370 DataType::Type::kReference,
6371 SideEffectsForArchRuntimeCalls(),
6372 dex_pc),
Vladimir Marko372f10e2016-05-17 16:30:10 +01006373 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006374 string_index_(string_index),
6375 dex_file_(dex_file) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006376 SetPackedField<LoadKindField>(LoadKind::kRuntimeCall);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006377 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006378
Artem Serovcced8ba2017-07-19 18:18:09 +01006379 bool IsClonable() const OVERRIDE { return true; }
6380
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006381 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006382
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006383 LoadKind GetLoadKind() const {
6384 return GetPackedField<LoadKindField>();
6385 }
6386
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006387 bool HasPcRelativeLoadKind() const {
6388 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6389 GetLoadKind() == LoadKind::kBootImageRelRo ||
6390 GetLoadKind() == LoadKind::kBssEntry;
6391 }
6392
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006393 const DexFile& GetDexFile() const {
6394 return dex_file_;
6395 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006396
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006397 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006398 return string_index_;
6399 }
6400
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006401 Handle<mirror::String> GetString() const {
6402 return string_;
6403 }
6404
6405 void SetString(Handle<mirror::String> str) {
6406 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006407 }
6408
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006409 bool CanBeMoved() const OVERRIDE { return true; }
6410
Vladimir Marko372f10e2016-05-17 16:30:10 +01006411 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006412
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006413 size_t ComputeHashCode() const OVERRIDE { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006414
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006415 // Will call the runtime if we need to load the string through
6416 // the dex cache and the string is not guaranteed to be there yet.
6417 bool NeedsEnvironment() const OVERRIDE {
6418 LoadKind load_kind = GetLoadKind();
Vladimir Marko764d4542017-05-16 10:31:41 +01006419 if (load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006420 load_kind == LoadKind::kBootImageAddress ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006421 load_kind == LoadKind::kBootImageRelRo ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006422 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006423 return false;
6424 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006425 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006426 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006427
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006428 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006429 return GetLoadKind() == LoadKind::kRuntimeCall;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006430 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006431
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07006432 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006433 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006434
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006435 static SideEffects SideEffectsForArchRuntimeCalls() {
6436 return SideEffects::CanTriggerGC();
6437 }
6438
Vladimir Marko372f10e2016-05-17 16:30:10 +01006439 void AddSpecialInput(HInstruction* special_input);
6440
6441 using HInstruction::GetInputRecords; // Keep the const version visible.
6442 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
6443 return ArrayRef<HUserRecord<HInstruction*>>(
6444 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006445 }
6446
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006447 DECLARE_INSTRUCTION(LoadString);
6448
Artem Serovcced8ba2017-07-19 18:18:09 +01006449 protected:
6450 DEFAULT_COPY_CONSTRUCTOR(LoadString);
6451
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006452 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006453 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006454 static constexpr size_t kFieldLoadKindSize =
6455 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
6456 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006457 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006458 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006459
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006460 void SetLoadKindInternal(LoadKind load_kind);
6461
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006462 // The special input is the HCurrentMethod for kRuntimeCall.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006463 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00006464 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01006465 HUserRecord<HInstruction*> special_input_;
6466
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006467 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006468 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006469
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006470 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006471};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006472std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
6473
6474// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006475inline void HLoadString::SetLoadKind(LoadKind load_kind) {
6476 // The load kind should be determined before inserting the instruction to the graph.
6477 DCHECK(GetBlock() == nullptr);
6478 DCHECK(GetEnvironment() == nullptr);
6479 DCHECK_EQ(GetLoadKind(), LoadKind::kRuntimeCall);
6480 SetPackedField<LoadKindField>(load_kind);
6481 if (load_kind != LoadKind::kRuntimeCall) {
6482 special_input_ = HUserRecord<HInstruction*>(nullptr);
6483 }
6484 if (!NeedsEnvironment()) {
6485 SetSideEffects(SideEffects::None());
6486 }
6487}
6488
6489// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006490inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006491 // The special input is used for PC-relative loads on some architectures,
6492 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006493 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006494 GetLoadKind() == LoadKind::kBootImageAddress ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006495 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006496 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01006497 // HLoadString::GetInputRecords() returns an empty array at this point,
6498 // so use the GetInputRecords() from the base class to set the input record.
6499 DCHECK(special_input_.GetInstruction() == nullptr);
6500 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006501 special_input->AddUseAt(this, 0);
6502}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006503
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006504class HLoadMethodHandle FINAL : public HInstruction {
6505 public:
6506 HLoadMethodHandle(HCurrentMethod* current_method,
6507 uint16_t method_handle_idx,
6508 const DexFile& dex_file,
6509 uint32_t dex_pc)
6510 : HInstruction(kLoadMethodHandle,
6511 DataType::Type::kReference,
6512 SideEffectsForArchRuntimeCalls(),
6513 dex_pc),
6514 special_input_(HUserRecord<HInstruction*>(current_method)),
6515 method_handle_idx_(method_handle_idx),
6516 dex_file_(dex_file) {
6517 }
6518
6519 using HInstruction::GetInputRecords; // Keep the const version visible.
6520 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
6521 return ArrayRef<HUserRecord<HInstruction*>>(
6522 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6523 }
6524
6525 bool IsClonable() const OVERRIDE { return true; }
6526
6527 uint16_t GetMethodHandleIndex() const { return method_handle_idx_; }
6528
6529 const DexFile& GetDexFile() const { return dex_file_; }
6530
6531 static SideEffects SideEffectsForArchRuntimeCalls() {
6532 return SideEffects::CanTriggerGC();
6533 }
6534
6535 DECLARE_INSTRUCTION(LoadMethodHandle);
6536
6537 protected:
6538 DEFAULT_COPY_CONSTRUCTOR(LoadMethodHandle);
6539
6540 private:
6541 // The special input is the HCurrentMethod for kRuntimeCall.
6542 HUserRecord<HInstruction*> special_input_;
6543
6544 const uint16_t method_handle_idx_;
6545 const DexFile& dex_file_;
6546};
6547
Orion Hodson18259d72018-04-12 11:18:23 +01006548class HLoadMethodType FINAL : public HInstruction {
6549 public:
6550 HLoadMethodType(HCurrentMethod* current_method,
Orion Hodson06d10a72018-05-14 08:53:38 +01006551 dex::ProtoIndex proto_index,
Orion Hodson18259d72018-04-12 11:18:23 +01006552 const DexFile& dex_file,
6553 uint32_t dex_pc)
6554 : HInstruction(kLoadMethodType,
6555 DataType::Type::kReference,
6556 SideEffectsForArchRuntimeCalls(),
6557 dex_pc),
6558 special_input_(HUserRecord<HInstruction*>(current_method)),
Orion Hodson06d10a72018-05-14 08:53:38 +01006559 proto_index_(proto_index),
Orion Hodson18259d72018-04-12 11:18:23 +01006560 dex_file_(dex_file) {
6561 }
6562
6563 using HInstruction::GetInputRecords; // Keep the const version visible.
6564 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
6565 return ArrayRef<HUserRecord<HInstruction*>>(
6566 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6567 }
6568
6569 bool IsClonable() const OVERRIDE { return true; }
6570
Orion Hodson06d10a72018-05-14 08:53:38 +01006571 dex::ProtoIndex GetProtoIndex() const { return proto_index_; }
Orion Hodson18259d72018-04-12 11:18:23 +01006572
6573 const DexFile& GetDexFile() const { return dex_file_; }
6574
6575 static SideEffects SideEffectsForArchRuntimeCalls() {
6576 return SideEffects::CanTriggerGC();
6577 }
6578
6579 DECLARE_INSTRUCTION(LoadMethodType);
6580
6581 protected:
6582 DEFAULT_COPY_CONSTRUCTOR(LoadMethodType);
6583
6584 private:
6585 // The special input is the HCurrentMethod for kRuntimeCall.
6586 HUserRecord<HInstruction*> special_input_;
6587
Orion Hodson06d10a72018-05-14 08:53:38 +01006588 const dex::ProtoIndex proto_index_;
Orion Hodson18259d72018-04-12 11:18:23 +01006589 const DexFile& dex_file_;
6590};
6591
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006592/**
6593 * Performs an initialization check on its Class object input.
6594 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01006595class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006596 public:
Roland Levillain3887c462015-08-12 18:15:42 +01006597 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07006598 : HExpression(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306599 kClinitCheck,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006600 DataType::Type::kReference,
Mingyao Yang217eb062017-12-11 15:20:07 -08006601 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006602 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006603 SetRawInputAt(0, constant);
6604 }
6605
Artem Serovcced8ba2017-07-19 18:18:09 +01006606 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006607 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006608 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006609 return true;
6610 }
6611
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006612 bool NeedsEnvironment() const OVERRIDE {
6613 // May call runtime to initialize the class.
6614 return true;
6615 }
6616
Nicolas Geoffray729645a2015-11-19 13:29:02 +00006617 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006618
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006619 HLoadClass* GetLoadClass() const {
6620 DCHECK(InputAt(0)->IsLoadClass());
6621 return InputAt(0)->AsLoadClass();
6622 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006623
6624 DECLARE_INSTRUCTION(ClinitCheck);
6625
Artem Serovcced8ba2017-07-19 18:18:09 +01006626
6627 protected:
6628 DEFAULT_COPY_CONSTRUCTOR(ClinitCheck);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006629};
6630
Vladimir Markofcb503c2016-05-18 12:48:17 +01006631class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006632 public:
6633 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006634 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006635 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006636 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006637 bool is_volatile,
6638 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006639 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006640 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006641 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306642 : HExpression(kStaticFieldGet,
6643 field_type,
6644 SideEffects::FieldReadOfType(field_type, is_volatile),
6645 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006646 field_info_(field,
6647 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006648 field_type,
6649 is_volatile,
6650 field_idx,
6651 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006652 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006653 SetRawInputAt(0, cls);
6654 }
6655
Calin Juravle52c48962014-12-16 17:02:57 +00006656
Artem Serovcced8ba2017-07-19 18:18:09 +01006657 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle10c9cbe2014-12-19 10:50:19 +00006658 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006659
Vladimir Marko372f10e2016-05-17 16:30:10 +01006660 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
6661 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006662 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006663 }
6664
6665 size_t ComputeHashCode() const OVERRIDE {
6666 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6667 }
6668
Calin Juravle52c48962014-12-16 17:02:57 +00006669 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006670 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006671 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006672 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006673
Vladimir Marko61b92282017-10-11 13:23:17 +01006674 void SetType(DataType::Type new_type) {
6675 DCHECK(DataType::IsIntegralType(GetType()));
6676 DCHECK(DataType::IsIntegralType(new_type));
6677 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6678 SetPackedField<TypeField>(new_type);
6679 }
6680
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006681 DECLARE_INSTRUCTION(StaticFieldGet);
6682
Artem Serovcced8ba2017-07-19 18:18:09 +01006683 protected:
6684 DEFAULT_COPY_CONSTRUCTOR(StaticFieldGet);
6685
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006686 private:
6687 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006688};
6689
Vladimir Markobd785672018-05-03 17:09:09 +01006690class HStaticFieldSet FINAL : public HExpression<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006691 public:
6692 HStaticFieldSet(HInstruction* cls,
6693 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006694 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006695 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006696 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006697 bool is_volatile,
6698 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006699 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006700 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006701 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006702 : HExpression(kStaticFieldSet,
6703 SideEffects::FieldWriteOfType(field_type, is_volatile),
6704 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006705 field_info_(field,
6706 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006707 field_type,
6708 is_volatile,
6709 field_idx,
6710 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006711 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006712 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006713 SetRawInputAt(0, cls);
6714 SetRawInputAt(1, value);
6715 }
6716
Artem Serovcced8ba2017-07-19 18:18:09 +01006717 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle52c48962014-12-16 17:02:57 +00006718 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006719 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006720 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006721 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006722
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006723 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006724 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6725 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006726
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006727 DECLARE_INSTRUCTION(StaticFieldSet);
6728
Artem Serovcced8ba2017-07-19 18:18:09 +01006729 protected:
6730 DEFAULT_COPY_CONSTRUCTOR(StaticFieldSet);
6731
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006732 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006733 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6734 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6735 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6736 "Too many packed fields.");
6737
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006738 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006739};
6740
Vladimir Markofcb503c2016-05-18 12:48:17 +01006741class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006742 public:
6743 HUnresolvedInstanceFieldGet(HInstruction* obj,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006744 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006745 uint32_t field_index,
6746 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306747 : HExpression(kUnresolvedInstanceFieldGet,
6748 field_type,
6749 SideEffects::AllExceptGCDependency(),
6750 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006751 field_index_(field_index) {
6752 SetRawInputAt(0, obj);
6753 }
6754
Artem Serovcced8ba2017-07-19 18:18:09 +01006755 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006756 bool NeedsEnvironment() const OVERRIDE { return true; }
6757 bool CanThrow() const OVERRIDE { return true; }
6758
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006759 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006760 uint32_t GetFieldIndex() const { return field_index_; }
6761
6762 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6763
Artem Serovcced8ba2017-07-19 18:18:09 +01006764 protected:
6765 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldGet);
6766
Calin Juravlee460d1d2015-09-29 04:52:17 +01006767 private:
6768 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006769};
6770
Vladimir Markobd785672018-05-03 17:09:09 +01006771class HUnresolvedInstanceFieldSet FINAL : public HExpression<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006772 public:
6773 HUnresolvedInstanceFieldSet(HInstruction* obj,
6774 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006775 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006776 uint32_t field_index,
6777 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006778 : HExpression(kUnresolvedInstanceFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006779 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006780 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006781 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006782 SetRawInputAt(0, obj);
6783 SetRawInputAt(1, value);
6784 }
6785
Artem Serovcced8ba2017-07-19 18:18:09 +01006786 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006787 bool NeedsEnvironment() const OVERRIDE { return true; }
6788 bool CanThrow() const OVERRIDE { return true; }
6789
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006790 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006791 uint32_t GetFieldIndex() const { return field_index_; }
6792
6793 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6794
Artem Serovcced8ba2017-07-19 18:18:09 +01006795 protected:
6796 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldSet);
6797
Calin Juravlee460d1d2015-09-29 04:52:17 +01006798 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006799 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6800 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006801 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006802 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6803 kFieldFieldType + kFieldFieldTypeSize;
6804 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6805 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006806 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006807
Calin Juravlee460d1d2015-09-29 04:52:17 +01006808 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006809};
6810
Vladimir Markofcb503c2016-05-18 12:48:17 +01006811class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006812 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006813 HUnresolvedStaticFieldGet(DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006814 uint32_t field_index,
6815 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306816 : HExpression(kUnresolvedStaticFieldGet,
6817 field_type,
6818 SideEffects::AllExceptGCDependency(),
6819 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006820 field_index_(field_index) {
6821 }
6822
Artem Serovcced8ba2017-07-19 18:18:09 +01006823 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006824 bool NeedsEnvironment() const OVERRIDE { return true; }
6825 bool CanThrow() const OVERRIDE { return true; }
6826
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006827 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006828 uint32_t GetFieldIndex() const { return field_index_; }
6829
6830 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6831
Artem Serovcced8ba2017-07-19 18:18:09 +01006832 protected:
6833 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldGet);
6834
Calin Juravlee460d1d2015-09-29 04:52:17 +01006835 private:
6836 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006837};
6838
Vladimir Markobd785672018-05-03 17:09:09 +01006839class HUnresolvedStaticFieldSet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006840 public:
6841 HUnresolvedStaticFieldSet(HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006842 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006843 uint32_t field_index,
6844 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006845 : HExpression(kUnresolvedStaticFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006846 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006847 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006848 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006849 SetRawInputAt(0, value);
6850 }
6851
Artem Serovcced8ba2017-07-19 18:18:09 +01006852 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006853 bool NeedsEnvironment() const OVERRIDE { return true; }
6854 bool CanThrow() const OVERRIDE { return true; }
6855
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006856 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006857 uint32_t GetFieldIndex() const { return field_index_; }
6858
6859 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6860
Artem Serovcced8ba2017-07-19 18:18:09 +01006861 protected:
6862 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldSet);
6863
Calin Juravlee460d1d2015-09-29 04:52:17 +01006864 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006865 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6866 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006867 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006868 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6869 kFieldFieldType + kFieldFieldTypeSize;
6870 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6871 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006872 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006873
Calin Juravlee460d1d2015-09-29 04:52:17 +01006874 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006875};
6876
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006877// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006878class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006879 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006880 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306881 : HExpression(kLoadException, DataType::Type::kReference, SideEffects::None(), dex_pc) {
6882 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006883
David Brazdilbbd733e2015-08-18 17:48:17 +01006884 bool CanBeNull() const OVERRIDE { return false; }
6885
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006886 DECLARE_INSTRUCTION(LoadException);
6887
Artem Serovcced8ba2017-07-19 18:18:09 +01006888 protected:
6889 DEFAULT_COPY_CONSTRUCTOR(LoadException);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006890};
6891
David Brazdilcb1c0552015-08-04 16:22:25 +01006892// Implicit part of move-exception which clears thread-local exception storage.
6893// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markobd785672018-05-03 17:09:09 +01006894class HClearException FINAL : public HExpression<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006895 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006896 explicit HClearException(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01006897 : HExpression(kClearException, SideEffects::AllWrites(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306898 }
David Brazdilcb1c0552015-08-04 16:22:25 +01006899
6900 DECLARE_INSTRUCTION(ClearException);
6901
Artem Serovcced8ba2017-07-19 18:18:09 +01006902 protected:
6903 DEFAULT_COPY_CONSTRUCTOR(ClearException);
David Brazdilcb1c0552015-08-04 16:22:25 +01006904};
6905
Vladimir Markobd785672018-05-03 17:09:09 +01006906class HThrow FINAL : public HExpression<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006907 public:
6908 HThrow(HInstruction* exception, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006909 : HExpression(kThrow, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006910 SetRawInputAt(0, exception);
6911 }
6912
6913 bool IsControlFlow() const OVERRIDE { return true; }
6914
6915 bool NeedsEnvironment() const OVERRIDE { return true; }
6916
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006917 bool CanThrow() const OVERRIDE { return true; }
6918
Aart Bika8b8e9b2018-01-09 11:01:02 -08006919 bool AlwaysThrows() const OVERRIDE { return true; }
6920
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006921 DECLARE_INSTRUCTION(Throw);
6922
Artem Serovcced8ba2017-07-19 18:18:09 +01006923 protected:
6924 DEFAULT_COPY_CONSTRUCTOR(Throw);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006925};
6926
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006927/**
6928 * Implementation strategies for the code generator of a HInstanceOf
6929 * or `HCheckCast`.
6930 */
6931enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006932 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006933 kExactCheck, // Can do a single class compare.
6934 kClassHierarchyCheck, // Can just walk the super class chain.
6935 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6936 kInterfaceCheck, // No optimization yet when checking against an interface.
6937 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006938 kArrayCheck, // No optimization yet when checking against a generic array.
Vladimir Marko175e7862018-03-27 09:03:13 +00006939 kBitstringCheck, // Compare the type check bitstring.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006940 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006941};
6942
Roland Levillain86503782016-02-11 19:07:30 +00006943std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6944
Vladimir Marko175e7862018-03-27 09:03:13 +00006945// Note: HTypeCheckInstruction is just a helper class, not an abstract instruction with an
6946// `IsTypeCheckInstruction()`. (New virtual methods in the HInstruction class have a high cost.)
6947class HTypeCheckInstruction : public HVariableInputSizeInstruction {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006948 public:
Vladimir Marko175e7862018-03-27 09:03:13 +00006949 HTypeCheckInstruction(InstructionKind kind,
Vladimir Markobd785672018-05-03 17:09:09 +01006950 DataType::Type type,
Vladimir Marko175e7862018-03-27 09:03:13 +00006951 HInstruction* object,
6952 HInstruction* target_class_or_null,
6953 TypeCheckKind check_kind,
6954 Handle<mirror::Class> klass,
6955 uint32_t dex_pc,
6956 ArenaAllocator* allocator,
6957 HIntConstant* bitstring_path_to_root,
6958 HIntConstant* bitstring_mask,
6959 SideEffects side_effects)
6960 : HVariableInputSizeInstruction(
6961 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01006962 type,
Vladimir Marko175e7862018-03-27 09:03:13 +00006963 side_effects,
6964 dex_pc,
6965 allocator,
6966 /* number_of_inputs */ check_kind == TypeCheckKind::kBitstringCheck ? 4u : 2u,
6967 kArenaAllocTypeCheckInputs),
6968 klass_(klass) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006969 SetPackedField<TypeCheckKindField>(check_kind);
6970 SetPackedFlag<kFlagMustDoNullCheck>(true);
Vladimir Marko175e7862018-03-27 09:03:13 +00006971 SetPackedFlag<kFlagValidTargetClassRTI>(false);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006972 SetRawInputAt(0, object);
Vladimir Marko175e7862018-03-27 09:03:13 +00006973 SetRawInputAt(1, target_class_or_null);
6974 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_path_to_root != nullptr);
6975 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_mask != nullptr);
6976 if (check_kind == TypeCheckKind::kBitstringCheck) {
6977 DCHECK(target_class_or_null->IsNullConstant());
6978 SetRawInputAt(2, bitstring_path_to_root);
6979 SetRawInputAt(3, bitstring_mask);
6980 } else {
6981 DCHECK(target_class_or_null->IsLoadClass());
6982 }
Vladimir Marko87584542017-12-12 17:47:52 +00006983 }
6984
6985 HLoadClass* GetTargetClass() const {
Vladimir Marko175e7862018-03-27 09:03:13 +00006986 DCHECK_NE(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
Vladimir Marko87584542017-12-12 17:47:52 +00006987 HInstruction* load_class = InputAt(1);
6988 DCHECK(load_class->IsLoadClass());
6989 return load_class->AsLoadClass();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006990 }
6991
Vladimir Marko175e7862018-03-27 09:03:13 +00006992 uint32_t GetBitstringPathToRoot() const {
6993 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
6994 HInstruction* path_to_root = InputAt(2);
6995 DCHECK(path_to_root->IsIntConstant());
6996 return static_cast<uint32_t>(path_to_root->AsIntConstant()->GetValue());
6997 }
6998
6999 uint32_t GetBitstringMask() const {
7000 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7001 HInstruction* mask = InputAt(3);
7002 DCHECK(mask->IsIntConstant());
7003 return static_cast<uint32_t>(mask->AsIntConstant()->GetValue());
7004 }
7005
Artem Serovcced8ba2017-07-19 18:18:09 +01007006 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007007 bool CanBeMoved() const OVERRIDE { return true; }
7008
Vladimir Marko175e7862018-03-27 09:03:13 +00007009 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
7010 DCHECK(other->IsInstanceOf() || other->IsCheckCast()) << other->DebugName();
7011 return GetPackedFields() == down_cast<const HTypeCheckInstruction*>(other)->GetPackedFields();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007012 }
7013
Andreas Gampe3fbd3ad2018-03-26 21:14:46 +00007014 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
7015 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
7016 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
7017 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
7018
Vladimir Marko175e7862018-03-27 09:03:13 +00007019 ReferenceTypeInfo GetTargetClassRTI() {
7020 if (GetPackedFlag<kFlagValidTargetClassRTI>()) {
7021 // Note: The is_exact flag from the return value should not be used.
7022 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact */ true);
7023 } else {
7024 return ReferenceTypeInfo::CreateInvalid();
7025 }
7026 }
7027
7028 // Target class RTI is marked as valid by RTP if the klass_ is admissible.
7029 void SetValidTargetClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
7030 DCHECK(klass_ != nullptr);
7031 SetPackedFlag<kFlagValidTargetClassRTI>(true);
7032 }
7033
7034 Handle<mirror::Class> GetClass() const {
7035 return klass_;
7036 }
7037
7038 protected:
7039 DEFAULT_COPY_CONSTRUCTOR(TypeCheckInstruction);
7040
7041 private:
7042 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
7043 static constexpr size_t kFieldTypeCheckKindSize =
7044 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
7045 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
7046 static constexpr size_t kFlagValidTargetClassRTI = kFlagMustDoNullCheck + 1;
7047 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagValidTargetClassRTI + 1;
7048 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7049 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
7050
7051 Handle<mirror::Class> klass_;
7052};
7053
7054class HInstanceOf FINAL : public HTypeCheckInstruction {
7055 public:
7056 HInstanceOf(HInstruction* object,
7057 HInstruction* target_class_or_null,
7058 TypeCheckKind check_kind,
7059 Handle<mirror::Class> klass,
7060 uint32_t dex_pc,
7061 ArenaAllocator* allocator,
7062 HIntConstant* bitstring_path_to_root,
7063 HIntConstant* bitstring_mask)
7064 : HTypeCheckInstruction(kInstanceOf,
Vladimir Markobd785672018-05-03 17:09:09 +01007065 DataType::Type::kBool,
Vladimir Marko175e7862018-03-27 09:03:13 +00007066 object,
7067 target_class_or_null,
7068 check_kind,
7069 klass,
7070 dex_pc,
7071 allocator,
7072 bitstring_path_to_root,
7073 bitstring_mask,
7074 SideEffectsForArchRuntimeCalls(check_kind)) {}
7075
Vladimir Marko175e7862018-03-27 09:03:13 +00007076 bool NeedsEnvironment() const OVERRIDE {
7077 return CanCallRuntime(GetTypeCheckKind());
7078 }
7079
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007080 static bool CanCallRuntime(TypeCheckKind check_kind) {
7081 // Mips currently does runtime calls for any other checks.
7082 return check_kind != TypeCheckKind::kExactCheck;
7083 }
7084
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007085 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007086 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01007087 }
7088
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007089 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007090
Artem Serovcced8ba2017-07-19 18:18:09 +01007091 protected:
7092 DEFAULT_COPY_CONSTRUCTOR(InstanceOf);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007093};
7094
Vladimir Markofcb503c2016-05-18 12:48:17 +01007095class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00007096 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07007097 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307098 : HExpression(kBoundType, DataType::Type::kReference, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00007099 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
7100 SetPackedFlag<kFlagUpperCanBeNull>(true);
7101 SetPackedFlag<kFlagCanBeNull>(true);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007102 DCHECK_EQ(input->GetType(), DataType::Type::kReference);
Calin Juravleb1498f62015-02-16 13:13:29 +00007103 SetRawInputAt(0, input);
7104 }
7105
Artem Serovcced8ba2017-07-19 18:18:09 +01007106 bool IsClonable() const OVERRIDE { return true; }
7107
David Brazdilf5552582015-12-27 13:36:12 +00007108 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007109 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00007110 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00007111 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007112
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007113 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007114 DCHECK(GetUpperCanBeNull() || !can_be_null);
7115 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007116 }
7117
Vladimir Markoa1de9182016-02-25 11:37:38 +00007118 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007119
Calin Juravleb1498f62015-02-16 13:13:29 +00007120 DECLARE_INSTRUCTION(BoundType);
7121
Artem Serovcced8ba2017-07-19 18:18:09 +01007122 protected:
7123 DEFAULT_COPY_CONSTRUCTOR(BoundType);
7124
Calin Juravleb1498f62015-02-16 13:13:29 +00007125 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007126 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
7127 // is false then CanBeNull() cannot be true).
Vladimir Markobd785672018-05-03 17:09:09 +01007128 static constexpr size_t kFlagUpperCanBeNull = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007129 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
7130 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
7131 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7132
Calin Juravleb1498f62015-02-16 13:13:29 +00007133 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007134 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
7135 // It is used to bound the type in cases like:
7136 // if (x instanceof ClassX) {
7137 // // uper_bound_ will be ClassX
7138 // }
David Brazdilf5552582015-12-27 13:36:12 +00007139 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00007140};
7141
Vladimir Marko175e7862018-03-27 09:03:13 +00007142class HCheckCast FINAL : public HTypeCheckInstruction {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007143 public:
7144 HCheckCast(HInstruction* object,
Vladimir Marko175e7862018-03-27 09:03:13 +00007145 HInstruction* target_class_or_null,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007146 TypeCheckKind check_kind,
Vladimir Marko175e7862018-03-27 09:03:13 +00007147 Handle<mirror::Class> klass,
7148 uint32_t dex_pc,
7149 ArenaAllocator* allocator,
7150 HIntConstant* bitstring_path_to_root,
7151 HIntConstant* bitstring_mask)
7152 : HTypeCheckInstruction(kCheckCast,
Vladimir Markobd785672018-05-03 17:09:09 +01007153 DataType::Type::kVoid,
Vladimir Marko175e7862018-03-27 09:03:13 +00007154 object,
7155 target_class_or_null,
7156 check_kind,
7157 klass,
7158 dex_pc,
7159 allocator,
7160 bitstring_path_to_root,
7161 bitstring_mask,
7162 SideEffects::CanTriggerGC()) {}
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007163
7164 bool NeedsEnvironment() const OVERRIDE {
7165 // Instruction may throw a CheckCastError.
7166 return true;
7167 }
7168
7169 bool CanThrow() const OVERRIDE { return true; }
7170
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007171 DECLARE_INSTRUCTION(CheckCast);
7172
Artem Serovcced8ba2017-07-19 18:18:09 +01007173 protected:
7174 DEFAULT_COPY_CONSTRUCTOR(CheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007175};
7176
Andreas Gampe26de38b2016-07-27 17:53:11 -07007177/**
7178 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
7179 * @details We define the combined barrier types that are actually required
7180 * by the Java Memory Model, rather than using exactly the terminology from
7181 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
7182 * primitives. Note that the JSR-133 cookbook generally does not deal with
7183 * store atomicity issues, and the recipes there are not always entirely sufficient.
7184 * The current recipe is as follows:
7185 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
7186 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
7187 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
7188 * -# Use StoreStore barrier after all stores but before return from any constructor whose
7189 * class has final fields.
7190 * -# Use NTStoreStore to order non-temporal stores with respect to all later
7191 * store-to-memory instructions. Only generated together with non-temporal stores.
7192 */
7193enum MemBarrierKind {
7194 kAnyStore,
7195 kLoadAny,
7196 kStoreStore,
7197 kAnyAny,
7198 kNTStoreStore,
7199 kLastBarrierKind = kNTStoreStore
7200};
7201std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
7202
Vladimir Markobd785672018-05-03 17:09:09 +01007203class HMemoryBarrier FINAL : public HExpression<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01007204 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007205 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007206 : HExpression(kMemoryBarrier,
7207 SideEffects::AllWritesAndReads(), // Assume write/read on all fields/arrays.
7208 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007209 SetPackedField<BarrierKindField>(barrier_kind);
7210 }
Calin Juravle27df7582015-04-17 19:12:31 +01007211
Artem Serovcced8ba2017-07-19 18:18:09 +01007212 bool IsClonable() const OVERRIDE { return true; }
7213
Vladimir Markoa1de9182016-02-25 11:37:38 +00007214 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01007215
7216 DECLARE_INSTRUCTION(MemoryBarrier);
7217
Artem Serovcced8ba2017-07-19 18:18:09 +01007218 protected:
7219 DEFAULT_COPY_CONSTRUCTOR(MemoryBarrier);
7220
Calin Juravle27df7582015-04-17 19:12:31 +01007221 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007222 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
7223 static constexpr size_t kFieldBarrierKindSize =
7224 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
7225 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
7226 kFieldBarrierKind + kFieldBarrierKindSize;
7227 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
7228 "Too many packed fields.");
7229 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01007230};
7231
Igor Murashkind01745e2017-04-05 16:40:31 -07007232// A constructor fence orders all prior stores to fields that could be accessed via a final field of
7233// the specified object(s), with respect to any subsequent store that might "publish"
7234// (i.e. make visible) the specified object to another thread.
7235//
7236// JLS 17.5.1 "Semantics of final fields" states that a freeze action happens
7237// for all final fields (that were set) at the end of the invoked constructor.
7238//
7239// The constructor fence models the freeze actions for the final fields of an object
7240// being constructed (semantically at the end of the constructor). Constructor fences
7241// have a per-object affinity; two separate objects being constructed get two separate
7242// constructor fences.
7243//
7244// (Note: that if calling a super-constructor or forwarding to another constructor,
7245// the freezes would happen at the end of *that* constructor being invoked).
7246//
7247// The memory model guarantees that when the object being constructed is "published" after
7248// constructor completion (i.e. escapes the current thread via a store), then any final field
7249// writes must be observable on other threads (once they observe that publication).
7250//
7251// Further, anything written before the freeze, and read by dereferencing through the final field,
7252// must also be visible (so final object field could itself have an object with non-final fields;
7253// yet the freeze must also extend to them).
7254//
7255// Constructor example:
7256//
7257// class HasFinal {
7258// final int field; Optimizing IR for <init>()V:
7259// HasFinal() {
7260// field = 123; HInstanceFieldSet(this, HasFinal.field, 123)
7261// // freeze(this.field); HConstructorFence(this)
7262// } HReturn
7263// }
7264//
7265// HConstructorFence can serve double duty as a fence for new-instance/new-array allocations of
7266// already-initialized classes; in that case the allocation must act as a "default-initializer"
7267// of the object which effectively writes the class pointer "final field".
7268//
7269// For example, we can model default-initialiation as roughly the equivalent of the following:
7270//
7271// class Object {
7272// private final Class header;
7273// }
7274//
7275// Java code: Optimizing IR:
7276//
7277// T new_instance<T>() {
7278// Object obj = allocate_memory(T.class.size); obj = HInvoke(art_quick_alloc_object, T)
7279// obj.header = T.class; // header write is done by above call.
7280// // freeze(obj.header) HConstructorFence(obj)
7281// return (T)obj;
7282// }
7283//
7284// See also:
7285// * CompilerDriver::RequiresConstructorBarrier
7286// * QuasiAtomic::ThreadFenceForConstructor
7287//
7288class HConstructorFence FINAL : public HVariableInputSizeInstruction {
7289 // A fence has variable inputs because the inputs can be removed
7290 // after prepare_for_register_allocation phase.
7291 // (TODO: In the future a fence could freeze multiple objects
7292 // after merging two fences together.)
7293 public:
7294 // `fence_object` is the reference that needs to be protected for correct publication.
7295 //
7296 // It makes sense in the following situations:
7297 // * <init> constructors, it's the "this" parameter (i.e. HParameterValue, s.t. IsThis() == true).
7298 // * new-instance-like instructions, it's the return value (i.e. HNewInstance).
7299 //
7300 // After construction the `fence_object` becomes the 0th input.
7301 // This is not an input in a real sense, but just a convenient place to stash the information
7302 // about the associated object.
7303 HConstructorFence(HInstruction* fence_object,
7304 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007305 ArenaAllocator* allocator)
Igor Murashkind01745e2017-04-05 16:40:31 -07007306 // We strongly suspect there is not a more accurate way to describe the fine-grained reordering
7307 // constraints described in the class header. We claim that these SideEffects constraints
7308 // enforce a superset of the real constraints.
7309 //
7310 // The ordering described above is conservatively modeled with SideEffects as follows:
7311 //
7312 // * To prevent reordering of the publication stores:
7313 // ----> "Reads of objects" is the initial SideEffect.
7314 // * For every primitive final field store in the constructor:
7315 // ----> Union that field's type as a read (e.g. "Read of T") into the SideEffect.
7316 // * If there are any stores to reference final fields in the constructor:
7317 // ----> Use a more conservative "AllReads" SideEffect because any stores to any references
7318 // that are reachable from `fence_object` also need to be prevented for reordering
7319 // (and we do not want to do alias analysis to figure out what those stores are).
7320 //
7321 // In the implementation, this initially starts out as an "all reads" side effect; this is an
7322 // even more conservative approach than the one described above, and prevents all of the
7323 // above reordering without analyzing any of the instructions in the constructor.
7324 //
7325 // If in a later phase we discover that there are no writes to reference final fields,
7326 // we can refine the side effect to a smaller set of type reads (see above constraints).
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307327 : HVariableInputSizeInstruction(kConstructorFence,
7328 SideEffects::AllReads(),
Igor Murashkind01745e2017-04-05 16:40:31 -07007329 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007330 allocator,
Igor Murashkind01745e2017-04-05 16:40:31 -07007331 /* number_of_inputs */ 1,
7332 kArenaAllocConstructorFenceInputs) {
7333 DCHECK(fence_object != nullptr);
7334 SetRawInputAt(0, fence_object);
7335 }
7336
7337 // The object associated with this constructor fence.
7338 //
7339 // (Note: This will be null after the prepare_for_register_allocation phase,
7340 // as all constructor fence inputs are removed there).
7341 HInstruction* GetFenceObject() const {
7342 return InputAt(0);
7343 }
7344
7345 // Find all the HConstructorFence uses (`fence_use`) for `this` and:
7346 // - Delete `fence_use` from `this`'s use list.
7347 // - Delete `this` from `fence_use`'s inputs list.
7348 // - If the `fence_use` is dead, remove it from the graph.
7349 //
7350 // A fence is considered dead once it no longer has any uses
7351 // and all of the inputs are dead.
7352 //
7353 // This must *not* be called during/after prepare_for_register_allocation,
7354 // because that removes all the inputs to the fences but the fence is actually
7355 // still considered live.
Igor Murashkin6ef45672017-08-08 13:59:55 -07007356 //
7357 // Returns how many HConstructorFence instructions were removed from graph.
7358 static size_t RemoveConstructorFences(HInstruction* instruction);
Igor Murashkind01745e2017-04-05 16:40:31 -07007359
Igor Murashkindd018df2017-08-09 10:38:31 -07007360 // Combine all inputs of `this` and `other` instruction and remove
7361 // `other` from the graph.
7362 //
7363 // Inputs are unique after the merge.
7364 //
7365 // Requirement: `this` must not be the same as `other.
7366 void Merge(HConstructorFence* other);
7367
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007368 // Check if this constructor fence is protecting
7369 // an HNewInstance or HNewArray that is also the immediate
7370 // predecessor of `this`.
7371 //
Igor Murashkindd018df2017-08-09 10:38:31 -07007372 // If `ignore_inputs` is true, then the immediate predecessor doesn't need
7373 // to be one of the inputs of `this`.
7374 //
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007375 // Returns the associated HNewArray or HNewInstance,
7376 // or null otherwise.
Igor Murashkindd018df2017-08-09 10:38:31 -07007377 HInstruction* GetAssociatedAllocation(bool ignore_inputs = false);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007378
Igor Murashkind01745e2017-04-05 16:40:31 -07007379 DECLARE_INSTRUCTION(ConstructorFence);
7380
Artem Serovcced8ba2017-07-19 18:18:09 +01007381 protected:
7382 DEFAULT_COPY_CONSTRUCTOR(ConstructorFence);
Igor Murashkind01745e2017-04-05 16:40:31 -07007383};
7384
Vladimir Markobd785672018-05-03 17:09:09 +01007385class HMonitorOperation FINAL : public HExpression<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007386 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007387 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007388 kEnter,
7389 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00007390 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007391 };
7392
7393 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007394 : HExpression(kMonitorOperation,
7395 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
7396 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007397 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007398 SetRawInputAt(0, object);
7399 }
7400
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007401 // Instruction may go into runtime, so we need an environment.
7402 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00007403
7404 bool CanThrow() const OVERRIDE {
7405 // Verifier guarantees that monitor-exit cannot throw.
7406 // This is important because it allows the HGraphBuilder to remove
7407 // a dead throw-catch loop generated for `synchronized` blocks/methods.
7408 return IsEnter();
7409 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007410
Vladimir Markoa1de9182016-02-25 11:37:38 +00007411 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
7412 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007413
7414 DECLARE_INSTRUCTION(MonitorOperation);
7415
Artem Serovcced8ba2017-07-19 18:18:09 +01007416 protected:
7417 DEFAULT_COPY_CONSTRUCTOR(MonitorOperation);
7418
Calin Juravle52c48962014-12-16 17:02:57 +00007419 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007420 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
7421 static constexpr size_t kFieldOperationKindSize =
7422 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
7423 static constexpr size_t kNumberOfMonitorOperationPackedBits =
7424 kFieldOperationKind + kFieldOperationKindSize;
7425 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7426 "Too many packed fields.");
7427 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007428};
7429
Vladimir Markofcb503c2016-05-18 12:48:17 +01007430class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00007431 public:
7432 HSelect(HInstruction* condition,
7433 HInstruction* true_value,
7434 HInstruction* false_value,
7435 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307436 : HExpression(kSelect, HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
David Brazdil74eb1b22015-12-14 11:44:01 +00007437 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
7438
7439 // First input must be `true_value` or `false_value` to allow codegens to
7440 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
7441 // that architectures which implement HSelect as a conditional move also
7442 // will not need to invert the condition.
7443 SetRawInputAt(0, false_value);
7444 SetRawInputAt(1, true_value);
7445 SetRawInputAt(2, condition);
7446 }
7447
Artem Serovcced8ba2017-07-19 18:18:09 +01007448 bool IsClonable() const OVERRIDE { return true; }
David Brazdil74eb1b22015-12-14 11:44:01 +00007449 HInstruction* GetFalseValue() const { return InputAt(0); }
7450 HInstruction* GetTrueValue() const { return InputAt(1); }
7451 HInstruction* GetCondition() const { return InputAt(2); }
7452
7453 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01007454 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
7455 return true;
7456 }
David Brazdil74eb1b22015-12-14 11:44:01 +00007457
7458 bool CanBeNull() const OVERRIDE {
7459 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
7460 }
7461
7462 DECLARE_INSTRUCTION(Select);
7463
Artem Serovcced8ba2017-07-19 18:18:09 +01007464 protected:
7465 DEFAULT_COPY_CONSTRUCTOR(Select);
David Brazdil74eb1b22015-12-14 11:44:01 +00007466};
7467
Vladimir Markof9f64412015-09-02 14:05:49 +01007468class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007469 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01007470 MoveOperands(Location source,
7471 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007472 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007473 HInstruction* instruction)
7474 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007475
7476 Location GetSource() const { return source_; }
7477 Location GetDestination() const { return destination_; }
7478
7479 void SetSource(Location value) { source_ = value; }
7480 void SetDestination(Location value) { destination_ = value; }
7481
7482 // The parallel move resolver marks moves as "in-progress" by clearing the
7483 // destination (but not the source).
7484 Location MarkPending() {
7485 DCHECK(!IsPending());
7486 Location dest = destination_;
7487 destination_ = Location::NoLocation();
7488 return dest;
7489 }
7490
7491 void ClearPending(Location dest) {
7492 DCHECK(IsPending());
7493 destination_ = dest;
7494 }
7495
7496 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00007497 DCHECK(source_.IsValid() || destination_.IsInvalid());
7498 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007499 }
7500
7501 // True if this blocks a move from the given location.
7502 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08007503 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007504 }
7505
7506 // A move is redundant if it's been eliminated, if its source and
7507 // destination are the same, or if its destination is unneeded.
7508 bool IsRedundant() const {
7509 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
7510 }
7511
7512 // We clear both operands to indicate move that's been eliminated.
7513 void Eliminate() {
7514 source_ = destination_ = Location::NoLocation();
7515 }
7516
7517 bool IsEliminated() const {
7518 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
7519 return source_.IsInvalid();
7520 }
7521
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007522 DataType::Type GetType() const { return type_; }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007523
Nicolas Geoffray90218252015-04-15 11:56:51 +01007524 bool Is64BitMove() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007525 return DataType::Is64BitType(type_);
Nicolas Geoffray90218252015-04-15 11:56:51 +01007526 }
7527
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007528 HInstruction* GetInstruction() const { return instruction_; }
7529
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007530 private:
7531 Location source_;
7532 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01007533 // The type this move is for.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007534 DataType::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007535 // The instruction this move is assocatied with. Null when this move is
7536 // for moving an input in the expected locations of user (including a phi user).
7537 // This is only used in debug mode, to ensure we do not connect interval siblings
7538 // in the same parallel move.
7539 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007540};
7541
Roland Levillainc9285912015-12-18 10:38:42 +00007542std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
7543
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007544static constexpr size_t kDefaultNumberOfMoves = 4;
7545
Vladimir Markobd785672018-05-03 17:09:09 +01007546class HParallelMove FINAL : public HExpression<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007547 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007548 explicit HParallelMove(ArenaAllocator* allocator, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007549 : HExpression(kParallelMove, SideEffects::None(), dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007550 moves_(allocator->Adapter(kArenaAllocMoveOperands)) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007551 moves_.reserve(kDefaultNumberOfMoves);
7552 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007553
Nicolas Geoffray90218252015-04-15 11:56:51 +01007554 void AddMove(Location source,
7555 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007556 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007557 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00007558 DCHECK(source.IsValid());
7559 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007560 if (kIsDebugBuild) {
7561 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007562 for (const MoveOperands& move : moves_) {
7563 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007564 // Special case the situation where the move is for the spill slot
7565 // of the instruction.
7566 if ((GetPrevious() == instruction)
7567 || ((GetPrevious() == nullptr)
7568 && instruction->IsPhi()
7569 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007570 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007571 << "Doing parallel moves for the same instruction.";
7572 } else {
7573 DCHECK(false) << "Doing parallel moves for the same instruction.";
7574 }
7575 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00007576 }
7577 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007578 for (const MoveOperands& move : moves_) {
7579 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007580 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01007581 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007582 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007583 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007584 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007585 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007586 }
7587
Vladimir Marko225b6462015-09-28 12:17:40 +01007588 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007589 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007590 }
7591
Vladimir Marko225b6462015-09-28 12:17:40 +01007592 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007593
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01007594 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007595
Artem Serovcced8ba2017-07-19 18:18:09 +01007596 protected:
7597 DEFAULT_COPY_CONSTRUCTOR(ParallelMove);
7598
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007599 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01007600 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007601};
7602
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007603// This instruction computes an intermediate address pointing in the 'middle' of an object. The
7604// result pointer cannot be handled by GC, so extra care is taken to make sure that this value is
7605// never used across anything that can trigger GC.
7606// The result of this instruction is not a pointer in the sense of `DataType::Type::kreference`.
7607// So we represent it by the type `DataType::Type::kInt`.
7608class HIntermediateAddress FINAL : public HExpression<2> {
7609 public:
7610 HIntermediateAddress(HInstruction* base_address, HInstruction* offset, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307611 : HExpression(kIntermediateAddress,
7612 DataType::Type::kInt32,
7613 SideEffects::DependsOnGC(),
7614 dex_pc) {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007615 DCHECK_EQ(DataType::Size(DataType::Type::kInt32),
7616 DataType::Size(DataType::Type::kReference))
7617 << "kPrimInt and kPrimNot have different sizes.";
7618 SetRawInputAt(0, base_address);
7619 SetRawInputAt(1, offset);
7620 }
7621
Artem Serovcced8ba2017-07-19 18:18:09 +01007622 bool IsClonable() const OVERRIDE { return true; }
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007623 bool CanBeMoved() const OVERRIDE { return true; }
7624 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
7625 return true;
7626 }
7627 bool IsActualObject() const OVERRIDE { return false; }
7628
7629 HInstruction* GetBaseAddress() const { return InputAt(0); }
7630 HInstruction* GetOffset() const { return InputAt(1); }
7631
7632 DECLARE_INSTRUCTION(IntermediateAddress);
7633
Artem Serovcced8ba2017-07-19 18:18:09 +01007634 protected:
7635 DEFAULT_COPY_CONSTRUCTOR(IntermediateAddress);
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007636};
7637
7638
Mark Mendell0616ae02015-04-17 12:49:27 -04007639} // namespace art
7640
Aart Bikf8f5a162017-02-06 15:35:29 -08007641#include "nodes_vector.h"
7642
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007643#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
7644#include "nodes_shared.h"
7645#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007646#ifdef ART_ENABLE_CODEGEN_mips
7647#include "nodes_mips.h"
7648#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04007649#ifdef ART_ENABLE_CODEGEN_x86
7650#include "nodes_x86.h"
7651#endif
7652
7653namespace art {
7654
Igor Murashkin6ef45672017-08-08 13:59:55 -07007655class OptimizingCompilerStats;
7656
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007657class HGraphVisitor : public ValueObject {
7658 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007659 explicit HGraphVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7660 : stats_(stats),
7661 graph_(graph) {}
Dave Allison20dfc792014-06-16 20:44:29 -07007662 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007663
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007664 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007665 virtual void VisitBasicBlock(HBasicBlock* block);
7666
Roland Levillain633021e2014-10-01 14:12:25 +01007667 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007668 void VisitInsertionOrder();
7669
Roland Levillain633021e2014-10-01 14:12:25 +01007670 // Visit the graph following dominator tree reverse post-order.
7671 void VisitReversePostOrder();
7672
Nicolas Geoffray787c3072014-03-17 10:20:19 +00007673 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007674
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007675 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007676#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007677 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
7678
7679 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7680
7681#undef DECLARE_VISIT_INSTRUCTION
7682
Igor Murashkin6ef45672017-08-08 13:59:55 -07007683 protected:
7684 OptimizingCompilerStats* stats_;
7685
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007686 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07007687 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007688
7689 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
7690};
7691
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007692class HGraphDelegateVisitor : public HGraphVisitor {
7693 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007694 explicit HGraphDelegateVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7695 : HGraphVisitor(graph, stats) {}
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007696 virtual ~HGraphDelegateVisitor() {}
7697
7698 // Visit functions that delegate to to super class.
7699#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00007700 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007701
7702 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7703
7704#undef DECLARE_VISIT_INSTRUCTION
7705
7706 private:
7707 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
7708};
7709
Artem Serovcced8ba2017-07-19 18:18:09 +01007710// Create a clone of the instruction, insert it into the graph; replace the old one with a new
7711// and remove the old instruction.
7712HInstruction* ReplaceInstrOrPhiByClone(HInstruction* instr);
7713
7714// Create a clone for each clonable instructions/phis and replace the original with the clone.
7715//
7716// Used for testing individual instruction cloner.
7717class CloneAndReplaceInstructionVisitor : public HGraphDelegateVisitor {
7718 public:
7719 explicit CloneAndReplaceInstructionVisitor(HGraph* graph)
Artem Serov7f4aff62017-06-21 17:02:18 +01007720 : HGraphDelegateVisitor(graph), instr_replaced_by_clones_count_(0) {}
Artem Serovcced8ba2017-07-19 18:18:09 +01007721
7722 void VisitInstruction(HInstruction* instruction) OVERRIDE {
7723 if (instruction->IsClonable()) {
7724 ReplaceInstrOrPhiByClone(instruction);
Artem Serov7f4aff62017-06-21 17:02:18 +01007725 instr_replaced_by_clones_count_++;
Artem Serovcced8ba2017-07-19 18:18:09 +01007726 }
7727 }
7728
Artem Serov7f4aff62017-06-21 17:02:18 +01007729 size_t GetInstrReplacedByClonesCount() const { return instr_replaced_by_clones_count_; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007730
7731 private:
Artem Serov7f4aff62017-06-21 17:02:18 +01007732 size_t instr_replaced_by_clones_count_;
Artem Serovcced8ba2017-07-19 18:18:09 +01007733
7734 DISALLOW_COPY_AND_ASSIGN(CloneAndReplaceInstructionVisitor);
7735};
7736
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007737// Iterator over the blocks that art part of the loop. Includes blocks part
7738// of an inner loop. The order in which the blocks are iterated is on their
7739// block id.
7740class HBlocksInLoopIterator : public ValueObject {
7741 public:
7742 explicit HBlocksInLoopIterator(const HLoopInformation& info)
7743 : blocks_in_loop_(info.GetBlocks()),
7744 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
7745 index_(0) {
7746 if (!blocks_in_loop_.IsBitSet(index_)) {
7747 Advance();
7748 }
7749 }
7750
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007751 bool Done() const { return index_ == blocks_.size(); }
7752 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007753 void Advance() {
7754 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007755 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007756 if (blocks_in_loop_.IsBitSet(index_)) {
7757 break;
7758 }
7759 }
7760 }
7761
7762 private:
7763 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007764 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007765 size_t index_;
7766
7767 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
7768};
7769
Mingyao Yang3584bce2015-05-19 16:01:59 -07007770// Iterator over the blocks that art part of the loop. Includes blocks part
7771// of an inner loop. The order in which the blocks are iterated is reverse
7772// post order.
7773class HBlocksInLoopReversePostOrderIterator : public ValueObject {
7774 public:
7775 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
7776 : blocks_in_loop_(info.GetBlocks()),
7777 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
7778 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007779 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007780 Advance();
7781 }
7782 }
7783
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007784 bool Done() const { return index_ == blocks_.size(); }
7785 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07007786 void Advance() {
7787 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007788 for (size_t e = blocks_.size(); index_ < e; ++index_) {
7789 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007790 break;
7791 }
7792 }
7793 }
7794
7795 private:
7796 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007797 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07007798 size_t index_;
7799
7800 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
7801};
7802
Aart Bikf3e61ee2017-04-12 17:09:20 -07007803// Returns int64_t value of a properly typed constant.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007804inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00007805 if (constant->IsIntConstant()) {
7806 return constant->AsIntConstant()->GetValue();
7807 } else if (constant->IsLongConstant()) {
7808 return constant->AsLongConstant()->GetValue();
7809 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00007810 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00007811 return 0;
7812 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007813}
7814
Aart Bikf3e61ee2017-04-12 17:09:20 -07007815// Returns true iff instruction is an integral constant (and sets value on success).
7816inline bool IsInt64AndGet(HInstruction* instruction, /*out*/ int64_t* value) {
7817 if (instruction->IsIntConstant()) {
7818 *value = instruction->AsIntConstant()->GetValue();
7819 return true;
7820 } else if (instruction->IsLongConstant()) {
7821 *value = instruction->AsLongConstant()->GetValue();
7822 return true;
7823 } else if (instruction->IsNullConstant()) {
7824 *value = 0;
7825 return true;
7826 }
7827 return false;
7828}
7829
Aart Bik0148de42017-09-05 09:25:01 -07007830// Returns true iff instruction is the given integral constant.
7831inline bool IsInt64Value(HInstruction* instruction, int64_t value) {
7832 int64_t val = 0;
7833 return IsInt64AndGet(instruction, &val) && val == value;
7834}
7835
7836// Returns true iff instruction is a zero bit pattern.
7837inline bool IsZeroBitPattern(HInstruction* instruction) {
7838 return instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern();
7839}
7840
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01007841// Implement HInstruction::Is##type() for concrete instructions.
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007842#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01007843 inline bool HInstruction::Is##type() const { return GetKind() == k##type; }
7844 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
7845#undef INSTRUCTION_TYPE_CHECK
7846
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01007847// Implement HInstruction::Is##type() for abstract instructions.
Vladimir Markoa90dd512018-05-04 15:04:45 +01007848#define INSTRUCTION_TYPE_CHECK_RESULT(type, super) \
7849 std::is_base_of<BaseType, H##type>::value,
7850#define INSTRUCTION_TYPE_CHECK(type, super) \
7851 inline bool HInstruction::Is##type() const { \
7852 DCHECK_LT(GetKind(), kLastInstructionKind); \
7853 using BaseType = H##type; \
7854 static constexpr bool results[] = { \
7855 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK_RESULT) \
7856 }; \
7857 return results[static_cast<size_t>(GetKind())]; \
7858 }
7859
7860 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
7861#undef INSTRUCTION_TYPE_CHECK
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01007862#undef INSTRUCTION_TYPE_CHECK_RESULT
Vladimir Markoa90dd512018-05-04 15:04:45 +01007863
7864#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007865 inline const H##type* HInstruction::As##type() const { \
7866 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
7867 } \
7868 inline H##type* HInstruction::As##type() { \
7869 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
7870 }
7871
Vladimir Markoa90dd512018-05-04 15:04:45 +01007872 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
7873#undef INSTRUCTION_TYPE_CAST
7874
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007875
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00007876// Create space in `blocks` for adding `number_of_new_blocks` entries
7877// starting at location `at`. Blocks after `at` are moved accordingly.
7878inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
7879 size_t number_of_new_blocks,
7880 size_t after) {
7881 DCHECK_LT(after, blocks->size());
7882 size_t old_size = blocks->size();
7883 size_t new_size = old_size + number_of_new_blocks;
7884 blocks->resize(new_size);
7885 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
7886}
7887
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00007888/*
7889 * Hunt "under the hood" of array lengths (leading to array references),
7890 * null checks (also leading to array references), and new arrays
7891 * (leading to the actual length). This makes it more likely related
7892 * instructions become actually comparable.
7893 */
7894inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
7895 while (instruction->IsArrayLength() ||
7896 instruction->IsNullCheck() ||
7897 instruction->IsNewArray()) {
7898 instruction = instruction->IsNewArray()
7899 ? instruction->AsNewArray()->GetLength()
7900 : instruction->InputAt(0);
7901 }
7902 return instruction;
7903}
7904
Artem Serov21c7e6f2017-07-27 16:04:42 +01007905void RemoveEnvironmentUses(HInstruction* instruction);
7906bool HasEnvironmentUsedByOthers(HInstruction* instruction);
7907void ResetEnvironmentInputRecords(HInstruction* instruction);
7908
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007909} // namespace art
7910
7911#endif // ART_COMPILER_OPTIMIZING_NODES_H_