blob: 3fd5b6b02dd289beb4965b937872c0483b82e24f [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
Vladimir Marko60584552015-09-03 13:35:12 +000020#include <algorithm>
Vladimir Markof9f64412015-09-02 14:05:49 +010021#include <array>
Roland Levillain9867bc72015-08-05 10:21:34 +010022#include <type_traits>
23
Mathieu Chartiere5d80f82015-10-15 17:47:48 -070024#include "base/arena_bit_vector.h"
David Brazdil8d5b8b22015-03-24 10:51:52 +000025#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080026#include "base/arena_object.h"
David Brazdild9c90372016-09-14 16:53:55 +010027#include "base/array_ref.h"
Vladimir Marko2c45bc92016-10-25 16:54:12 +010028#include "base/iteration_range.h"
David Sehrc431b9d2018-03-02 12:01:51 -080029#include "base/quasi_atomic.h"
Vladimir Marko60584552015-09-03 13:35:12 +000030#include "base/stl_util.h"
David Brazdild9c90372016-09-14 16:53:55 +010031#include "base/transform_array_ref.h"
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010032#include "data_type.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070033#include "deoptimization_kind.h"
David Sehr9e734c72018-01-04 17:56:19 -080034#include "dex/dex_file.h"
35#include "dex/dex_file_types.h"
David Sehr8c0961f2018-01-23 16:11:38 -080036#include "dex/invoke_type.h"
David Sehr312f3b22018-03-19 08:39:26 -070037#include "dex/method_reference.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000038#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000039#include "handle.h"
40#include "handle_scope.h"
Nicolas Geoffray762869d2016-07-15 15:28:35 +010041#include "intrinsics_enum.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010042#include "locations.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000043#include "mirror/class.h"
Orion Hodson18259d72018-04-12 11:18:23 +010044#include "mirror/method_type.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010045#include "offsets.h"
Vladimir Marko46817b82016-03-29 12:21:58 +010046#include "utils/intrusive_forward_list.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000047
48namespace art {
49
Vladimir Markoca6fff82017-10-03 14:49:14 +010050class ArenaStack;
David Brazdil1abb4192015-02-17 18:33:36 +000051class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000052class HBasicBlock;
Igor Murashkind01745e2017-04-05 16:40:31 -070053class HConstructorFence;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010054class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000055class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010056class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000057class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000058class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000059class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000060class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000061class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000062class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000063class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000064class HNullConstant;
Igor Murashkind01745e2017-04-05 16:40:31 -070065class HParameterValue;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010066class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010067class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010068class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010069class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000070class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010071class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000072class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000073
Mathieu Chartier736b5602015-09-02 14:54:11 -070074namespace mirror {
75class DexCache;
76} // namespace mirror
77
Nicolas Geoffray818f2102014-02-18 16:43:35 +000078static const int kDefaultNumberOfBlocks = 8;
79static const int kDefaultNumberOfSuccessors = 2;
80static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010081static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010082static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000083static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000084
Roland Levillain5b5b9312016-03-22 14:57:31 +000085// The maximum (meaningful) distance (31) that can be used in an integer shift/rotate operation.
86static constexpr int32_t kMaxIntShiftDistance = 0x1f;
87// The maximum (meaningful) distance (63) that can be used in a long shift/rotate operation.
88static constexpr int32_t kMaxLongShiftDistance = 0x3f;
Calin Juravle9aec02f2014-11-18 23:06:35 +000089
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010090static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070091static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010092
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010093static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
94
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060095static constexpr uint32_t kNoDexPc = -1;
96
Vladimir Markodbb7f5b2016-03-30 13:23:58 +010097inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
98 // For the purposes of the compiler, the dex files must actually be the same object
99 // if we want to safely treat them as the same. This is especially important for JIT
100 // as custom class loaders can open the same underlying file (or memory) multiple
101 // times and provide different class resolution but no two class loaders should ever
102 // use the same DexFile object - doing so is an unsupported hack that can lead to
103 // all sorts of weird failures.
104 return &lhs == &rhs;
105}
106
Dave Allison20dfc792014-06-16 20:44:29 -0700107enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -0700108 // All types.
109 kCondEQ, // ==
110 kCondNE, // !=
111 // Signed integers and floating-point numbers.
112 kCondLT, // <
113 kCondLE, // <=
114 kCondGT, // >
115 kCondGE, // >=
116 // Unsigned integers.
117 kCondB, // <
118 kCondBE, // <=
119 kCondA, // >
120 kCondAE, // >=
Scott Wakeling2c76e062016-08-31 09:48:54 +0100121 // First and last aliases.
122 kCondFirst = kCondEQ,
123 kCondLast = kCondAE,
Dave Allison20dfc792014-06-16 20:44:29 -0700124};
125
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000126enum GraphAnalysisResult {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000127 kAnalysisSkipped,
David Brazdilbadd8262016-02-02 16:28:56 +0000128 kAnalysisInvalidBytecode,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000129 kAnalysisFailThrowCatchLoop,
130 kAnalysisFailAmbiguousArrayOp,
131 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000132};
133
Andreas Gampe9186ced2016-12-12 14:28:21 -0800134template <typename T>
135static inline typename std::make_unsigned<T>::type MakeUnsigned(T x) {
136 return static_cast<typename std::make_unsigned<T>::type>(x);
137}
138
Vladimir Markof9f64412015-09-02 14:05:49 +0100139class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100140 public:
141 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
142
143 void AddInstruction(HInstruction* instruction);
144 void RemoveInstruction(HInstruction* instruction);
145
David Brazdilc3d743f2015-04-22 13:40:50 +0100146 // Insert `instruction` before/after an existing instruction `cursor`.
147 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
148 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
149
Roland Levillain6b469232014-09-25 10:10:38 +0100150 // Return true if this list contains `instruction`.
151 bool Contains(HInstruction* instruction) const;
152
Roland Levillainccc07a92014-09-16 14:48:16 +0100153 // Return true if `instruction1` is found before `instruction2` in
154 // this instruction list and false otherwise. Abort if none
155 // of these instructions is found.
156 bool FoundBefore(const HInstruction* instruction1,
157 const HInstruction* instruction2) const;
158
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000159 bool IsEmpty() const { return first_instruction_ == nullptr; }
160 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
161
162 // Update the block of all instructions to be `block`.
163 void SetBlockOfInstructions(HBasicBlock* block) const;
164
165 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000166 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000167 void Add(const HInstructionList& instruction_list);
168
David Brazdil2d7352b2015-04-20 14:52:42 +0100169 // Return the number of instructions in the list. This is an expensive operation.
170 size_t CountSize() const;
171
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100172 private:
173 HInstruction* first_instruction_;
174 HInstruction* last_instruction_;
175
176 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000177 friend class HGraph;
178 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100179 friend class HInstructionIterator;
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +0000180 friend class HInstructionIteratorHandleChanges;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100181 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100182
183 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
184};
185
David Brazdil4833f5a2015-12-16 10:37:39 +0000186class ReferenceTypeInfo : ValueObject {
187 public:
188 typedef Handle<mirror::Class> TypeHandle;
189
Vladimir Markoa1de9182016-02-25 11:37:38 +0000190 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
191
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700192 static ReferenceTypeInfo Create(TypeHandle type_handle) REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil94ab38f2016-06-21 17:48:19 +0100193 return Create(type_handle, type_handle->CannotBeAssignedFromOtherTypes());
194 }
195
Vladimir Markoa1de9182016-02-25 11:37:38 +0000196 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000197 return ReferenceTypeInfo(type_handle, is_exact);
198 }
199
200 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
201
Vladimir Markof39745e2016-01-26 12:16:55 +0000202 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000203 return handle.GetReference() != nullptr;
204 }
205
Vladimir Marko456307a2016-04-19 14:12:13 +0000206 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000207 return IsValidHandle(type_handle_);
208 }
209
210 bool IsExact() const { return is_exact_; }
211
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700212 bool IsObjectClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000213 DCHECK(IsValid());
214 return GetTypeHandle()->IsObjectClass();
215 }
216
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700217 bool IsStringClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000218 DCHECK(IsValid());
219 return GetTypeHandle()->IsStringClass();
220 }
221
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700222 bool IsObjectArray() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000223 DCHECK(IsValid());
224 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
225 }
226
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700227 bool IsInterface() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000228 DCHECK(IsValid());
229 return GetTypeHandle()->IsInterface();
230 }
231
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700232 bool IsArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000233 DCHECK(IsValid());
234 return GetTypeHandle()->IsArrayClass();
235 }
236
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700237 bool IsPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000238 DCHECK(IsValid());
239 return GetTypeHandle()->IsPrimitiveArray();
240 }
241
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700242 bool IsNonPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000243 DCHECK(IsValid());
244 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
245 }
246
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700247 bool CanArrayHold(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000248 DCHECK(IsValid());
249 if (!IsExact()) return false;
250 if (!IsArrayClass()) return false;
251 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
252 }
253
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700254 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000255 DCHECK(IsValid());
256 if (!IsExact()) return false;
257 if (!IsArrayClass()) return false;
258 if (!rti.IsArrayClass()) return false;
259 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
260 rti.GetTypeHandle()->GetComponentType());
261 }
262
263 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
264
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700265 bool IsSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000266 DCHECK(IsValid());
267 DCHECK(rti.IsValid());
268 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
269 }
270
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700271 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000272 DCHECK(IsValid());
273 DCHECK(rti.IsValid());
274 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
275 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
276 }
277
278 // Returns true if the type information provide the same amount of details.
279 // Note that it does not mean that the instructions have the same actual type
280 // (because the type can be the result of a merge).
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700281 bool IsEqual(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000282 if (!IsValid() && !rti.IsValid()) {
283 // Invalid types are equal.
284 return true;
285 }
286 if (!IsValid() || !rti.IsValid()) {
287 // One is valid, the other not.
288 return false;
289 }
290 return IsExact() == rti.IsExact()
291 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
292 }
293
294 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000295 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
296 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
297 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000298
299 // The class of the object.
300 TypeHandle type_handle_;
301 // Whether or not the type is exact or a superclass of the actual type.
302 // Whether or not we have any information about this type.
303 bool is_exact_;
304};
305
306std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
307
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000308// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100309class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000310 public:
Vladimir Markoca6fff82017-10-03 14:49:14 +0100311 HGraph(ArenaAllocator* allocator,
312 ArenaStack* arena_stack,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100313 const DexFile& dex_file,
314 uint32_t method_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700315 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100316 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100317 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000318 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100319 int start_instruction_id = 0)
Vladimir Markoca6fff82017-10-03 14:49:14 +0100320 : allocator_(allocator),
321 arena_stack_(arena_stack),
322 blocks_(allocator->Adapter(kArenaAllocBlockList)),
323 reverse_post_order_(allocator->Adapter(kArenaAllocReversePostOrder)),
324 linear_order_(allocator->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700325 entry_block_(nullptr),
326 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100327 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100328 number_of_vregs_(0),
329 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000330 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400331 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000332 has_try_catch_(false),
Aart Bikb13c65b2017-03-21 20:14:07 -0700333 has_simd_(false),
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800334 has_loops_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000335 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000336 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000337 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100338 dex_file_(dex_file),
339 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100340 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100341 in_ssa_form_(false),
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800342 number_of_cha_guards_(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700343 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000344 cached_null_constant_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100345 cached_int_constants_(std::less<int32_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
346 cached_float_constants_(std::less<int32_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
347 cached_long_constants_(std::less<int64_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
348 cached_double_constants_(std::less<int64_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000349 cached_current_method_(nullptr),
Nicolas Geoffray53fec082017-03-27 12:56:16 +0100350 art_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000351 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
Mingyao Yang063fc772016-08-02 11:02:54 -0700352 osr_(osr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100353 cha_single_implementation_list_(allocator->Adapter(kArenaAllocCHA)) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100354 blocks_.reserve(kDefaultNumberOfBlocks);
355 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000356
David Brazdilbadd8262016-02-02 16:28:56 +0000357 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700358 void InitializeInexactObjectRTI(VariableSizedHandleScope* handles);
David Brazdilbadd8262016-02-02 16:28:56 +0000359
Vladimir Markoca6fff82017-10-03 14:49:14 +0100360 ArenaAllocator* GetAllocator() const { return allocator_; }
361 ArenaStack* GetArenaStack() const { return arena_stack_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100362 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
363
David Brazdil69ba7b72015-06-23 18:27:30 +0100364 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000365 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100366
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000367 HBasicBlock* GetEntryBlock() const { return entry_block_; }
368 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100369 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000370
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000371 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
372 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000373
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000374 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100375
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100376 void ComputeDominanceInformation();
377 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000378 void ClearLoopInformation();
379 void FindBackEdges(ArenaBitVector* visited);
380 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100381 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100382 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000383
David Brazdil4833f5a2015-12-16 10:37:39 +0000384 // Analyze all natural loops in this graph. Returns a code specifying that it
385 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000386 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000387 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100388
David Brazdilffee3d32015-07-06 11:48:53 +0100389 // Iterate over blocks to compute try block membership. Needs reverse post
390 // order and loop information.
391 void ComputeTryBlockInformation();
392
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000393 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000394 // Returns the instruction to replace the invoke expression or null if the
395 // invoke is for a void method. Note that the caller is responsible for replacing
396 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000397 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000398
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000399 // Update the loop and try membership of `block`, which was spawned from `reference`.
400 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
401 // should be the new back edge.
402 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
403 HBasicBlock* reference,
404 bool replace_if_back_edge);
405
Mingyao Yang3584bce2015-05-19 16:01:59 -0700406 // Need to add a couple of blocks to test if the loop body is entered and
407 // put deoptimization instructions, etc.
408 void TransformLoopHeaderForBCE(HBasicBlock* header);
409
Aart Bikf8f5a162017-02-06 15:35:29 -0800410 // Adds a new loop directly after the loop with the given header and exit.
411 // Returns the new preheader.
412 HBasicBlock* TransformLoopForVectorization(HBasicBlock* header,
413 HBasicBlock* body,
414 HBasicBlock* exit);
415
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000416 // Removes `block` from the graph. Assumes `block` has been disconnected from
417 // other blocks and has no instructions or phis.
418 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000419
David Brazdilfc6a86a2015-06-26 10:33:45 +0000420 // Splits the edge between `block` and `successor` while preserving the
421 // indices in the predecessor/successor lists. If there are multiple edges
422 // between the blocks, the lowest indices are used.
423 // Returns the new block which is empty and has the same dex pc as `successor`.
424 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
425
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100426 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
Artem Serovc73ee372017-07-31 15:08:40 +0100427 void OrderLoopHeaderPredecessors(HBasicBlock* header);
Artem Serov09faaea2017-12-07 14:36:01 +0000428
429 // Transform a loop into a format with a single preheader.
430 //
431 // Each phi in the header should be split: original one in the header should only hold
432 // inputs reachable from the back edges and a single input from the preheader. The newly created
433 // phi in the preheader should collate the inputs from the original multiple incoming blocks.
434 //
435 // Loops in the graph typically have a single preheader, so this method is used to "repair" loops
436 // that no longer have this property.
437 void TransformLoopToSinglePreheaderFormat(HBasicBlock* header);
438
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100439 void SimplifyLoop(HBasicBlock* header);
440
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000441 int32_t GetNextInstructionId() {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100442 CHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000443 return current_instruction_id_++;
444 }
445
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000446 int32_t GetCurrentInstructionId() const {
447 return current_instruction_id_;
448 }
449
450 void SetCurrentInstructionId(int32_t id) {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100451 CHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000452 current_instruction_id_ = id;
453 }
454
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100455 uint16_t GetMaximumNumberOfOutVRegs() const {
456 return maximum_number_of_out_vregs_;
457 }
458
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000459 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
460 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100461 }
462
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100463 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
464 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
465 }
466
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000467 void UpdateTemporariesVRegSlots(size_t slots) {
468 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100469 }
470
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000471 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100472 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000473 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100474 }
475
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100476 void SetNumberOfVRegs(uint16_t number_of_vregs) {
477 number_of_vregs_ = number_of_vregs;
478 }
479
480 uint16_t GetNumberOfVRegs() const {
481 return number_of_vregs_;
482 }
483
484 void SetNumberOfInVRegs(uint16_t value) {
485 number_of_in_vregs_ = value;
486 }
487
David Brazdildee58d62016-04-07 09:54:26 +0000488 uint16_t GetNumberOfInVRegs() const {
489 return number_of_in_vregs_;
490 }
491
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100492 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100493 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100494 return number_of_vregs_ - number_of_in_vregs_;
495 }
496
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100497 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100498 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100499 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100500
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100501 ArrayRef<HBasicBlock* const> GetReversePostOrderSkipEntryBlock() {
502 DCHECK(GetReversePostOrder()[0] == entry_block_);
503 return ArrayRef<HBasicBlock* const>(GetReversePostOrder()).SubArray(1);
504 }
505
506 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetPostOrder() const {
507 return ReverseRange(GetReversePostOrder());
508 }
509
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100510 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100511 return linear_order_;
512 }
513
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100514 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetLinearPostOrder() const {
515 return ReverseRange(GetLinearOrder());
516 }
517
Mark Mendell1152c922015-04-24 17:06:35 -0400518 bool HasBoundsChecks() const {
519 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800520 }
521
Mark Mendell1152c922015-04-24 17:06:35 -0400522 void SetHasBoundsChecks(bool value) {
523 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800524 }
525
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000526 bool IsDebuggable() const { return debuggable_; }
527
David Brazdil8d5b8b22015-03-24 10:51:52 +0000528 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000529 // already, it is created and inserted into the graph. This method is only for
530 // integral types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100531 HConstant* GetConstant(DataType::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000532
533 // TODO: This is problematic for the consistency of reference type propagation
534 // because it can be created anytime after the pass and thus it will be left
535 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600536 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000537
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600538 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
539 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000540 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600541 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
542 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000543 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600544 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
545 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000546 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600547 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
548 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000549 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000550
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100551 HCurrentMethod* GetCurrentMethod();
552
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100553 const DexFile& GetDexFile() const {
554 return dex_file_;
555 }
556
557 uint32_t GetMethodIdx() const {
558 return method_idx_;
559 }
560
Igor Murashkind01745e2017-04-05 16:40:31 -0700561 // Get the method name (without the signature), e.g. "<init>"
562 const char* GetMethodName() const;
563
564 // Get the pretty method name (class + name + optionally signature).
565 std::string PrettyMethod(bool with_signature = true) const;
566
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100567 InvokeType GetInvokeType() const {
568 return invoke_type_;
569 }
570
Mark Mendellc4701932015-04-10 13:18:51 -0400571 InstructionSet GetInstructionSet() const {
572 return instruction_set_;
573 }
574
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000575 bool IsCompilingOsr() const { return osr_; }
576
Mingyao Yang063fc772016-08-02 11:02:54 -0700577 ArenaSet<ArtMethod*>& GetCHASingleImplementationList() {
578 return cha_single_implementation_list_;
579 }
580
581 void AddCHASingleImplementationDependency(ArtMethod* method) {
582 cha_single_implementation_list_.insert(method);
583 }
584
585 bool HasShouldDeoptimizeFlag() const {
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800586 return number_of_cha_guards_ != 0;
Mingyao Yang063fc772016-08-02 11:02:54 -0700587 }
588
David Brazdil77a48ae2015-09-15 12:34:04 +0000589 bool HasTryCatch() const { return has_try_catch_; }
590 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100591
Aart Bikb13c65b2017-03-21 20:14:07 -0700592 bool HasSIMD() const { return has_simd_; }
593 void SetHasSIMD(bool value) { has_simd_ = value; }
594
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800595 bool HasLoops() const { return has_loops_; }
596 void SetHasLoops(bool value) { has_loops_ = value; }
597
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000598 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
599 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
600
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100601 ArtMethod* GetArtMethod() const { return art_method_; }
602 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
603
Roland Levillain5e8d5f02016-10-18 18:03:43 +0100604 // Returns an instruction with the opposite Boolean value from 'cond'.
Mark Mendellf6529172015-11-17 11:16:56 -0500605 // The instruction has been inserted into the graph, either as a constant, or
606 // before cursor.
607 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
608
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000609 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
610
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800611 uint32_t GetNumberOfCHAGuards() { return number_of_cha_guards_; }
612 void SetNumberOfCHAGuards(uint32_t num) { number_of_cha_guards_ = num; }
613 void IncrementNumberOfCHAGuards() { number_of_cha_guards_++; }
614
David Brazdil2d7352b2015-04-20 14:52:42 +0100615 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000616 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100617 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000618
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000619 template <class InstructionType, typename ValueType>
620 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600621 ArenaSafeMap<ValueType, InstructionType*>* cache,
622 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000623 // Try to find an existing constant of the given value.
624 InstructionType* constant = nullptr;
625 auto cached_constant = cache->find(value);
626 if (cached_constant != cache->end()) {
627 constant = cached_constant->second;
628 }
629
630 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100631 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000632 if (constant == nullptr || constant->GetBlock() == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100633 constant = new (allocator_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000634 cache->Overwrite(value, constant);
635 InsertConstant(constant);
636 }
637 return constant;
638 }
639
David Brazdil8d5b8b22015-03-24 10:51:52 +0000640 void InsertConstant(HConstant* instruction);
641
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000642 // Cache a float constant into the graph. This method should only be
643 // called by the SsaBuilder when creating "equivalent" instructions.
644 void CacheFloatConstant(HFloatConstant* constant);
645
646 // See CacheFloatConstant comment.
647 void CacheDoubleConstant(HDoubleConstant* constant);
648
Vladimir Markoca6fff82017-10-03 14:49:14 +0100649 ArenaAllocator* const allocator_;
650 ArenaStack* const arena_stack_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000651
652 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100653 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000654
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100655 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100656 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000657
Aart Bik281c6812016-08-26 11:31:48 -0700658 // List of blocks to perform a linear order tree traversal. Unlike the reverse
659 // post order, this order is not incrementally kept up-to-date.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100660 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100661
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000662 HBasicBlock* entry_block_;
663 HBasicBlock* exit_block_;
664
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100665 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100666 uint16_t maximum_number_of_out_vregs_;
667
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100668 // The number of virtual registers in this method. Contains the parameters.
669 uint16_t number_of_vregs_;
670
671 // The number of virtual registers used by parameters of this method.
672 uint16_t number_of_in_vregs_;
673
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000674 // Number of vreg size slots that the temporaries use (used in baseline compiler).
675 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100676
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800677 // Flag whether there are bounds checks in the graph. We can skip
678 // BCE if it's false. It's only best effort to keep it up to date in
679 // the presence of code elimination so there might be false positives.
Mark Mendell1152c922015-04-24 17:06:35 -0400680 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800681
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800682 // Flag whether there are try/catch blocks in the graph. We will skip
683 // try/catch-related passes if it's false. It's only best effort to keep
684 // it up to date in the presence of code elimination so there might be
685 // false positives.
David Brazdil77a48ae2015-09-15 12:34:04 +0000686 bool has_try_catch_;
687
Aart Bikb13c65b2017-03-21 20:14:07 -0700688 // Flag whether SIMD instructions appear in the graph. If true, the
689 // code generators may have to be more careful spilling the wider
690 // contents of SIMD registers.
691 bool has_simd_;
692
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800693 // Flag whether there are any loops in the graph. We can skip loop
694 // optimization if it's false. It's only best effort to keep it up
695 // to date in the presence of code elimination so there might be false
696 // positives.
697 bool has_loops_;
698
699 // Flag whether there are any irreducible loops in the graph. It's only
700 // best effort to keep it up to date in the presence of code elimination
701 // so there might be false positives.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000702 bool has_irreducible_loops_;
703
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000704 // Indicates whether the graph should be compiled in a way that
705 // ensures full debuggability. If false, we can apply more
706 // aggressive optimizations that may limit the level of debugging.
707 const bool debuggable_;
708
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000709 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000710 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000711
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100712 // The dex file from which the method is from.
713 const DexFile& dex_file_;
714
715 // The method index in the dex file.
716 const uint32_t method_idx_;
717
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100718 // If inlined, this encodes how the callee is being invoked.
719 const InvokeType invoke_type_;
720
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100721 // Whether the graph has been transformed to SSA form. Only used
722 // in debug mode to ensure we are not using properties only valid
723 // for non-SSA form (like the number of temporaries).
724 bool in_ssa_form_;
725
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800726 // Number of CHA guards in the graph. Used to short-circuit the
727 // CHA guard optimization pass when there is no CHA guard left.
728 uint32_t number_of_cha_guards_;
729
Mathieu Chartiere401d142015-04-22 13:56:20 -0700730 const InstructionSet instruction_set_;
731
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000732 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000733 HNullConstant* cached_null_constant_;
734 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000735 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000736 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000737 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000738
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100739 HCurrentMethod* cached_current_method_;
740
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100741 // The ArtMethod this graph is for. Note that for AOT, it may be null,
742 // for example for methods whose declaring class could not be resolved
743 // (such as when the superclass could not be found).
744 ArtMethod* art_method_;
745
David Brazdil4833f5a2015-12-16 10:37:39 +0000746 // Keep the RTI of inexact Object to avoid having to pass stack handle
747 // collection pointer to passes which may create NullConstant.
748 ReferenceTypeInfo inexact_object_rti_;
749
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000750 // Whether we are compiling this graph for on stack replacement: this will
751 // make all loops seen as irreducible and emit special stack maps to mark
752 // compiled code entries which the interpreter can directly jump to.
753 const bool osr_;
754
Mingyao Yang063fc772016-08-02 11:02:54 -0700755 // List of methods that are assumed to have single implementation.
756 ArenaSet<ArtMethod*> cha_single_implementation_list_;
757
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000758 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100759 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000760 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000761 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000762 DISALLOW_COPY_AND_ASSIGN(HGraph);
763};
764
Vladimir Markof9f64412015-09-02 14:05:49 +0100765class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000766 public:
767 HLoopInformation(HBasicBlock* header, HGraph* graph)
768 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100769 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000770 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100771 contains_irreducible_loop_(false),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100772 back_edges_(graph->GetAllocator()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100773 // Make bit vector growable, as the number of blocks may change.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100774 blocks_(graph->GetAllocator(),
775 graph->GetBlocks().size(),
776 true,
777 kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100778 back_edges_.reserve(kDefaultNumberOfBackEdges);
779 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100780
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000781 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100782 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000783
784 void Dump(std::ostream& os);
785
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100786 HBasicBlock* GetHeader() const {
787 return header_;
788 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000789
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000790 void SetHeader(HBasicBlock* block) {
791 header_ = block;
792 }
793
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100794 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
795 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
796 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
797
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000798 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100799 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000800 }
801
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100802 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100803 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100804 }
805
David Brazdil46e2a392015-03-16 17:31:52 +0000806 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100807 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100808 }
809
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000810 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100811 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000812 }
813
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100814 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100815
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100816 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100817 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100818 }
819
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100820 // Returns the lifetime position of the back edge that has the
821 // greatest lifetime position.
822 size_t GetLifetimeEnd() const;
823
824 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100825 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100826 }
827
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000828 // Finds blocks that are part of this loop.
829 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100830
Artem Serov7f4aff62017-06-21 17:02:18 +0100831 // Updates blocks population of the loop and all of its outer' ones recursively after the
832 // population of the inner loop is updated.
833 void PopulateInnerLoopUpwards(HLoopInformation* inner_loop);
834
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100835 // Returns whether this loop information contains `block`.
836 // Note that this loop information *must* be populated before entering this function.
837 bool Contains(const HBasicBlock& block) const;
838
839 // Returns whether this loop information is an inner loop of `other`.
840 // Note that `other` *must* be populated before entering this function.
841 bool IsIn(const HLoopInformation& other) const;
842
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800843 // Returns true if instruction is not defined within this loop.
844 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700845
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100846 const ArenaBitVector& GetBlocks() const { return blocks_; }
847
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000848 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000849 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000850
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000851 void ClearAllBlocks() {
852 blocks_.ClearAllBits();
853 }
854
David Brazdil3f4a5222016-05-06 12:46:21 +0100855 bool HasBackEdgeNotDominatedByHeader() const;
856
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100857 bool IsPopulated() const {
858 return blocks_.GetHighestBitSet() != -1;
859 }
860
Anton Shaminf89381f2016-05-16 16:44:13 +0600861 bool DominatesAllBackEdges(HBasicBlock* block);
862
David Sehrc757dec2016-11-04 15:48:34 -0700863 bool HasExitEdge() const;
864
Artem Serov7f4aff62017-06-21 17:02:18 +0100865 // Resets back edge and blocks-in-loop data.
866 void ResetBasicBlockData() {
867 back_edges_.clear();
868 ClearAllBlocks();
869 }
870
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000871 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100872 // Internal recursive implementation of `Populate`.
873 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100874 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100875
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000876 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100877 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000878 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100879 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100880 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100881 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000882
883 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
884};
885
David Brazdilec16f792015-08-19 15:04:01 +0100886// Stores try/catch information for basic blocks.
887// Note that HGraph is constructed so that catch blocks cannot simultaneously
888// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100889class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100890 public:
891 // Try block information constructor.
892 explicit TryCatchInformation(const HTryBoundary& try_entry)
893 : try_entry_(&try_entry),
894 catch_dex_file_(nullptr),
895 catch_type_index_(DexFile::kDexNoIndex16) {
896 DCHECK(try_entry_ != nullptr);
897 }
898
899 // Catch block information constructor.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800900 TryCatchInformation(dex::TypeIndex catch_type_index, const DexFile& dex_file)
David Brazdilec16f792015-08-19 15:04:01 +0100901 : try_entry_(nullptr),
902 catch_dex_file_(&dex_file),
903 catch_type_index_(catch_type_index) {}
904
905 bool IsTryBlock() const { return try_entry_ != nullptr; }
906
907 const HTryBoundary& GetTryEntry() const {
908 DCHECK(IsTryBlock());
909 return *try_entry_;
910 }
911
912 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
913
914 bool IsCatchAllTypeIndex() const {
915 DCHECK(IsCatchBlock());
Andreas Gampea5b09a62016-11-17 15:21:22 -0800916 return !catch_type_index_.IsValid();
David Brazdilec16f792015-08-19 15:04:01 +0100917 }
918
Andreas Gampea5b09a62016-11-17 15:21:22 -0800919 dex::TypeIndex GetCatchTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +0100920 DCHECK(IsCatchBlock());
921 return catch_type_index_;
922 }
923
924 const DexFile& GetCatchDexFile() const {
925 DCHECK(IsCatchBlock());
926 return *catch_dex_file_;
927 }
928
929 private:
930 // One of possibly several TryBoundary instructions entering the block's try.
931 // Only set for try blocks.
932 const HTryBoundary* try_entry_;
933
934 // Exception type information. Only set for catch blocks.
935 const DexFile* catch_dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800936 const dex::TypeIndex catch_type_index_;
David Brazdilec16f792015-08-19 15:04:01 +0100937};
938
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100939static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100940static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100941
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000942// A block in a method. Contains the list of instructions represented
943// as a double linked list. Each block knows its predecessors and
944// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100945
Vladimir Markof9f64412015-09-02 14:05:49 +0100946class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000947 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700948 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000949 : graph_(graph),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100950 predecessors_(graph->GetAllocator()->Adapter(kArenaAllocPredecessors)),
951 successors_(graph->GetAllocator()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000952 loop_information_(nullptr),
953 dominator_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100954 dominated_blocks_(graph->GetAllocator()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100955 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100956 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100957 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000958 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000959 try_catch_information_(nullptr) {
960 predecessors_.reserve(kDefaultNumberOfPredecessors);
961 successors_.reserve(kDefaultNumberOfSuccessors);
962 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
963 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000964
Vladimir Marko60584552015-09-03 13:35:12 +0000965 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100966 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000967 }
968
Vladimir Marko60584552015-09-03 13:35:12 +0000969 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100970 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000971 }
972
David Brazdild26a4112015-11-10 11:07:31 +0000973 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
974 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
975
Vladimir Marko60584552015-09-03 13:35:12 +0000976 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
977 return ContainsElement(successors_, block, start_from);
978 }
979
980 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100981 return dominated_blocks_;
982 }
983
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100984 bool IsEntryBlock() const {
985 return graph_->GetEntryBlock() == this;
986 }
987
988 bool IsExitBlock() const {
989 return graph_->GetExitBlock() == this;
990 }
991
David Brazdil46e2a392015-03-16 17:31:52 +0000992 bool IsSingleGoto() const;
Mads Ager16e52892017-07-14 13:11:37 +0200993 bool IsSingleReturn() const;
Mingyao Yang46721ef2017-10-05 14:45:17 -0700994 bool IsSingleReturnOrReturnVoidAllowingPhis() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000995 bool IsSingleTryBoundary() const;
996
997 // Returns true if this block emits nothing but a jump.
998 bool IsSingleJump() const {
999 HLoopInformation* loop_info = GetLoopInformation();
1000 return (IsSingleGoto() || IsSingleTryBoundary())
1001 // Back edges generate a suspend check.
1002 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
1003 }
David Brazdil46e2a392015-03-16 17:31:52 +00001004
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001005 void AddBackEdge(HBasicBlock* back_edge) {
1006 if (loop_information_ == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001007 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001008 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001009 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001010 loop_information_->AddBackEdge(back_edge);
1011 }
1012
Artem Serov7f4aff62017-06-21 17:02:18 +01001013 // Registers a back edge; if the block was not a loop header before the call associates a newly
1014 // created loop info with it.
1015 //
1016 // Used in SuperblockCloner to preserve LoopInformation object instead of reseting loop
1017 // info for all blocks during back edges recalculation.
1018 void AddBackEdgeWhileUpdating(HBasicBlock* back_edge) {
1019 if (loop_information_ == nullptr || loop_information_->GetHeader() != this) {
1020 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
1021 }
1022 loop_information_->AddBackEdge(back_edge);
1023 }
1024
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001025 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001026 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001027
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001028 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001029 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001030 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001031
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001032 HBasicBlock* GetDominator() const { return dominator_; }
1033 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +00001034 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
1035
1036 void RemoveDominatedBlock(HBasicBlock* block) {
1037 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +01001038 }
Vladimir Marko60584552015-09-03 13:35:12 +00001039
1040 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
1041 ReplaceElement(dominated_blocks_, existing, new_block);
1042 }
1043
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001044 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001045
1046 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001047 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001048 }
1049
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001050 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
1051 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +01001052 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001053 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +01001054 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
1055 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001056
Nicolas Geoffray09aa1472016-01-19 10:52:54 +00001057 HInstruction* GetFirstInstructionDisregardMoves() const;
1058
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001059 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001060 successors_.push_back(block);
1061 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001062 }
1063
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001064 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
1065 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001066 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001067 new_block->predecessors_.push_back(this);
1068 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001069 }
1070
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001071 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
1072 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001073 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001074 new_block->successors_.push_back(this);
1075 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001076 }
1077
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001078 // Insert `this` between `predecessor` and `successor. This method
1079 // preserves the indicies, and will update the first edge found between
1080 // `predecessor` and `successor`.
1081 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
1082 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001083 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +00001084 successor->predecessors_[predecessor_index] = this;
1085 predecessor->successors_[successor_index] = this;
1086 successors_.push_back(successor);
1087 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001088 }
1089
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001090 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001091 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001092 }
1093
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001094 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001095 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001096 }
1097
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001098 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001099 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001100 }
1101
1102 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001103 predecessors_.push_back(block);
1104 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001105 }
1106
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001107 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001108 DCHECK_EQ(predecessors_.size(), 2u);
1109 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001110 }
1111
David Brazdil769c9e52015-04-27 13:54:09 +01001112 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001113 DCHECK_EQ(successors_.size(), 2u);
1114 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +01001115 }
1116
David Brazdilfc6a86a2015-06-26 10:33:45 +00001117 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001118 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001119 }
1120
David Brazdilfc6a86a2015-06-26 10:33:45 +00001121 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001122 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001123 }
1124
David Brazdilfc6a86a2015-06-26 10:33:45 +00001125 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001126 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001127 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001128 }
1129
1130 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001131 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001132 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001133 }
1134
1135 // Returns whether the first occurrence of `predecessor` in the list of
1136 // predecessors is at index `idx`.
1137 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001138 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001139 return GetPredecessorIndexOf(predecessor) == idx;
1140 }
1141
David Brazdild7558da2015-09-22 13:04:14 +01001142 // Create a new block between this block and its predecessors. The new block
1143 // is added to the graph, all predecessor edges are relinked to it and an edge
1144 // is created to `this`. Returns the new empty block. Reverse post order or
1145 // loop and try/catch information are not updated.
1146 HBasicBlock* CreateImmediateDominator();
1147
David Brazdilfc6a86a2015-06-26 10:33:45 +00001148 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001149 // created, latter block. Note that this method will add the block to the
1150 // graph, create a Goto at the end of the former block and will create an edge
1151 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001152 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001153 HBasicBlock* SplitBefore(HInstruction* cursor);
1154
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001155 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001156 // created block. Note that this method just updates raw block information,
1157 // like predecessors, successors, dominators, and instruction list. It does not
1158 // update the graph, reverse post order, loop information, nor make sure the
1159 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001160 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1161
1162 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1163 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001164
1165 // Merge `other` at the end of `this`. Successors and dominated blocks of
1166 // `other` are changed to be successors and dominated blocks of `this`. Note
1167 // that this method does not update the graph, reverse post order, loop
1168 // information, nor make sure the blocks are consistent (for example ending
1169 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001170 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001171
1172 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1173 // of `this` are moved to `other`.
1174 // Note that this method does not update the graph, reverse post order, loop
1175 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001176 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001177 void ReplaceWith(HBasicBlock* other);
1178
Aart Bik6b69e0a2017-01-11 10:20:43 -08001179 // Merges the instructions of `other` at the end of `this`.
1180 void MergeInstructionsWith(HBasicBlock* other);
1181
David Brazdil2d7352b2015-04-20 14:52:42 +01001182 // Merge `other` at the end of `this`. This method updates loops, reverse post
1183 // order, links to predecessors, successors, dominators and deletes the block
1184 // from the graph. The two blocks must be successive, i.e. `this` the only
1185 // predecessor of `other` and vice versa.
1186 void MergeWith(HBasicBlock* other);
1187
1188 // Disconnects `this` from all its predecessors, successors and dominator,
1189 // removes it from all loops it is included in and eventually from the graph.
1190 // The block must not dominate any other block. Predecessors and successors
1191 // are safely updated.
1192 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001193
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001194 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001195 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001196 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001197 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Artem Serovcced8ba2017-07-19 18:18:09 +01001198 // Replace phi `initial` with `replacement` within this block.
1199 void ReplaceAndRemovePhiWith(HPhi* initial, HPhi* replacement);
Roland Levillainccc07a92014-09-16 14:48:16 +01001200 // Replace instruction `initial` with `replacement` within this block.
1201 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1202 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001203 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001204 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001205 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1206 // instruction list. With 'ensure_safety' set to true, it verifies that the
1207 // instruction is not in use and removes it from the use lists of its inputs.
1208 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1209 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001210 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001211
1212 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001213 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001214 }
1215
Roland Levillain6b879dd2014-09-22 17:13:44 +01001216 bool IsLoopPreHeaderFirstPredecessor() const {
1217 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001218 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001219 }
1220
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001221 bool IsFirstPredecessorBackEdge() const {
1222 DCHECK(IsLoopHeader());
1223 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1224 }
1225
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001226 HLoopInformation* GetLoopInformation() const {
1227 return loop_information_;
1228 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001229
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001230 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001231 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001232 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001233 void SetInLoop(HLoopInformation* info) {
1234 if (IsLoopHeader()) {
1235 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001236 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001237 loop_information_ = info;
1238 } else if (loop_information_->Contains(*info->GetHeader())) {
1239 // Block is currently part of an outer loop. Make it part of this inner loop.
1240 // Note that a non loop header having a loop information means this loop information
1241 // has already been populated
1242 loop_information_ = info;
1243 } else {
1244 // Block is part of an inner loop. Do not update the loop information.
1245 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1246 // at this point, because this method is being called while populating `info`.
1247 }
1248 }
1249
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001250 // Raw update of the loop information.
1251 void SetLoopInformation(HLoopInformation* info) {
1252 loop_information_ = info;
1253 }
1254
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001255 bool IsInLoop() const { return loop_information_ != nullptr; }
1256
David Brazdilec16f792015-08-19 15:04:01 +01001257 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1258
1259 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1260 try_catch_information_ = try_catch_information;
1261 }
1262
1263 bool IsTryBlock() const {
1264 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1265 }
1266
1267 bool IsCatchBlock() const {
1268 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1269 }
David Brazdilffee3d32015-07-06 11:48:53 +01001270
1271 // Returns the try entry that this block's successors should have. They will
1272 // be in the same try, unless the block ends in a try boundary. In that case,
1273 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001274 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001275
Aart Bik75ff2c92018-04-21 01:28:11 +00001276 bool HasThrowingInstructions() const;
1277
David Brazdila4b8c212015-05-07 09:59:30 +01001278 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001279 bool Dominates(HBasicBlock* block) const;
1280
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001281 size_t GetLifetimeStart() const { return lifetime_start_; }
1282 size_t GetLifetimeEnd() const { return lifetime_end_; }
1283
1284 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1285 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1286
David Brazdil8d5b8b22015-03-24 10:51:52 +00001287 bool EndsWithControlFlowInstruction() const;
Aart Bik4dc09e72018-05-11 14:40:31 -07001288 bool EndsWithReturn() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001289 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001290 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001291 bool HasSinglePhi() const;
1292
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001293 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001294 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001295 ArenaVector<HBasicBlock*> predecessors_;
1296 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001297 HInstructionList instructions_;
1298 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001299 HLoopInformation* loop_information_;
1300 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001301 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001302 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001303 // The dex program counter of the first instruction of this block.
1304 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001305 size_t lifetime_start_;
1306 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001307 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001308
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001309 friend class HGraph;
1310 friend class HInstruction;
1311
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001312 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1313};
1314
David Brazdilb2bd1c52015-03-25 11:17:37 +00001315// Iterates over the LoopInformation of all loops which contain 'block'
1316// from the innermost to the outermost.
1317class HLoopInformationOutwardIterator : public ValueObject {
1318 public:
1319 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1320 : current_(block.GetLoopInformation()) {}
1321
1322 bool Done() const { return current_ == nullptr; }
1323
1324 void Advance() {
1325 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001326 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001327 }
1328
1329 HLoopInformation* Current() const {
1330 DCHECK(!Done());
1331 return current_;
1332 }
1333
1334 private:
1335 HLoopInformation* current_;
1336
1337 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1338};
1339
Alexandre Ramesef20f712015-06-09 10:29:30 +01001340#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001341 M(Above, Condition) \
1342 M(AboveOrEqual, Condition) \
Aart Bik3dad3412018-02-28 12:01:46 -08001343 M(Abs, UnaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001344 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001345 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001346 M(ArrayGet, Instruction) \
1347 M(ArrayLength, Instruction) \
1348 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001349 M(Below, Condition) \
1350 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001351 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001352 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001353 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001354 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001355 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001356 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001357 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001358 M(Compare, BinaryOperation) \
Igor Murashkind01745e2017-04-05 16:40:31 -07001359 M(ConstructorFence, Instruction) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001360 M(CurrentMethod, Instruction) \
Mingyao Yang063fc772016-08-02 11:02:54 -07001361 M(ShouldDeoptimizeFlag, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001362 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001363 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001364 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001365 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001366 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001367 M(Exit, Instruction) \
1368 M(FloatConstant, Constant) \
1369 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001370 M(GreaterThan, Condition) \
1371 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001372 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001373 M(InstanceFieldGet, Instruction) \
1374 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001375 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001376 M(IntConstant, Constant) \
xueliang.zhonge0eb4832017-10-30 13:43:14 +00001377 M(IntermediateAddress, Instruction) \
Calin Juravle175dc732015-08-25 15:42:32 +01001378 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001379 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001380 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001381 M(InvokeVirtual, Invoke) \
Orion Hodsonac141392017-01-13 11:53:47 +00001382 M(InvokePolymorphic, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001383 M(LessThan, Condition) \
1384 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001385 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001386 M(LoadException, Instruction) \
Orion Hodsondbaa5c72018-05-10 08:22:46 +01001387 M(LoadMethodHandle, Instruction) \
Orion Hodson18259d72018-04-12 11:18:23 +01001388 M(LoadMethodType, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001389 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001390 M(LongConstant, Constant) \
Aart Bik1f8d51b2018-02-15 10:42:37 -08001391 M(Max, Instruction) \
Calin Juravle27df7582015-04-17 19:12:31 +01001392 M(MemoryBarrier, Instruction) \
Aart Bik1f8d51b2018-02-15 10:42:37 -08001393 M(Min, BinaryOperation) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001394 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001395 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001396 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001397 M(Neg, UnaryOperation) \
1398 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001399 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001400 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001401 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001402 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001403 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001404 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001405 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001406 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001407 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001408 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001409 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001410 M(Return, Instruction) \
1411 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001412 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001413 M(Shl, BinaryOperation) \
1414 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001415 M(StaticFieldGet, Instruction) \
1416 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001417 M(UnresolvedInstanceFieldGet, Instruction) \
1418 M(UnresolvedInstanceFieldSet, Instruction) \
1419 M(UnresolvedStaticFieldGet, Instruction) \
1420 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001421 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001422 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001423 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001424 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001425 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001426 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001427 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001428 M(Xor, BinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001429 M(VecReplicateScalar, VecUnaryOperation) \
Aart Bik0148de42017-09-05 09:25:01 -07001430 M(VecExtractScalar, VecUnaryOperation) \
1431 M(VecReduce, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001432 M(VecCnv, VecUnaryOperation) \
1433 M(VecNeg, VecUnaryOperation) \
Aart Bik6daebeb2017-04-03 14:35:41 -07001434 M(VecAbs, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001435 M(VecNot, VecUnaryOperation) \
1436 M(VecAdd, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001437 M(VecHalvingAdd, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001438 M(VecSub, VecBinaryOperation) \
1439 M(VecMul, VecBinaryOperation) \
1440 M(VecDiv, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001441 M(VecMin, VecBinaryOperation) \
1442 M(VecMax, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001443 M(VecAnd, VecBinaryOperation) \
1444 M(VecAndNot, VecBinaryOperation) \
1445 M(VecOr, VecBinaryOperation) \
1446 M(VecXor, VecBinaryOperation) \
Aart Bik29aa0822018-03-08 11:28:00 -08001447 M(VecSaturationAdd, VecBinaryOperation) \
1448 M(VecSaturationSub, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001449 M(VecShl, VecBinaryOperation) \
1450 M(VecShr, VecBinaryOperation) \
1451 M(VecUShr, VecBinaryOperation) \
Aart Bik8de59162017-04-21 09:42:01 -07001452 M(VecSetScalars, VecOperation) \
Artem Serovf34dd202017-04-10 17:41:46 +01001453 M(VecMultiplyAccumulate, VecOperation) \
Aart Bikdbbac8f2017-09-01 13:06:08 -07001454 M(VecSADAccumulate, VecOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001455 M(VecLoad, VecMemoryOperation) \
1456 M(VecStore, VecMemoryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001457
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001458/*
1459 * Instructions, shared across several (not all) architectures.
1460 */
1461#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1462#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1463#else
1464#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001465 M(BitwiseNegatedRight, Instruction) \
Anton Kirilov74234da2017-01-13 14:42:47 +00001466 M(DataProcWithShifterOp, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001467 M(MultiplyAccumulate, Instruction) \
Artem Serove1811ed2017-04-27 16:50:47 +01001468 M(IntermediateAddressIndex, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001469#endif
1470
Alexandre Ramesef20f712015-06-09 10:29:30 +01001471#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1472
1473#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1474
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001475#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001476#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001477#else
1478#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1479 M(MipsComputeBaseMethodAddress, Instruction) \
Lena Djokica2901602017-09-21 13:50:52 +02001480 M(MipsPackedSwitch, Instruction) \
1481 M(IntermediateArrayAddressIndex, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001482#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001483
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001484#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1485
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001486#ifndef ART_ENABLE_CODEGEN_x86
1487#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1488#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001489#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1490 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001491 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001492 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001493 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001494#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001495
1496#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1497
1498#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1499 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001500 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001501 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1502 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001503 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001504 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001505 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1506 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1507
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001508#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1509 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001510 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001511 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001512 M(BinaryOperation, Instruction) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001513 M(Invoke, Instruction) \
1514 M(VecOperation, Instruction) \
1515 M(VecUnaryOperation, VecOperation) \
1516 M(VecBinaryOperation, VecOperation) \
1517 M(VecMemoryOperation, VecOperation)
Dave Allison20dfc792014-06-16 20:44:29 -07001518
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001519#define FOR_EACH_INSTRUCTION(M) \
1520 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1521 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1522
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001523#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001524FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1525#undef FORWARD_DECLARATION
1526
Vladimir Marko372f10e2016-05-17 16:30:10 +01001527#define DECLARE_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001528 private: \
1529 H##type& operator=(const H##type&) = delete; \
1530 public: \
Vladimir Marko372f10e2016-05-17 16:30:10 +01001531 const char* DebugName() const OVERRIDE { return #type; } \
Artem Serovcced8ba2017-07-19 18:18:09 +01001532 HInstruction* Clone(ArenaAllocator* arena) const OVERRIDE { \
1533 DCHECK(IsClonable()); \
1534 return new (arena) H##type(*this->As##type()); \
1535 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001536 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001537
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001538#define DECLARE_ABSTRACT_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001539 private: \
1540 H##type& operator=(const H##type&) = delete; \
Vladimir Markoa90dd512018-05-04 15:04:45 +01001541 public:
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001542
Artem Serovcced8ba2017-07-19 18:18:09 +01001543#define DEFAULT_COPY_CONSTRUCTOR(type) \
1544 explicit H##type(const H##type& other) = default;
1545
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001546template <typename T>
Vladimir Marko82b07402017-03-01 19:02:04 +00001547class HUseListNode : public ArenaObject<kArenaAllocUseListNode>,
1548 public IntrusiveForwardListNode<HUseListNode<T>> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001549 public:
Igor Murashkind01745e2017-04-05 16:40:31 -07001550 // Get the instruction which has this use as one of the inputs.
David Brazdiled596192015-01-23 10:39:45 +00001551 T GetUser() const { return user_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001552 // Get the position of the input record that this use corresponds to.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001553 size_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001554 // Set the position of the input record that this use corresponds to.
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001555 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001556
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001557 private:
David Brazdiled596192015-01-23 10:39:45 +00001558 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001559 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001560
1561 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001562 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001563
Vladimir Marko46817b82016-03-29 12:21:58 +01001564 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001565
1566 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1567};
1568
David Brazdiled596192015-01-23 10:39:45 +00001569template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001570using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001571
David Brazdil1abb4192015-02-17 18:33:36 +00001572// This class is used by HEnvironment and HInstruction classes to record the
1573// instructions they use and pointers to the corresponding HUseListNodes kept
1574// by the used instructions.
1575template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001576class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001577 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001578 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1579 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001580
Vladimir Marko46817b82016-03-29 12:21:58 +01001581 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1582 : HUserRecord(old_record.instruction_, before_use_node) {}
1583 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1584 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001585 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001586 }
1587
1588 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001589 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1590 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001591
1592 private:
1593 // Instruction used by the user.
1594 HInstruction* instruction_;
1595
Vladimir Marko46817b82016-03-29 12:21:58 +01001596 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1597 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001598};
1599
Vladimir Markoe9004912016-06-16 16:50:52 +01001600// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1601// This is used for HInstruction::GetInputs() to return a container wrapper providing
1602// HInstruction* values even though the underlying container has HUserRecord<>s.
1603struct HInputExtractor {
1604 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1605 return record.GetInstruction();
1606 }
1607 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1608 return record.GetInstruction();
1609 }
1610};
1611
1612using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1613using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1614
Aart Bik854a02b2015-07-14 16:07:00 -07001615/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001616 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001617 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001618 * For write/read dependences on fields/arrays, the dependence analysis uses
1619 * type disambiguation (e.g. a float field write cannot modify the value of an
1620 * integer field read) and the access type (e.g. a reference array write cannot
1621 * modify the value of a reference field read [although it may modify the
1622 * reference fetch prior to reading the field, which is represented by its own
1623 * write/read dependence]). The analysis makes conservative points-to
1624 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1625 * the same, and any reference read depends on any reference read without
1626 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001627 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001628 * The internal representation uses 38-bit and is described in the table below.
1629 * The first line indicates the side effect, and for field/array accesses the
1630 * second line indicates the type of the access (in the order of the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001631 * DataType::Type enum).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001632 * The two numbered lines below indicate the bit position in the bitfield (read
1633 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001634 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001635 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1636 * +-------------+---------+---------+--------------+---------+---------+
1637 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1638 * | 3 |333333322|222222221| 1 |111111110|000000000|
1639 * | 7 |654321098|765432109| 8 |765432109|876543210|
1640 *
1641 * Note that, to ease the implementation, 'changes' bits are least significant
1642 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001643 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001644class SideEffects : public ValueObject {
1645 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001646 SideEffects() : flags_(0) {}
1647
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001648 static SideEffects None() {
1649 return SideEffects(0);
1650 }
1651
1652 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001653 return SideEffects(kAllChangeBits | kAllDependOnBits);
1654 }
1655
1656 static SideEffects AllChanges() {
1657 return SideEffects(kAllChangeBits);
1658 }
1659
1660 static SideEffects AllDependencies() {
1661 return SideEffects(kAllDependOnBits);
1662 }
1663
1664 static SideEffects AllExceptGCDependency() {
1665 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1666 }
1667
1668 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001669 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001670 }
1671
Aart Bik34c3ba92015-07-20 14:08:59 -07001672 static SideEffects AllWrites() {
1673 return SideEffects(kAllWrites);
1674 }
1675
1676 static SideEffects AllReads() {
1677 return SideEffects(kAllReads);
1678 }
1679
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001680 static SideEffects FieldWriteOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001681 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001682 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001683 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001684 }
1685
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001686 static SideEffects ArrayWriteOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001687 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001688 }
1689
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001690 static SideEffects FieldReadOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001691 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001692 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001693 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001694 }
1695
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001696 static SideEffects ArrayReadOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001697 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001698 }
1699
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001700 static SideEffects CanTriggerGC() {
1701 return SideEffects(1ULL << kCanTriggerGCBit);
1702 }
1703
1704 static SideEffects DependsOnGC() {
1705 return SideEffects(1ULL << kDependsOnGCBit);
1706 }
1707
Aart Bik854a02b2015-07-14 16:07:00 -07001708 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001709 SideEffects Union(SideEffects other) const {
1710 return SideEffects(flags_ | other.flags_);
1711 }
1712
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001713 SideEffects Exclusion(SideEffects other) const {
1714 return SideEffects(flags_ & ~other.flags_);
1715 }
1716
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001717 void Add(SideEffects other) {
1718 flags_ |= other.flags_;
1719 }
1720
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001721 bool Includes(SideEffects other) const {
1722 return (other.flags_ & flags_) == other.flags_;
1723 }
1724
1725 bool HasSideEffects() const {
1726 return (flags_ & kAllChangeBits);
1727 }
1728
1729 bool HasDependencies() const {
1730 return (flags_ & kAllDependOnBits);
1731 }
1732
1733 // Returns true if there are no side effects or dependencies.
1734 bool DoesNothing() const {
1735 return flags_ == 0;
1736 }
1737
Aart Bik854a02b2015-07-14 16:07:00 -07001738 // Returns true if something is written.
1739 bool DoesAnyWrite() const {
1740 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001741 }
1742
Aart Bik854a02b2015-07-14 16:07:00 -07001743 // Returns true if something is read.
1744 bool DoesAnyRead() const {
1745 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001746 }
1747
Aart Bik854a02b2015-07-14 16:07:00 -07001748 // Returns true if potentially everything is written and read
1749 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001750 bool DoesAllReadWrite() const {
1751 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1752 }
1753
Aart Bik854a02b2015-07-14 16:07:00 -07001754 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001755 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001756 }
1757
Roland Levillain0d5a2812015-11-13 10:07:31 +00001758 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001759 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001760 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1761 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001762 }
1763
1764 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001765 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001766 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001767 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001768 for (int s = kLastBit; s >= 0; s--) {
1769 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1770 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1771 // This is a bit for the GC side effect.
1772 if (current_bit_is_set) {
1773 flags += "GC";
1774 }
Aart Bik854a02b2015-07-14 16:07:00 -07001775 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001776 } else {
1777 // This is a bit for the array/field analysis.
1778 // The underscore character stands for the 'can trigger GC' bit.
1779 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1780 if (current_bit_is_set) {
1781 flags += kDebug[s];
1782 }
1783 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1784 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1785 flags += "|";
1786 }
1787 }
Aart Bik854a02b2015-07-14 16:07:00 -07001788 }
1789 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001790 }
1791
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001792 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001793
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001794 private:
1795 static constexpr int kFieldArrayAnalysisBits = 9;
1796
1797 static constexpr int kFieldWriteOffset = 0;
1798 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1799 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1800 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1801
1802 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1803
1804 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1805 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1806 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1807 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1808
1809 static constexpr int kLastBit = kDependsOnGCBit;
1810 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1811
1812 // Aliases.
1813
1814 static_assert(kChangeBits == kDependOnBits,
1815 "the 'change' bits should match the 'depend on' bits.");
1816
1817 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1818 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1819 static constexpr uint64_t kAllWrites =
1820 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1821 static constexpr uint64_t kAllReads =
1822 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001823
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001824 // Translates type to bit flag. The type must correspond to a Java type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001825 static uint64_t TypeFlag(DataType::Type type, int offset) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001826 int shift;
1827 switch (type) {
1828 case DataType::Type::kReference: shift = 0; break;
1829 case DataType::Type::kBool: shift = 1; break;
1830 case DataType::Type::kInt8: shift = 2; break;
1831 case DataType::Type::kUint16: shift = 3; break;
1832 case DataType::Type::kInt16: shift = 4; break;
1833 case DataType::Type::kInt32: shift = 5; break;
1834 case DataType::Type::kInt64: shift = 6; break;
1835 case DataType::Type::kFloat32: shift = 7; break;
1836 case DataType::Type::kFloat64: shift = 8; break;
1837 default:
1838 LOG(FATAL) << "Unexpected data type " << type;
1839 UNREACHABLE();
1840 }
Aart Bik854a02b2015-07-14 16:07:00 -07001841 DCHECK_LE(kFieldWriteOffset, shift);
1842 DCHECK_LT(shift, kArrayWriteOffset);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001843 return UINT64_C(1) << (shift + offset);
Aart Bik854a02b2015-07-14 16:07:00 -07001844 }
1845
1846 // Private constructor on direct flags value.
1847 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1848
1849 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001850};
1851
David Brazdiled596192015-01-23 10:39:45 +00001852// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001853class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001854 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001855 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
Mingyao Yang01b47b02017-02-03 12:09:57 -08001856 size_t number_of_vregs,
1857 ArtMethod* method,
1858 uint32_t dex_pc,
1859 HInstruction* holder)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001860 : vregs_(number_of_vregs, allocator->Adapter(kArenaAllocEnvironmentVRegs)),
1861 locations_(allocator->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001862 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001863 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001864 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001865 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001866 }
1867
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001868 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
1869 const HEnvironment& to_copy,
1870 HInstruction* holder)
1871 : HEnvironment(allocator,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001872 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001873 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001874 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001875 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001876
Vladimir Markoec32f642017-06-02 10:51:55 +01001877 void AllocateLocations() {
1878 DCHECK(locations_.empty());
1879 locations_.resize(vregs_.size());
1880 }
1881
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001882 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001883 if (parent_ != nullptr) {
1884 parent_->SetAndCopyParentChain(allocator, parent);
1885 } else {
1886 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1887 parent_->CopyFrom(parent);
1888 if (parent->GetParent() != nullptr) {
1889 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1890 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001891 }
David Brazdiled596192015-01-23 10:39:45 +00001892 }
1893
Vladimir Marko69d310e2017-10-09 14:12:23 +01001894 void CopyFrom(ArrayRef<HInstruction* const> locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001895 void CopyFrom(HEnvironment* environment);
1896
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001897 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1898 // input to the loop phi instead. This is for inserting instructions that
1899 // require an environment (like HDeoptimization) in the loop pre-header.
1900 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001901
1902 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001903 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001904 }
1905
1906 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001907 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001908 }
1909
David Brazdil1abb4192015-02-17 18:33:36 +00001910 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001911
Artem Serovca210e32017-12-15 13:43:20 +00001912 // Replaces the input at the position 'index' with the replacement; the replacement and old
1913 // input instructions' env_uses_ lists are adjusted. The function works similar to
1914 // HInstruction::ReplaceInput.
1915 void ReplaceInput(HInstruction* replacement, size_t index);
1916
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001917 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001918
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001919 HEnvironment* GetParent() const { return parent_; }
1920
1921 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001922 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001923 }
1924
1925 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001926 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001927 }
1928
1929 uint32_t GetDexPc() const {
1930 return dex_pc_;
1931 }
1932
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001933 ArtMethod* GetMethod() const {
1934 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001935 }
1936
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001937 HInstruction* GetHolder() const {
1938 return holder_;
1939 }
1940
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001941
1942 bool IsFromInlinedInvoke() const {
1943 return GetParent() != nullptr;
1944 }
1945
David Brazdiled596192015-01-23 10:39:45 +00001946 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001947 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1948 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001949 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001950 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001951 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00001952
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001953 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001954 HInstruction* const holder_;
1955
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001956 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001957
1958 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1959};
1960
Vladimir Markof9f64412015-09-02 14:05:49 +01001961class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001962 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05301963#define DECLARE_KIND(type, super) k##type,
1964 enum InstructionKind {
Vladimir Markoe3946222018-05-04 14:18:47 +01001965 FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_KIND)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05301966 kLastInstructionKind
1967 };
1968#undef DECLARE_KIND
1969
1970 HInstruction(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01001971 : HInstruction(kind, DataType::Type::kVoid, side_effects, dex_pc) {}
1972
1973 HInstruction(InstructionKind kind, DataType::Type type, SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001974 : previous_(nullptr),
1975 next_(nullptr),
1976 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001977 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001978 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001979 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001980 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001981 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001982 locations_(nullptr),
1983 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001984 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001985 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001986 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05301987 SetPackedField<InstructionKindField>(kind);
Vladimir Markobd785672018-05-03 17:09:09 +01001988 SetPackedField<TypeField>(type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001989 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1990 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001991
Dave Allison20dfc792014-06-16 20:44:29 -07001992 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001993
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001994
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001995 HInstruction* GetNext() const { return next_; }
1996 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001997
Calin Juravle77520bc2015-01-12 18:45:46 +00001998 HInstruction* GetNextDisregardingMoves() const;
1999 HInstruction* GetPreviousDisregardingMoves() const;
2000
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002001 HBasicBlock* GetBlock() const { return block_; }
Vladimir Markoca6fff82017-10-03 14:49:14 +01002002 ArenaAllocator* GetAllocator() const { return block_->GetGraph()->GetAllocator(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002003 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002004 bool IsInBlock() const { return block_ != nullptr; }
2005 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00002006 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
2007 bool IsIrreducibleLoopHeaderPhi() const {
2008 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
2009 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002010
Vladimir Marko372f10e2016-05-17 16:30:10 +01002011 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
2012
2013 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
2014 // One virtual method is enough, just const_cast<> and then re-add the const.
2015 return ArrayRef<const HUserRecord<HInstruction*>>(
2016 const_cast<HInstruction*>(this)->GetInputRecords());
2017 }
2018
Vladimir Markoe9004912016-06-16 16:50:52 +01002019 HInputsRef GetInputs() {
2020 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002021 }
2022
Vladimir Markoe9004912016-06-16 16:50:52 +01002023 HConstInputsRef GetInputs() const {
2024 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002025 }
2026
2027 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00002028 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002029
Aart Bik2767f4b2016-10-28 15:03:53 -07002030 bool HasInput(HInstruction* input) const {
2031 for (const HInstruction* i : GetInputs()) {
2032 if (i == input) {
2033 return true;
2034 }
2035 }
2036 return false;
2037 }
2038
Vladimir Marko372f10e2016-05-17 16:30:10 +01002039 void SetRawInputAt(size_t index, HInstruction* input) {
2040 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
2041 }
2042
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002043 virtual void Accept(HGraphVisitor* visitor) = 0;
2044 virtual const char* DebugName() const = 0;
2045
Vladimir Markobd785672018-05-03 17:09:09 +01002046 DataType::Type GetType() const {
2047 return TypeField::Decode(GetPackedFields());
2048 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002049
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002050 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002051
2052 uint32_t GetDexPc() const { return dex_pc_; }
2053
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002054 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01002055
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002056 // Can the instruction throw?
2057 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
2058 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01002059 virtual bool CanThrow() const { return false; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08002060
2061 // Does the instruction always throw an exception unconditionally?
2062 virtual bool AlwaysThrows() const { return false; }
2063
David Brazdilec16f792015-08-19 15:04:01 +01002064 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002065
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002066 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002067 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002068
Calin Juravle10e244f2015-01-26 18:54:32 +00002069 // Does not apply for all instructions, but having this at top level greatly
2070 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00002071 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002072 virtual bool CanBeNull() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002073 DCHECK_EQ(GetType(), DataType::Type::kReference) << "CanBeNull only applies to reference types";
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002074 return true;
2075 }
Calin Juravle10e244f2015-01-26 18:54:32 +00002076
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002077 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01002078 return false;
2079 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002080
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002081 virtual bool IsActualObject() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002082 return GetType() == DataType::Type::kReference;
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002083 }
2084
Calin Juravle2e768302015-07-28 14:41:11 +00002085 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00002086
Calin Juravle61d544b2015-02-23 16:46:57 +00002087 ReferenceTypeInfo GetReferenceTypeInfo() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002088 DCHECK_EQ(GetType(), DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002089 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00002090 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00002091 }
Calin Juravleacf735c2015-02-12 15:25:22 +00002092
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002093 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00002094 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002095 // Note: fixup_end remains valid across push_front().
2096 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
2097 HUseListNode<HInstruction*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002098 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HInstruction*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002099 uses_.push_front(*new_node);
2100 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002101 }
2102
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002103 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01002104 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002105 // Note: env_fixup_end remains valid across push_front().
2106 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
2107 HUseListNode<HEnvironment*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002108 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HEnvironment*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002109 env_uses_.push_front(*new_node);
2110 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002111 }
2112
David Brazdil1abb4192015-02-17 18:33:36 +00002113 void RemoveAsUserOfInput(size_t input) {
2114 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002115 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2116 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2117 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00002118 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002119
Vladimir Marko372f10e2016-05-17 16:30:10 +01002120 void RemoveAsUserOfAllInputs() {
2121 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2122 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2123 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2124 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
2125 }
2126 }
2127
David Brazdil1abb4192015-02-17 18:33:36 +00002128 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2129 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002130
Vladimir Marko46817b82016-03-29 12:21:58 +01002131 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2132 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2133 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002134 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002135 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002136 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002137
Aart Bikcc42be02016-10-20 16:14:16 -07002138 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002139 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002140 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002141 !CanThrow() &&
2142 !IsSuspendCheck() &&
2143 !IsControlFlow() &&
2144 !IsNativeDebugInfo() &&
2145 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002146 // If we added an explicit barrier then we should keep it.
Igor Murashkind01745e2017-04-05 16:40:31 -07002147 !IsMemoryBarrier() &&
2148 !IsConstructorFence();
Aart Bik482095d2016-10-10 15:39:10 -07002149 }
2150
Aart Bikcc42be02016-10-20 16:14:16 -07002151 bool IsDeadAndRemovable() const {
2152 return IsRemovable() && !HasUses();
2153 }
2154
Roland Levillain6c82d402014-10-13 16:10:27 +01002155 // Does this instruction strictly dominate `other_instruction`?
2156 // Returns false if this instruction and `other_instruction` are the same.
2157 // Aborts if this instruction and `other_instruction` are both phis.
2158 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002159
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002160 int GetId() const { return id_; }
2161 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002162
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002163 int GetSsaIndex() const { return ssa_index_; }
2164 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2165 bool HasSsaIndex() const { return ssa_index_ != -1; }
2166
2167 bool HasEnvironment() const { return environment_ != nullptr; }
2168 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002169 // Set the `environment_` field. Raw because this method does not
2170 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002171 void SetRawEnvironment(HEnvironment* environment) {
2172 DCHECK(environment_ == nullptr);
2173 DCHECK_EQ(environment->GetHolder(), this);
2174 environment_ = environment;
2175 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002176
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002177 void InsertRawEnvironment(HEnvironment* environment) {
2178 DCHECK(environment_ != nullptr);
2179 DCHECK_EQ(environment->GetHolder(), this);
2180 DCHECK(environment->GetParent() == nullptr);
2181 environment->parent_ = environment_;
2182 environment_ = environment;
2183 }
2184
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002185 void RemoveEnvironment();
2186
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002187 // Set the environment of this instruction, copying it from `environment`. While
2188 // copying, the uses lists are being updated.
2189 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002190 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002191 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002192 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002193 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002194 if (environment->GetParent() != nullptr) {
2195 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2196 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002197 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002198
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002199 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2200 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002201 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002202 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002203 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002204 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002205 if (environment->GetParent() != nullptr) {
2206 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2207 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002208 }
2209
Nicolas Geoffray39468442014-09-02 15:17:15 +01002210 // Returns the number of entries in the environment. Typically, that is the
2211 // number of dex registers in a method. It could be more in case of inlining.
2212 size_t EnvironmentSize() const;
2213
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002214 LocationSummary* GetLocations() const { return locations_; }
2215 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002216
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002217 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002218 void ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002219 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002220
Alexandre Rames188d4312015-04-09 18:30:21 +01002221 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2222 // uses of this instruction by `other` are *not* updated.
2223 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2224 ReplaceWith(other);
2225 other->ReplaceInput(this, use_index);
2226 }
2227
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002228 // Move `this` instruction before `cursor`
2229 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002230
Vladimir Markofb337ea2015-11-25 15:25:10 +00002231 // Move `this` before its first user and out of any loops. If there is no
2232 // out-of-loop user that dominates all other users, move the instruction
2233 // to the end of the out-of-loop common dominator of the user's blocks.
2234 //
2235 // This can be used only on non-throwing instructions with no side effects that
2236 // have at least one use but no environment uses.
2237 void MoveBeforeFirstUserAndOutOfLoops();
2238
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002239#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01002240 bool Is##type() const;
2241
2242 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2243#undef INSTRUCTION_TYPE_CHECK
2244
2245#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002246 const H##type* As##type() const; \
2247 H##type* As##type();
2248
Vladimir Markoa90dd512018-05-04 15:04:45 +01002249 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
2250#undef INSTRUCTION_TYPE_CAST
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002251
Artem Serovcced8ba2017-07-19 18:18:09 +01002252 // Return a clone of the instruction if it is clonable (shallow copy by default, custom copy
2253 // if a custom copy-constructor is provided for a particular type). If IsClonable() is false for
2254 // the instruction then the behaviour of this function is undefined.
2255 //
2256 // Note: It is semantically valid to create a clone of the instruction only until
2257 // prepare_for_register_allocator phase as lifetime, intervals and codegen info are not
2258 // copied.
2259 //
2260 // Note: HEnvironment and some other fields are not copied and are set to default values, see
2261 // 'explicit HInstruction(const HInstruction& other)' for details.
2262 virtual HInstruction* Clone(ArenaAllocator* arena ATTRIBUTE_UNUSED) const {
2263 LOG(FATAL) << "Cloning is not implemented for the instruction " <<
2264 DebugName() << " " << GetId();
2265 UNREACHABLE();
2266 }
2267
2268 // Return whether instruction can be cloned (copied).
2269 virtual bool IsClonable() const { return false; }
2270
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002271 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002272 // TODO: this method is used by LICM and GVN with possibly different
2273 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002274 virtual bool CanBeMoved() const { return false; }
2275
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002276 // Returns whether any data encoded in the two instructions is equal.
2277 // This method does not look at the inputs. Both instructions must be
2278 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002279 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002280 return false;
2281 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002282
2283 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002284 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002285 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002286 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002287
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002288 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2289 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2290 // the virtual function because the __attribute__((__pure__)) doesn't really
2291 // apply the strong requirement for virtual functions, preventing optimizations.
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302292 InstructionKind GetKind() const { return GetPackedField<InstructionKindField>(); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002293
2294 virtual size_t ComputeHashCode() const {
2295 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002296 for (const HInstruction* input : GetInputs()) {
2297 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002298 }
2299 return result;
2300 }
2301
2302 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002303 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002304 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002305
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002306 size_t GetLifetimePosition() const { return lifetime_position_; }
2307 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2308 LiveInterval* GetLiveInterval() const { return live_interval_; }
2309 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2310 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2311
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002312 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2313
2314 // Returns whether the code generation of the instruction will require to have access
2315 // to the current method. Such instructions are:
2316 // (1): Instructions that require an environment, as calling the runtime requires
2317 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002318 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2319 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002320 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002321 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002322 }
2323
Vladimir Markodc151b22015-10-15 18:02:30 +01002324 // Returns whether the code generation of the instruction will require to have access
2325 // to the dex cache of the current method's declaring class via the current method.
2326 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002327
Mark Mendellc4701932015-04-10 13:18:51 -04002328 // Does this instruction have any use in an environment before
2329 // control flow hits 'other'?
2330 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2331
2332 // Remove all references to environment uses of this instruction.
2333 // The caller must ensure that this is safe to do.
2334 void RemoveEnvironmentUsers();
2335
Vladimir Markoa1de9182016-02-25 11:37:38 +00002336 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2337 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002338
David Brazdil1abb4192015-02-17 18:33:36 +00002339 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002340 // If set, the machine code for this instruction is assumed to be generated by
2341 // its users. Used by liveness analysis to compute use positions accordingly.
2342 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2343 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302344 static constexpr size_t kFieldInstructionKind = kFlagReferenceTypeIsExact + 1;
2345 static constexpr size_t kFieldInstructionKindSize =
2346 MinimumBitsToStore(static_cast<size_t>(InstructionKind::kLastInstructionKind - 1));
Vladimir Markobd785672018-05-03 17:09:09 +01002347 static constexpr size_t kFieldType =
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302348 kFieldInstructionKind + kFieldInstructionKindSize;
Vladimir Markobd785672018-05-03 17:09:09 +01002349 static constexpr size_t kFieldTypeSize =
2350 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
2351 static constexpr size_t kNumberOfGenericPackedBits = kFieldType + kFieldTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00002352 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2353
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302354 static_assert(kNumberOfGenericPackedBits <= kMaxNumberOfPackedBits,
2355 "Too many generic packed fields");
2356
Vladimir Markobd785672018-05-03 17:09:09 +01002357 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
2358
Vladimir Marko372f10e2016-05-17 16:30:10 +01002359 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2360 return GetInputRecords()[i];
2361 }
2362
2363 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2364 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2365 input_records[index] = input;
2366 }
David Brazdil1abb4192015-02-17 18:33:36 +00002367
Vladimir Markoa1de9182016-02-25 11:37:38 +00002368 uint32_t GetPackedFields() const {
2369 return packed_fields_;
2370 }
2371
2372 template <size_t flag>
2373 bool GetPackedFlag() const {
2374 return (packed_fields_ & (1u << flag)) != 0u;
2375 }
2376
2377 template <size_t flag>
2378 void SetPackedFlag(bool value = true) {
2379 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2380 }
2381
2382 template <typename BitFieldType>
2383 typename BitFieldType::value_type GetPackedField() const {
2384 return BitFieldType::Decode(packed_fields_);
2385 }
2386
2387 template <typename BitFieldType>
2388 void SetPackedField(typename BitFieldType::value_type value) {
2389 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2390 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2391 }
2392
Artem Serovcced8ba2017-07-19 18:18:09 +01002393 // Copy construction for the instruction (used for Clone function).
2394 //
2395 // Fields (e.g. lifetime, intervals and codegen info) associated with phases starting from
2396 // prepare_for_register_allocator are not copied (set to default values).
2397 //
2398 // Copy constructors must be provided for every HInstruction type; default copy constructor is
2399 // fine for most of them. However for some of the instructions a custom copy constructor must be
2400 // specified (when instruction has non-trivially copyable fields and must have a special behaviour
2401 // for copying them).
2402 explicit HInstruction(const HInstruction& other)
2403 : previous_(nullptr),
2404 next_(nullptr),
2405 block_(nullptr),
2406 dex_pc_(other.dex_pc_),
2407 id_(-1),
2408 ssa_index_(-1),
2409 packed_fields_(other.packed_fields_),
2410 environment_(nullptr),
2411 locations_(nullptr),
2412 live_interval_(nullptr),
2413 lifetime_position_(kNoLifetime),
2414 side_effects_(other.side_effects_),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302415 reference_type_handle_(other.reference_type_handle_) {
2416 }
Artem Serovcced8ba2017-07-19 18:18:09 +01002417
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002418 private:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302419 using InstructionKindField =
2420 BitField<InstructionKind, kFieldInstructionKind, kFieldInstructionKindSize>;
2421
Vladimir Marko46817b82016-03-29 12:21:58 +01002422 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2423 auto before_use_node = uses_.before_begin();
2424 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2425 HInstruction* user = use_node->GetUser();
2426 size_t input_index = use_node->GetIndex();
2427 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2428 before_use_node = use_node;
2429 }
2430 }
2431
2432 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2433 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2434 if (next != uses_.end()) {
2435 HInstruction* next_user = next->GetUser();
2436 size_t next_index = next->GetIndex();
2437 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2438 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2439 }
2440 }
2441
2442 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2443 auto before_env_use_node = env_uses_.before_begin();
2444 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2445 HEnvironment* user = env_use_node->GetUser();
2446 size_t input_index = env_use_node->GetIndex();
2447 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2448 before_env_use_node = env_use_node;
2449 }
2450 }
2451
2452 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2453 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2454 if (next != env_uses_.end()) {
2455 HEnvironment* next_user = next->GetUser();
2456 size_t next_index = next->GetIndex();
2457 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2458 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2459 }
2460 }
David Brazdil1abb4192015-02-17 18:33:36 +00002461
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002462 HInstruction* previous_;
2463 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002464 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002465 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002466
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002467 // An instruction gets an id when it is added to the graph.
2468 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002469 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002470 int id_;
2471
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002472 // When doing liveness analysis, instructions that have uses get an SSA index.
2473 int ssa_index_;
2474
Vladimir Markoa1de9182016-02-25 11:37:38 +00002475 // Packed fields.
2476 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002477
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002478 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002479 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002480
2481 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002482 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002483
Nicolas Geoffray39468442014-09-02 15:17:15 +01002484 // The environment associated with this instruction. Not null if the instruction
2485 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002486 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002487
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002488 // Set by the code generator.
2489 LocationSummary* locations_;
2490
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002491 // Set by the liveness analysis.
2492 LiveInterval* live_interval_;
2493
2494 // Set by the liveness analysis, this is the position in a linear
2495 // order of blocks where this instruction's live interval start.
2496 size_t lifetime_position_;
2497
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002498 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002499
Vladimir Markoa1de9182016-02-25 11:37:38 +00002500 // The reference handle part of the reference type info.
2501 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002502 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002503 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002504
David Brazdil1abb4192015-02-17 18:33:36 +00002505 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002506 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002507 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002508 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002509 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002510};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002511std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002512
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002513// Iterates over the instructions, while preserving the next instruction
2514// in case the current instruction gets removed from the list by the user
2515// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002516class HInstructionIterator : public ValueObject {
2517 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002518 explicit HInstructionIterator(const HInstructionList& instructions)
2519 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002520 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002521 }
2522
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002523 bool Done() const { return instruction_ == nullptr; }
2524 HInstruction* Current() const { return instruction_; }
2525 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002526 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002527 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002528 }
2529
2530 private:
2531 HInstruction* instruction_;
2532 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002533
2534 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002535};
2536
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002537// Iterates over the instructions without saving the next instruction,
2538// therefore handling changes in the graph potentially made by the user
2539// of this iterator.
2540class HInstructionIteratorHandleChanges : public ValueObject {
2541 public:
2542 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2543 : instruction_(instructions.first_instruction_) {
2544 }
2545
2546 bool Done() const { return instruction_ == nullptr; }
2547 HInstruction* Current() const { return instruction_; }
2548 void Advance() {
2549 instruction_ = instruction_->GetNext();
2550 }
2551
2552 private:
2553 HInstruction* instruction_;
2554
2555 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2556};
2557
2558
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002559class HBackwardInstructionIterator : public ValueObject {
2560 public:
2561 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2562 : instruction_(instructions.last_instruction_) {
2563 next_ = Done() ? nullptr : instruction_->GetPrevious();
2564 }
2565
2566 bool Done() const { return instruction_ == nullptr; }
2567 HInstruction* Current() const { return instruction_; }
2568 void Advance() {
2569 instruction_ = next_;
2570 next_ = Done() ? nullptr : instruction_->GetPrevious();
2571 }
2572
2573 private:
2574 HInstruction* instruction_;
2575 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002576
2577 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002578};
2579
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002580class HVariableInputSizeInstruction : public HInstruction {
2581 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002582 using HInstruction::GetInputRecords; // Keep the const version visible.
2583 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
2584 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2585 }
2586
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002587 void AddInput(HInstruction* input);
2588 void InsertInputAt(size_t index, HInstruction* input);
2589 void RemoveInputAt(size_t index);
2590
Igor Murashkind01745e2017-04-05 16:40:31 -07002591 // Removes all the inputs.
2592 // Also removes this instructions from each input's use list
2593 // (for non-environment uses only).
2594 void RemoveAllInputs();
2595
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002596 protected:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302597 HVariableInputSizeInstruction(InstructionKind inst_kind,
2598 SideEffects side_effects,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002599 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002600 ArenaAllocator* allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002601 size_t number_of_inputs,
2602 ArenaAllocKind kind)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302603 : HInstruction(inst_kind, side_effects, dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002604 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Vladimir Markobd785672018-05-03 17:09:09 +01002605 HVariableInputSizeInstruction(InstructionKind inst_kind,
2606 DataType::Type type,
2607 SideEffects side_effects,
2608 uint32_t dex_pc,
2609 ArenaAllocator* allocator,
2610 size_t number_of_inputs,
2611 ArenaAllocKind kind)
2612 : HInstruction(inst_kind, type, side_effects, dex_pc),
2613 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002614
Artem Serovcced8ba2017-07-19 18:18:09 +01002615 DEFAULT_COPY_CONSTRUCTOR(VariableInputSizeInstruction);
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002616
Artem Serovcced8ba2017-07-19 18:18:09 +01002617 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002618};
2619
Vladimir Markof9f64412015-09-02 14:05:49 +01002620template<size_t N>
Vladimir Markobd785672018-05-03 17:09:09 +01002621class HExpression : public HInstruction {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002622 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002623 HExpression<N>(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302624 : HInstruction(kind, side_effects, dex_pc), inputs_() {}
Vladimir Markobd785672018-05-03 17:09:09 +01002625 HExpression<N>(InstructionKind kind,
2626 DataType::Type type,
2627 SideEffects side_effects,
2628 uint32_t dex_pc)
2629 : HInstruction(kind, type, side_effects, dex_pc), inputs_() {}
2630 virtual ~HExpression() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002631
Vladimir Marko372f10e2016-05-17 16:30:10 +01002632 using HInstruction::GetInputRecords; // Keep the const version visible.
2633 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2634 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002635 }
2636
Artem Serovcced8ba2017-07-19 18:18:09 +01002637 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002638 DEFAULT_COPY_CONSTRUCTOR(Expression<N>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002639
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002640 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002641 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002642
2643 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002644};
2645
Vladimir Markobd785672018-05-03 17:09:09 +01002646// HExpression specialization for N=0.
Vladimir Markof9f64412015-09-02 14:05:49 +01002647template<>
Vladimir Markobd785672018-05-03 17:09:09 +01002648class HExpression<0> : public HInstruction {
Vladimir Markof9f64412015-09-02 14:05:49 +01002649 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002650 using HInstruction::HInstruction;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002651
Vladimir Markobd785672018-05-03 17:09:09 +01002652 virtual ~HExpression() {}
Vladimir Markof9f64412015-09-02 14:05:49 +01002653
Vladimir Marko372f10e2016-05-17 16:30:10 +01002654 using HInstruction::GetInputRecords; // Keep the const version visible.
2655 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2656 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002657 }
2658
Artem Serovcced8ba2017-07-19 18:18:09 +01002659 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002660 DEFAULT_COPY_CONSTRUCTOR(Expression<0>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002661
Vladimir Markof9f64412015-09-02 14:05:49 +01002662 private:
2663 friend class SsaBuilder;
2664};
2665
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002666// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2667// instruction that branches to the exit block.
Vladimir Markobd785672018-05-03 17:09:09 +01002668class HReturnVoid FINAL : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002669 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002670 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002671 : HExpression(kReturnVoid, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302672 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002673
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002674 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002675
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002676 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002677
Artem Serovcced8ba2017-07-19 18:18:09 +01002678 protected:
2679 DEFAULT_COPY_CONSTRUCTOR(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002680};
2681
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002682// Represents dex's RETURN opcodes. A HReturn is a control flow
2683// instruction that branches to the exit block.
Vladimir Markobd785672018-05-03 17:09:09 +01002684class HReturn FINAL : public HExpression<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002685 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002686 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002687 : HExpression(kReturn, SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002688 SetRawInputAt(0, value);
2689 }
2690
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002691 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002692
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002693 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002694
Artem Serovcced8ba2017-07-19 18:18:09 +01002695 protected:
2696 DEFAULT_COPY_CONSTRUCTOR(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002697};
2698
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002699class HPhi FINAL : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002700 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002701 HPhi(ArenaAllocator* allocator,
David Brazdildee58d62016-04-07 09:54:26 +00002702 uint32_t reg_number,
2703 size_t number_of_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002704 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +00002705 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002706 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302707 kPhi,
Vladimir Markobd785672018-05-03 17:09:09 +01002708 ToPhiType(type),
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002709 SideEffects::None(),
2710 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002711 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002712 number_of_inputs,
2713 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002714 reg_number_(reg_number) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002715 DCHECK_NE(GetType(), DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +00002716 // Phis are constructed live and marked dead if conflicting or unused.
2717 // Individual steps of SsaBuilder should assume that if a phi has been
2718 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2719 SetPackedFlag<kFlagIsLive>(true);
2720 SetPackedFlag<kFlagCanBeNull>(true);
2721 }
2722
Artem Serovcced8ba2017-07-19 18:18:09 +01002723 bool IsClonable() const OVERRIDE { return true; }
2724
David Brazdildee58d62016-04-07 09:54:26 +00002725 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002726 static DataType::Type ToPhiType(DataType::Type type) {
2727 return DataType::Kind(type);
David Brazdildee58d62016-04-07 09:54:26 +00002728 }
2729
2730 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2731
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002732 void SetType(DataType::Type new_type) {
David Brazdildee58d62016-04-07 09:54:26 +00002733 // Make sure that only valid type changes occur. The following are allowed:
2734 // (1) int -> float/ref (primitive type propagation),
2735 // (2) long -> double (primitive type propagation).
2736 DCHECK(GetType() == new_type ||
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002737 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kFloat32) ||
2738 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kReference) ||
2739 (GetType() == DataType::Type::kInt64 && new_type == DataType::Type::kFloat64));
David Brazdildee58d62016-04-07 09:54:26 +00002740 SetPackedField<TypeField>(new_type);
2741 }
2742
2743 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2744 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2745
2746 uint32_t GetRegNumber() const { return reg_number_; }
2747
2748 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2749 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2750 bool IsDead() const { return !IsLive(); }
2751 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2752
Vladimir Markoe9004912016-06-16 16:50:52 +01002753 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002754 return other != nullptr
2755 && other->IsPhi()
2756 && other->AsPhi()->GetBlock() == GetBlock()
2757 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2758 }
2759
Nicolas Geoffrayf57c1ae2017-06-28 17:40:18 +01002760 bool HasEquivalentPhi() const {
2761 if (GetPrevious() != nullptr && GetPrevious()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2762 return true;
2763 }
2764 if (GetNext() != nullptr && GetNext()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2765 return true;
2766 }
2767 return false;
2768 }
2769
David Brazdildee58d62016-04-07 09:54:26 +00002770 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2771 // An equivalent phi is a phi having the same dex register and type.
2772 // It assumes that phis with the same dex register are adjacent.
2773 HPhi* GetNextEquivalentPhiWithSameType() {
2774 HInstruction* next = GetNext();
2775 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2776 if (next->GetType() == GetType()) {
2777 return next->AsPhi();
2778 }
2779 next = next->GetNext();
2780 }
2781 return nullptr;
2782 }
2783
2784 DECLARE_INSTRUCTION(Phi);
2785
Artem Serovcced8ba2017-07-19 18:18:09 +01002786 protected:
2787 DEFAULT_COPY_CONSTRUCTOR(Phi);
2788
David Brazdildee58d62016-04-07 09:54:26 +00002789 private:
Vladimir Markobd785672018-05-03 17:09:09 +01002790 static constexpr size_t kFlagIsLive = HInstruction::kNumberOfGenericPackedBits;
David Brazdildee58d62016-04-07 09:54:26 +00002791 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2792 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2793 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
David Brazdildee58d62016-04-07 09:54:26 +00002794
David Brazdildee58d62016-04-07 09:54:26 +00002795 const uint32_t reg_number_;
David Brazdildee58d62016-04-07 09:54:26 +00002796};
2797
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002798// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002799// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002800// exit block.
Vladimir Markobd785672018-05-03 17:09:09 +01002801class HExit FINAL : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002802 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302803 explicit HExit(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002804 : HExpression(kExit, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302805 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002806
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002807 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002808
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002809 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002810
Artem Serovcced8ba2017-07-19 18:18:09 +01002811 protected:
2812 DEFAULT_COPY_CONSTRUCTOR(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002813};
2814
2815// Jumps from one block to another.
Vladimir Markobd785672018-05-03 17:09:09 +01002816class HGoto FINAL : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002817 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302818 explicit HGoto(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002819 : HExpression(kGoto, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302820 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002821
Artem Serovcced8ba2017-07-19 18:18:09 +01002822 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002823 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002824
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002825 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002826 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002827 }
2828
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002829 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002830
Artem Serovcced8ba2017-07-19 18:18:09 +01002831 protected:
2832 DEFAULT_COPY_CONSTRUCTOR(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002833};
2834
Roland Levillain9867bc72015-08-05 10:21:34 +01002835class HConstant : public HExpression<0> {
2836 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302837 explicit HConstant(InstructionKind kind, DataType::Type type, uint32_t dex_pc = kNoDexPc)
2838 : HExpression(kind, type, SideEffects::None(), dex_pc) {
2839 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002840
2841 bool CanBeMoved() const OVERRIDE { return true; }
2842
Roland Levillain1a653882016-03-18 18:05:57 +00002843 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002844 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002845 // Is this constant 0 in the arithmetic sense?
2846 virtual bool IsArithmeticZero() const { return false; }
2847 // Is this constant a 0-bit pattern?
2848 virtual bool IsZeroBitPattern() const { return false; }
2849 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002850 virtual bool IsOne() const { return false; }
2851
David Brazdil77a48ae2015-09-15 12:34:04 +00002852 virtual uint64_t GetValueAsUint64() const = 0;
2853
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002854 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002855
Artem Serovcced8ba2017-07-19 18:18:09 +01002856 protected:
2857 DEFAULT_COPY_CONSTRUCTOR(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002858};
2859
Vladimir Markofcb503c2016-05-18 12:48:17 +01002860class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002861 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002862 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002863 return true;
2864 }
2865
David Brazdil77a48ae2015-09-15 12:34:04 +00002866 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2867
Roland Levillain9867bc72015-08-05 10:21:34 +01002868 size_t ComputeHashCode() const OVERRIDE { return 0; }
2869
Roland Levillain1a653882016-03-18 18:05:57 +00002870 // The null constant representation is a 0-bit pattern.
2871 virtual bool IsZeroBitPattern() const { return true; }
2872
Roland Levillain9867bc72015-08-05 10:21:34 +01002873 DECLARE_INSTRUCTION(NullConstant);
2874
Artem Serovcced8ba2017-07-19 18:18:09 +01002875 protected:
2876 DEFAULT_COPY_CONSTRUCTOR(NullConstant);
2877
Roland Levillain9867bc72015-08-05 10:21:34 +01002878 private:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002879 explicit HNullConstant(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302880 : HConstant(kNullConstant, DataType::Type::kReference, dex_pc) {
2881 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002882
2883 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01002884};
2885
2886// Constants of the type int. Those can be from Dex instructions, or
2887// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002888class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002889 public:
2890 int32_t GetValue() const { return value_; }
2891
David Brazdil9f389d42015-10-01 14:32:56 +01002892 uint64_t GetValueAsUint64() const OVERRIDE {
2893 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2894 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002895
Vladimir Marko372f10e2016-05-17 16:30:10 +01002896 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002897 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002898 return other->AsIntConstant()->value_ == value_;
2899 }
2900
2901 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2902
2903 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002904 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2905 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002906 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2907
Roland Levillain1a653882016-03-18 18:05:57 +00002908 // Integer constants are used to encode Boolean values as well,
2909 // where 1 means true and 0 means false.
2910 bool IsTrue() const { return GetValue() == 1; }
2911 bool IsFalse() const { return GetValue() == 0; }
2912
Roland Levillain9867bc72015-08-05 10:21:34 +01002913 DECLARE_INSTRUCTION(IntConstant);
2914
Artem Serovcced8ba2017-07-19 18:18:09 +01002915 protected:
2916 DEFAULT_COPY_CONSTRUCTOR(IntConstant);
2917
Roland Levillain9867bc72015-08-05 10:21:34 +01002918 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002919 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302920 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc), value_(value) {
2921 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002922 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302923 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc),
2924 value_(value ? 1 : 0) {
2925 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002926
2927 const int32_t value_;
2928
2929 friend class HGraph;
2930 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2931 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Roland Levillain9867bc72015-08-05 10:21:34 +01002932};
2933
Vladimir Markofcb503c2016-05-18 12:48:17 +01002934class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002935 public:
2936 int64_t GetValue() const { return value_; }
2937
David Brazdil77a48ae2015-09-15 12:34:04 +00002938 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2939
Vladimir Marko372f10e2016-05-17 16:30:10 +01002940 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002941 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002942 return other->AsLongConstant()->value_ == value_;
2943 }
2944
2945 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2946
2947 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002948 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2949 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002950 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2951
2952 DECLARE_INSTRUCTION(LongConstant);
2953
Artem Serovcced8ba2017-07-19 18:18:09 +01002954 protected:
2955 DEFAULT_COPY_CONSTRUCTOR(LongConstant);
2956
Roland Levillain9867bc72015-08-05 10:21:34 +01002957 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002958 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302959 : HConstant(kLongConstant, DataType::Type::kInt64, dex_pc),
2960 value_(value) {
2961 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002962
2963 const int64_t value_;
2964
2965 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01002966};
Dave Allison20dfc792014-06-16 20:44:29 -07002967
Vladimir Markofcb503c2016-05-18 12:48:17 +01002968class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002969 public:
2970 float GetValue() const { return value_; }
2971
2972 uint64_t GetValueAsUint64() const OVERRIDE {
2973 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2974 }
2975
Vladimir Marko372f10e2016-05-17 16:30:10 +01002976 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002977 DCHECK(other->IsFloatConstant()) << other->DebugName();
2978 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2979 }
2980
2981 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2982
2983 bool IsMinusOne() const OVERRIDE {
2984 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2985 }
Roland Levillain1a653882016-03-18 18:05:57 +00002986 bool IsArithmeticZero() const OVERRIDE {
2987 return std::fpclassify(value_) == FP_ZERO;
2988 }
2989 bool IsArithmeticPositiveZero() const {
2990 return IsArithmeticZero() && !std::signbit(value_);
2991 }
2992 bool IsArithmeticNegativeZero() const {
2993 return IsArithmeticZero() && std::signbit(value_);
2994 }
2995 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002996 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002997 }
2998 bool IsOne() const OVERRIDE {
2999 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3000 }
3001 bool IsNaN() const {
3002 return std::isnan(value_);
3003 }
3004
3005 DECLARE_INSTRUCTION(FloatConstant);
3006
Artem Serovcced8ba2017-07-19 18:18:09 +01003007 protected:
3008 DEFAULT_COPY_CONSTRUCTOR(FloatConstant);
3009
Roland Levillain31dd3d62016-02-16 12:21:02 +00003010 private:
3011 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303012 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3013 value_(value) {
3014 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003015 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303016 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3017 value_(bit_cast<float, int32_t>(value)) {
3018 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003019
3020 const float value_;
3021
3022 // Only the SsaBuilder and HGraph can create floating-point constants.
3023 friend class SsaBuilder;
3024 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003025};
3026
Vladimir Markofcb503c2016-05-18 12:48:17 +01003027class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003028 public:
3029 double GetValue() const { return value_; }
3030
3031 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
3032
Vladimir Marko372f10e2016-05-17 16:30:10 +01003033 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003034 DCHECK(other->IsDoubleConstant()) << other->DebugName();
3035 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
3036 }
3037
3038 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
3039
3040 bool IsMinusOne() const OVERRIDE {
3041 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
3042 }
Roland Levillain1a653882016-03-18 18:05:57 +00003043 bool IsArithmeticZero() const OVERRIDE {
3044 return std::fpclassify(value_) == FP_ZERO;
3045 }
3046 bool IsArithmeticPositiveZero() const {
3047 return IsArithmeticZero() && !std::signbit(value_);
3048 }
3049 bool IsArithmeticNegativeZero() const {
3050 return IsArithmeticZero() && std::signbit(value_);
3051 }
3052 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003053 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00003054 }
3055 bool IsOne() const OVERRIDE {
3056 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3057 }
3058 bool IsNaN() const {
3059 return std::isnan(value_);
3060 }
3061
3062 DECLARE_INSTRUCTION(DoubleConstant);
3063
Artem Serovcced8ba2017-07-19 18:18:09 +01003064 protected:
3065 DEFAULT_COPY_CONSTRUCTOR(DoubleConstant);
3066
Roland Levillain31dd3d62016-02-16 12:21:02 +00003067 private:
3068 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303069 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3070 value_(value) {
3071 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003072 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303073 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3074 value_(bit_cast<double, int64_t>(value)) {
3075 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003076
3077 const double value_;
3078
3079 // Only the SsaBuilder and HGraph can create floating-point constants.
3080 friend class SsaBuilder;
3081 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003082};
3083
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003084// Conditional branch. A block ending with an HIf instruction must have
3085// two successors.
Vladimir Markobd785672018-05-03 17:09:09 +01003086class HIf FINAL : public HExpression<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003087 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003088 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003089 : HExpression(kIf, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003090 SetRawInputAt(0, input);
3091 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003092
Artem Serovcced8ba2017-07-19 18:18:09 +01003093 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003094 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003095
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003096 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003097 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003098 }
3099
3100 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003101 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003102 }
3103
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003104 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003105
Artem Serovcced8ba2017-07-19 18:18:09 +01003106 protected:
3107 DEFAULT_COPY_CONSTRUCTOR(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003108};
3109
David Brazdilfc6a86a2015-06-26 10:33:45 +00003110
3111// Abstract instruction which marks the beginning and/or end of a try block and
3112// links it to the respective exception handlers. Behaves the same as a Goto in
3113// non-exceptional control flow.
3114// Normal-flow successor is stored at index zero, exception handlers under
3115// higher indices in no particular order.
Vladimir Markobd785672018-05-03 17:09:09 +01003116class HTryBoundary FINAL : public HExpression<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003117 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003118 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01003119 kEntry,
3120 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003121 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01003122 };
3123
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003124 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003125 : HExpression(kTryBoundary, SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003126 SetPackedField<BoundaryKindField>(kind);
3127 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003128
3129 bool IsControlFlow() const OVERRIDE { return true; }
3130
3131 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01003132 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003133
David Brazdild26a4112015-11-10 11:07:31 +00003134 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
3135 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
3136 }
3137
David Brazdilfc6a86a2015-06-26 10:33:45 +00003138 // Returns whether `handler` is among its exception handlers (non-zero index
3139 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01003140 bool HasExceptionHandler(const HBasicBlock& handler) const {
3141 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00003142 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00003143 }
3144
3145 // If not present already, adds `handler` to its block's list of exception
3146 // handlers.
3147 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01003148 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003149 GetBlock()->AddSuccessor(handler);
3150 }
3151 }
3152
Vladimir Markoa1de9182016-02-25 11:37:38 +00003153 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
3154 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003155
David Brazdilffee3d32015-07-06 11:48:53 +01003156 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
3157
David Brazdilfc6a86a2015-06-26 10:33:45 +00003158 DECLARE_INSTRUCTION(TryBoundary);
3159
Artem Serovcced8ba2017-07-19 18:18:09 +01003160 protected:
3161 DEFAULT_COPY_CONSTRUCTOR(TryBoundary);
3162
David Brazdilfc6a86a2015-06-26 10:33:45 +00003163 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003164 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
3165 static constexpr size_t kFieldBoundaryKindSize =
3166 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
3167 static constexpr size_t kNumberOfTryBoundaryPackedBits =
3168 kFieldBoundaryKind + kFieldBoundaryKindSize;
3169 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
3170 "Too many packed fields.");
3171 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00003172};
3173
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003174// Deoptimize to interpreter, upon checking a condition.
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003175class HDeoptimize FINAL : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003176 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003177 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
3178 // across.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003179 HDeoptimize(ArenaAllocator* allocator,
3180 HInstruction* cond,
3181 DeoptimizationKind kind,
3182 uint32_t dex_pc)
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003183 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303184 kDeoptimize,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003185 SideEffects::All(),
3186 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003187 allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003188 /* number_of_inputs */ 1,
3189 kArenaAllocMisc) {
3190 SetPackedFlag<kFieldCanBeMoved>(false);
3191 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003192 SetRawInputAt(0, cond);
3193 }
3194
Artem Serovcced8ba2017-07-19 18:18:09 +01003195 bool IsClonable() const OVERRIDE { return true; }
3196
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003197 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
3198 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
3199 // instead of `guard`.
3200 // We set CanTriggerGC to prevent any intermediate address to be live
3201 // at the point of the `HDeoptimize`.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003202 HDeoptimize(ArenaAllocator* allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003203 HInstruction* cond,
3204 HInstruction* guard,
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003205 DeoptimizationKind kind,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003206 uint32_t dex_pc)
3207 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303208 kDeoptimize,
Vladimir Markobd785672018-05-03 17:09:09 +01003209 guard->GetType(),
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003210 SideEffects::CanTriggerGC(),
3211 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003212 allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003213 /* number_of_inputs */ 2,
3214 kArenaAllocMisc) {
3215 SetPackedFlag<kFieldCanBeMoved>(true);
3216 SetPackedField<DeoptimizeKindField>(kind);
3217 SetRawInputAt(0, cond);
3218 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003219 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003220
3221 bool CanBeMoved() const OVERRIDE { return GetPackedFlag<kFieldCanBeMoved>(); }
3222
3223 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
3224 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
3225 }
3226
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003227 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003228
Aart Bik75ff2c92018-04-21 01:28:11 +00003229 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003230
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003231 DeoptimizationKind GetDeoptimizationKind() const { return GetPackedField<DeoptimizeKindField>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003232
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003233 bool GuardsAnInput() const {
3234 return InputCount() == 2;
3235 }
3236
3237 HInstruction* GuardedInput() const {
3238 DCHECK(GuardsAnInput());
3239 return InputAt(1);
3240 }
3241
3242 void RemoveGuard() {
3243 RemoveInputAt(1);
3244 }
3245
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003246 DECLARE_INSTRUCTION(Deoptimize);
3247
Artem Serovcced8ba2017-07-19 18:18:09 +01003248 protected:
3249 DEFAULT_COPY_CONSTRUCTOR(Deoptimize);
3250
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003251 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003252 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3253 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3254 static constexpr size_t kFieldDeoptimizeKindSize =
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003255 MinimumBitsToStore(static_cast<size_t>(DeoptimizationKind::kLast));
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003256 static constexpr size_t kNumberOfDeoptimizePackedBits =
3257 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3258 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3259 "Too many packed fields.");
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003260 using DeoptimizeKindField =
3261 BitField<DeoptimizationKind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003262};
3263
Mingyao Yang063fc772016-08-02 11:02:54 -07003264// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3265// The compiled code checks this flag value in a guard before devirtualized call and
3266// if it's true, starts to do deoptimization.
3267// It has a 4-byte slot on stack.
3268// TODO: allocate a register for this flag.
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003269class HShouldDeoptimizeFlag FINAL : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003270 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003271 // CHA guards are only optimized in a separate pass and it has no side effects
3272 // with regard to other passes.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003273 HShouldDeoptimizeFlag(ArenaAllocator* allocator, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303274 : HVariableInputSizeInstruction(kShouldDeoptimizeFlag,
Vladimir Markobd785672018-05-03 17:09:09 +01003275 DataType::Type::kInt32,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303276 SideEffects::None(),
3277 dex_pc,
3278 allocator,
3279 0,
3280 kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003281 }
3282
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003283 // We do all CHA guard elimination/motion in a single pass, after which there is no
3284 // further guard elimination/motion since a guard might have been used for justification
3285 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3286 // to avoid other optimizations trying to move it.
Mingyao Yang063fc772016-08-02 11:02:54 -07003287 bool CanBeMoved() const OVERRIDE { return false; }
3288
3289 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3290
Artem Serovcced8ba2017-07-19 18:18:09 +01003291 protected:
3292 DEFAULT_COPY_CONSTRUCTOR(ShouldDeoptimizeFlag);
Mingyao Yang063fc772016-08-02 11:02:54 -07003293};
3294
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003295// Represents the ArtMethod that was passed as a first argument to
3296// the method. It is used by instructions that depend on it, like
3297// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003298class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003299 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003300 explicit HCurrentMethod(DataType::Type type, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303301 : HExpression(kCurrentMethod, type, SideEffects::None(), dex_pc) {
3302 }
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003303
3304 DECLARE_INSTRUCTION(CurrentMethod);
3305
Artem Serovcced8ba2017-07-19 18:18:09 +01003306 protected:
3307 DEFAULT_COPY_CONSTRUCTOR(CurrentMethod);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003308};
3309
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003310// Fetches an ArtMethod from the virtual table or the interface method table
3311// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003312class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003313 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003314 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003315 kVTable,
3316 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003317 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003318 };
3319 HClassTableGet(HInstruction* cls,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003320 DataType::Type type,
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003321 TableKind kind,
3322 size_t index,
3323 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303324 : HExpression(kClassTableGet, type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003325 index_(index) {
3326 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003327 SetRawInputAt(0, cls);
3328 }
3329
Artem Serovcced8ba2017-07-19 18:18:09 +01003330 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003331 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003332 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003333 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003334 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003335 }
3336
Vladimir Markoa1de9182016-02-25 11:37:38 +00003337 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003338 size_t GetIndex() const { return index_; }
3339
3340 DECLARE_INSTRUCTION(ClassTableGet);
3341
Artem Serovcced8ba2017-07-19 18:18:09 +01003342 protected:
3343 DEFAULT_COPY_CONSTRUCTOR(ClassTableGet);
3344
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003345 private:
Vladimir Markobd785672018-05-03 17:09:09 +01003346 static constexpr size_t kFieldTableKind = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003347 static constexpr size_t kFieldTableKindSize =
3348 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3349 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3350 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3351 "Too many packed fields.");
3352 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3353
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003354 // The index of the ArtMethod in the table.
3355 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003356};
3357
Mark Mendellfe57faa2015-09-18 09:26:15 -04003358// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3359// have one successor for each entry in the switch table, and the final successor
3360// will be the block containing the next Dex opcode.
Vladimir Markobd785672018-05-03 17:09:09 +01003361class HPackedSwitch FINAL : public HExpression<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003362 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003363 HPackedSwitch(int32_t start_value,
3364 uint32_t num_entries,
3365 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003366 uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003367 : HExpression(kPackedSwitch, SideEffects::None(), dex_pc),
Mark Mendellfe57faa2015-09-18 09:26:15 -04003368 start_value_(start_value),
3369 num_entries_(num_entries) {
3370 SetRawInputAt(0, input);
3371 }
3372
Artem Serovcced8ba2017-07-19 18:18:09 +01003373 bool IsClonable() const OVERRIDE { return true; }
3374
Mark Mendellfe57faa2015-09-18 09:26:15 -04003375 bool IsControlFlow() const OVERRIDE { return true; }
3376
3377 int32_t GetStartValue() const { return start_value_; }
3378
Vladimir Marko211c2112015-09-24 16:52:33 +01003379 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003380
3381 HBasicBlock* GetDefaultBlock() const {
3382 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003383 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003384 }
3385 DECLARE_INSTRUCTION(PackedSwitch);
3386
Artem Serovcced8ba2017-07-19 18:18:09 +01003387 protected:
3388 DEFAULT_COPY_CONSTRUCTOR(PackedSwitch);
3389
Mark Mendellfe57faa2015-09-18 09:26:15 -04003390 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003391 const int32_t start_value_;
3392 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003393};
3394
Roland Levillain88cb1752014-10-20 16:36:47 +01003395class HUnaryOperation : public HExpression<1> {
3396 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303397 HUnaryOperation(InstructionKind kind,
3398 DataType::Type result_type,
3399 HInstruction* input,
3400 uint32_t dex_pc = kNoDexPc)
3401 : HExpression(kind, result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003402 SetRawInputAt(0, input);
3403 }
3404
Artem Serovcced8ba2017-07-19 18:18:09 +01003405 // All of the UnaryOperation instructions are clonable.
3406 bool IsClonable() const OVERRIDE { return true; }
3407
Roland Levillain88cb1752014-10-20 16:36:47 +01003408 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003409 DataType::Type GetResultType() const { return GetType(); }
Roland Levillain88cb1752014-10-20 16:36:47 +01003410
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003411 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003412 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003413 return true;
3414 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003415
Roland Levillain31dd3d62016-02-16 12:21:02 +00003416 // Try to statically evaluate `this` and return a HConstant
3417 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003418 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003419 HConstant* TryStaticEvaluation() const;
3420
3421 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003422 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3423 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003424 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3425 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003426
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003427 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003428
Artem Serovcced8ba2017-07-19 18:18:09 +01003429 protected:
3430 DEFAULT_COPY_CONSTRUCTOR(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003431};
3432
Dave Allison20dfc792014-06-16 20:44:29 -07003433class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003434 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303435 HBinaryOperation(InstructionKind kind,
3436 DataType::Type result_type,
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003437 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003438 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003439 SideEffects side_effects = SideEffects::None(),
3440 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303441 : HExpression(kind, result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003442 SetRawInputAt(0, left);
3443 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003444 }
3445
Artem Serovcced8ba2017-07-19 18:18:09 +01003446 // All of the BinaryOperation instructions are clonable.
3447 bool IsClonable() const OVERRIDE { return true; }
3448
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003449 HInstruction* GetLeft() const { return InputAt(0); }
3450 HInstruction* GetRight() const { return InputAt(1); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003451 DataType::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003452
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003453 virtual bool IsCommutative() const { return false; }
3454
3455 // Put constant on the right.
3456 // Returns whether order is changed.
3457 bool OrderInputsWithConstantOnTheRight() {
3458 HInstruction* left = InputAt(0);
3459 HInstruction* right = InputAt(1);
3460 if (left->IsConstant() && !right->IsConstant()) {
3461 ReplaceInput(right, 0);
3462 ReplaceInput(left, 1);
3463 return true;
3464 }
3465 return false;
3466 }
3467
3468 // Order inputs by instruction id, but favor constant on the right side.
3469 // This helps GVN for commutative ops.
3470 void OrderInputs() {
3471 DCHECK(IsCommutative());
3472 HInstruction* left = InputAt(0);
3473 HInstruction* right = InputAt(1);
3474 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3475 return;
3476 }
3477 if (OrderInputsWithConstantOnTheRight()) {
3478 return;
3479 }
3480 // Order according to instruction id.
3481 if (left->GetId() > right->GetId()) {
3482 ReplaceInput(right, 0);
3483 ReplaceInput(left, 1);
3484 }
3485 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003486
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003487 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003488 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003489 return true;
3490 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003491
Roland Levillain31dd3d62016-02-16 12:21:02 +00003492 // Try to statically evaluate `this` and return a HConstant
3493 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003494 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003495 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003496
3497 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003498 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3499 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003500 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3501 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003502 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003503 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3504 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003505 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3506 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003507 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3508 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003509 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003510 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3511 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003512
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003513 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003514 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003515 HConstant* GetConstantRight() const;
3516
3517 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003518 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003519 HInstruction* GetLeastConstantLeft() const;
3520
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003521 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003522
Artem Serovcced8ba2017-07-19 18:18:09 +01003523 protected:
3524 DEFAULT_COPY_CONSTRUCTOR(BinaryOperation);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003525};
3526
Mark Mendellc4701932015-04-10 13:18:51 -04003527// The comparison bias applies for floating point operations and indicates how NaN
3528// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003529enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003530 kNoBias, // bias is not applicable (i.e. for long operation)
3531 kGtBias, // return 1 for NaN comparisons
3532 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003533 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003534};
3535
Roland Levillain31dd3d62016-02-16 12:21:02 +00003536std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3537
Dave Allison20dfc792014-06-16 20:44:29 -07003538class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003539 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303540 HCondition(InstructionKind kind,
3541 HInstruction* first,
3542 HInstruction* second,
3543 uint32_t dex_pc = kNoDexPc)
3544 : HBinaryOperation(kind,
3545 DataType::Type::kBool,
3546 first,
3547 second,
3548 SideEffects::None(),
3549 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003550 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3551 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003552
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003553 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003554 // `instruction`, and disregard moves in between.
3555 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003556
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003557 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003558
3559 virtual IfCondition GetCondition() const = 0;
3560
Mark Mendellc4701932015-04-10 13:18:51 -04003561 virtual IfCondition GetOppositeCondition() const = 0;
3562
Vladimir Markoa1de9182016-02-25 11:37:38 +00003563 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003564 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3565
Vladimir Markoa1de9182016-02-25 11:37:38 +00003566 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3567 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003568
Vladimir Marko372f10e2016-05-17 16:30:10 +01003569 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003570 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003571 }
3572
Roland Levillain4fa13f62015-07-06 18:11:54 +01003573 bool IsFPConditionTrueIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003574 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003575 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003576 if (if_cond == kCondNE) {
3577 return true;
3578 } else if (if_cond == kCondEQ) {
3579 return false;
3580 }
3581 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003582 }
3583
3584 bool IsFPConditionFalseIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003585 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003586 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003587 if (if_cond == kCondEQ) {
3588 return true;
3589 } else if (if_cond == kCondNE) {
3590 return false;
3591 }
3592 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003593 }
3594
Roland Levillain31dd3d62016-02-16 12:21:02 +00003595 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003596 // Needed if we merge a HCompare into a HCondition.
Vladimir Markobd785672018-05-03 17:09:09 +01003597 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003598 static constexpr size_t kFieldComparisonBiasSize =
3599 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3600 static constexpr size_t kNumberOfConditionPackedBits =
3601 kFieldComparisonBias + kFieldComparisonBiasSize;
3602 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3603 using ComparisonBiasField =
3604 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3605
Roland Levillain31dd3d62016-02-16 12:21:02 +00003606 template <typename T>
3607 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3608
3609 template <typename T>
3610 int32_t CompareFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003611 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003612 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3613 // Handle the bias.
3614 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3615 }
3616
3617 // Return an integer constant containing the result of a condition evaluated at compile time.
3618 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3619 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3620 }
3621
Artem Serovcced8ba2017-07-19 18:18:09 +01003622 DEFAULT_COPY_CONSTRUCTOR(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003623};
3624
3625// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003626class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003627 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003628 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303629 : HCondition(kEqual, first, second, dex_pc) {
3630 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003631
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003632 bool IsCommutative() const OVERRIDE { return true; }
3633
Vladimir Marko9e23df52015-11-10 17:14:35 +00003634 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3635 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003636 return MakeConstantCondition(true, GetDexPc());
3637 }
3638 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3639 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3640 }
3641 // In the following Evaluate methods, a HCompare instruction has
3642 // been merged into this HEqual instruction; evaluate it as
3643 // `Compare(x, y) == 0`.
3644 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3645 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3646 GetDexPc());
3647 }
3648 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3649 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3650 }
3651 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3652 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003653 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003654
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003655 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003656
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003657 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003658 return kCondEQ;
3659 }
3660
Mark Mendellc4701932015-04-10 13:18:51 -04003661 IfCondition GetOppositeCondition() const OVERRIDE {
3662 return kCondNE;
3663 }
3664
Artem Serovcced8ba2017-07-19 18:18:09 +01003665 protected:
3666 DEFAULT_COPY_CONSTRUCTOR(Equal);
3667
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003668 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003669 template <typename T> static bool Compute(T x, T y) { return x == y; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003670};
3671
Vladimir Markofcb503c2016-05-18 12:48:17 +01003672class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003673 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303674 HNotEqual(HInstruction* first, HInstruction* second,
3675 uint32_t dex_pc = kNoDexPc)
3676 : HCondition(kNotEqual, first, second, dex_pc) {
3677 }
Dave Allison20dfc792014-06-16 20:44:29 -07003678
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003679 bool IsCommutative() const OVERRIDE { return true; }
3680
Vladimir Marko9e23df52015-11-10 17:14:35 +00003681 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3682 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003683 return MakeConstantCondition(false, GetDexPc());
3684 }
3685 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3686 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3687 }
3688 // In the following Evaluate methods, a HCompare instruction has
3689 // been merged into this HNotEqual instruction; evaluate it as
3690 // `Compare(x, y) != 0`.
3691 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3692 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3693 }
3694 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3695 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3696 }
3697 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3698 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003699 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003700
Dave Allison20dfc792014-06-16 20:44:29 -07003701 DECLARE_INSTRUCTION(NotEqual);
3702
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003703 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003704 return kCondNE;
3705 }
3706
Mark Mendellc4701932015-04-10 13:18:51 -04003707 IfCondition GetOppositeCondition() const OVERRIDE {
3708 return kCondEQ;
3709 }
3710
Artem Serovcced8ba2017-07-19 18:18:09 +01003711 protected:
3712 DEFAULT_COPY_CONSTRUCTOR(NotEqual);
3713
Dave Allison20dfc792014-06-16 20:44:29 -07003714 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003715 template <typename T> static bool Compute(T x, T y) { return x != y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003716};
3717
Vladimir Markofcb503c2016-05-18 12:48:17 +01003718class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003719 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303720 HLessThan(HInstruction* first, HInstruction* second,
3721 uint32_t dex_pc = kNoDexPc)
3722 : HCondition(kLessThan, first, second, dex_pc) {
3723 }
Dave Allison20dfc792014-06-16 20:44:29 -07003724
Roland Levillain9867bc72015-08-05 10:21:34 +01003725 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003726 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003727 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003728 // In the following Evaluate methods, a HCompare instruction has
3729 // been merged into this HLessThan instruction; evaluate it as
3730 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003731 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003732 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3733 }
3734 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3735 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3736 }
3737 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3738 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003739 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003740
Dave Allison20dfc792014-06-16 20:44:29 -07003741 DECLARE_INSTRUCTION(LessThan);
3742
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003743 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003744 return kCondLT;
3745 }
3746
Mark Mendellc4701932015-04-10 13:18:51 -04003747 IfCondition GetOppositeCondition() const OVERRIDE {
3748 return kCondGE;
3749 }
3750
Artem Serovcced8ba2017-07-19 18:18:09 +01003751 protected:
3752 DEFAULT_COPY_CONSTRUCTOR(LessThan);
3753
Dave Allison20dfc792014-06-16 20:44:29 -07003754 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003755 template <typename T> static bool Compute(T x, T y) { return x < y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003756};
3757
Vladimir Markofcb503c2016-05-18 12:48:17 +01003758class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003759 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303760 HLessThanOrEqual(HInstruction* first, HInstruction* second,
3761 uint32_t dex_pc = kNoDexPc)
3762 : HCondition(kLessThanOrEqual, first, second, dex_pc) {
3763 }
Dave Allison20dfc792014-06-16 20:44:29 -07003764
Roland Levillain9867bc72015-08-05 10:21:34 +01003765 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003766 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003767 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003768 // In the following Evaluate methods, a HCompare instruction has
3769 // been merged into this HLessThanOrEqual instruction; evaluate it as
3770 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003771 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003772 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3773 }
3774 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3775 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3776 }
3777 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3778 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003779 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003780
Dave Allison20dfc792014-06-16 20:44:29 -07003781 DECLARE_INSTRUCTION(LessThanOrEqual);
3782
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003783 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003784 return kCondLE;
3785 }
3786
Mark Mendellc4701932015-04-10 13:18:51 -04003787 IfCondition GetOppositeCondition() const OVERRIDE {
3788 return kCondGT;
3789 }
3790
Artem Serovcced8ba2017-07-19 18:18:09 +01003791 protected:
3792 DEFAULT_COPY_CONSTRUCTOR(LessThanOrEqual);
3793
Dave Allison20dfc792014-06-16 20:44:29 -07003794 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003795 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003796};
3797
Vladimir Markofcb503c2016-05-18 12:48:17 +01003798class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003799 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003800 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303801 : HCondition(kGreaterThan, first, second, dex_pc) {
3802 }
Dave Allison20dfc792014-06-16 20:44:29 -07003803
Roland Levillain9867bc72015-08-05 10:21:34 +01003804 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003805 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003806 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003807 // In the following Evaluate methods, a HCompare instruction has
3808 // been merged into this HGreaterThan instruction; evaluate it as
3809 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003810 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003811 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3812 }
3813 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3814 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3815 }
3816 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3817 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003818 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003819
Dave Allison20dfc792014-06-16 20:44:29 -07003820 DECLARE_INSTRUCTION(GreaterThan);
3821
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003822 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003823 return kCondGT;
3824 }
3825
Mark Mendellc4701932015-04-10 13:18:51 -04003826 IfCondition GetOppositeCondition() const OVERRIDE {
3827 return kCondLE;
3828 }
3829
Artem Serovcced8ba2017-07-19 18:18:09 +01003830 protected:
3831 DEFAULT_COPY_CONSTRUCTOR(GreaterThan);
3832
Dave Allison20dfc792014-06-16 20:44:29 -07003833 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003834 template <typename T> static bool Compute(T x, T y) { return x > y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003835};
3836
Vladimir Markofcb503c2016-05-18 12:48:17 +01003837class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003838 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003839 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303840 : HCondition(kGreaterThanOrEqual, first, second, dex_pc) {
3841 }
Dave Allison20dfc792014-06-16 20:44:29 -07003842
Roland Levillain9867bc72015-08-05 10:21:34 +01003843 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003844 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003845 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003846 // In the following Evaluate methods, a HCompare instruction has
3847 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3848 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003849 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003850 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3851 }
3852 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3853 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3854 }
3855 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3856 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003857 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003858
Dave Allison20dfc792014-06-16 20:44:29 -07003859 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3860
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003861 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003862 return kCondGE;
3863 }
3864
Mark Mendellc4701932015-04-10 13:18:51 -04003865 IfCondition GetOppositeCondition() const OVERRIDE {
3866 return kCondLT;
3867 }
3868
Artem Serovcced8ba2017-07-19 18:18:09 +01003869 protected:
3870 DEFAULT_COPY_CONSTRUCTOR(GreaterThanOrEqual);
3871
Dave Allison20dfc792014-06-16 20:44:29 -07003872 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003873 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003874};
3875
Vladimir Markofcb503c2016-05-18 12:48:17 +01003876class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003877 public:
3878 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303879 : HCondition(kBelow, first, second, dex_pc) {
3880 }
Aart Bike9f37602015-10-09 11:15:55 -07003881
3882 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003883 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003884 }
3885 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003886 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3887 }
3888 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3889 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3890 LOG(FATAL) << DebugName() << " is not defined for float values";
3891 UNREACHABLE();
3892 }
3893 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3894 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3895 LOG(FATAL) << DebugName() << " is not defined for double values";
3896 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003897 }
3898
3899 DECLARE_INSTRUCTION(Below);
3900
3901 IfCondition GetCondition() const OVERRIDE {
3902 return kCondB;
3903 }
3904
3905 IfCondition GetOppositeCondition() const OVERRIDE {
3906 return kCondAE;
3907 }
3908
Artem Serovcced8ba2017-07-19 18:18:09 +01003909 protected:
3910 DEFAULT_COPY_CONSTRUCTOR(Below);
3911
Aart Bike9f37602015-10-09 11:15:55 -07003912 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003913 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003914 return MakeUnsigned(x) < MakeUnsigned(y);
3915 }
Aart Bike9f37602015-10-09 11:15:55 -07003916};
3917
Vladimir Markofcb503c2016-05-18 12:48:17 +01003918class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003919 public:
3920 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303921 : HCondition(kBelowOrEqual, first, second, dex_pc) {
3922 }
Aart Bike9f37602015-10-09 11:15:55 -07003923
3924 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003925 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003926 }
3927 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003928 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3929 }
3930 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3931 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3932 LOG(FATAL) << DebugName() << " is not defined for float values";
3933 UNREACHABLE();
3934 }
3935 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3936 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3937 LOG(FATAL) << DebugName() << " is not defined for double values";
3938 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003939 }
3940
3941 DECLARE_INSTRUCTION(BelowOrEqual);
3942
3943 IfCondition GetCondition() const OVERRIDE {
3944 return kCondBE;
3945 }
3946
3947 IfCondition GetOppositeCondition() const OVERRIDE {
3948 return kCondA;
3949 }
3950
Artem Serovcced8ba2017-07-19 18:18:09 +01003951 protected:
3952 DEFAULT_COPY_CONSTRUCTOR(BelowOrEqual);
3953
Aart Bike9f37602015-10-09 11:15:55 -07003954 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003955 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003956 return MakeUnsigned(x) <= MakeUnsigned(y);
3957 }
Aart Bike9f37602015-10-09 11:15:55 -07003958};
3959
Vladimir Markofcb503c2016-05-18 12:48:17 +01003960class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003961 public:
3962 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303963 : HCondition(kAbove, first, second, dex_pc) {
3964 }
Aart Bike9f37602015-10-09 11:15:55 -07003965
3966 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003967 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003968 }
3969 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003970 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3971 }
3972 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3973 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3974 LOG(FATAL) << DebugName() << " is not defined for float values";
3975 UNREACHABLE();
3976 }
3977 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3978 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3979 LOG(FATAL) << DebugName() << " is not defined for double values";
3980 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003981 }
3982
3983 DECLARE_INSTRUCTION(Above);
3984
3985 IfCondition GetCondition() const OVERRIDE {
3986 return kCondA;
3987 }
3988
3989 IfCondition GetOppositeCondition() const OVERRIDE {
3990 return kCondBE;
3991 }
3992
Artem Serovcced8ba2017-07-19 18:18:09 +01003993 protected:
3994 DEFAULT_COPY_CONSTRUCTOR(Above);
3995
Aart Bike9f37602015-10-09 11:15:55 -07003996 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003997 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003998 return MakeUnsigned(x) > MakeUnsigned(y);
3999 }
Aart Bike9f37602015-10-09 11:15:55 -07004000};
4001
Vladimir Markofcb503c2016-05-18 12:48:17 +01004002class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004003 public:
4004 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304005 : HCondition(kAboveOrEqual, first, second, dex_pc) {
4006 }
Aart Bike9f37602015-10-09 11:15:55 -07004007
4008 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004009 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004010 }
4011 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004012 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4013 }
4014 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4015 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4016 LOG(FATAL) << DebugName() << " is not defined for float values";
4017 UNREACHABLE();
4018 }
4019 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4020 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4021 LOG(FATAL) << DebugName() << " is not defined for double values";
4022 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004023 }
4024
4025 DECLARE_INSTRUCTION(AboveOrEqual);
4026
4027 IfCondition GetCondition() const OVERRIDE {
4028 return kCondAE;
4029 }
4030
4031 IfCondition GetOppositeCondition() const OVERRIDE {
4032 return kCondB;
4033 }
4034
Artem Serovcced8ba2017-07-19 18:18:09 +01004035 protected:
4036 DEFAULT_COPY_CONSTRUCTOR(AboveOrEqual);
4037
Aart Bike9f37602015-10-09 11:15:55 -07004038 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004039 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004040 return MakeUnsigned(x) >= MakeUnsigned(y);
4041 }
Aart Bike9f37602015-10-09 11:15:55 -07004042};
Dave Allison20dfc792014-06-16 20:44:29 -07004043
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004044// Instruction to check how two inputs compare to each other.
4045// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01004046class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004047 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00004048 // Note that `comparison_type` is the type of comparison performed
4049 // between the comparison's inputs, not the type of the instantiated
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004050 // HCompare instruction (which is always DataType::Type::kInt).
4051 HCompare(DataType::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004052 HInstruction* first,
4053 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04004054 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004055 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304056 : HBinaryOperation(kCompare,
4057 DataType::Type::kInt32,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004058 first,
4059 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00004060 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004061 dex_pc) {
4062 SetPackedField<ComparisonBiasField>(bias);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004063 DCHECK_EQ(comparison_type, DataType::Kind(first->GetType()));
4064 DCHECK_EQ(comparison_type, DataType::Kind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004065 }
4066
Roland Levillain9867bc72015-08-05 10:21:34 +01004067 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004068 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
4069
4070 template <typename T>
4071 int32_t ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004072 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004073 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
4074 // Handle the bias.
4075 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
4076 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004077
Roland Levillain9867bc72015-08-05 10:21:34 +01004078 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004079 // Note that there is no "cmp-int" Dex instruction so we shouldn't
4080 // reach this code path when processing a freshly built HIR
4081 // graph. However HCompare integer instructions can be synthesized
4082 // by the instruction simplifier to implement IntegerCompare and
4083 // IntegerSignum intrinsics, so we have to handle this case.
4084 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004085 }
4086 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004087 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4088 }
4089 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4090 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4091 }
4092 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4093 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01004094 }
4095
Vladimir Marko372f10e2016-05-17 16:30:10 +01004096 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004097 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00004098 }
4099
Vladimir Markoa1de9182016-02-25 11:37:38 +00004100 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04004101
Roland Levillain31dd3d62016-02-16 12:21:02 +00004102 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00004103 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00004104 bool IsGtBias() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004105 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004106 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00004107 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004108
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004109 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type type ATTRIBUTE_UNUSED) {
Roland Levillain1693a1f2016-03-15 14:57:31 +00004110 // Comparisons do not require a runtime call in any back end.
4111 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004112 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004113
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004114 DECLARE_INSTRUCTION(Compare);
4115
Roland Levillain31dd3d62016-02-16 12:21:02 +00004116 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01004117 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004118 static constexpr size_t kFieldComparisonBiasSize =
4119 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
4120 static constexpr size_t kNumberOfComparePackedBits =
4121 kFieldComparisonBias + kFieldComparisonBiasSize;
4122 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4123 using ComparisonBiasField =
4124 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
4125
Roland Levillain31dd3d62016-02-16 12:21:02 +00004126 // Return an integer constant containing the result of a comparison evaluated at compile time.
4127 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
4128 DCHECK(value == -1 || value == 0 || value == 1) << value;
4129 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
4130 }
4131
Artem Serovcced8ba2017-07-19 18:18:09 +01004132 DEFAULT_COPY_CONSTRUCTOR(Compare);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004133};
4134
Nicolas Geoffray2b615ba2017-01-06 14:40:07 +00004135class HNewInstance FINAL : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00004136 public:
4137 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00004138 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004139 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00004140 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00004141 bool finalizable,
4142 QuickEntrypointEnum entrypoint)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304143 : HExpression(kNewInstance,
4144 DataType::Type::kReference,
4145 SideEffects::CanTriggerGC(),
4146 dex_pc),
David Brazdil6de19382016-01-08 17:37:10 +00004147 type_index_(type_index),
4148 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00004149 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004150 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00004151 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00004152 }
4153
Artem Serovcced8ba2017-07-19 18:18:09 +01004154 bool IsClonable() const OVERRIDE { return true; }
4155
Andreas Gampea5b09a62016-11-17 15:21:22 -08004156 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00004157 const DexFile& GetDexFile() const { return dex_file_; }
4158
4159 // Calls runtime so needs an environment.
4160 bool NeedsEnvironment() const OVERRIDE { return true; }
4161
Mingyao Yang062157f2016-03-02 10:15:36 -08004162 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
4163 bool CanThrow() const OVERRIDE { return true; }
4164
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004165 bool NeedsChecks() const {
4166 return entrypoint_ == kQuickAllocObjectWithChecks;
4167 }
David Brazdil6de19382016-01-08 17:37:10 +00004168
Vladimir Markoa1de9182016-02-25 11:37:38 +00004169 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00004170
4171 bool CanBeNull() const OVERRIDE { return false; }
4172
4173 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4174
4175 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
4176 entrypoint_ = entrypoint;
4177 }
4178
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004179 HLoadClass* GetLoadClass() const {
4180 HInstruction* input = InputAt(0);
4181 if (input->IsClinitCheck()) {
4182 input = input->InputAt(0);
4183 }
4184 DCHECK(input->IsLoadClass());
4185 return input->AsLoadClass();
4186 }
4187
David Brazdil6de19382016-01-08 17:37:10 +00004188 bool IsStringAlloc() const;
4189
4190 DECLARE_INSTRUCTION(NewInstance);
4191
Artem Serovcced8ba2017-07-19 18:18:09 +01004192 protected:
4193 DEFAULT_COPY_CONSTRUCTOR(NewInstance);
4194
David Brazdil6de19382016-01-08 17:37:10 +00004195 private:
Vladimir Markobd785672018-05-03 17:09:09 +01004196 static constexpr size_t kFlagFinalizable = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004197 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
4198 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
4199 "Too many packed fields.");
4200
Andreas Gampea5b09a62016-11-17 15:21:22 -08004201 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00004202 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00004203 QuickEntrypointEnum entrypoint_;
David Brazdil6de19382016-01-08 17:37:10 +00004204};
4205
Agi Csaki05f20562015-08-19 14:58:14 -07004206enum IntrinsicNeedsEnvironmentOrCache {
4207 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
4208 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07004209};
4210
Aart Bik5d75afe2015-12-14 11:57:01 -08004211enum IntrinsicSideEffects {
4212 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
4213 kReadSideEffects, // Intrinsic may read heap memory.
4214 kWriteSideEffects, // Intrinsic may write heap memory.
4215 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
4216};
4217
4218enum IntrinsicExceptions {
4219 kNoThrow, // Intrinsic does not throw any exceptions.
4220 kCanThrow // Intrinsic may throw exceptions.
4221};
4222
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004223class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004224 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004225 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004226
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01004227 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004228 SetRawInputAt(index, argument);
4229 }
4230
Roland Levillain3e3d7332015-04-28 11:00:54 +01004231 // Return the number of arguments. This number can be lower than
4232 // the number of inputs returned by InputCount(), as some invoke
4233 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
4234 // inputs at the end of their list of inputs.
4235 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
4236
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004237 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4238
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004239 InvokeType GetInvokeType() const {
4240 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004241 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004242
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01004243 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004244 return intrinsic_;
4245 }
4246
Aart Bik5d75afe2015-12-14 11:57:01 -08004247 void SetIntrinsic(Intrinsics intrinsic,
4248 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
4249 IntrinsicSideEffects side_effects,
4250 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004251
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004252 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00004253 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004254 }
4255
Aart Bikff7d89c2016-11-07 08:49:28 -08004256 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4257
Vladimir Markoa1de9182016-02-25 11:37:38 +00004258 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004259
Aart Bika8b8e9b2018-01-09 11:01:02 -08004260 void SetAlwaysThrows(bool always_throws) { SetPackedFlag<kFlagAlwaysThrows>(always_throws); }
4261
4262 bool AlwaysThrows() const OVERRIDE { return GetPackedFlag<kFlagAlwaysThrows>(); }
4263
Aart Bik71bf7b42016-11-16 10:17:46 -08004264 bool CanBeMoved() const OVERRIDE { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004265
Vladimir Marko372f10e2016-05-17 16:30:10 +01004266 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08004267 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4268 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004269
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004270 uint32_t* GetIntrinsicOptimizations() {
4271 return &intrinsic_optimizations_;
4272 }
4273
4274 const uint32_t* GetIntrinsicOptimizations() const {
4275 return &intrinsic_optimizations_;
4276 }
4277
4278 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4279
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004280 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004281 void SetResolvedMethod(ArtMethod* method) { resolved_method_ = method; }
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004282
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004283 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004284
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004285 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004286 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4287 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004288 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
Vladimir Markobd785672018-05-03 17:09:09 +01004289 static constexpr size_t kFlagCanThrow = kFieldInvokeType + kFieldInvokeTypeSize;
Aart Bika8b8e9b2018-01-09 11:01:02 -08004290 static constexpr size_t kFlagAlwaysThrows = kFlagCanThrow + 1;
4291 static constexpr size_t kNumberOfInvokePackedBits = kFlagAlwaysThrows + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004292 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004293 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004294
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304295 HInvoke(InstructionKind kind,
4296 ArenaAllocator* allocator,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004297 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004298 uint32_t number_of_other_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004299 DataType::Type return_type,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004300 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004301 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004302 ArtMethod* resolved_method,
4303 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004304 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304305 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01004306 return_type,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004307 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4308 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004309 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004310 number_of_arguments + number_of_other_inputs,
4311 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004312 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004313 resolved_method_(resolved_method),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004314 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07004315 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004316 intrinsic_optimizations_(0) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004317 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004318 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004319 }
4320
Artem Serovcced8ba2017-07-19 18:18:09 +01004321 DEFAULT_COPY_CONSTRUCTOR(Invoke);
4322
Roland Levillain3e3d7332015-04-28 11:00:54 +01004323 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004324 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004325 const uint32_t dex_method_index_;
4326 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004327
4328 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4329 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004330};
4331
Vladimir Markofcb503c2016-05-18 12:48:17 +01004332class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004333 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004334 HInvokeUnresolved(ArenaAllocator* allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004335 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004336 DataType::Type return_type,
Calin Juravle175dc732015-08-25 15:42:32 +01004337 uint32_t dex_pc,
4338 uint32_t dex_method_index,
4339 InvokeType invoke_type)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304340 : HInvoke(kInvokeUnresolved,
4341 allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004342 number_of_arguments,
4343 0u /* number_of_other_inputs */,
4344 return_type,
4345 dex_pc,
4346 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004347 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01004348 invoke_type) {
4349 }
4350
Artem Serovcced8ba2017-07-19 18:18:09 +01004351 bool IsClonable() const OVERRIDE { return true; }
4352
Calin Juravle175dc732015-08-25 15:42:32 +01004353 DECLARE_INSTRUCTION(InvokeUnresolved);
4354
Artem Serovcced8ba2017-07-19 18:18:09 +01004355 protected:
4356 DEFAULT_COPY_CONSTRUCTOR(InvokeUnresolved);
Calin Juravle175dc732015-08-25 15:42:32 +01004357};
4358
Orion Hodsonac141392017-01-13 11:53:47 +00004359class HInvokePolymorphic FINAL : public HInvoke {
4360 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004361 HInvokePolymorphic(ArenaAllocator* allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004362 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004363 DataType::Type return_type,
Orion Hodsonac141392017-01-13 11:53:47 +00004364 uint32_t dex_pc,
4365 uint32_t dex_method_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304366 : HInvoke(kInvokePolymorphic,
4367 allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004368 number_of_arguments,
4369 0u /* number_of_other_inputs */,
4370 return_type,
4371 dex_pc,
4372 dex_method_index,
4373 nullptr,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304374 kVirtual) {
4375 }
Orion Hodsonac141392017-01-13 11:53:47 +00004376
Artem Serovcced8ba2017-07-19 18:18:09 +01004377 bool IsClonable() const OVERRIDE { return true; }
4378
Orion Hodsonac141392017-01-13 11:53:47 +00004379 DECLARE_INSTRUCTION(InvokePolymorphic);
4380
Artem Serovcced8ba2017-07-19 18:18:09 +01004381 protected:
4382 DEFAULT_COPY_CONSTRUCTOR(InvokePolymorphic);
Orion Hodsonac141392017-01-13 11:53:47 +00004383};
4384
Vladimir Markofcb503c2016-05-18 12:48:17 +01004385class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004386 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004387 // Requirements of this method call regarding the class
4388 // initialization (clinit) check of its declaring class.
4389 enum class ClinitCheckRequirement {
4390 kNone, // Class already initialized.
4391 kExplicit, // Static call having explicit clinit check as last input.
4392 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004393 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004394 };
4395
Vladimir Marko58155012015-08-19 12:49:41 +00004396 // Determines how to load the target ArtMethod*.
4397 enum class MethodLoadKind {
4398 // Use a String init ArtMethod* loaded from Thread entrypoints.
4399 kStringInit,
4400
4401 // Use the method's own ArtMethod* loaded by the register allocator.
4402 kRecursive,
4403
Vladimir Marko65979462017-05-19 17:25:12 +01004404 // Use PC-relative boot image ArtMethod* address that will be known at link time.
4405 // Used for boot image methods referenced by boot image code.
4406 kBootImageLinkTimePcRelative,
4407
Vladimir Marko58155012015-08-19 12:49:41 +00004408 // Use ArtMethod* at a known address, embed the direct address in the code.
4409 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
4410 kDirectAddress,
4411
Vladimir Markob066d432018-01-03 13:14:37 +00004412 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
4413 // Used for app->boot calls with relocatable image.
4414 kBootImageRelRo,
4415
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004416 // Load from an entry in the .bss section using a PC-relative load.
4417 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
4418 kBssEntry,
Vladimir Marko58155012015-08-19 12:49:41 +00004419
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004420 // Make a runtime call to resolve and call the method. This is the last-resort-kind
4421 // used when other kinds are unimplemented on a particular architecture.
4422 kRuntimeCall,
Vladimir Marko58155012015-08-19 12:49:41 +00004423 };
4424
4425 // Determines the location of the code pointer.
4426 enum class CodePtrLocation {
4427 // Recursive call, use local PC-relative call instruction.
4428 kCallSelf,
4429
Vladimir Marko58155012015-08-19 12:49:41 +00004430 // Use code pointer from the ArtMethod*.
4431 // Used when we don't know the target code. This is also the last-resort-kind used when
4432 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4433 kCallArtMethod,
4434 };
4435
4436 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004437 MethodLoadKind method_load_kind;
4438 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004439 // The method load data holds
4440 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4441 // Note that there are multiple string init methods, each having its own offset.
4442 // - the method address for kDirectAddress
Vladimir Markodc151b22015-10-15 18:02:30 +01004443 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004444 };
4445
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004446 HInvokeStaticOrDirect(ArenaAllocator* allocator,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004447 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004448 DataType::Type return_type,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004449 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004450 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004451 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004452 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004453 InvokeType invoke_type,
4454 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004455 ClinitCheckRequirement clinit_check_requirement)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304456 : HInvoke(kInvokeStaticOrDirect,
4457 allocator,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004458 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004459 // There is potentially one extra argument for the HCurrentMethod node, and
4460 // potentially one other if the clinit check is explicit, and potentially
4461 // one other if the method is a string factory.
4462 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004463 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004464 return_type,
4465 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004466 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004467 resolved_method,
4468 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004469 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004470 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004471 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4472 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004473
Artem Serovcced8ba2017-07-19 18:18:09 +01004474 bool IsClonable() const OVERRIDE { return true; }
4475
Vladimir Markodc151b22015-10-15 18:02:30 +01004476 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004477 bool had_current_method_input = HasCurrentMethodInput();
4478 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4479
4480 // Using the current method is the default and once we find a better
4481 // method load kind, we should not go back to using the current method.
4482 DCHECK(had_current_method_input || !needs_current_method_input);
4483
4484 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004485 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4486 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004487 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004488 dispatch_info_ = dispatch_info;
4489 }
4490
Aart Bik6daebeb2017-04-03 14:35:41 -07004491 DispatchInfo GetDispatchInfo() const {
4492 return dispatch_info_;
4493 }
4494
Vladimir Markoc53c0792015-11-19 15:48:33 +00004495 void AddSpecialInput(HInstruction* input) {
4496 // We allow only one special input.
4497 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4498 DCHECK(InputCount() == GetSpecialInputIndex() ||
4499 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4500 InsertInputAt(GetSpecialInputIndex(), input);
4501 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004502
Vladimir Marko372f10e2016-05-17 16:30:10 +01004503 using HInstruction::GetInputRecords; // Keep the const version visible.
4504 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
4505 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4506 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4507 DCHECK(!input_records.empty());
4508 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4509 HInstruction* last_input = input_records.back().GetInstruction();
4510 // Note: `last_input` may be null during arguments setup.
4511 if (last_input != nullptr) {
4512 // `last_input` is the last input of a static invoke marked as having
4513 // an explicit clinit check. It must either be:
4514 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4515 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4516 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4517 }
4518 }
4519 return input_records;
4520 }
4521
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004522 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004523 // We access the method via the dex cache so we can't do an implicit null check.
4524 // TODO: for intrinsics we can generate implicit null checks.
4525 return false;
4526 }
4527
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004528 bool CanBeNull() const OVERRIDE {
Vladimir Markobd785672018-05-03 17:09:09 +01004529 return GetType() == DataType::Type::kReference && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004530 }
4531
Vladimir Markoc53c0792015-11-19 15:48:33 +00004532 // Get the index of the special input, if any.
4533 //
David Brazdil6de19382016-01-08 17:37:10 +00004534 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4535 // method pointer; otherwise there may be one platform-specific special input,
4536 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004537 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004538 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004539
Vladimir Marko58155012015-08-19 12:49:41 +00004540 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4541 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4542 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004543 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004544 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004545 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko65979462017-05-19 17:25:12 +01004546 bool HasPcRelativeMethodLoadKind() const {
4547 return GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markob066d432018-01-03 13:14:37 +00004548 GetMethodLoadKind() == MethodLoadKind::kBootImageRelRo ||
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004549 GetMethodLoadKind() == MethodLoadKind::kBssEntry;
Vladimir Marko65979462017-05-19 17:25:12 +01004550 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004551 bool HasCurrentMethodInput() const {
4552 // This function can be called only after the invoke has been fully initialized by the builder.
4553 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004554 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004555 return true;
4556 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004557 DCHECK(InputCount() == GetSpecialInputIndex() ||
4558 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004559 return false;
4560 }
4561 }
Vladimir Marko58155012015-08-19 12:49:41 +00004562
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004563 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004564 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004565 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004566 }
4567
4568 uint64_t GetMethodAddress() const {
4569 DCHECK(HasMethodAddress());
4570 return dispatch_info_.method_load_data;
4571 }
4572
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004573 const DexFile& GetDexFileForPcRelativeDexCache() const;
4574
Vladimir Markoa1de9182016-02-25 11:37:38 +00004575 ClinitCheckRequirement GetClinitCheckRequirement() const {
4576 return GetPackedField<ClinitCheckRequirementField>();
4577 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004578
Roland Levillain4c0eb422015-04-24 16:43:49 +01004579 // Is this instruction a call to a static method?
4580 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004581 return GetInvokeType() == kStatic;
4582 }
4583
4584 MethodReference GetTargetMethod() const {
4585 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004586 }
4587
Vladimir Markofbb184a2015-11-13 14:47:00 +00004588 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4589 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4590 // instruction; only relevant for static calls with explicit clinit check.
4591 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004592 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004593 size_t last_input_index = inputs_.size() - 1u;
4594 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004595 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004596 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004597 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004598 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004599 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004600 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004601 }
4602
4603 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004604 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004605 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004606 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004607 }
4608
4609 // Is this a call to a static method whose declaring class has an
4610 // implicit intialization check requirement?
4611 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004612 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004613 }
4614
Vladimir Markob554b5a2015-11-06 12:57:55 +00004615 // Does this method load kind need the current method as an input?
4616 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004617 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kRuntimeCall;
Vladimir Markob554b5a2015-11-06 12:57:55 +00004618 }
4619
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004620 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004621
Artem Serovcced8ba2017-07-19 18:18:09 +01004622 protected:
4623 DEFAULT_COPY_CONSTRUCTOR(InvokeStaticOrDirect);
4624
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004625 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004626 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004627 static constexpr size_t kFieldClinitCheckRequirementSize =
4628 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4629 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4630 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4631 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4632 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004633 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4634 kFieldClinitCheckRequirement,
4635 kFieldClinitCheckRequirementSize>;
4636
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004637 // Cached values of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffrayea179f42018-02-08 22:30:18 +00004638 const MethodReference target_method_;
Vladimir Marko58155012015-08-19 12:49:41 +00004639 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004640};
Vladimir Markof64242a2015-12-01 14:58:23 +00004641std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004642std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004643
Vladimir Markofcb503c2016-05-18 12:48:17 +01004644class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004645 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004646 HInvokeVirtual(ArenaAllocator* allocator,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004647 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004648 DataType::Type return_type,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004649 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004650 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004651 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004652 uint32_t vtable_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304653 : HInvoke(kInvokeVirtual,
4654 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004655 number_of_arguments,
4656 0u,
4657 return_type,
4658 dex_pc,
4659 dex_method_index,
4660 resolved_method,
4661 kVirtual),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304662 vtable_index_(vtable_index) {
4663 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004664
Artem Serovcced8ba2017-07-19 18:18:09 +01004665 bool IsClonable() const OVERRIDE { return true; }
4666
Aart Bik71bf7b42016-11-16 10:17:46 -08004667 bool CanBeNull() const OVERRIDE {
4668 switch (GetIntrinsic()) {
4669 case Intrinsics::kThreadCurrentThread:
4670 case Intrinsics::kStringBufferAppend:
4671 case Intrinsics::kStringBufferToString:
4672 case Intrinsics::kStringBuilderAppend:
4673 case Intrinsics::kStringBuilderToString:
4674 return false;
4675 default:
4676 return HInvoke::CanBeNull();
4677 }
4678 }
4679
Calin Juravle641547a2015-04-21 22:08:51 +01004680 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004681 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004682 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004683 }
4684
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004685 uint32_t GetVTableIndex() const { return vtable_index_; }
4686
4687 DECLARE_INSTRUCTION(InvokeVirtual);
4688
Artem Serovcced8ba2017-07-19 18:18:09 +01004689 protected:
4690 DEFAULT_COPY_CONSTRUCTOR(InvokeVirtual);
4691
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004692 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004693 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004694 const uint32_t vtable_index_;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004695};
4696
Vladimir Markofcb503c2016-05-18 12:48:17 +01004697class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004698 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004699 HInvokeInterface(ArenaAllocator* allocator,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004700 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004701 DataType::Type return_type,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004702 uint32_t dex_pc,
4703 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004704 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004705 uint32_t imt_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304706 : HInvoke(kInvokeInterface,
4707 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004708 number_of_arguments,
4709 0u,
4710 return_type,
4711 dex_pc,
4712 dex_method_index,
4713 resolved_method,
4714 kInterface),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304715 imt_index_(imt_index) {
4716 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004717
Artem Serovcced8ba2017-07-19 18:18:09 +01004718 bool IsClonable() const OVERRIDE { return true; }
4719
Calin Juravle641547a2015-04-21 22:08:51 +01004720 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004721 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004722 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004723 }
4724
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004725 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
4726 // The assembly stub currently needs it.
4727 return true;
4728 }
4729
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004730 uint32_t GetImtIndex() const { return imt_index_; }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004731
4732 DECLARE_INSTRUCTION(InvokeInterface);
4733
Artem Serovcced8ba2017-07-19 18:18:09 +01004734 protected:
4735 DEFAULT_COPY_CONSTRUCTOR(InvokeInterface);
4736
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004737 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004738 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004739 const uint32_t imt_index_;
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004740};
4741
Vladimir Markofcb503c2016-05-18 12:48:17 +01004742class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004743 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004744 HNeg(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304745 : HUnaryOperation(kNeg, result_type, input, dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004746 DCHECK_EQ(result_type, DataType::Kind(input->GetType()));
Roland Levillain937e6cd2016-03-22 11:54:37 +00004747 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004748
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004749 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004750
4751 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004752 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004753 }
4754 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004755 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004756 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004757 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4758 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4759 }
4760 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4761 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4762 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004763
Roland Levillain88cb1752014-10-20 16:36:47 +01004764 DECLARE_INSTRUCTION(Neg);
4765
Artem Serovcced8ba2017-07-19 18:18:09 +01004766 protected:
4767 DEFAULT_COPY_CONSTRUCTOR(Neg);
Roland Levillain88cb1752014-10-20 16:36:47 +01004768};
4769
Vladimir Markofcb503c2016-05-18 12:48:17 +01004770class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004771 public:
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004772 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304773 : HExpression(kNewArray, DataType::Type::kReference, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004774 SetRawInputAt(0, cls);
4775 SetRawInputAt(1, length);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004776 }
4777
Artem Serovcced8ba2017-07-19 18:18:09 +01004778 bool IsClonable() const OVERRIDE { return true; }
4779
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004780 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004781 bool NeedsEnvironment() const OVERRIDE { return true; }
4782
Mingyao Yang0c365e62015-03-31 15:09:29 -07004783 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4784 bool CanThrow() const OVERRIDE { return true; }
4785
Calin Juravle10e244f2015-01-26 18:54:32 +00004786 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004787
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004788 HLoadClass* GetLoadClass() const {
4789 DCHECK(InputAt(0)->IsLoadClass());
4790 return InputAt(0)->AsLoadClass();
4791 }
4792
4793 HInstruction* GetLength() const {
4794 return InputAt(1);
4795 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004796
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004797 DECLARE_INSTRUCTION(NewArray);
4798
Artem Serovcced8ba2017-07-19 18:18:09 +01004799 protected:
4800 DEFAULT_COPY_CONSTRUCTOR(NewArray);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004801};
4802
Vladimir Markofcb503c2016-05-18 12:48:17 +01004803class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004804 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004805 HAdd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004806 HInstruction* left,
4807 HInstruction* right,
4808 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304809 : HBinaryOperation(kAdd, result_type, left, right, SideEffects::None(), dex_pc) {
4810 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004811
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004812 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004813
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004814 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004815
4816 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004817 return GetBlock()->GetGraph()->GetIntConstant(
4818 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004819 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004820 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004821 return GetBlock()->GetGraph()->GetLongConstant(
4822 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004823 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004824 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4825 return GetBlock()->GetGraph()->GetFloatConstant(
4826 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4827 }
4828 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4829 return GetBlock()->GetGraph()->GetDoubleConstant(
4830 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4831 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004832
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004833 DECLARE_INSTRUCTION(Add);
4834
Artem Serovcced8ba2017-07-19 18:18:09 +01004835 protected:
4836 DEFAULT_COPY_CONSTRUCTOR(Add);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004837};
4838
Vladimir Markofcb503c2016-05-18 12:48:17 +01004839class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004840 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004841 HSub(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004842 HInstruction* left,
4843 HInstruction* right,
4844 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304845 : HBinaryOperation(kSub, result_type, left, right, SideEffects::None(), dex_pc) {
4846 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004847
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004848 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004849
4850 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004851 return GetBlock()->GetGraph()->GetIntConstant(
4852 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004853 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004854 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004855 return GetBlock()->GetGraph()->GetLongConstant(
4856 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004857 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004858 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4859 return GetBlock()->GetGraph()->GetFloatConstant(
4860 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4861 }
4862 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4863 return GetBlock()->GetGraph()->GetDoubleConstant(
4864 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4865 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004866
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004867 DECLARE_INSTRUCTION(Sub);
4868
Artem Serovcced8ba2017-07-19 18:18:09 +01004869 protected:
4870 DEFAULT_COPY_CONSTRUCTOR(Sub);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004871};
4872
Vladimir Markofcb503c2016-05-18 12:48:17 +01004873class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004874 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004875 HMul(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004876 HInstruction* left,
4877 HInstruction* right,
4878 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304879 : HBinaryOperation(kMul, result_type, left, right, SideEffects::None(), dex_pc) {
4880 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004881
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004882 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004883
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004884 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004885
4886 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004887 return GetBlock()->GetGraph()->GetIntConstant(
4888 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004889 }
4890 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004891 return GetBlock()->GetGraph()->GetLongConstant(
4892 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004893 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004894 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4895 return GetBlock()->GetGraph()->GetFloatConstant(
4896 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4897 }
4898 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4899 return GetBlock()->GetGraph()->GetDoubleConstant(
4900 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4901 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004902
4903 DECLARE_INSTRUCTION(Mul);
4904
Artem Serovcced8ba2017-07-19 18:18:09 +01004905 protected:
4906 DEFAULT_COPY_CONSTRUCTOR(Mul);
Calin Juravle34bacdf2014-10-07 20:23:36 +01004907};
4908
Vladimir Markofcb503c2016-05-18 12:48:17 +01004909class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004910 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004911 HDiv(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004912 HInstruction* left,
4913 HInstruction* right,
4914 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304915 : HBinaryOperation(kDiv, result_type, left, right, SideEffects::None(), dex_pc) {
4916 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004917
Roland Levillain9867bc72015-08-05 10:21:34 +01004918 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004919 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004920 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004921 // Our graph structure ensures we never have 0 for `y` during
4922 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004923 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004924 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004925 return (y == -1) ? -x : x / y;
4926 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004927
Roland Levillain31dd3d62016-02-16 12:21:02 +00004928 template <typename T>
4929 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004930 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004931 return x / y;
4932 }
4933
Roland Levillain9867bc72015-08-05 10:21:34 +01004934 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004935 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004936 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004937 }
4938 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004939 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004940 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4941 }
4942 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4943 return GetBlock()->GetGraph()->GetFloatConstant(
4944 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4945 }
4946 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4947 return GetBlock()->GetGraph()->GetDoubleConstant(
4948 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004949 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004950
4951 DECLARE_INSTRUCTION(Div);
4952
Artem Serovcced8ba2017-07-19 18:18:09 +01004953 protected:
4954 DEFAULT_COPY_CONSTRUCTOR(Div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00004955};
4956
Vladimir Markofcb503c2016-05-18 12:48:17 +01004957class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004958 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004959 HRem(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004960 HInstruction* left,
4961 HInstruction* right,
4962 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304963 : HBinaryOperation(kRem, result_type, left, right, SideEffects::None(), dex_pc) {
4964 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004965
Roland Levillain9867bc72015-08-05 10:21:34 +01004966 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004967 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004968 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004969 // Our graph structure ensures we never have 0 for `y` during
4970 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004971 DCHECK_NE(y, 0);
4972 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4973 return (y == -1) ? 0 : x % y;
4974 }
4975
Roland Levillain31dd3d62016-02-16 12:21:02 +00004976 template <typename T>
4977 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004978 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004979 return std::fmod(x, y);
4980 }
4981
Roland Levillain9867bc72015-08-05 10:21:34 +01004982 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004983 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004984 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004985 }
4986 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004987 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004988 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004989 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004990 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4991 return GetBlock()->GetGraph()->GetFloatConstant(
4992 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4993 }
4994 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4995 return GetBlock()->GetGraph()->GetDoubleConstant(
4996 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4997 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004998
4999 DECLARE_INSTRUCTION(Rem);
5000
Artem Serovcced8ba2017-07-19 18:18:09 +01005001 protected:
5002 DEFAULT_COPY_CONSTRUCTOR(Rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00005003};
5004
Aart Bik1f8d51b2018-02-15 10:42:37 -08005005class HMin FINAL : public HBinaryOperation {
5006 public:
5007 HMin(DataType::Type result_type,
5008 HInstruction* left,
5009 HInstruction* right,
5010 uint32_t dex_pc)
5011 : HBinaryOperation(kMin, result_type, left, right, SideEffects::None(), dex_pc) {}
5012
5013 bool IsCommutative() const OVERRIDE { return true; }
5014
5015 // Evaluation for integral values.
5016 template <typename T> static T ComputeIntegral(T x, T y) {
5017 return (x <= y) ? x : y;
5018 }
5019
5020 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
5021 return GetBlock()->GetGraph()->GetIntConstant(
5022 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5023 }
5024 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
5025 return GetBlock()->GetGraph()->GetLongConstant(
5026 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5027 }
5028 // TODO: Evaluation for floating-point values.
5029 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5030 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { return nullptr; }
5031 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5032 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { return nullptr; }
5033
5034 DECLARE_INSTRUCTION(Min);
5035
5036 protected:
5037 DEFAULT_COPY_CONSTRUCTOR(Min);
5038};
5039
5040class HMax FINAL : public HBinaryOperation {
5041 public:
5042 HMax(DataType::Type result_type,
5043 HInstruction* left,
5044 HInstruction* right,
5045 uint32_t dex_pc)
5046 : HBinaryOperation(kMax, result_type, left, right, SideEffects::None(), dex_pc) {}
5047
5048 bool IsCommutative() const OVERRIDE { return true; }
5049
5050 // Evaluation for integral values.
5051 template <typename T> static T ComputeIntegral(T x, T y) {
5052 return (x >= y) ? x : y;
5053 }
5054
5055 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
5056 return GetBlock()->GetGraph()->GetIntConstant(
5057 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5058 }
5059 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
5060 return GetBlock()->GetGraph()->GetLongConstant(
5061 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5062 }
5063 // TODO: Evaluation for floating-point values.
5064 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5065 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { return nullptr; }
5066 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5067 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { return nullptr; }
5068
5069 DECLARE_INSTRUCTION(Max);
5070
5071 protected:
5072 DEFAULT_COPY_CONSTRUCTOR(Max);
5073};
5074
Aart Bik3dad3412018-02-28 12:01:46 -08005075class HAbs FINAL : public HUnaryOperation {
5076 public:
5077 HAbs(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
5078 : HUnaryOperation(kAbs, result_type, input, dex_pc) {}
5079
5080 // Evaluation for integral values.
5081 template <typename T> static T ComputeIntegral(T x) {
5082 return x < 0 ? -x : x;
5083 }
5084
5085 // Evaluation for floating-point values.
5086 // Note, as a "quality of implementation", rather than pure "spec compliance",
5087 // we require that Math.abs() clears the sign bit (but changes nothing else)
5088 // for all floating-point numbers, including NaN (signaling NaN may become quiet though).
5089 // http://b/30758343
5090 template <typename T, typename S> static T ComputeFP(T x) {
5091 S bits = bit_cast<S, T>(x);
5092 return bit_cast<T, S>(bits & std::numeric_limits<S>::max());
5093 }
5094
5095 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
5096 return GetBlock()->GetGraph()->GetIntConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5097 }
5098 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
5099 return GetBlock()->GetGraph()->GetLongConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5100 }
5101 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
5102 return GetBlock()->GetGraph()->GetFloatConstant(
5103 ComputeFP<float, int32_t>(x->GetValue()), GetDexPc());
5104 }
5105 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
5106 return GetBlock()->GetGraph()->GetDoubleConstant(
5107 ComputeFP<double, int64_t>(x->GetValue()), GetDexPc());
5108 }
5109
5110 DECLARE_INSTRUCTION(Abs);
5111
5112 protected:
5113 DEFAULT_COPY_CONSTRUCTOR(Abs);
5114};
5115
Vladimir Markofcb503c2016-05-18 12:48:17 +01005116class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00005117 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00005118 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
5119 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00005120 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305121 : HExpression(kDivZeroCheck, value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00005122 SetRawInputAt(0, value);
5123 }
5124
5125 bool CanBeMoved() const OVERRIDE { return true; }
5126
Vladimir Marko372f10e2016-05-17 16:30:10 +01005127 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00005128 return true;
5129 }
5130
5131 bool NeedsEnvironment() const OVERRIDE { return true; }
5132 bool CanThrow() const OVERRIDE { return true; }
5133
Calin Juravled0d48522014-11-04 16:40:20 +00005134 DECLARE_INSTRUCTION(DivZeroCheck);
5135
Artem Serovcced8ba2017-07-19 18:18:09 +01005136 protected:
5137 DEFAULT_COPY_CONSTRUCTOR(DivZeroCheck);
Calin Juravled0d48522014-11-04 16:40:20 +00005138};
5139
Vladimir Markofcb503c2016-05-18 12:48:17 +01005140class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005141 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005142 HShl(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005143 HInstruction* value,
5144 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005145 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305146 : HBinaryOperation(kShl, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005147 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5148 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005149 }
5150
Roland Levillain5b5b9312016-03-22 14:57:31 +00005151 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005152 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005153 return value << (distance & max_shift_distance);
5154 }
5155
5156 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005157 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005158 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005159 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005160 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005161 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005162 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005163 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005164 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5165 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5166 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5167 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005168 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005169 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5170 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005171 LOG(FATAL) << DebugName() << " is not defined for float values";
5172 UNREACHABLE();
5173 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005174 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5175 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005176 LOG(FATAL) << DebugName() << " is not defined for double values";
5177 UNREACHABLE();
5178 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005179
5180 DECLARE_INSTRUCTION(Shl);
5181
Artem Serovcced8ba2017-07-19 18:18:09 +01005182 protected:
5183 DEFAULT_COPY_CONSTRUCTOR(Shl);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005184};
5185
Vladimir Markofcb503c2016-05-18 12:48:17 +01005186class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005187 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005188 HShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005189 HInstruction* value,
5190 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005191 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305192 : HBinaryOperation(kShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005193 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5194 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005195 }
5196
Roland Levillain5b5b9312016-03-22 14:57:31 +00005197 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005198 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005199 return value >> (distance & max_shift_distance);
5200 }
5201
5202 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005203 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005204 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005205 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005206 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005207 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005208 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005209 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005210 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5211 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5212 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5213 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005214 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005215 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5216 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005217 LOG(FATAL) << DebugName() << " is not defined for float values";
5218 UNREACHABLE();
5219 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005220 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5221 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005222 LOG(FATAL) << DebugName() << " is not defined for double values";
5223 UNREACHABLE();
5224 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005225
5226 DECLARE_INSTRUCTION(Shr);
5227
Artem Serovcced8ba2017-07-19 18:18:09 +01005228 protected:
5229 DEFAULT_COPY_CONSTRUCTOR(Shr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005230};
5231
Vladimir Markofcb503c2016-05-18 12:48:17 +01005232class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005233 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005234 HUShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005235 HInstruction* value,
5236 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005237 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305238 : HBinaryOperation(kUShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005239 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5240 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00005241 }
5242
Roland Levillain5b5b9312016-03-22 14:57:31 +00005243 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005244 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005245 typedef typename std::make_unsigned<T>::type V;
5246 V ux = static_cast<V>(value);
5247 return static_cast<T>(ux >> (distance & max_shift_distance));
5248 }
5249
5250 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005251 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005252 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005253 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005254 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005255 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005256 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005257 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005258 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5259 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5260 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5261 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00005262 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005263 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5264 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005265 LOG(FATAL) << DebugName() << " is not defined for float values";
5266 UNREACHABLE();
5267 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005268 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5269 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005270 LOG(FATAL) << DebugName() << " is not defined for double values";
5271 UNREACHABLE();
5272 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005273
5274 DECLARE_INSTRUCTION(UShr);
5275
Artem Serovcced8ba2017-07-19 18:18:09 +01005276 protected:
5277 DEFAULT_COPY_CONSTRUCTOR(UShr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005278};
5279
Vladimir Markofcb503c2016-05-18 12:48:17 +01005280class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005281 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005282 HAnd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005283 HInstruction* left,
5284 HInstruction* right,
5285 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305286 : HBinaryOperation(kAnd, result_type, left, right, SideEffects::None(), dex_pc) {
5287 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005288
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005289 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005290
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005291 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005292
5293 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005294 return GetBlock()->GetGraph()->GetIntConstant(
5295 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005296 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005297 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005298 return GetBlock()->GetGraph()->GetLongConstant(
5299 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005300 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005301 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5302 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5303 LOG(FATAL) << DebugName() << " is not defined for float values";
5304 UNREACHABLE();
5305 }
5306 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5307 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5308 LOG(FATAL) << DebugName() << " is not defined for double values";
5309 UNREACHABLE();
5310 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005311
5312 DECLARE_INSTRUCTION(And);
5313
Artem Serovcced8ba2017-07-19 18:18:09 +01005314 protected:
5315 DEFAULT_COPY_CONSTRUCTOR(And);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005316};
5317
Vladimir Markofcb503c2016-05-18 12:48:17 +01005318class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005319 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005320 HOr(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005321 HInstruction* left,
5322 HInstruction* right,
5323 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305324 : HBinaryOperation(kOr, result_type, left, right, SideEffects::None(), dex_pc) {
5325 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005326
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005327 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005328
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005329 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005330
5331 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005332 return GetBlock()->GetGraph()->GetIntConstant(
5333 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005334 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005335 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005336 return GetBlock()->GetGraph()->GetLongConstant(
5337 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005338 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005339 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5340 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5341 LOG(FATAL) << DebugName() << " is not defined for float values";
5342 UNREACHABLE();
5343 }
5344 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5345 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5346 LOG(FATAL) << DebugName() << " is not defined for double values";
5347 UNREACHABLE();
5348 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005349
5350 DECLARE_INSTRUCTION(Or);
5351
Artem Serovcced8ba2017-07-19 18:18:09 +01005352 protected:
5353 DEFAULT_COPY_CONSTRUCTOR(Or);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005354};
5355
Vladimir Markofcb503c2016-05-18 12:48:17 +01005356class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005357 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005358 HXor(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005359 HInstruction* left,
5360 HInstruction* right,
5361 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305362 : HBinaryOperation(kXor, result_type, left, right, SideEffects::None(), dex_pc) {
5363 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005364
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005365 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005366
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005367 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005368
5369 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005370 return GetBlock()->GetGraph()->GetIntConstant(
5371 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005372 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005373 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005374 return GetBlock()->GetGraph()->GetLongConstant(
5375 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005376 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005377 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5378 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5379 LOG(FATAL) << DebugName() << " is not defined for float values";
5380 UNREACHABLE();
5381 }
5382 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5383 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5384 LOG(FATAL) << DebugName() << " is not defined for double values";
5385 UNREACHABLE();
5386 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005387
5388 DECLARE_INSTRUCTION(Xor);
5389
Artem Serovcced8ba2017-07-19 18:18:09 +01005390 protected:
5391 DEFAULT_COPY_CONSTRUCTOR(Xor);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005392};
5393
Vladimir Markofcb503c2016-05-18 12:48:17 +01005394class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005395 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005396 HRor(DataType::Type result_type, HInstruction* value, HInstruction* distance)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305397 : HBinaryOperation(kRor, result_type, value, distance) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005398 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5399 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00005400 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005401
Roland Levillain5b5b9312016-03-22 14:57:31 +00005402 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005403 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005404 typedef typename std::make_unsigned<T>::type V;
5405 V ux = static_cast<V>(value);
5406 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005407 return static_cast<T>(ux);
5408 } else {
5409 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005410 return static_cast<T>(ux >> (distance & max_shift_value)) |
5411 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005412 }
5413 }
5414
Roland Levillain5b5b9312016-03-22 14:57:31 +00005415 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005416 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005417 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005418 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005419 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005420 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005421 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005422 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005423 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5424 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5425 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5426 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005427 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005428 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5429 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005430 LOG(FATAL) << DebugName() << " is not defined for float values";
5431 UNREACHABLE();
5432 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005433 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5434 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005435 LOG(FATAL) << DebugName() << " is not defined for double values";
5436 UNREACHABLE();
5437 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005438
5439 DECLARE_INSTRUCTION(Ror);
5440
Artem Serovcced8ba2017-07-19 18:18:09 +01005441 protected:
5442 DEFAULT_COPY_CONSTRUCTOR(Ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005443};
5444
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005445// The value of a parameter in this method. Its location depends on
5446// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01005447class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005448 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005449 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005450 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005451 uint8_t index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005452 DataType::Type parameter_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005453 bool is_this = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305454 : HExpression(kParameterValue, parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005455 dex_file_(dex_file),
5456 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005457 index_(index) {
5458 SetPackedFlag<kFlagIsThis>(is_this);
5459 SetPackedFlag<kFlagCanBeNull>(!is_this);
5460 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005461
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005462 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005463 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005464 uint8_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07005465 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005466
Vladimir Markoa1de9182016-02-25 11:37:38 +00005467 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
5468 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005469
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005470 DECLARE_INSTRUCTION(ParameterValue);
5471
Artem Serovcced8ba2017-07-19 18:18:09 +01005472 protected:
5473 DEFAULT_COPY_CONSTRUCTOR(ParameterValue);
5474
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005475 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005476 // Whether or not the parameter value corresponds to 'this' argument.
Vladimir Markobd785672018-05-03 17:09:09 +01005477 static constexpr size_t kFlagIsThis = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005478 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5479 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5480 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5481 "Too many packed fields.");
5482
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005483 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005484 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005485 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005486 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005487 const uint8_t index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005488};
5489
Vladimir Markofcb503c2016-05-18 12:48:17 +01005490class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005491 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005492 HNot(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305493 : HUnaryOperation(kNot, result_type, input, dex_pc) {
5494 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005495
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005496 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005497 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005498 return true;
5499 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005500
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005501 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005502
5503 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005504 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005505 }
5506 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005507 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005508 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005509 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5510 LOG(FATAL) << DebugName() << " is not defined for float values";
5511 UNREACHABLE();
5512 }
5513 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5514 LOG(FATAL) << DebugName() << " is not defined for double values";
5515 UNREACHABLE();
5516 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005517
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005518 DECLARE_INSTRUCTION(Not);
5519
Artem Serovcced8ba2017-07-19 18:18:09 +01005520 protected:
5521 DEFAULT_COPY_CONSTRUCTOR(Not);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005522};
5523
Vladimir Markofcb503c2016-05-18 12:48:17 +01005524class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005525 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005526 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305527 : HUnaryOperation(kBooleanNot, DataType::Type::kBool, input, dex_pc) {
5528 }
David Brazdil66d126e2015-04-03 16:02:44 +01005529
5530 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005531 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01005532 return true;
5533 }
5534
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005535 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005536 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005537 return !x;
5538 }
5539
Roland Levillain9867bc72015-08-05 10:21:34 +01005540 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005541 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005542 }
5543 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5544 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005545 UNREACHABLE();
5546 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005547 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5548 LOG(FATAL) << DebugName() << " is not defined for float values";
5549 UNREACHABLE();
5550 }
5551 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5552 LOG(FATAL) << DebugName() << " is not defined for double values";
5553 UNREACHABLE();
5554 }
David Brazdil66d126e2015-04-03 16:02:44 +01005555
5556 DECLARE_INSTRUCTION(BooleanNot);
5557
Artem Serovcced8ba2017-07-19 18:18:09 +01005558 protected:
5559 DEFAULT_COPY_CONSTRUCTOR(BooleanNot);
David Brazdil66d126e2015-04-03 16:02:44 +01005560};
5561
Vladimir Markofcb503c2016-05-18 12:48:17 +01005562class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005563 public:
5564 // Instantiate a type conversion of `input` to `result_type`.
Vladimir Markoc8fb2112017-10-03 11:37:52 +01005565 HTypeConversion(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305566 : HExpression(kTypeConversion, result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005567 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005568 // Invariant: We should never generate a conversion to a Boolean value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005569 DCHECK_NE(DataType::Type::kBool, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005570 }
5571
5572 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005573 DataType::Type GetInputType() const { return GetInput()->GetType(); }
5574 DataType::Type GetResultType() const { return GetType(); }
Roland Levillaindff1f282014-11-05 14:15:05 +00005575
5576 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005577 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5578 return true;
5579 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005580
Mark Mendelle82549b2015-05-06 10:55:34 -04005581 // Try to statically evaluate the conversion and return a HConstant
5582 // containing the result. If the input cannot be converted, return nullptr.
5583 HConstant* TryStaticEvaluation() const;
5584
Roland Levillaindff1f282014-11-05 14:15:05 +00005585 DECLARE_INSTRUCTION(TypeConversion);
5586
Artem Serovcced8ba2017-07-19 18:18:09 +01005587 protected:
5588 DEFAULT_COPY_CONSTRUCTOR(TypeConversion);
Roland Levillaindff1f282014-11-05 14:15:05 +00005589};
5590
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005591static constexpr uint32_t kNoRegNumber = -1;
5592
Vladimir Markofcb503c2016-05-18 12:48:17 +01005593class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005594 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005595 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
5596 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005597 HNullCheck(HInstruction* value, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305598 : HExpression(kNullCheck, value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005599 SetRawInputAt(0, value);
5600 }
5601
Artem Serovcced8ba2017-07-19 18:18:09 +01005602 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00005603 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005604 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005605 return true;
5606 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005607
Calin Juravle10e244f2015-01-26 18:54:32 +00005608 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005609
Calin Juravle10e244f2015-01-26 18:54:32 +00005610 bool CanThrow() const OVERRIDE { return true; }
5611
5612 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005613
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005614 DECLARE_INSTRUCTION(NullCheck);
5615
Artem Serovcced8ba2017-07-19 18:18:09 +01005616 protected:
5617 DEFAULT_COPY_CONSTRUCTOR(NullCheck);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005618};
5619
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005620// Embeds an ArtField and all the information required by the compiler. We cache
5621// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005622class FieldInfo : public ValueObject {
5623 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005624 FieldInfo(ArtField* field,
5625 MemberOffset field_offset,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005626 DataType::Type field_type,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005627 bool is_volatile,
5628 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005629 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005630 const DexFile& dex_file)
5631 : field_(field),
5632 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005633 field_type_(field_type),
5634 is_volatile_(is_volatile),
5635 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005636 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005637 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005638
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005639 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005640 MemberOffset GetFieldOffset() const { return field_offset_; }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005641 DataType::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005642 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005643 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005644 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005645 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005646
5647 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005648 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005649 const MemberOffset field_offset_;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005650 const DataType::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005651 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005652 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005653 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005654 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005655};
5656
Vladimir Markofcb503c2016-05-18 12:48:17 +01005657class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005658 public:
5659 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005660 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005661 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005662 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005663 bool is_volatile,
5664 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005665 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005666 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005667 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305668 : HExpression(kInstanceFieldGet,
5669 field_type,
5670 SideEffects::FieldReadOfType(field_type, is_volatile),
5671 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005672 field_info_(field,
5673 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005674 field_type,
5675 is_volatile,
5676 field_idx,
5677 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005678 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005679 SetRawInputAt(0, value);
5680 }
5681
Artem Serovcced8ba2017-07-19 18:18:09 +01005682 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005683 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005684
Vladimir Marko372f10e2016-05-17 16:30:10 +01005685 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5686 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005687 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005688 }
5689
Calin Juravle641547a2015-04-21 22:08:51 +01005690 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005691 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005692 }
5693
5694 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005695 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5696 }
5697
Calin Juravle52c48962014-12-16 17:02:57 +00005698 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005699 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005700 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005701 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005702
Vladimir Marko61b92282017-10-11 13:23:17 +01005703 void SetType(DataType::Type new_type) {
5704 DCHECK(DataType::IsIntegralType(GetType()));
5705 DCHECK(DataType::IsIntegralType(new_type));
5706 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5707 SetPackedField<TypeField>(new_type);
5708 }
5709
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005710 DECLARE_INSTRUCTION(InstanceFieldGet);
5711
Artem Serovcced8ba2017-07-19 18:18:09 +01005712 protected:
5713 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldGet);
5714
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005715 private:
5716 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005717};
5718
Vladimir Markobd785672018-05-03 17:09:09 +01005719class HInstanceFieldSet FINAL : public HExpression<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005720 public:
5721 HInstanceFieldSet(HInstruction* object,
5722 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005723 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005724 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005725 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005726 bool is_volatile,
5727 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005728 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005729 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005730 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01005731 : HExpression(kInstanceFieldSet,
5732 SideEffects::FieldWriteOfType(field_type, is_volatile),
5733 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005734 field_info_(field,
5735 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005736 field_type,
5737 is_volatile,
5738 field_idx,
5739 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005740 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005741 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005742 SetRawInputAt(0, object);
5743 SetRawInputAt(1, value);
5744 }
5745
Artem Serovcced8ba2017-07-19 18:18:09 +01005746 bool IsClonable() const OVERRIDE { return true; }
5747
Calin Juravle641547a2015-04-21 22:08:51 +01005748 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005749 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005750 }
5751
Calin Juravle52c48962014-12-16 17:02:57 +00005752 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005753 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005754 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005755 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005756 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005757 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5758 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005759
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005760 DECLARE_INSTRUCTION(InstanceFieldSet);
5761
Artem Serovcced8ba2017-07-19 18:18:09 +01005762 protected:
5763 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldSet);
5764
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005765 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005766 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5767 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5768 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5769 "Too many packed fields.");
5770
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005771 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005772};
5773
Vladimir Markofcb503c2016-05-18 12:48:17 +01005774class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005775 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005776 HArrayGet(HInstruction* array,
5777 HInstruction* index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005778 DataType::Type type,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005779 uint32_t dex_pc)
5780 : HArrayGet(array,
5781 index,
5782 type,
5783 SideEffects::ArrayReadOfType(type),
5784 dex_pc,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305785 /* is_string_char_at */ false) {
5786 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005787
5788 HArrayGet(HInstruction* array,
5789 HInstruction* index,
5790 DataType::Type type,
5791 SideEffects side_effects,
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005792 uint32_t dex_pc,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005793 bool is_string_char_at)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305794 : HExpression(kArrayGet, type, side_effects, dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005795 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005796 SetRawInputAt(0, array);
5797 SetRawInputAt(1, index);
5798 }
5799
Artem Serovcced8ba2017-07-19 18:18:09 +01005800 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005801 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005802 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005803 return true;
5804 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005805 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005806 // TODO: We can be smarter here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01005807 // Currently, unless the array is the result of NewArray, the array access is always
5808 // preceded by some form of null NullCheck necessary for the bounds check, usually
5809 // implicit null check on the ArrayLength input to BoundsCheck or Deoptimize for
5810 // dynamic BCE. There are cases when these could be removed to produce better code.
5811 // If we ever add optimizations to do so we should allow an implicit check here
5812 // (as long as the address falls in the first page).
5813 //
5814 // As an example of such fancy optimization, we could eliminate BoundsCheck for
5815 // a = cond ? new int[1] : null;
5816 // a[0]; // The Phi does not need bounds check for either input.
Calin Juravle77520bc2015-01-12 18:45:46 +00005817 return false;
5818 }
5819
David Brazdil4833f5a2015-12-16 10:37:39 +00005820 bool IsEquivalentOf(HArrayGet* other) const {
5821 bool result = (GetDexPc() == other->GetDexPc());
5822 if (kIsDebugBuild && result) {
5823 DCHECK_EQ(GetBlock(), other->GetBlock());
5824 DCHECK_EQ(GetArray(), other->GetArray());
5825 DCHECK_EQ(GetIndex(), other->GetIndex());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005826 if (DataType::IsIntOrLongType(GetType())) {
5827 DCHECK(DataType::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005828 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005829 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
5830 DCHECK(DataType::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005831 }
5832 }
5833 return result;
5834 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005835
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005836 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5837
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005838 HInstruction* GetArray() const { return InputAt(0); }
5839 HInstruction* GetIndex() const { return InputAt(1); }
5840
Vladimir Marko61b92282017-10-11 13:23:17 +01005841 void SetType(DataType::Type new_type) {
5842 DCHECK(DataType::IsIntegralType(GetType()));
5843 DCHECK(DataType::IsIntegralType(new_type));
5844 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5845 SetPackedField<TypeField>(new_type);
5846 }
5847
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005848 DECLARE_INSTRUCTION(ArrayGet);
5849
Artem Serovcced8ba2017-07-19 18:18:09 +01005850 protected:
5851 DEFAULT_COPY_CONSTRUCTOR(ArrayGet);
5852
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005853 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005854 // We treat a String as an array, creating the HArrayGet from String.charAt()
5855 // intrinsic in the instruction simplifier. We can always determine whether
5856 // a particular HArrayGet is actually a String.charAt() by looking at the type
5857 // of the input but that requires holding the mutator lock, so we prefer to use
5858 // a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01005859 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005860 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5861 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5862 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005863};
5864
Vladimir Markobd785672018-05-03 17:09:09 +01005865class HArraySet FINAL : public HExpression<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005866 public:
5867 HArraySet(HInstruction* array,
5868 HInstruction* index,
5869 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005870 DataType::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005871 uint32_t dex_pc)
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005872 : HArraySet(array,
5873 index,
5874 value,
5875 expected_component_type,
5876 // Make a best guess for side effects now, may be refined during SSA building.
5877 ComputeSideEffects(GetComponentType(value->GetType(), expected_component_type)),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305878 dex_pc) {
5879 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005880
5881 HArraySet(HInstruction* array,
5882 HInstruction* index,
5883 HInstruction* value,
5884 DataType::Type expected_component_type,
5885 SideEffects side_effects,
5886 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01005887 : HExpression(kArraySet, side_effects, dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005888 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005889 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005890 SetPackedFlag<kFlagValueCanBeNull>(true);
5891 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005892 SetRawInputAt(0, array);
5893 SetRawInputAt(1, index);
5894 SetRawInputAt(2, value);
5895 }
5896
Artem Serovcced8ba2017-07-19 18:18:09 +01005897 bool IsClonable() const OVERRIDE { return true; }
5898
Calin Juravle77520bc2015-01-12 18:45:46 +00005899 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005900 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005901 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005902 }
5903
Mingyao Yang81014cb2015-06-02 03:16:27 -07005904 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005905 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005906
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005907 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005908 // TODO: Same as for ArrayGet.
5909 return false;
5910 }
5911
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005912 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005913 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005914 }
5915
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005916 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005917 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005918 }
5919
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005920 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005921 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005922 }
5923
Vladimir Markoa1de9182016-02-25 11:37:38 +00005924 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5925 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5926 bool StaticTypeOfArrayIsObjectArray() const {
5927 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5928 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005929
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005930 HInstruction* GetArray() const { return InputAt(0); }
5931 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005932 HInstruction* GetValue() const { return InputAt(2); }
5933
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005934 DataType::Type GetComponentType() const {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005935 return GetComponentType(GetValue()->GetType(), GetRawExpectedComponentType());
5936 }
5937
5938 static DataType::Type GetComponentType(DataType::Type value_type,
5939 DataType::Type expected_component_type) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005940 // The Dex format does not type floating point index operations. Since the
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005941 // `expected_component_type` comes from SSA building and can therefore not
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005942 // be correct, we also check what is the value type. If it is a floating
5943 // point type, we must use that type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005944 return ((value_type == DataType::Type::kFloat32) || (value_type == DataType::Type::kFloat64))
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005945 ? value_type
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005946 : expected_component_type;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005947 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005948
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005949 DataType::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005950 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005951 }
5952
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005953 static SideEffects ComputeSideEffects(DataType::Type type) {
5954 return SideEffects::ArrayWriteOfType(type).Union(SideEffectsForArchRuntimeCalls(type));
Aart Bik18b36ab2016-04-13 16:41:35 -07005955 }
5956
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005957 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type value_type) {
5958 return (value_type == DataType::Type::kReference) ? SideEffects::CanTriggerGC()
5959 : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005960 }
5961
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005962 DECLARE_INSTRUCTION(ArraySet);
5963
Artem Serovcced8ba2017-07-19 18:18:09 +01005964 protected:
5965 DEFAULT_COPY_CONSTRUCTOR(ArraySet);
5966
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005967 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005968 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5969 static constexpr size_t kFieldExpectedComponentTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005970 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00005971 static constexpr size_t kFlagNeedsTypeCheck =
5972 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5973 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005974 // Cached information for the reference_type_info_ so that codegen
5975 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005976 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5977 static constexpr size_t kNumberOfArraySetPackedBits =
5978 kFlagStaticTypeOfArrayIsObjectArray + 1;
5979 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5980 using ExpectedComponentTypeField =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005981 BitField<DataType::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005982};
5983
Vladimir Markofcb503c2016-05-18 12:48:17 +01005984class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005985 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005986 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305987 : HExpression(kArrayLength, DataType::Type::kInt32, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005988 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005989 // Note that arrays do not change length, so the instruction does not
5990 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005991 SetRawInputAt(0, array);
5992 }
5993
Artem Serovcced8ba2017-07-19 18:18:09 +01005994 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle77520bc2015-01-12 18:45:46 +00005995 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005996 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005997 return true;
5998 }
Calin Juravle641547a2015-04-21 22:08:51 +01005999 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
6000 return obj == InputAt(0);
6001 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006002
Vladimir Markodce016e2016-04-28 13:10:02 +01006003 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
6004
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006005 DECLARE_INSTRUCTION(ArrayLength);
6006
Artem Serovcced8ba2017-07-19 18:18:09 +01006007 protected:
6008 DEFAULT_COPY_CONSTRUCTOR(ArrayLength);
6009
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006010 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01006011 // We treat a String as an array, creating the HArrayLength from String.length()
6012 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
6013 // determine whether a particular HArrayLength is actually a String.length() by
6014 // looking at the type of the input but that requires holding the mutator lock, so
6015 // we prefer to use a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01006016 static constexpr size_t kFlagIsStringLength = kNumberOfGenericPackedBits;
Vladimir Markodce016e2016-04-28 13:10:02 +01006017 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
6018 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6019 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006020};
6021
Vladimir Markofcb503c2016-05-18 12:48:17 +01006022class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006023 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00006024 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
6025 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006026 HBoundsCheck(HInstruction* index,
6027 HInstruction* length,
6028 uint32_t dex_pc,
Vladimir Marko0259c242017-12-04 11:27:47 +00006029 bool is_string_char_at = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306030 : HExpression(kBoundsCheck, index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006031 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(index->GetType()));
Vladimir Marko0259c242017-12-04 11:27:47 +00006032 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006033 SetRawInputAt(0, index);
6034 SetRawInputAt(1, length);
6035 }
6036
Artem Serovcced8ba2017-07-19 18:18:09 +01006037 bool IsClonable() const OVERRIDE { return true; }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006038 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006039 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006040 return true;
6041 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006042
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006043 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006044
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006045 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01006046
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00006047 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006048
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006049 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006050
6051 DECLARE_INSTRUCTION(BoundsCheck);
6052
Artem Serovcced8ba2017-07-19 18:18:09 +01006053 protected:
6054 DEFAULT_COPY_CONSTRUCTOR(BoundsCheck);
6055
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006056 private:
Vladimir Markobd785672018-05-03 17:09:09 +01006057 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006058};
6059
Vladimir Markobd785672018-05-03 17:09:09 +01006060class HSuspendCheck FINAL : public HExpression<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006061 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00006062 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01006063 : HExpression(kSuspendCheck, SideEffects::CanTriggerGC(), dex_pc),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306064 slow_path_(nullptr) {
6065 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006066
Artem Serovcced8ba2017-07-19 18:18:09 +01006067 bool IsClonable() const OVERRIDE { return true; }
6068
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006069 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006070 return true;
6071 }
6072
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006073 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
6074 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006075
6076 DECLARE_INSTRUCTION(SuspendCheck);
6077
Artem Serovcced8ba2017-07-19 18:18:09 +01006078 protected:
6079 DEFAULT_COPY_CONSTRUCTOR(SuspendCheck);
6080
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006081 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006082 // Only used for code generation, in order to share the same slow path between back edges
6083 // of a same loop.
6084 SlowPathCode* slow_path_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006085};
6086
David Srbecky0cf44932015-12-09 14:09:59 +00006087// Pseudo-instruction which provides the native debugger with mapping information.
6088// It ensures that we can generate line number and local variables at this point.
Vladimir Markobd785672018-05-03 17:09:09 +01006089class HNativeDebugInfo : public HExpression<0> {
David Srbecky0cf44932015-12-09 14:09:59 +00006090 public:
6091 explicit HNativeDebugInfo(uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006092 : HExpression<0>(kNativeDebugInfo, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306093 }
David Srbecky0cf44932015-12-09 14:09:59 +00006094
6095 bool NeedsEnvironment() const OVERRIDE {
6096 return true;
6097 }
6098
6099 DECLARE_INSTRUCTION(NativeDebugInfo);
6100
Artem Serovcced8ba2017-07-19 18:18:09 +01006101 protected:
6102 DEFAULT_COPY_CONSTRUCTOR(NativeDebugInfo);
David Srbecky0cf44932015-12-09 14:09:59 +00006103};
6104
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006105/**
6106 * Instruction to load a Class object.
6107 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006108class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006109 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006110 // Determines how to load the Class.
6111 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006112 // We cannot load this class. See HSharpening::SharpenLoadClass.
6113 kInvalid = -1,
6114
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006115 // Use the Class* from the method's own ArtMethod*.
6116 kReferrersClass,
6117
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006118 // Use PC-relative boot image Class* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006119 // Used for boot image classes referenced by boot image code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006120 kBootImageLinkTimePcRelative,
6121
6122 // Use a known boot image Class* address, embedded in the code by the codegen.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006123 // Used for boot image classes referenced by apps in JIT- and AOT-compiled code (non-PIC).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006124 kBootImageAddress,
6125
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006126 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
6127 // Used for boot image classes referenced by apps in AOT-compiled code (PIC).
6128 kBootImageRelRo,
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006129
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006130 // Load from an entry in the .bss section using a PC-relative load.
6131 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
6132 kBssEntry,
6133
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006134 // Load from the root table associated with the JIT compiled method.
6135 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006136
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006137 // Load using a simple runtime call. This is the fall-back load kind when
6138 // the codegen is unable to use another appropriate kind.
6139 kRuntimeCall,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006140
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006141 kLast = kRuntimeCall
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006142 };
6143
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006144 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08006145 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006146 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006147 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006148 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01006149 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00006150 bool needs_access_check)
Vladimir Markobd785672018-05-03 17:09:09 +01006151 : HInstruction(kLoadClass,
6152 DataType::Type::kReference,
6153 SideEffectsForArchRuntimeCalls(),
6154 dex_pc),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006155 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006156 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006157 dex_file_(dex_file),
Vladimir Marko175e7862018-03-27 09:03:13 +00006158 klass_(klass) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01006159 // Referrers class should not need access check. We never inline unverified
6160 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006161 DCHECK(!is_referrers_class || !needs_access_check);
6162
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006163 SetPackedField<LoadKindField>(
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006164 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kRuntimeCall);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006165 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00006166 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006167 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Marko175e7862018-03-27 09:03:13 +00006168 SetPackedFlag<kFlagValidLoadedClassRTI>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006169 }
6170
Artem Serovcced8ba2017-07-19 18:18:09 +01006171 bool IsClonable() const OVERRIDE { return true; }
6172
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006173 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006174
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006175 LoadKind GetLoadKind() const {
6176 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006177 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006178
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006179 bool HasPcRelativeLoadKind() const {
6180 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6181 GetLoadKind() == LoadKind::kBootImageRelRo ||
6182 GetLoadKind() == LoadKind::kBssEntry;
6183 }
6184
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006185 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006186
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006187 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006188
Andreas Gampea5b09a62016-11-17 15:21:22 -08006189 size_t ComputeHashCode() const OVERRIDE { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006190
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01006191 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006192
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006193 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006194 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006195 }
6196
Calin Juravle0ba218d2015-05-19 18:46:01 +01006197 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00006198 // The entrypoint the code generator is going to call does not do
6199 // clinit of the class.
6200 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00006201 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006202 }
6203
6204 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00006205 return NeedsAccessCheck() ||
6206 MustGenerateClinitCheck() ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006207 GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006208 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01006209 }
6210
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006211 bool CanThrow() const OVERRIDE {
Vladimir Marko41559982017-01-06 14:04:23 +00006212 return NeedsAccessCheck() ||
6213 MustGenerateClinitCheck() ||
6214 // If the class is in the boot image, the lookup in the runtime call cannot throw.
6215 // This keeps CanThrow() consistent between non-PIC (using kBootImageAddress) and
6216 // PIC and subsequently avoids a DCE behavior dependency on the PIC option.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006217 ((GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006218 GetLoadKind() == LoadKind::kBssEntry) &&
6219 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006220 }
6221
Calin Juravleacf735c2015-02-12 15:25:22 +00006222 ReferenceTypeInfo GetLoadedClassRTI() {
Vladimir Marko175e7862018-03-27 09:03:13 +00006223 if (GetPackedFlag<kFlagValidLoadedClassRTI>()) {
6224 // Note: The is_exact flag from the return value should not be used.
6225 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact */ true);
6226 } else {
6227 return ReferenceTypeInfo::CreateInvalid();
6228 }
Calin Juravleacf735c2015-02-12 15:25:22 +00006229 }
6230
Vladimir Marko175e7862018-03-27 09:03:13 +00006231 // Loaded class RTI is marked as valid by RTP if the klass_ is admissible.
6232 void SetValidLoadedClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
6233 DCHECK(klass_ != nullptr);
6234 SetPackedFlag<kFlagValidLoadedClassRTI>(true);
Calin Juravleacf735c2015-02-12 15:25:22 +00006235 }
6236
Andreas Gampea5b09a62016-11-17 15:21:22 -08006237 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006238 const DexFile& GetDexFile() const { return dex_file_; }
6239
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006240 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006241 return GetLoadKind() == LoadKind::kRuntimeCall;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006242 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00006243
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006244 static SideEffects SideEffectsForArchRuntimeCalls() {
6245 return SideEffects::CanTriggerGC();
6246 }
6247
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006248 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006249 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006250 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006251 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006252
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006253 void MarkInBootImage() {
6254 SetPackedFlag<kFlagIsInBootImage>(true);
6255 }
6256
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006257 void AddSpecialInput(HInstruction* special_input);
6258
6259 using HInstruction::GetInputRecords; // Keep the const version visible.
6260 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
6261 return ArrayRef<HUserRecord<HInstruction*>>(
6262 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6263 }
6264
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006265 Handle<mirror::Class> GetClass() const {
6266 return klass_;
6267 }
6268
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006269 DECLARE_INSTRUCTION(LoadClass);
6270
Artem Serovcced8ba2017-07-19 18:18:09 +01006271 protected:
6272 DEFAULT_COPY_CONSTRUCTOR(LoadClass);
6273
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006274 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006275 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00006276 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006277 // Whether this instruction must generate the initialization check.
6278 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006279 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006280 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
6281 static constexpr size_t kFieldLoadKindSize =
6282 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
Vladimir Marko175e7862018-03-27 09:03:13 +00006283 static constexpr size_t kFlagValidLoadedClassRTI = kFieldLoadKind + kFieldLoadKindSize;
6284 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagValidLoadedClassRTI + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006285 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006286 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
6287
6288 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006289 return load_kind == LoadKind::kReferrersClass ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006290 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006291 load_kind == LoadKind::kBssEntry ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006292 load_kind == LoadKind::kRuntimeCall;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006293 }
6294
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006295 void SetLoadKindInternal(LoadKind load_kind);
6296
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006297 // The special input is the HCurrentMethod for kRuntimeCall or kReferrersClass.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006298 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006299 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006300 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006301
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006302 // A type index and dex file where the class can be accessed. The dex file can be:
6303 // - The compiling method's dex file if the class is defined there too.
6304 // - The compiling method's dex file if the class is referenced there.
6305 // - The dex file where the class is defined. When the load kind can only be
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006306 // kBssEntry or kRuntimeCall, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08006307 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006308 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006309
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006310 Handle<mirror::Class> klass_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006311};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006312std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
6313
6314// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006315inline void HLoadClass::SetLoadKind(LoadKind load_kind) {
6316 // The load kind should be determined before inserting the instruction to the graph.
6317 DCHECK(GetBlock() == nullptr);
6318 DCHECK(GetEnvironment() == nullptr);
6319 SetPackedField<LoadKindField>(load_kind);
6320 if (load_kind != LoadKind::kRuntimeCall && load_kind != LoadKind::kReferrersClass) {
6321 special_input_ = HUserRecord<HInstruction*>(nullptr);
6322 }
6323 if (!NeedsEnvironment()) {
6324 SetSideEffects(SideEffects::None());
6325 }
6326}
6327
6328// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006329inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006330 // The special input is used for PC-relative loads on some architectures,
6331 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006332 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006333 GetLoadKind() == LoadKind::kBootImageAddress ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006334 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006335 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006336 DCHECK(special_input_.GetInstruction() == nullptr);
6337 special_input_ = HUserRecord<HInstruction*>(special_input);
6338 special_input->AddUseAt(this, 0);
6339}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006340
Vladimir Marko372f10e2016-05-17 16:30:10 +01006341class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006342 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006343 // Determines how to load the String.
6344 enum class LoadKind {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006345 // Use PC-relative boot image String* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006346 // Used for boot image strings referenced by boot image code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006347 kBootImageLinkTimePcRelative,
6348
6349 // Use a known boot image String* address, embedded in the code by the codegen.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006350 // Used for boot image strings referenced by apps in JIT- and AOT-compiled code (non-PIC).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006351 kBootImageAddress,
6352
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006353 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
6354 // Used for boot image strings referenced by apps in AOT-compiled code (PIC).
6355 kBootImageRelRo,
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006356
Vladimir Markoaad75c62016-10-03 08:46:48 +00006357 // Load from an entry in the .bss section using a PC-relative load.
6358 // Used for strings outside boot image when .bss is accessible with a PC-relative load.
6359 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006360
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006361 // Load from the root table associated with the JIT compiled method.
6362 kJitTableAddress,
6363
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006364 // Load using a simple runtime call. This is the fall-back load kind when
6365 // the codegen is unable to use another appropriate kind.
6366 kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006367
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006368 kLast = kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006369 };
6370
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006371 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006372 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006373 const DexFile& dex_file,
6374 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006375 : HInstruction(kLoadString,
6376 DataType::Type::kReference,
6377 SideEffectsForArchRuntimeCalls(),
6378 dex_pc),
Vladimir Marko372f10e2016-05-17 16:30:10 +01006379 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006380 string_index_(string_index),
6381 dex_file_(dex_file) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006382 SetPackedField<LoadKindField>(LoadKind::kRuntimeCall);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006383 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006384
Artem Serovcced8ba2017-07-19 18:18:09 +01006385 bool IsClonable() const OVERRIDE { return true; }
6386
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006387 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006388
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006389 LoadKind GetLoadKind() const {
6390 return GetPackedField<LoadKindField>();
6391 }
6392
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006393 bool HasPcRelativeLoadKind() const {
6394 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6395 GetLoadKind() == LoadKind::kBootImageRelRo ||
6396 GetLoadKind() == LoadKind::kBssEntry;
6397 }
6398
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006399 const DexFile& GetDexFile() const {
6400 return dex_file_;
6401 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006402
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006403 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006404 return string_index_;
6405 }
6406
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006407 Handle<mirror::String> GetString() const {
6408 return string_;
6409 }
6410
6411 void SetString(Handle<mirror::String> str) {
6412 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006413 }
6414
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006415 bool CanBeMoved() const OVERRIDE { return true; }
6416
Vladimir Marko372f10e2016-05-17 16:30:10 +01006417 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006418
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006419 size_t ComputeHashCode() const OVERRIDE { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006420
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006421 // Will call the runtime if we need to load the string through
6422 // the dex cache and the string is not guaranteed to be there yet.
6423 bool NeedsEnvironment() const OVERRIDE {
6424 LoadKind load_kind = GetLoadKind();
Vladimir Marko764d4542017-05-16 10:31:41 +01006425 if (load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006426 load_kind == LoadKind::kBootImageAddress ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006427 load_kind == LoadKind::kBootImageRelRo ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006428 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006429 return false;
6430 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006431 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006432 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006433
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006434 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006435 return GetLoadKind() == LoadKind::kRuntimeCall;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006436 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006437
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07006438 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006439 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006440
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006441 static SideEffects SideEffectsForArchRuntimeCalls() {
6442 return SideEffects::CanTriggerGC();
6443 }
6444
Vladimir Marko372f10e2016-05-17 16:30:10 +01006445 void AddSpecialInput(HInstruction* special_input);
6446
6447 using HInstruction::GetInputRecords; // Keep the const version visible.
6448 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
6449 return ArrayRef<HUserRecord<HInstruction*>>(
6450 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006451 }
6452
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006453 DECLARE_INSTRUCTION(LoadString);
6454
Artem Serovcced8ba2017-07-19 18:18:09 +01006455 protected:
6456 DEFAULT_COPY_CONSTRUCTOR(LoadString);
6457
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006458 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006459 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006460 static constexpr size_t kFieldLoadKindSize =
6461 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
6462 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006463 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006464 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006465
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006466 void SetLoadKindInternal(LoadKind load_kind);
6467
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006468 // The special input is the HCurrentMethod for kRuntimeCall.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006469 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00006470 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01006471 HUserRecord<HInstruction*> special_input_;
6472
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006473 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006474 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006475
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006476 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006477};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006478std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
6479
6480// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006481inline void HLoadString::SetLoadKind(LoadKind load_kind) {
6482 // The load kind should be determined before inserting the instruction to the graph.
6483 DCHECK(GetBlock() == nullptr);
6484 DCHECK(GetEnvironment() == nullptr);
6485 DCHECK_EQ(GetLoadKind(), LoadKind::kRuntimeCall);
6486 SetPackedField<LoadKindField>(load_kind);
6487 if (load_kind != LoadKind::kRuntimeCall) {
6488 special_input_ = HUserRecord<HInstruction*>(nullptr);
6489 }
6490 if (!NeedsEnvironment()) {
6491 SetSideEffects(SideEffects::None());
6492 }
6493}
6494
6495// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006496inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006497 // The special input is used for PC-relative loads on some architectures,
6498 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006499 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006500 GetLoadKind() == LoadKind::kBootImageAddress ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006501 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006502 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01006503 // HLoadString::GetInputRecords() returns an empty array at this point,
6504 // so use the GetInputRecords() from the base class to set the input record.
6505 DCHECK(special_input_.GetInstruction() == nullptr);
6506 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006507 special_input->AddUseAt(this, 0);
6508}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006509
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006510class HLoadMethodHandle FINAL : public HInstruction {
6511 public:
6512 HLoadMethodHandle(HCurrentMethod* current_method,
6513 uint16_t method_handle_idx,
6514 const DexFile& dex_file,
6515 uint32_t dex_pc)
6516 : HInstruction(kLoadMethodHandle,
6517 DataType::Type::kReference,
6518 SideEffectsForArchRuntimeCalls(),
6519 dex_pc),
6520 special_input_(HUserRecord<HInstruction*>(current_method)),
6521 method_handle_idx_(method_handle_idx),
6522 dex_file_(dex_file) {
6523 }
6524
6525 using HInstruction::GetInputRecords; // Keep the const version visible.
6526 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
6527 return ArrayRef<HUserRecord<HInstruction*>>(
6528 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6529 }
6530
6531 bool IsClonable() const OVERRIDE { return true; }
6532
6533 uint16_t GetMethodHandleIndex() const { return method_handle_idx_; }
6534
6535 const DexFile& GetDexFile() const { return dex_file_; }
6536
6537 static SideEffects SideEffectsForArchRuntimeCalls() {
6538 return SideEffects::CanTriggerGC();
6539 }
6540
6541 DECLARE_INSTRUCTION(LoadMethodHandle);
6542
6543 protected:
6544 DEFAULT_COPY_CONSTRUCTOR(LoadMethodHandle);
6545
6546 private:
6547 // The special input is the HCurrentMethod for kRuntimeCall.
6548 HUserRecord<HInstruction*> special_input_;
6549
6550 const uint16_t method_handle_idx_;
6551 const DexFile& dex_file_;
6552};
6553
Orion Hodson18259d72018-04-12 11:18:23 +01006554class HLoadMethodType FINAL : public HInstruction {
6555 public:
6556 HLoadMethodType(HCurrentMethod* current_method,
Orion Hodson06d10a72018-05-14 08:53:38 +01006557 dex::ProtoIndex proto_index,
Orion Hodson18259d72018-04-12 11:18:23 +01006558 const DexFile& dex_file,
6559 uint32_t dex_pc)
6560 : HInstruction(kLoadMethodType,
6561 DataType::Type::kReference,
6562 SideEffectsForArchRuntimeCalls(),
6563 dex_pc),
6564 special_input_(HUserRecord<HInstruction*>(current_method)),
Orion Hodson06d10a72018-05-14 08:53:38 +01006565 proto_index_(proto_index),
Orion Hodson18259d72018-04-12 11:18:23 +01006566 dex_file_(dex_file) {
6567 }
6568
6569 using HInstruction::GetInputRecords; // Keep the const version visible.
6570 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
6571 return ArrayRef<HUserRecord<HInstruction*>>(
6572 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6573 }
6574
6575 bool IsClonable() const OVERRIDE { return true; }
6576
Orion Hodson06d10a72018-05-14 08:53:38 +01006577 dex::ProtoIndex GetProtoIndex() const { return proto_index_; }
Orion Hodson18259d72018-04-12 11:18:23 +01006578
6579 const DexFile& GetDexFile() const { return dex_file_; }
6580
6581 static SideEffects SideEffectsForArchRuntimeCalls() {
6582 return SideEffects::CanTriggerGC();
6583 }
6584
6585 DECLARE_INSTRUCTION(LoadMethodType);
6586
6587 protected:
6588 DEFAULT_COPY_CONSTRUCTOR(LoadMethodType);
6589
6590 private:
6591 // The special input is the HCurrentMethod for kRuntimeCall.
6592 HUserRecord<HInstruction*> special_input_;
6593
Orion Hodson06d10a72018-05-14 08:53:38 +01006594 const dex::ProtoIndex proto_index_;
Orion Hodson18259d72018-04-12 11:18:23 +01006595 const DexFile& dex_file_;
6596};
6597
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006598/**
6599 * Performs an initialization check on its Class object input.
6600 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01006601class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006602 public:
Roland Levillain3887c462015-08-12 18:15:42 +01006603 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07006604 : HExpression(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306605 kClinitCheck,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006606 DataType::Type::kReference,
Mingyao Yang217eb062017-12-11 15:20:07 -08006607 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006608 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006609 SetRawInputAt(0, constant);
6610 }
6611
Artem Serovcced8ba2017-07-19 18:18:09 +01006612 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006613 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006614 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006615 return true;
6616 }
6617
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006618 bool NeedsEnvironment() const OVERRIDE {
6619 // May call runtime to initialize the class.
6620 return true;
6621 }
6622
Nicolas Geoffray729645a2015-11-19 13:29:02 +00006623 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006624
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006625 HLoadClass* GetLoadClass() const {
6626 DCHECK(InputAt(0)->IsLoadClass());
6627 return InputAt(0)->AsLoadClass();
6628 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006629
6630 DECLARE_INSTRUCTION(ClinitCheck);
6631
Artem Serovcced8ba2017-07-19 18:18:09 +01006632
6633 protected:
6634 DEFAULT_COPY_CONSTRUCTOR(ClinitCheck);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006635};
6636
Vladimir Markofcb503c2016-05-18 12:48:17 +01006637class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006638 public:
6639 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006640 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006641 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006642 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006643 bool is_volatile,
6644 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006645 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006646 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006647 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306648 : HExpression(kStaticFieldGet,
6649 field_type,
6650 SideEffects::FieldReadOfType(field_type, is_volatile),
6651 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006652 field_info_(field,
6653 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006654 field_type,
6655 is_volatile,
6656 field_idx,
6657 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006658 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006659 SetRawInputAt(0, cls);
6660 }
6661
Calin Juravle52c48962014-12-16 17:02:57 +00006662
Artem Serovcced8ba2017-07-19 18:18:09 +01006663 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle10c9cbe2014-12-19 10:50:19 +00006664 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006665
Vladimir Marko372f10e2016-05-17 16:30:10 +01006666 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
6667 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006668 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006669 }
6670
6671 size_t ComputeHashCode() const OVERRIDE {
6672 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6673 }
6674
Calin Juravle52c48962014-12-16 17:02:57 +00006675 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006676 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006677 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006678 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006679
Vladimir Marko61b92282017-10-11 13:23:17 +01006680 void SetType(DataType::Type new_type) {
6681 DCHECK(DataType::IsIntegralType(GetType()));
6682 DCHECK(DataType::IsIntegralType(new_type));
6683 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6684 SetPackedField<TypeField>(new_type);
6685 }
6686
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006687 DECLARE_INSTRUCTION(StaticFieldGet);
6688
Artem Serovcced8ba2017-07-19 18:18:09 +01006689 protected:
6690 DEFAULT_COPY_CONSTRUCTOR(StaticFieldGet);
6691
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006692 private:
6693 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006694};
6695
Vladimir Markobd785672018-05-03 17:09:09 +01006696class HStaticFieldSet FINAL : public HExpression<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006697 public:
6698 HStaticFieldSet(HInstruction* cls,
6699 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006700 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006701 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006702 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006703 bool is_volatile,
6704 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006705 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006706 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006707 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006708 : HExpression(kStaticFieldSet,
6709 SideEffects::FieldWriteOfType(field_type, is_volatile),
6710 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006711 field_info_(field,
6712 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006713 field_type,
6714 is_volatile,
6715 field_idx,
6716 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006717 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006718 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006719 SetRawInputAt(0, cls);
6720 SetRawInputAt(1, value);
6721 }
6722
Artem Serovcced8ba2017-07-19 18:18:09 +01006723 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle52c48962014-12-16 17:02:57 +00006724 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006725 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006726 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006727 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006728
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006729 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006730 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6731 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006732
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006733 DECLARE_INSTRUCTION(StaticFieldSet);
6734
Artem Serovcced8ba2017-07-19 18:18:09 +01006735 protected:
6736 DEFAULT_COPY_CONSTRUCTOR(StaticFieldSet);
6737
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006738 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006739 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6740 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6741 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6742 "Too many packed fields.");
6743
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006744 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006745};
6746
Vladimir Markofcb503c2016-05-18 12:48:17 +01006747class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006748 public:
6749 HUnresolvedInstanceFieldGet(HInstruction* obj,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006750 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006751 uint32_t field_index,
6752 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306753 : HExpression(kUnresolvedInstanceFieldGet,
6754 field_type,
6755 SideEffects::AllExceptGCDependency(),
6756 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006757 field_index_(field_index) {
6758 SetRawInputAt(0, obj);
6759 }
6760
Artem Serovcced8ba2017-07-19 18:18:09 +01006761 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006762 bool NeedsEnvironment() const OVERRIDE { return true; }
6763 bool CanThrow() const OVERRIDE { return true; }
6764
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006765 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006766 uint32_t GetFieldIndex() const { return field_index_; }
6767
6768 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6769
Artem Serovcced8ba2017-07-19 18:18:09 +01006770 protected:
6771 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldGet);
6772
Calin Juravlee460d1d2015-09-29 04:52:17 +01006773 private:
6774 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006775};
6776
Vladimir Markobd785672018-05-03 17:09:09 +01006777class HUnresolvedInstanceFieldSet FINAL : public HExpression<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006778 public:
6779 HUnresolvedInstanceFieldSet(HInstruction* obj,
6780 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006781 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006782 uint32_t field_index,
6783 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006784 : HExpression(kUnresolvedInstanceFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006785 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006786 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006787 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006788 SetRawInputAt(0, obj);
6789 SetRawInputAt(1, value);
6790 }
6791
Artem Serovcced8ba2017-07-19 18:18:09 +01006792 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006793 bool NeedsEnvironment() const OVERRIDE { return true; }
6794 bool CanThrow() const OVERRIDE { return true; }
6795
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006796 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006797 uint32_t GetFieldIndex() const { return field_index_; }
6798
6799 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6800
Artem Serovcced8ba2017-07-19 18:18:09 +01006801 protected:
6802 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldSet);
6803
Calin Juravlee460d1d2015-09-29 04:52:17 +01006804 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006805 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6806 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006807 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006808 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6809 kFieldFieldType + kFieldFieldTypeSize;
6810 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6811 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006812 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006813
Calin Juravlee460d1d2015-09-29 04:52:17 +01006814 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006815};
6816
Vladimir Markofcb503c2016-05-18 12:48:17 +01006817class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006818 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006819 HUnresolvedStaticFieldGet(DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006820 uint32_t field_index,
6821 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306822 : HExpression(kUnresolvedStaticFieldGet,
6823 field_type,
6824 SideEffects::AllExceptGCDependency(),
6825 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006826 field_index_(field_index) {
6827 }
6828
Artem Serovcced8ba2017-07-19 18:18:09 +01006829 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006830 bool NeedsEnvironment() const OVERRIDE { return true; }
6831 bool CanThrow() const OVERRIDE { return true; }
6832
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006833 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006834 uint32_t GetFieldIndex() const { return field_index_; }
6835
6836 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6837
Artem Serovcced8ba2017-07-19 18:18:09 +01006838 protected:
6839 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldGet);
6840
Calin Juravlee460d1d2015-09-29 04:52:17 +01006841 private:
6842 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006843};
6844
Vladimir Markobd785672018-05-03 17:09:09 +01006845class HUnresolvedStaticFieldSet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006846 public:
6847 HUnresolvedStaticFieldSet(HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006848 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006849 uint32_t field_index,
6850 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006851 : HExpression(kUnresolvedStaticFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006852 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006853 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006854 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006855 SetRawInputAt(0, value);
6856 }
6857
Artem Serovcced8ba2017-07-19 18:18:09 +01006858 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006859 bool NeedsEnvironment() const OVERRIDE { return true; }
6860 bool CanThrow() const OVERRIDE { return true; }
6861
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006862 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006863 uint32_t GetFieldIndex() const { return field_index_; }
6864
6865 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6866
Artem Serovcced8ba2017-07-19 18:18:09 +01006867 protected:
6868 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldSet);
6869
Calin Juravlee460d1d2015-09-29 04:52:17 +01006870 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006871 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6872 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006873 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006874 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6875 kFieldFieldType + kFieldFieldTypeSize;
6876 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6877 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006878 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006879
Calin Juravlee460d1d2015-09-29 04:52:17 +01006880 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006881};
6882
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006883// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006884class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006885 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006886 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306887 : HExpression(kLoadException, DataType::Type::kReference, SideEffects::None(), dex_pc) {
6888 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006889
David Brazdilbbd733e2015-08-18 17:48:17 +01006890 bool CanBeNull() const OVERRIDE { return false; }
6891
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006892 DECLARE_INSTRUCTION(LoadException);
6893
Artem Serovcced8ba2017-07-19 18:18:09 +01006894 protected:
6895 DEFAULT_COPY_CONSTRUCTOR(LoadException);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006896};
6897
David Brazdilcb1c0552015-08-04 16:22:25 +01006898// Implicit part of move-exception which clears thread-local exception storage.
6899// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markobd785672018-05-03 17:09:09 +01006900class HClearException FINAL : public HExpression<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006901 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006902 explicit HClearException(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01006903 : HExpression(kClearException, SideEffects::AllWrites(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306904 }
David Brazdilcb1c0552015-08-04 16:22:25 +01006905
6906 DECLARE_INSTRUCTION(ClearException);
6907
Artem Serovcced8ba2017-07-19 18:18:09 +01006908 protected:
6909 DEFAULT_COPY_CONSTRUCTOR(ClearException);
David Brazdilcb1c0552015-08-04 16:22:25 +01006910};
6911
Vladimir Markobd785672018-05-03 17:09:09 +01006912class HThrow FINAL : public HExpression<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006913 public:
6914 HThrow(HInstruction* exception, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006915 : HExpression(kThrow, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006916 SetRawInputAt(0, exception);
6917 }
6918
6919 bool IsControlFlow() const OVERRIDE { return true; }
6920
6921 bool NeedsEnvironment() const OVERRIDE { return true; }
6922
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006923 bool CanThrow() const OVERRIDE { return true; }
6924
Aart Bika8b8e9b2018-01-09 11:01:02 -08006925 bool AlwaysThrows() const OVERRIDE { return true; }
6926
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006927 DECLARE_INSTRUCTION(Throw);
6928
Artem Serovcced8ba2017-07-19 18:18:09 +01006929 protected:
6930 DEFAULT_COPY_CONSTRUCTOR(Throw);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006931};
6932
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006933/**
6934 * Implementation strategies for the code generator of a HInstanceOf
6935 * or `HCheckCast`.
6936 */
6937enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006938 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006939 kExactCheck, // Can do a single class compare.
6940 kClassHierarchyCheck, // Can just walk the super class chain.
6941 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6942 kInterfaceCheck, // No optimization yet when checking against an interface.
6943 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006944 kArrayCheck, // No optimization yet when checking against a generic array.
Vladimir Marko175e7862018-03-27 09:03:13 +00006945 kBitstringCheck, // Compare the type check bitstring.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006946 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006947};
6948
Roland Levillain86503782016-02-11 19:07:30 +00006949std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6950
Vladimir Marko175e7862018-03-27 09:03:13 +00006951// Note: HTypeCheckInstruction is just a helper class, not an abstract instruction with an
6952// `IsTypeCheckInstruction()`. (New virtual methods in the HInstruction class have a high cost.)
6953class HTypeCheckInstruction : public HVariableInputSizeInstruction {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006954 public:
Vladimir Marko175e7862018-03-27 09:03:13 +00006955 HTypeCheckInstruction(InstructionKind kind,
Vladimir Markobd785672018-05-03 17:09:09 +01006956 DataType::Type type,
Vladimir Marko175e7862018-03-27 09:03:13 +00006957 HInstruction* object,
6958 HInstruction* target_class_or_null,
6959 TypeCheckKind check_kind,
6960 Handle<mirror::Class> klass,
6961 uint32_t dex_pc,
6962 ArenaAllocator* allocator,
6963 HIntConstant* bitstring_path_to_root,
6964 HIntConstant* bitstring_mask,
6965 SideEffects side_effects)
6966 : HVariableInputSizeInstruction(
6967 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01006968 type,
Vladimir Marko175e7862018-03-27 09:03:13 +00006969 side_effects,
6970 dex_pc,
6971 allocator,
6972 /* number_of_inputs */ check_kind == TypeCheckKind::kBitstringCheck ? 4u : 2u,
6973 kArenaAllocTypeCheckInputs),
6974 klass_(klass) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006975 SetPackedField<TypeCheckKindField>(check_kind);
6976 SetPackedFlag<kFlagMustDoNullCheck>(true);
Vladimir Marko175e7862018-03-27 09:03:13 +00006977 SetPackedFlag<kFlagValidTargetClassRTI>(false);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006978 SetRawInputAt(0, object);
Vladimir Marko175e7862018-03-27 09:03:13 +00006979 SetRawInputAt(1, target_class_or_null);
6980 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_path_to_root != nullptr);
6981 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_mask != nullptr);
6982 if (check_kind == TypeCheckKind::kBitstringCheck) {
6983 DCHECK(target_class_or_null->IsNullConstant());
6984 SetRawInputAt(2, bitstring_path_to_root);
6985 SetRawInputAt(3, bitstring_mask);
6986 } else {
6987 DCHECK(target_class_or_null->IsLoadClass());
6988 }
Vladimir Marko87584542017-12-12 17:47:52 +00006989 }
6990
6991 HLoadClass* GetTargetClass() const {
Vladimir Marko175e7862018-03-27 09:03:13 +00006992 DCHECK_NE(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
Vladimir Marko87584542017-12-12 17:47:52 +00006993 HInstruction* load_class = InputAt(1);
6994 DCHECK(load_class->IsLoadClass());
6995 return load_class->AsLoadClass();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006996 }
6997
Vladimir Marko175e7862018-03-27 09:03:13 +00006998 uint32_t GetBitstringPathToRoot() const {
6999 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7000 HInstruction* path_to_root = InputAt(2);
7001 DCHECK(path_to_root->IsIntConstant());
7002 return static_cast<uint32_t>(path_to_root->AsIntConstant()->GetValue());
7003 }
7004
7005 uint32_t GetBitstringMask() const {
7006 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7007 HInstruction* mask = InputAt(3);
7008 DCHECK(mask->IsIntConstant());
7009 return static_cast<uint32_t>(mask->AsIntConstant()->GetValue());
7010 }
7011
Artem Serovcced8ba2017-07-19 18:18:09 +01007012 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007013 bool CanBeMoved() const OVERRIDE { return true; }
7014
Vladimir Marko175e7862018-03-27 09:03:13 +00007015 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
7016 DCHECK(other->IsInstanceOf() || other->IsCheckCast()) << other->DebugName();
7017 return GetPackedFields() == down_cast<const HTypeCheckInstruction*>(other)->GetPackedFields();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007018 }
7019
Andreas Gampe3fbd3ad2018-03-26 21:14:46 +00007020 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
7021 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
7022 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
7023 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
7024
Vladimir Marko175e7862018-03-27 09:03:13 +00007025 ReferenceTypeInfo GetTargetClassRTI() {
7026 if (GetPackedFlag<kFlagValidTargetClassRTI>()) {
7027 // Note: The is_exact flag from the return value should not be used.
7028 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact */ true);
7029 } else {
7030 return ReferenceTypeInfo::CreateInvalid();
7031 }
7032 }
7033
7034 // Target class RTI is marked as valid by RTP if the klass_ is admissible.
7035 void SetValidTargetClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
7036 DCHECK(klass_ != nullptr);
7037 SetPackedFlag<kFlagValidTargetClassRTI>(true);
7038 }
7039
7040 Handle<mirror::Class> GetClass() const {
7041 return klass_;
7042 }
7043
7044 protected:
7045 DEFAULT_COPY_CONSTRUCTOR(TypeCheckInstruction);
7046
7047 private:
7048 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
7049 static constexpr size_t kFieldTypeCheckKindSize =
7050 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
7051 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
7052 static constexpr size_t kFlagValidTargetClassRTI = kFlagMustDoNullCheck + 1;
7053 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagValidTargetClassRTI + 1;
7054 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7055 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
7056
7057 Handle<mirror::Class> klass_;
7058};
7059
7060class HInstanceOf FINAL : public HTypeCheckInstruction {
7061 public:
7062 HInstanceOf(HInstruction* object,
7063 HInstruction* target_class_or_null,
7064 TypeCheckKind check_kind,
7065 Handle<mirror::Class> klass,
7066 uint32_t dex_pc,
7067 ArenaAllocator* allocator,
7068 HIntConstant* bitstring_path_to_root,
7069 HIntConstant* bitstring_mask)
7070 : HTypeCheckInstruction(kInstanceOf,
Vladimir Markobd785672018-05-03 17:09:09 +01007071 DataType::Type::kBool,
Vladimir Marko175e7862018-03-27 09:03:13 +00007072 object,
7073 target_class_or_null,
7074 check_kind,
7075 klass,
7076 dex_pc,
7077 allocator,
7078 bitstring_path_to_root,
7079 bitstring_mask,
7080 SideEffectsForArchRuntimeCalls(check_kind)) {}
7081
Vladimir Marko175e7862018-03-27 09:03:13 +00007082 bool NeedsEnvironment() const OVERRIDE {
7083 return CanCallRuntime(GetTypeCheckKind());
7084 }
7085
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007086 static bool CanCallRuntime(TypeCheckKind check_kind) {
7087 // Mips currently does runtime calls for any other checks.
7088 return check_kind != TypeCheckKind::kExactCheck;
7089 }
7090
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007091 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007092 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01007093 }
7094
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007095 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007096
Artem Serovcced8ba2017-07-19 18:18:09 +01007097 protected:
7098 DEFAULT_COPY_CONSTRUCTOR(InstanceOf);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007099};
7100
Vladimir Markofcb503c2016-05-18 12:48:17 +01007101class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00007102 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07007103 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307104 : HExpression(kBoundType, DataType::Type::kReference, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00007105 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
7106 SetPackedFlag<kFlagUpperCanBeNull>(true);
7107 SetPackedFlag<kFlagCanBeNull>(true);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007108 DCHECK_EQ(input->GetType(), DataType::Type::kReference);
Calin Juravleb1498f62015-02-16 13:13:29 +00007109 SetRawInputAt(0, input);
7110 }
7111
Artem Serovcced8ba2017-07-19 18:18:09 +01007112 bool IsClonable() const OVERRIDE { return true; }
7113
David Brazdilf5552582015-12-27 13:36:12 +00007114 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007115 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00007116 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00007117 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007118
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007119 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007120 DCHECK(GetUpperCanBeNull() || !can_be_null);
7121 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007122 }
7123
Vladimir Markoa1de9182016-02-25 11:37:38 +00007124 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007125
Calin Juravleb1498f62015-02-16 13:13:29 +00007126 DECLARE_INSTRUCTION(BoundType);
7127
Artem Serovcced8ba2017-07-19 18:18:09 +01007128 protected:
7129 DEFAULT_COPY_CONSTRUCTOR(BoundType);
7130
Calin Juravleb1498f62015-02-16 13:13:29 +00007131 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007132 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
7133 // is false then CanBeNull() cannot be true).
Vladimir Markobd785672018-05-03 17:09:09 +01007134 static constexpr size_t kFlagUpperCanBeNull = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007135 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
7136 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
7137 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7138
Calin Juravleb1498f62015-02-16 13:13:29 +00007139 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007140 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
7141 // It is used to bound the type in cases like:
7142 // if (x instanceof ClassX) {
7143 // // uper_bound_ will be ClassX
7144 // }
David Brazdilf5552582015-12-27 13:36:12 +00007145 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00007146};
7147
Vladimir Marko175e7862018-03-27 09:03:13 +00007148class HCheckCast FINAL : public HTypeCheckInstruction {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007149 public:
7150 HCheckCast(HInstruction* object,
Vladimir Marko175e7862018-03-27 09:03:13 +00007151 HInstruction* target_class_or_null,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007152 TypeCheckKind check_kind,
Vladimir Marko175e7862018-03-27 09:03:13 +00007153 Handle<mirror::Class> klass,
7154 uint32_t dex_pc,
7155 ArenaAllocator* allocator,
7156 HIntConstant* bitstring_path_to_root,
7157 HIntConstant* bitstring_mask)
7158 : HTypeCheckInstruction(kCheckCast,
Vladimir Markobd785672018-05-03 17:09:09 +01007159 DataType::Type::kVoid,
Vladimir Marko175e7862018-03-27 09:03:13 +00007160 object,
7161 target_class_or_null,
7162 check_kind,
7163 klass,
7164 dex_pc,
7165 allocator,
7166 bitstring_path_to_root,
7167 bitstring_mask,
7168 SideEffects::CanTriggerGC()) {}
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007169
7170 bool NeedsEnvironment() const OVERRIDE {
7171 // Instruction may throw a CheckCastError.
7172 return true;
7173 }
7174
7175 bool CanThrow() const OVERRIDE { return true; }
7176
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007177 DECLARE_INSTRUCTION(CheckCast);
7178
Artem Serovcced8ba2017-07-19 18:18:09 +01007179 protected:
7180 DEFAULT_COPY_CONSTRUCTOR(CheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007181};
7182
Andreas Gampe26de38b2016-07-27 17:53:11 -07007183/**
7184 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
7185 * @details We define the combined barrier types that are actually required
7186 * by the Java Memory Model, rather than using exactly the terminology from
7187 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
7188 * primitives. Note that the JSR-133 cookbook generally does not deal with
7189 * store atomicity issues, and the recipes there are not always entirely sufficient.
7190 * The current recipe is as follows:
7191 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
7192 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
7193 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
7194 * -# Use StoreStore barrier after all stores but before return from any constructor whose
7195 * class has final fields.
7196 * -# Use NTStoreStore to order non-temporal stores with respect to all later
7197 * store-to-memory instructions. Only generated together with non-temporal stores.
7198 */
7199enum MemBarrierKind {
7200 kAnyStore,
7201 kLoadAny,
7202 kStoreStore,
7203 kAnyAny,
7204 kNTStoreStore,
7205 kLastBarrierKind = kNTStoreStore
7206};
7207std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
7208
Vladimir Markobd785672018-05-03 17:09:09 +01007209class HMemoryBarrier FINAL : public HExpression<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01007210 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007211 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007212 : HExpression(kMemoryBarrier,
7213 SideEffects::AllWritesAndReads(), // Assume write/read on all fields/arrays.
7214 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007215 SetPackedField<BarrierKindField>(barrier_kind);
7216 }
Calin Juravle27df7582015-04-17 19:12:31 +01007217
Artem Serovcced8ba2017-07-19 18:18:09 +01007218 bool IsClonable() const OVERRIDE { return true; }
7219
Vladimir Markoa1de9182016-02-25 11:37:38 +00007220 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01007221
7222 DECLARE_INSTRUCTION(MemoryBarrier);
7223
Artem Serovcced8ba2017-07-19 18:18:09 +01007224 protected:
7225 DEFAULT_COPY_CONSTRUCTOR(MemoryBarrier);
7226
Calin Juravle27df7582015-04-17 19:12:31 +01007227 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007228 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
7229 static constexpr size_t kFieldBarrierKindSize =
7230 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
7231 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
7232 kFieldBarrierKind + kFieldBarrierKindSize;
7233 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
7234 "Too many packed fields.");
7235 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01007236};
7237
Igor Murashkind01745e2017-04-05 16:40:31 -07007238// A constructor fence orders all prior stores to fields that could be accessed via a final field of
7239// the specified object(s), with respect to any subsequent store that might "publish"
7240// (i.e. make visible) the specified object to another thread.
7241//
7242// JLS 17.5.1 "Semantics of final fields" states that a freeze action happens
7243// for all final fields (that were set) at the end of the invoked constructor.
7244//
7245// The constructor fence models the freeze actions for the final fields of an object
7246// being constructed (semantically at the end of the constructor). Constructor fences
7247// have a per-object affinity; two separate objects being constructed get two separate
7248// constructor fences.
7249//
7250// (Note: that if calling a super-constructor or forwarding to another constructor,
7251// the freezes would happen at the end of *that* constructor being invoked).
7252//
7253// The memory model guarantees that when the object being constructed is "published" after
7254// constructor completion (i.e. escapes the current thread via a store), then any final field
7255// writes must be observable on other threads (once they observe that publication).
7256//
7257// Further, anything written before the freeze, and read by dereferencing through the final field,
7258// must also be visible (so final object field could itself have an object with non-final fields;
7259// yet the freeze must also extend to them).
7260//
7261// Constructor example:
7262//
7263// class HasFinal {
7264// final int field; Optimizing IR for <init>()V:
7265// HasFinal() {
7266// field = 123; HInstanceFieldSet(this, HasFinal.field, 123)
7267// // freeze(this.field); HConstructorFence(this)
7268// } HReturn
7269// }
7270//
7271// HConstructorFence can serve double duty as a fence for new-instance/new-array allocations of
7272// already-initialized classes; in that case the allocation must act as a "default-initializer"
7273// of the object which effectively writes the class pointer "final field".
7274//
7275// For example, we can model default-initialiation as roughly the equivalent of the following:
7276//
7277// class Object {
7278// private final Class header;
7279// }
7280//
7281// Java code: Optimizing IR:
7282//
7283// T new_instance<T>() {
7284// Object obj = allocate_memory(T.class.size); obj = HInvoke(art_quick_alloc_object, T)
7285// obj.header = T.class; // header write is done by above call.
7286// // freeze(obj.header) HConstructorFence(obj)
7287// return (T)obj;
7288// }
7289//
7290// See also:
7291// * CompilerDriver::RequiresConstructorBarrier
7292// * QuasiAtomic::ThreadFenceForConstructor
7293//
7294class HConstructorFence FINAL : public HVariableInputSizeInstruction {
7295 // A fence has variable inputs because the inputs can be removed
7296 // after prepare_for_register_allocation phase.
7297 // (TODO: In the future a fence could freeze multiple objects
7298 // after merging two fences together.)
7299 public:
7300 // `fence_object` is the reference that needs to be protected for correct publication.
7301 //
7302 // It makes sense in the following situations:
7303 // * <init> constructors, it's the "this" parameter (i.e. HParameterValue, s.t. IsThis() == true).
7304 // * new-instance-like instructions, it's the return value (i.e. HNewInstance).
7305 //
7306 // After construction the `fence_object` becomes the 0th input.
7307 // This is not an input in a real sense, but just a convenient place to stash the information
7308 // about the associated object.
7309 HConstructorFence(HInstruction* fence_object,
7310 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007311 ArenaAllocator* allocator)
Igor Murashkind01745e2017-04-05 16:40:31 -07007312 // We strongly suspect there is not a more accurate way to describe the fine-grained reordering
7313 // constraints described in the class header. We claim that these SideEffects constraints
7314 // enforce a superset of the real constraints.
7315 //
7316 // The ordering described above is conservatively modeled with SideEffects as follows:
7317 //
7318 // * To prevent reordering of the publication stores:
7319 // ----> "Reads of objects" is the initial SideEffect.
7320 // * For every primitive final field store in the constructor:
7321 // ----> Union that field's type as a read (e.g. "Read of T") into the SideEffect.
7322 // * If there are any stores to reference final fields in the constructor:
7323 // ----> Use a more conservative "AllReads" SideEffect because any stores to any references
7324 // that are reachable from `fence_object` also need to be prevented for reordering
7325 // (and we do not want to do alias analysis to figure out what those stores are).
7326 //
7327 // In the implementation, this initially starts out as an "all reads" side effect; this is an
7328 // even more conservative approach than the one described above, and prevents all of the
7329 // above reordering without analyzing any of the instructions in the constructor.
7330 //
7331 // If in a later phase we discover that there are no writes to reference final fields,
7332 // we can refine the side effect to a smaller set of type reads (see above constraints).
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307333 : HVariableInputSizeInstruction(kConstructorFence,
7334 SideEffects::AllReads(),
Igor Murashkind01745e2017-04-05 16:40:31 -07007335 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007336 allocator,
Igor Murashkind01745e2017-04-05 16:40:31 -07007337 /* number_of_inputs */ 1,
7338 kArenaAllocConstructorFenceInputs) {
7339 DCHECK(fence_object != nullptr);
7340 SetRawInputAt(0, fence_object);
7341 }
7342
7343 // The object associated with this constructor fence.
7344 //
7345 // (Note: This will be null after the prepare_for_register_allocation phase,
7346 // as all constructor fence inputs are removed there).
7347 HInstruction* GetFenceObject() const {
7348 return InputAt(0);
7349 }
7350
7351 // Find all the HConstructorFence uses (`fence_use`) for `this` and:
7352 // - Delete `fence_use` from `this`'s use list.
7353 // - Delete `this` from `fence_use`'s inputs list.
7354 // - If the `fence_use` is dead, remove it from the graph.
7355 //
7356 // A fence is considered dead once it no longer has any uses
7357 // and all of the inputs are dead.
7358 //
7359 // This must *not* be called during/after prepare_for_register_allocation,
7360 // because that removes all the inputs to the fences but the fence is actually
7361 // still considered live.
Igor Murashkin6ef45672017-08-08 13:59:55 -07007362 //
7363 // Returns how many HConstructorFence instructions were removed from graph.
7364 static size_t RemoveConstructorFences(HInstruction* instruction);
Igor Murashkind01745e2017-04-05 16:40:31 -07007365
Igor Murashkindd018df2017-08-09 10:38:31 -07007366 // Combine all inputs of `this` and `other` instruction and remove
7367 // `other` from the graph.
7368 //
7369 // Inputs are unique after the merge.
7370 //
7371 // Requirement: `this` must not be the same as `other.
7372 void Merge(HConstructorFence* other);
7373
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007374 // Check if this constructor fence is protecting
7375 // an HNewInstance or HNewArray that is also the immediate
7376 // predecessor of `this`.
7377 //
Igor Murashkindd018df2017-08-09 10:38:31 -07007378 // If `ignore_inputs` is true, then the immediate predecessor doesn't need
7379 // to be one of the inputs of `this`.
7380 //
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007381 // Returns the associated HNewArray or HNewInstance,
7382 // or null otherwise.
Igor Murashkindd018df2017-08-09 10:38:31 -07007383 HInstruction* GetAssociatedAllocation(bool ignore_inputs = false);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007384
Igor Murashkind01745e2017-04-05 16:40:31 -07007385 DECLARE_INSTRUCTION(ConstructorFence);
7386
Artem Serovcced8ba2017-07-19 18:18:09 +01007387 protected:
7388 DEFAULT_COPY_CONSTRUCTOR(ConstructorFence);
Igor Murashkind01745e2017-04-05 16:40:31 -07007389};
7390
Vladimir Markobd785672018-05-03 17:09:09 +01007391class HMonitorOperation FINAL : public HExpression<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007392 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007393 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007394 kEnter,
7395 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00007396 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007397 };
7398
7399 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007400 : HExpression(kMonitorOperation,
7401 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
7402 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007403 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007404 SetRawInputAt(0, object);
7405 }
7406
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007407 // Instruction may go into runtime, so we need an environment.
7408 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00007409
7410 bool CanThrow() const OVERRIDE {
7411 // Verifier guarantees that monitor-exit cannot throw.
7412 // This is important because it allows the HGraphBuilder to remove
7413 // a dead throw-catch loop generated for `synchronized` blocks/methods.
7414 return IsEnter();
7415 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007416
Vladimir Markoa1de9182016-02-25 11:37:38 +00007417 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
7418 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007419
7420 DECLARE_INSTRUCTION(MonitorOperation);
7421
Artem Serovcced8ba2017-07-19 18:18:09 +01007422 protected:
7423 DEFAULT_COPY_CONSTRUCTOR(MonitorOperation);
7424
Calin Juravle52c48962014-12-16 17:02:57 +00007425 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007426 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
7427 static constexpr size_t kFieldOperationKindSize =
7428 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
7429 static constexpr size_t kNumberOfMonitorOperationPackedBits =
7430 kFieldOperationKind + kFieldOperationKindSize;
7431 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7432 "Too many packed fields.");
7433 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007434};
7435
Vladimir Markofcb503c2016-05-18 12:48:17 +01007436class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00007437 public:
7438 HSelect(HInstruction* condition,
7439 HInstruction* true_value,
7440 HInstruction* false_value,
7441 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307442 : HExpression(kSelect, HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
David Brazdil74eb1b22015-12-14 11:44:01 +00007443 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
7444
7445 // First input must be `true_value` or `false_value` to allow codegens to
7446 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
7447 // that architectures which implement HSelect as a conditional move also
7448 // will not need to invert the condition.
7449 SetRawInputAt(0, false_value);
7450 SetRawInputAt(1, true_value);
7451 SetRawInputAt(2, condition);
7452 }
7453
Artem Serovcced8ba2017-07-19 18:18:09 +01007454 bool IsClonable() const OVERRIDE { return true; }
David Brazdil74eb1b22015-12-14 11:44:01 +00007455 HInstruction* GetFalseValue() const { return InputAt(0); }
7456 HInstruction* GetTrueValue() const { return InputAt(1); }
7457 HInstruction* GetCondition() const { return InputAt(2); }
7458
7459 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01007460 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
7461 return true;
7462 }
David Brazdil74eb1b22015-12-14 11:44:01 +00007463
7464 bool CanBeNull() const OVERRIDE {
7465 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
7466 }
7467
7468 DECLARE_INSTRUCTION(Select);
7469
Artem Serovcced8ba2017-07-19 18:18:09 +01007470 protected:
7471 DEFAULT_COPY_CONSTRUCTOR(Select);
David Brazdil74eb1b22015-12-14 11:44:01 +00007472};
7473
Vladimir Markof9f64412015-09-02 14:05:49 +01007474class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007475 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01007476 MoveOperands(Location source,
7477 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007478 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007479 HInstruction* instruction)
7480 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007481
7482 Location GetSource() const { return source_; }
7483 Location GetDestination() const { return destination_; }
7484
7485 void SetSource(Location value) { source_ = value; }
7486 void SetDestination(Location value) { destination_ = value; }
7487
7488 // The parallel move resolver marks moves as "in-progress" by clearing the
7489 // destination (but not the source).
7490 Location MarkPending() {
7491 DCHECK(!IsPending());
7492 Location dest = destination_;
7493 destination_ = Location::NoLocation();
7494 return dest;
7495 }
7496
7497 void ClearPending(Location dest) {
7498 DCHECK(IsPending());
7499 destination_ = dest;
7500 }
7501
7502 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00007503 DCHECK(source_.IsValid() || destination_.IsInvalid());
7504 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007505 }
7506
7507 // True if this blocks a move from the given location.
7508 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08007509 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007510 }
7511
7512 // A move is redundant if it's been eliminated, if its source and
7513 // destination are the same, or if its destination is unneeded.
7514 bool IsRedundant() const {
7515 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
7516 }
7517
7518 // We clear both operands to indicate move that's been eliminated.
7519 void Eliminate() {
7520 source_ = destination_ = Location::NoLocation();
7521 }
7522
7523 bool IsEliminated() const {
7524 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
7525 return source_.IsInvalid();
7526 }
7527
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007528 DataType::Type GetType() const { return type_; }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007529
Nicolas Geoffray90218252015-04-15 11:56:51 +01007530 bool Is64BitMove() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007531 return DataType::Is64BitType(type_);
Nicolas Geoffray90218252015-04-15 11:56:51 +01007532 }
7533
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007534 HInstruction* GetInstruction() const { return instruction_; }
7535
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007536 private:
7537 Location source_;
7538 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01007539 // The type this move is for.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007540 DataType::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007541 // The instruction this move is assocatied with. Null when this move is
7542 // for moving an input in the expected locations of user (including a phi user).
7543 // This is only used in debug mode, to ensure we do not connect interval siblings
7544 // in the same parallel move.
7545 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007546};
7547
Roland Levillainc9285912015-12-18 10:38:42 +00007548std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
7549
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007550static constexpr size_t kDefaultNumberOfMoves = 4;
7551
Vladimir Markobd785672018-05-03 17:09:09 +01007552class HParallelMove FINAL : public HExpression<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007553 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007554 explicit HParallelMove(ArenaAllocator* allocator, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007555 : HExpression(kParallelMove, SideEffects::None(), dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007556 moves_(allocator->Adapter(kArenaAllocMoveOperands)) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007557 moves_.reserve(kDefaultNumberOfMoves);
7558 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007559
Nicolas Geoffray90218252015-04-15 11:56:51 +01007560 void AddMove(Location source,
7561 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007562 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007563 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00007564 DCHECK(source.IsValid());
7565 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007566 if (kIsDebugBuild) {
7567 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007568 for (const MoveOperands& move : moves_) {
7569 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007570 // Special case the situation where the move is for the spill slot
7571 // of the instruction.
7572 if ((GetPrevious() == instruction)
7573 || ((GetPrevious() == nullptr)
7574 && instruction->IsPhi()
7575 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007576 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007577 << "Doing parallel moves for the same instruction.";
7578 } else {
7579 DCHECK(false) << "Doing parallel moves for the same instruction.";
7580 }
7581 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00007582 }
7583 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007584 for (const MoveOperands& move : moves_) {
7585 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007586 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01007587 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007588 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007589 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007590 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007591 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007592 }
7593
Vladimir Marko225b6462015-09-28 12:17:40 +01007594 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007595 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007596 }
7597
Vladimir Marko225b6462015-09-28 12:17:40 +01007598 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007599
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01007600 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007601
Artem Serovcced8ba2017-07-19 18:18:09 +01007602 protected:
7603 DEFAULT_COPY_CONSTRUCTOR(ParallelMove);
7604
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007605 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01007606 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007607};
7608
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007609// This instruction computes an intermediate address pointing in the 'middle' of an object. The
7610// result pointer cannot be handled by GC, so extra care is taken to make sure that this value is
7611// never used across anything that can trigger GC.
7612// The result of this instruction is not a pointer in the sense of `DataType::Type::kreference`.
7613// So we represent it by the type `DataType::Type::kInt`.
7614class HIntermediateAddress FINAL : public HExpression<2> {
7615 public:
7616 HIntermediateAddress(HInstruction* base_address, HInstruction* offset, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307617 : HExpression(kIntermediateAddress,
7618 DataType::Type::kInt32,
7619 SideEffects::DependsOnGC(),
7620 dex_pc) {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007621 DCHECK_EQ(DataType::Size(DataType::Type::kInt32),
7622 DataType::Size(DataType::Type::kReference))
7623 << "kPrimInt and kPrimNot have different sizes.";
7624 SetRawInputAt(0, base_address);
7625 SetRawInputAt(1, offset);
7626 }
7627
Artem Serovcced8ba2017-07-19 18:18:09 +01007628 bool IsClonable() const OVERRIDE { return true; }
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007629 bool CanBeMoved() const OVERRIDE { return true; }
7630 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
7631 return true;
7632 }
7633 bool IsActualObject() const OVERRIDE { return false; }
7634
7635 HInstruction* GetBaseAddress() const { return InputAt(0); }
7636 HInstruction* GetOffset() const { return InputAt(1); }
7637
7638 DECLARE_INSTRUCTION(IntermediateAddress);
7639
Artem Serovcced8ba2017-07-19 18:18:09 +01007640 protected:
7641 DEFAULT_COPY_CONSTRUCTOR(IntermediateAddress);
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007642};
7643
7644
Mark Mendell0616ae02015-04-17 12:49:27 -04007645} // namespace art
7646
Aart Bikf8f5a162017-02-06 15:35:29 -08007647#include "nodes_vector.h"
7648
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007649#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
7650#include "nodes_shared.h"
7651#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007652#ifdef ART_ENABLE_CODEGEN_mips
7653#include "nodes_mips.h"
7654#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04007655#ifdef ART_ENABLE_CODEGEN_x86
7656#include "nodes_x86.h"
7657#endif
7658
7659namespace art {
7660
Igor Murashkin6ef45672017-08-08 13:59:55 -07007661class OptimizingCompilerStats;
7662
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007663class HGraphVisitor : public ValueObject {
7664 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007665 explicit HGraphVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7666 : stats_(stats),
7667 graph_(graph) {}
Dave Allison20dfc792014-06-16 20:44:29 -07007668 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007669
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007670 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007671 virtual void VisitBasicBlock(HBasicBlock* block);
7672
Roland Levillain633021e2014-10-01 14:12:25 +01007673 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007674 void VisitInsertionOrder();
7675
Roland Levillain633021e2014-10-01 14:12:25 +01007676 // Visit the graph following dominator tree reverse post-order.
7677 void VisitReversePostOrder();
7678
Nicolas Geoffray787c3072014-03-17 10:20:19 +00007679 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007680
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007681 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007682#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007683 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
7684
7685 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7686
7687#undef DECLARE_VISIT_INSTRUCTION
7688
Igor Murashkin6ef45672017-08-08 13:59:55 -07007689 protected:
7690 OptimizingCompilerStats* stats_;
7691
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007692 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07007693 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007694
7695 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
7696};
7697
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007698class HGraphDelegateVisitor : public HGraphVisitor {
7699 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007700 explicit HGraphDelegateVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7701 : HGraphVisitor(graph, stats) {}
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007702 virtual ~HGraphDelegateVisitor() {}
7703
7704 // Visit functions that delegate to to super class.
7705#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00007706 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007707
7708 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7709
7710#undef DECLARE_VISIT_INSTRUCTION
7711
7712 private:
7713 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
7714};
7715
Artem Serovcced8ba2017-07-19 18:18:09 +01007716// Create a clone of the instruction, insert it into the graph; replace the old one with a new
7717// and remove the old instruction.
7718HInstruction* ReplaceInstrOrPhiByClone(HInstruction* instr);
7719
7720// Create a clone for each clonable instructions/phis and replace the original with the clone.
7721//
7722// Used for testing individual instruction cloner.
7723class CloneAndReplaceInstructionVisitor : public HGraphDelegateVisitor {
7724 public:
7725 explicit CloneAndReplaceInstructionVisitor(HGraph* graph)
Artem Serov7f4aff62017-06-21 17:02:18 +01007726 : HGraphDelegateVisitor(graph), instr_replaced_by_clones_count_(0) {}
Artem Serovcced8ba2017-07-19 18:18:09 +01007727
7728 void VisitInstruction(HInstruction* instruction) OVERRIDE {
7729 if (instruction->IsClonable()) {
7730 ReplaceInstrOrPhiByClone(instruction);
Artem Serov7f4aff62017-06-21 17:02:18 +01007731 instr_replaced_by_clones_count_++;
Artem Serovcced8ba2017-07-19 18:18:09 +01007732 }
7733 }
7734
Artem Serov7f4aff62017-06-21 17:02:18 +01007735 size_t GetInstrReplacedByClonesCount() const { return instr_replaced_by_clones_count_; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007736
7737 private:
Artem Serov7f4aff62017-06-21 17:02:18 +01007738 size_t instr_replaced_by_clones_count_;
Artem Serovcced8ba2017-07-19 18:18:09 +01007739
7740 DISALLOW_COPY_AND_ASSIGN(CloneAndReplaceInstructionVisitor);
7741};
7742
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007743// Iterator over the blocks that art part of the loop. Includes blocks part
7744// of an inner loop. The order in which the blocks are iterated is on their
7745// block id.
7746class HBlocksInLoopIterator : public ValueObject {
7747 public:
7748 explicit HBlocksInLoopIterator(const HLoopInformation& info)
7749 : blocks_in_loop_(info.GetBlocks()),
7750 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
7751 index_(0) {
7752 if (!blocks_in_loop_.IsBitSet(index_)) {
7753 Advance();
7754 }
7755 }
7756
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007757 bool Done() const { return index_ == blocks_.size(); }
7758 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007759 void Advance() {
7760 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007761 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007762 if (blocks_in_loop_.IsBitSet(index_)) {
7763 break;
7764 }
7765 }
7766 }
7767
7768 private:
7769 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007770 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007771 size_t index_;
7772
7773 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
7774};
7775
Mingyao Yang3584bce2015-05-19 16:01:59 -07007776// Iterator over the blocks that art part of the loop. Includes blocks part
7777// of an inner loop. The order in which the blocks are iterated is reverse
7778// post order.
7779class HBlocksInLoopReversePostOrderIterator : public ValueObject {
7780 public:
7781 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
7782 : blocks_in_loop_(info.GetBlocks()),
7783 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
7784 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007785 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007786 Advance();
7787 }
7788 }
7789
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007790 bool Done() const { return index_ == blocks_.size(); }
7791 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07007792 void Advance() {
7793 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007794 for (size_t e = blocks_.size(); index_ < e; ++index_) {
7795 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007796 break;
7797 }
7798 }
7799 }
7800
7801 private:
7802 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007803 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07007804 size_t index_;
7805
7806 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
7807};
7808
Aart Bikf3e61ee2017-04-12 17:09:20 -07007809// Returns int64_t value of a properly typed constant.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007810inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00007811 if (constant->IsIntConstant()) {
7812 return constant->AsIntConstant()->GetValue();
7813 } else if (constant->IsLongConstant()) {
7814 return constant->AsLongConstant()->GetValue();
7815 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00007816 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00007817 return 0;
7818 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007819}
7820
Aart Bikf3e61ee2017-04-12 17:09:20 -07007821// Returns true iff instruction is an integral constant (and sets value on success).
7822inline bool IsInt64AndGet(HInstruction* instruction, /*out*/ int64_t* value) {
7823 if (instruction->IsIntConstant()) {
7824 *value = instruction->AsIntConstant()->GetValue();
7825 return true;
7826 } else if (instruction->IsLongConstant()) {
7827 *value = instruction->AsLongConstant()->GetValue();
7828 return true;
7829 } else if (instruction->IsNullConstant()) {
7830 *value = 0;
7831 return true;
7832 }
7833 return false;
7834}
7835
Aart Bik0148de42017-09-05 09:25:01 -07007836// Returns true iff instruction is the given integral constant.
7837inline bool IsInt64Value(HInstruction* instruction, int64_t value) {
7838 int64_t val = 0;
7839 return IsInt64AndGet(instruction, &val) && val == value;
7840}
7841
7842// Returns true iff instruction is a zero bit pattern.
7843inline bool IsZeroBitPattern(HInstruction* instruction) {
7844 return instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern();
7845}
7846
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01007847// Implement HInstruction::Is##type() for concrete instructions.
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007848#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01007849 inline bool HInstruction::Is##type() const { return GetKind() == k##type; }
7850 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
7851#undef INSTRUCTION_TYPE_CHECK
7852
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01007853// Implement HInstruction::Is##type() for abstract instructions.
Vladimir Markoa90dd512018-05-04 15:04:45 +01007854#define INSTRUCTION_TYPE_CHECK_RESULT(type, super) \
7855 std::is_base_of<BaseType, H##type>::value,
7856#define INSTRUCTION_TYPE_CHECK(type, super) \
7857 inline bool HInstruction::Is##type() const { \
7858 DCHECK_LT(GetKind(), kLastInstructionKind); \
7859 using BaseType = H##type; \
7860 static constexpr bool results[] = { \
7861 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK_RESULT) \
7862 }; \
7863 return results[static_cast<size_t>(GetKind())]; \
7864 }
7865
7866 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
7867#undef INSTRUCTION_TYPE_CHECK
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01007868#undef INSTRUCTION_TYPE_CHECK_RESULT
Vladimir Markoa90dd512018-05-04 15:04:45 +01007869
7870#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007871 inline const H##type* HInstruction::As##type() const { \
7872 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
7873 } \
7874 inline H##type* HInstruction::As##type() { \
7875 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
7876 }
7877
Vladimir Markoa90dd512018-05-04 15:04:45 +01007878 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
7879#undef INSTRUCTION_TYPE_CAST
7880
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007881
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00007882// Create space in `blocks` for adding `number_of_new_blocks` entries
7883// starting at location `at`. Blocks after `at` are moved accordingly.
7884inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
7885 size_t number_of_new_blocks,
7886 size_t after) {
7887 DCHECK_LT(after, blocks->size());
7888 size_t old_size = blocks->size();
7889 size_t new_size = old_size + number_of_new_blocks;
7890 blocks->resize(new_size);
7891 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
7892}
7893
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00007894/*
7895 * Hunt "under the hood" of array lengths (leading to array references),
7896 * null checks (also leading to array references), and new arrays
7897 * (leading to the actual length). This makes it more likely related
7898 * instructions become actually comparable.
7899 */
7900inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
7901 while (instruction->IsArrayLength() ||
7902 instruction->IsNullCheck() ||
7903 instruction->IsNewArray()) {
7904 instruction = instruction->IsNewArray()
7905 ? instruction->AsNewArray()->GetLength()
7906 : instruction->InputAt(0);
7907 }
7908 return instruction;
7909}
7910
Artem Serov21c7e6f2017-07-27 16:04:42 +01007911void RemoveEnvironmentUses(HInstruction* instruction);
7912bool HasEnvironmentUsedByOthers(HInstruction* instruction);
7913void ResetEnvironmentInputRecords(HInstruction* instruction);
7914
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007915} // namespace art
7916
7917#endif // ART_COMPILER_OPTIMIZING_NODES_H_