blob: fedad0c69a4267f17d97bfc1ffb2ad4225d27318 [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"
Andreas Gampe7fbc4a52018-11-28 08:26:47 -080029#include "base/mutex.h"
David Sehrc431b9d2018-03-02 12:01:51 -080030#include "base/quasi_atomic.h"
Vladimir Marko60584552015-09-03 13:35:12 +000031#include "base/stl_util.h"
David Brazdild9c90372016-09-14 16:53:55 +010032#include "base/transform_array_ref.h"
Nicolas Geoffray76d4bb0f32018-09-21 12:58:45 +010033#include "art_method.h"
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010034#include "data_type.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070035#include "deoptimization_kind.h"
David Sehr9e734c72018-01-04 17:56:19 -080036#include "dex/dex_file.h"
37#include "dex/dex_file_types.h"
David Sehr8c0961f2018-01-23 16:11:38 -080038#include "dex/invoke_type.h"
David Sehr312f3b22018-03-19 08:39:26 -070039#include "dex/method_reference.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000040#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000041#include "handle.h"
42#include "handle_scope.h"
Nicolas Geoffray762869d2016-07-15 15:28:35 +010043#include "intrinsics_enum.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010044#include "locations.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000045#include "mirror/class.h"
Orion Hodson18259d72018-04-12 11:18:23 +010046#include "mirror/method_type.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010047#include "offsets.h"
Vladimir Marko46817b82016-03-29 12:21:58 +010048#include "utils/intrusive_forward_list.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000049
50namespace art {
51
Vladimir Markoca6fff82017-10-03 14:49:14 +010052class ArenaStack;
David Brazdil1abb4192015-02-17 18:33:36 +000053class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000054class HBasicBlock;
Igor Murashkind01745e2017-04-05 16:40:31 -070055class HConstructorFence;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010056class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000057class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010058class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000059class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000060class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000061class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000062class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000063class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000064class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000065class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000066class HNullConstant;
Igor Murashkind01745e2017-04-05 16:40:31 -070067class HParameterValue;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010068class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010069class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010070class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010071class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000072class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010073class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000074class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000075
Mathieu Chartier736b5602015-09-02 14:54:11 -070076namespace mirror {
77class DexCache;
78} // namespace mirror
79
Nicolas Geoffray818f2102014-02-18 16:43:35 +000080static const int kDefaultNumberOfBlocks = 8;
81static const int kDefaultNumberOfSuccessors = 2;
82static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010083static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010084static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000085static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000086
Roland Levillain5b5b9312016-03-22 14:57:31 +000087// The maximum (meaningful) distance (31) that can be used in an integer shift/rotate operation.
88static constexpr int32_t kMaxIntShiftDistance = 0x1f;
89// The maximum (meaningful) distance (63) that can be used in a long shift/rotate operation.
90static constexpr int32_t kMaxLongShiftDistance = 0x3f;
Calin Juravle9aec02f2014-11-18 23:06:35 +000091
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010092static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070093static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010094
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010095static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
96
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060097static constexpr uint32_t kNoDexPc = -1;
98
Vladimir Markodbb7f5b2016-03-30 13:23:58 +010099inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
100 // For the purposes of the compiler, the dex files must actually be the same object
101 // if we want to safely treat them as the same. This is especially important for JIT
102 // as custom class loaders can open the same underlying file (or memory) multiple
103 // times and provide different class resolution but no two class loaders should ever
104 // use the same DexFile object - doing so is an unsupported hack that can lead to
105 // all sorts of weird failures.
106 return &lhs == &rhs;
107}
108
Dave Allison20dfc792014-06-16 20:44:29 -0700109enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -0700110 // All types.
111 kCondEQ, // ==
112 kCondNE, // !=
113 // Signed integers and floating-point numbers.
114 kCondLT, // <
115 kCondLE, // <=
116 kCondGT, // >
117 kCondGE, // >=
118 // Unsigned integers.
119 kCondB, // <
120 kCondBE, // <=
121 kCondA, // >
122 kCondAE, // >=
Scott Wakeling2c76e062016-08-31 09:48:54 +0100123 // First and last aliases.
124 kCondFirst = kCondEQ,
125 kCondLast = kCondAE,
Dave Allison20dfc792014-06-16 20:44:29 -0700126};
127
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000128enum GraphAnalysisResult {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000129 kAnalysisSkipped,
David Brazdilbadd8262016-02-02 16:28:56 +0000130 kAnalysisInvalidBytecode,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000131 kAnalysisFailThrowCatchLoop,
132 kAnalysisFailAmbiguousArrayOp,
Nicolas Geoffray0846a8f2018-09-12 15:21:07 +0100133 kAnalysisFailIrreducibleLoopAndStringInit,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000134 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000135};
136
Andreas Gampe9186ced2016-12-12 14:28:21 -0800137template <typename T>
138static inline typename std::make_unsigned<T>::type MakeUnsigned(T x) {
139 return static_cast<typename std::make_unsigned<T>::type>(x);
140}
141
Vladimir Markof9f64412015-09-02 14:05:49 +0100142class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100143 public:
144 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
145
146 void AddInstruction(HInstruction* instruction);
147 void RemoveInstruction(HInstruction* instruction);
148
David Brazdilc3d743f2015-04-22 13:40:50 +0100149 // Insert `instruction` before/after an existing instruction `cursor`.
150 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
151 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
152
Roland Levillain6b469232014-09-25 10:10:38 +0100153 // Return true if this list contains `instruction`.
154 bool Contains(HInstruction* instruction) const;
155
Roland Levillainccc07a92014-09-16 14:48:16 +0100156 // Return true if `instruction1` is found before `instruction2` in
157 // this instruction list and false otherwise. Abort if none
158 // of these instructions is found.
159 bool FoundBefore(const HInstruction* instruction1,
160 const HInstruction* instruction2) const;
161
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000162 bool IsEmpty() const { return first_instruction_ == nullptr; }
163 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
164
165 // Update the block of all instructions to be `block`.
166 void SetBlockOfInstructions(HBasicBlock* block) const;
167
168 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000169 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000170 void Add(const HInstructionList& instruction_list);
171
David Brazdil2d7352b2015-04-20 14:52:42 +0100172 // Return the number of instructions in the list. This is an expensive operation.
173 size_t CountSize() const;
174
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100175 private:
176 HInstruction* first_instruction_;
177 HInstruction* last_instruction_;
178
179 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000180 friend class HGraph;
181 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100182 friend class HInstructionIterator;
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +0000183 friend class HInstructionIteratorHandleChanges;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100184 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100185
186 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
187};
188
David Brazdil4833f5a2015-12-16 10:37:39 +0000189class ReferenceTypeInfo : ValueObject {
190 public:
191 typedef Handle<mirror::Class> TypeHandle;
192
Vladimir Markoa1de9182016-02-25 11:37:38 +0000193 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
194
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700195 static ReferenceTypeInfo Create(TypeHandle type_handle) REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil94ab38f2016-06-21 17:48:19 +0100196 return Create(type_handle, type_handle->CannotBeAssignedFromOtherTypes());
197 }
198
Vladimir Markoa1de9182016-02-25 11:37:38 +0000199 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000200 return ReferenceTypeInfo(type_handle, is_exact);
201 }
202
203 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
204
Vladimir Markof39745e2016-01-26 12:16:55 +0000205 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000206 return handle.GetReference() != nullptr;
207 }
208
Vladimir Marko456307a2016-04-19 14:12:13 +0000209 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000210 return IsValidHandle(type_handle_);
211 }
212
213 bool IsExact() const { return is_exact_; }
214
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700215 bool IsObjectClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000216 DCHECK(IsValid());
217 return GetTypeHandle()->IsObjectClass();
218 }
219
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700220 bool IsStringClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000221 DCHECK(IsValid());
222 return GetTypeHandle()->IsStringClass();
223 }
224
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700225 bool IsObjectArray() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000226 DCHECK(IsValid());
227 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
228 }
229
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700230 bool IsInterface() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000231 DCHECK(IsValid());
232 return GetTypeHandle()->IsInterface();
233 }
234
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700235 bool IsArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000236 DCHECK(IsValid());
237 return GetTypeHandle()->IsArrayClass();
238 }
239
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700240 bool IsPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000241 DCHECK(IsValid());
242 return GetTypeHandle()->IsPrimitiveArray();
243 }
244
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700245 bool IsNonPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000246 DCHECK(IsValid());
247 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
248 }
249
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700250 bool CanArrayHold(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000251 DCHECK(IsValid());
252 if (!IsExact()) return false;
253 if (!IsArrayClass()) return false;
254 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
255 }
256
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700257 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000258 DCHECK(IsValid());
259 if (!IsExact()) return false;
260 if (!IsArrayClass()) return false;
261 if (!rti.IsArrayClass()) return false;
262 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
263 rti.GetTypeHandle()->GetComponentType());
264 }
265
266 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
267
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700268 bool IsSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000269 DCHECK(IsValid());
270 DCHECK(rti.IsValid());
271 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
272 }
273
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700274 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000275 DCHECK(IsValid());
276 DCHECK(rti.IsValid());
277 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
278 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
279 }
280
281 // Returns true if the type information provide the same amount of details.
282 // Note that it does not mean that the instructions have the same actual type
283 // (because the type can be the result of a merge).
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700284 bool IsEqual(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000285 if (!IsValid() && !rti.IsValid()) {
286 // Invalid types are equal.
287 return true;
288 }
289 if (!IsValid() || !rti.IsValid()) {
290 // One is valid, the other not.
291 return false;
292 }
293 return IsExact() == rti.IsExact()
294 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
295 }
296
297 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000298 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
299 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
300 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000301
302 // The class of the object.
303 TypeHandle type_handle_;
304 // Whether or not the type is exact or a superclass of the actual type.
305 // Whether or not we have any information about this type.
306 bool is_exact_;
307};
308
309std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
310
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000311// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100312class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000313 public:
Vladimir Markoca6fff82017-10-03 14:49:14 +0100314 HGraph(ArenaAllocator* allocator,
315 ArenaStack* arena_stack,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100316 const DexFile& dex_file,
317 uint32_t method_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700318 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100319 InvokeType invoke_type = kInvalidInvokeType,
Hans Boehm206348c2018-12-05 11:11:33 -0800320 bool dead_reference_safe = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100321 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000322 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100323 int start_instruction_id = 0)
Vladimir Markoca6fff82017-10-03 14:49:14 +0100324 : allocator_(allocator),
325 arena_stack_(arena_stack),
326 blocks_(allocator->Adapter(kArenaAllocBlockList)),
327 reverse_post_order_(allocator->Adapter(kArenaAllocReversePostOrder)),
328 linear_order_(allocator->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700329 entry_block_(nullptr),
330 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100331 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100332 number_of_vregs_(0),
333 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000334 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400335 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000336 has_try_catch_(false),
Aart Bikb13c65b2017-03-21 20:14:07 -0700337 has_simd_(false),
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800338 has_loops_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000339 has_irreducible_loops_(false),
Hans Boehm206348c2018-12-05 11:11:33 -0800340 dead_reference_safe_(dead_reference_safe),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000341 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000342 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100343 dex_file_(dex_file),
344 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100345 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100346 in_ssa_form_(false),
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800347 number_of_cha_guards_(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700348 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000349 cached_null_constant_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100350 cached_int_constants_(std::less<int32_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
351 cached_float_constants_(std::less<int32_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
352 cached_long_constants_(std::less<int64_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
353 cached_double_constants_(std::less<int64_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000354 cached_current_method_(nullptr),
Nicolas Geoffray53fec082017-03-27 12:56:16 +0100355 art_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000356 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
Mingyao Yang063fc772016-08-02 11:02:54 -0700357 osr_(osr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100358 cha_single_implementation_list_(allocator->Adapter(kArenaAllocCHA)) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100359 blocks_.reserve(kDefaultNumberOfBlocks);
360 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000361
David Brazdilbadd8262016-02-02 16:28:56 +0000362 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700363 void InitializeInexactObjectRTI(VariableSizedHandleScope* handles);
David Brazdilbadd8262016-02-02 16:28:56 +0000364
Vladimir Markoca6fff82017-10-03 14:49:14 +0100365 ArenaAllocator* GetAllocator() const { return allocator_; }
366 ArenaStack* GetArenaStack() const { return arena_stack_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100367 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
368
David Brazdil69ba7b72015-06-23 18:27:30 +0100369 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000370 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100371
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000372 HBasicBlock* GetEntryBlock() const { return entry_block_; }
373 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100374 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000375
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000376 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
377 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000378
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000379 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100380
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100381 void ComputeDominanceInformation();
382 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000383 void ClearLoopInformation();
384 void FindBackEdges(ArenaBitVector* visited);
385 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100386 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100387 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000388
David Brazdil4833f5a2015-12-16 10:37:39 +0000389 // Analyze all natural loops in this graph. Returns a code specifying that it
390 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000391 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000392 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100393
David Brazdilffee3d32015-07-06 11:48:53 +0100394 // Iterate over blocks to compute try block membership. Needs reverse post
395 // order and loop information.
396 void ComputeTryBlockInformation();
397
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000398 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000399 // Returns the instruction to replace the invoke expression or null if the
400 // invoke is for a void method. Note that the caller is responsible for replacing
401 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000402 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000403
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000404 // Update the loop and try membership of `block`, which was spawned from `reference`.
405 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
406 // should be the new back edge.
407 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
408 HBasicBlock* reference,
409 bool replace_if_back_edge);
410
Mingyao Yang3584bce2015-05-19 16:01:59 -0700411 // Need to add a couple of blocks to test if the loop body is entered and
412 // put deoptimization instructions, etc.
413 void TransformLoopHeaderForBCE(HBasicBlock* header);
414
Aart Bikf8f5a162017-02-06 15:35:29 -0800415 // Adds a new loop directly after the loop with the given header and exit.
416 // Returns the new preheader.
417 HBasicBlock* TransformLoopForVectorization(HBasicBlock* header,
418 HBasicBlock* body,
419 HBasicBlock* exit);
420
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000421 // Removes `block` from the graph. Assumes `block` has been disconnected from
422 // other blocks and has no instructions or phis.
423 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000424
David Brazdilfc6a86a2015-06-26 10:33:45 +0000425 // Splits the edge between `block` and `successor` while preserving the
426 // indices in the predecessor/successor lists. If there are multiple edges
427 // between the blocks, the lowest indices are used.
428 // Returns the new block which is empty and has the same dex pc as `successor`.
429 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
430
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100431 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
Artem Serovc73ee372017-07-31 15:08:40 +0100432 void OrderLoopHeaderPredecessors(HBasicBlock* header);
Artem Serov09faaea2017-12-07 14:36:01 +0000433
434 // Transform a loop into a format with a single preheader.
435 //
436 // Each phi in the header should be split: original one in the header should only hold
437 // inputs reachable from the back edges and a single input from the preheader. The newly created
438 // phi in the preheader should collate the inputs from the original multiple incoming blocks.
439 //
440 // Loops in the graph typically have a single preheader, so this method is used to "repair" loops
441 // that no longer have this property.
442 void TransformLoopToSinglePreheaderFormat(HBasicBlock* header);
443
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100444 void SimplifyLoop(HBasicBlock* header);
445
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000446 int32_t GetNextInstructionId() {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100447 CHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000448 return current_instruction_id_++;
449 }
450
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000451 int32_t GetCurrentInstructionId() const {
452 return current_instruction_id_;
453 }
454
455 void SetCurrentInstructionId(int32_t id) {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100456 CHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000457 current_instruction_id_ = id;
458 }
459
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100460 uint16_t GetMaximumNumberOfOutVRegs() const {
461 return maximum_number_of_out_vregs_;
462 }
463
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000464 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
465 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100466 }
467
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100468 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
469 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
470 }
471
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000472 void UpdateTemporariesVRegSlots(size_t slots) {
473 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100474 }
475
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000476 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100477 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000478 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100479 }
480
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100481 void SetNumberOfVRegs(uint16_t number_of_vregs) {
482 number_of_vregs_ = number_of_vregs;
483 }
484
485 uint16_t GetNumberOfVRegs() const {
486 return number_of_vregs_;
487 }
488
489 void SetNumberOfInVRegs(uint16_t value) {
490 number_of_in_vregs_ = value;
491 }
492
David Brazdildee58d62016-04-07 09:54:26 +0000493 uint16_t GetNumberOfInVRegs() const {
494 return number_of_in_vregs_;
495 }
496
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100497 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100498 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100499 return number_of_vregs_ - number_of_in_vregs_;
500 }
501
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100502 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100503 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100504 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100505
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100506 ArrayRef<HBasicBlock* const> GetReversePostOrderSkipEntryBlock() {
507 DCHECK(GetReversePostOrder()[0] == entry_block_);
508 return ArrayRef<HBasicBlock* const>(GetReversePostOrder()).SubArray(1);
509 }
510
511 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetPostOrder() const {
512 return ReverseRange(GetReversePostOrder());
513 }
514
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100515 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100516 return linear_order_;
517 }
518
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100519 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetLinearPostOrder() const {
520 return ReverseRange(GetLinearOrder());
521 }
522
Mark Mendell1152c922015-04-24 17:06:35 -0400523 bool HasBoundsChecks() const {
524 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800525 }
526
Mark Mendell1152c922015-04-24 17:06:35 -0400527 void SetHasBoundsChecks(bool value) {
528 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800529 }
530
Hans Boehm206348c2018-12-05 11:11:33 -0800531 // Is the code known to be robust against eliminating dead references
532 // and the effects of early finalization?
533 bool IsDeadReferenceSafe() const { return dead_reference_safe_; }
534
535 void MarkDeadReferenceUnsafe() { dead_reference_safe_ = false; }
536
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000537 bool IsDebuggable() const { return debuggable_; }
538
David Brazdil8d5b8b22015-03-24 10:51:52 +0000539 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000540 // already, it is created and inserted into the graph. This method is only for
541 // integral types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100542 HConstant* GetConstant(DataType::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000543
544 // TODO: This is problematic for the consistency of reference type propagation
545 // because it can be created anytime after the pass and thus it will be left
546 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600547 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000548
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600549 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
550 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000551 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600552 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
553 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000554 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600555 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
556 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000557 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600558 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
559 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000560 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000561
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100562 HCurrentMethod* GetCurrentMethod();
563
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100564 const DexFile& GetDexFile() const {
565 return dex_file_;
566 }
567
568 uint32_t GetMethodIdx() const {
569 return method_idx_;
570 }
571
Igor Murashkind01745e2017-04-05 16:40:31 -0700572 // Get the method name (without the signature), e.g. "<init>"
573 const char* GetMethodName() const;
574
575 // Get the pretty method name (class + name + optionally signature).
576 std::string PrettyMethod(bool with_signature = true) const;
577
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100578 InvokeType GetInvokeType() const {
579 return invoke_type_;
580 }
581
Mark Mendellc4701932015-04-10 13:18:51 -0400582 InstructionSet GetInstructionSet() const {
583 return instruction_set_;
584 }
585
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000586 bool IsCompilingOsr() const { return osr_; }
587
Mingyao Yang063fc772016-08-02 11:02:54 -0700588 ArenaSet<ArtMethod*>& GetCHASingleImplementationList() {
589 return cha_single_implementation_list_;
590 }
591
592 void AddCHASingleImplementationDependency(ArtMethod* method) {
593 cha_single_implementation_list_.insert(method);
594 }
595
596 bool HasShouldDeoptimizeFlag() const {
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800597 return number_of_cha_guards_ != 0;
Mingyao Yang063fc772016-08-02 11:02:54 -0700598 }
599
David Brazdil77a48ae2015-09-15 12:34:04 +0000600 bool HasTryCatch() const { return has_try_catch_; }
601 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100602
Aart Bikb13c65b2017-03-21 20:14:07 -0700603 bool HasSIMD() const { return has_simd_; }
604 void SetHasSIMD(bool value) { has_simd_ = value; }
605
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800606 bool HasLoops() const { return has_loops_; }
607 void SetHasLoops(bool value) { has_loops_ = value; }
608
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000609 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
610 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
611
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100612 ArtMethod* GetArtMethod() const { return art_method_; }
613 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
614
Roland Levillain5e8d5f02016-10-18 18:03:43 +0100615 // Returns an instruction with the opposite Boolean value from 'cond'.
Mark Mendellf6529172015-11-17 11:16:56 -0500616 // The instruction has been inserted into the graph, either as a constant, or
617 // before cursor.
618 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
619
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000620 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
621
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800622 uint32_t GetNumberOfCHAGuards() { return number_of_cha_guards_; }
623 void SetNumberOfCHAGuards(uint32_t num) { number_of_cha_guards_ = num; }
624 void IncrementNumberOfCHAGuards() { number_of_cha_guards_++; }
625
David Brazdil2d7352b2015-04-20 14:52:42 +0100626 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000627 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100628 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000629
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000630 template <class InstructionType, typename ValueType>
631 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600632 ArenaSafeMap<ValueType, InstructionType*>* cache,
633 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000634 // Try to find an existing constant of the given value.
635 InstructionType* constant = nullptr;
636 auto cached_constant = cache->find(value);
637 if (cached_constant != cache->end()) {
638 constant = cached_constant->second;
639 }
640
641 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100642 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000643 if (constant == nullptr || constant->GetBlock() == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100644 constant = new (allocator_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000645 cache->Overwrite(value, constant);
646 InsertConstant(constant);
647 }
648 return constant;
649 }
650
David Brazdil8d5b8b22015-03-24 10:51:52 +0000651 void InsertConstant(HConstant* instruction);
652
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000653 // Cache a float constant into the graph. This method should only be
654 // called by the SsaBuilder when creating "equivalent" instructions.
655 void CacheFloatConstant(HFloatConstant* constant);
656
657 // See CacheFloatConstant comment.
658 void CacheDoubleConstant(HDoubleConstant* constant);
659
Vladimir Markoca6fff82017-10-03 14:49:14 +0100660 ArenaAllocator* const allocator_;
661 ArenaStack* const arena_stack_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000662
663 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100664 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000665
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100666 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100667 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000668
Aart Bik281c6812016-08-26 11:31:48 -0700669 // List of blocks to perform a linear order tree traversal. Unlike the reverse
670 // post order, this order is not incrementally kept up-to-date.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100671 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100672
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000673 HBasicBlock* entry_block_;
674 HBasicBlock* exit_block_;
675
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100676 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100677 uint16_t maximum_number_of_out_vregs_;
678
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100679 // The number of virtual registers in this method. Contains the parameters.
680 uint16_t number_of_vregs_;
681
682 // The number of virtual registers used by parameters of this method.
683 uint16_t number_of_in_vregs_;
684
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000685 // Number of vreg size slots that the temporaries use (used in baseline compiler).
686 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100687
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800688 // Flag whether there are bounds checks in the graph. We can skip
689 // BCE if it's false. It's only best effort to keep it up to date in
690 // the presence of code elimination so there might be false positives.
Mark Mendell1152c922015-04-24 17:06:35 -0400691 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800692
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800693 // Flag whether there are try/catch blocks in the graph. We will skip
694 // try/catch-related passes if it's false. It's only best effort to keep
695 // it up to date in the presence of code elimination so there might be
696 // false positives.
David Brazdil77a48ae2015-09-15 12:34:04 +0000697 bool has_try_catch_;
698
Aart Bikb13c65b2017-03-21 20:14:07 -0700699 // Flag whether SIMD instructions appear in the graph. If true, the
700 // code generators may have to be more careful spilling the wider
701 // contents of SIMD registers.
702 bool has_simd_;
703
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800704 // Flag whether there are any loops in the graph. We can skip loop
705 // optimization if it's false. It's only best effort to keep it up
706 // to date in the presence of code elimination so there might be false
707 // positives.
708 bool has_loops_;
709
710 // Flag whether there are any irreducible loops in the graph. It's only
711 // best effort to keep it up to date in the presence of code elimination
712 // so there might be false positives.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000713 bool has_irreducible_loops_;
714
Hans Boehm206348c2018-12-05 11:11:33 -0800715 // Is the code known to be robust against eliminating dead references
716 // and the effects of early finalization? If false, dead reference variables
717 // are kept if they might be visible to the garbage collector.
718 // Currently this means that the class was declared to be dead-reference-safe,
719 // the method accesses no reachability-sensitive fields or data, and the same
720 // is true for any methods that were inlined into the current one.
721 bool dead_reference_safe_;
722
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000723 // Indicates whether the graph should be compiled in a way that
724 // ensures full debuggability. If false, we can apply more
725 // aggressive optimizations that may limit the level of debugging.
726 const bool debuggable_;
727
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000728 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000729 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000730
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100731 // The dex file from which the method is from.
732 const DexFile& dex_file_;
733
734 // The method index in the dex file.
735 const uint32_t method_idx_;
736
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100737 // If inlined, this encodes how the callee is being invoked.
738 const InvokeType invoke_type_;
739
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100740 // Whether the graph has been transformed to SSA form. Only used
741 // in debug mode to ensure we are not using properties only valid
742 // for non-SSA form (like the number of temporaries).
743 bool in_ssa_form_;
744
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800745 // Number of CHA guards in the graph. Used to short-circuit the
746 // CHA guard optimization pass when there is no CHA guard left.
747 uint32_t number_of_cha_guards_;
748
Mathieu Chartiere401d142015-04-22 13:56:20 -0700749 const InstructionSet instruction_set_;
750
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000751 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000752 HNullConstant* cached_null_constant_;
753 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000754 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000755 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000756 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000757
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100758 HCurrentMethod* cached_current_method_;
759
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100760 // The ArtMethod this graph is for. Note that for AOT, it may be null,
761 // for example for methods whose declaring class could not be resolved
762 // (such as when the superclass could not be found).
763 ArtMethod* art_method_;
764
David Brazdil4833f5a2015-12-16 10:37:39 +0000765 // Keep the RTI of inexact Object to avoid having to pass stack handle
766 // collection pointer to passes which may create NullConstant.
767 ReferenceTypeInfo inexact_object_rti_;
768
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000769 // Whether we are compiling this graph for on stack replacement: this will
770 // make all loops seen as irreducible and emit special stack maps to mark
771 // compiled code entries which the interpreter can directly jump to.
772 const bool osr_;
773
Mingyao Yang063fc772016-08-02 11:02:54 -0700774 // List of methods that are assumed to have single implementation.
775 ArenaSet<ArtMethod*> cha_single_implementation_list_;
776
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000777 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100778 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000779 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000780 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000781 DISALLOW_COPY_AND_ASSIGN(HGraph);
782};
783
Vladimir Markof9f64412015-09-02 14:05:49 +0100784class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000785 public:
786 HLoopInformation(HBasicBlock* header, HGraph* graph)
787 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100788 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000789 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100790 contains_irreducible_loop_(false),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100791 back_edges_(graph->GetAllocator()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100792 // Make bit vector growable, as the number of blocks may change.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100793 blocks_(graph->GetAllocator(),
794 graph->GetBlocks().size(),
795 true,
796 kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100797 back_edges_.reserve(kDefaultNumberOfBackEdges);
798 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100799
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000800 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100801 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000802
803 void Dump(std::ostream& os);
804
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100805 HBasicBlock* GetHeader() const {
806 return header_;
807 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000808
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000809 void SetHeader(HBasicBlock* block) {
810 header_ = block;
811 }
812
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100813 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
814 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
815 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
816
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000817 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100818 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000819 }
820
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100821 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100822 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100823 }
824
David Brazdil46e2a392015-03-16 17:31:52 +0000825 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100826 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100827 }
828
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000829 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100830 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000831 }
832
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100833 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100834
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100835 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100836 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100837 }
838
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100839 // Returns the lifetime position of the back edge that has the
840 // greatest lifetime position.
841 size_t GetLifetimeEnd() const;
842
843 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100844 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100845 }
846
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000847 // Finds blocks that are part of this loop.
848 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100849
Artem Serov7f4aff62017-06-21 17:02:18 +0100850 // Updates blocks population of the loop and all of its outer' ones recursively after the
851 // population of the inner loop is updated.
852 void PopulateInnerLoopUpwards(HLoopInformation* inner_loop);
853
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100854 // Returns whether this loop information contains `block`.
855 // Note that this loop information *must* be populated before entering this function.
856 bool Contains(const HBasicBlock& block) const;
857
858 // Returns whether this loop information is an inner loop of `other`.
859 // Note that `other` *must* be populated before entering this function.
860 bool IsIn(const HLoopInformation& other) const;
861
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800862 // Returns true if instruction is not defined within this loop.
863 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700864
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100865 const ArenaBitVector& GetBlocks() const { return blocks_; }
866
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000867 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000868 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000869
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000870 void ClearAllBlocks() {
871 blocks_.ClearAllBits();
872 }
873
David Brazdil3f4a5222016-05-06 12:46:21 +0100874 bool HasBackEdgeNotDominatedByHeader() const;
875
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100876 bool IsPopulated() const {
877 return blocks_.GetHighestBitSet() != -1;
878 }
879
Anton Shaminf89381f2016-05-16 16:44:13 +0600880 bool DominatesAllBackEdges(HBasicBlock* block);
881
David Sehrc757dec2016-11-04 15:48:34 -0700882 bool HasExitEdge() const;
883
Artem Serov7f4aff62017-06-21 17:02:18 +0100884 // Resets back edge and blocks-in-loop data.
885 void ResetBasicBlockData() {
886 back_edges_.clear();
887 ClearAllBlocks();
888 }
889
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000890 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100891 // Internal recursive implementation of `Populate`.
892 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100893 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100894
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000895 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100896 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000897 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100898 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100899 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100900 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000901
902 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
903};
904
David Brazdilec16f792015-08-19 15:04:01 +0100905// Stores try/catch information for basic blocks.
906// Note that HGraph is constructed so that catch blocks cannot simultaneously
907// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100908class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100909 public:
910 // Try block information constructor.
911 explicit TryCatchInformation(const HTryBoundary& try_entry)
912 : try_entry_(&try_entry),
913 catch_dex_file_(nullptr),
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +0000914 catch_type_index_(dex::TypeIndex::Invalid()) {
David Brazdilec16f792015-08-19 15:04:01 +0100915 DCHECK(try_entry_ != nullptr);
916 }
917
918 // Catch block information constructor.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800919 TryCatchInformation(dex::TypeIndex catch_type_index, const DexFile& dex_file)
David Brazdilec16f792015-08-19 15:04:01 +0100920 : try_entry_(nullptr),
921 catch_dex_file_(&dex_file),
922 catch_type_index_(catch_type_index) {}
923
924 bool IsTryBlock() const { return try_entry_ != nullptr; }
925
926 const HTryBoundary& GetTryEntry() const {
927 DCHECK(IsTryBlock());
928 return *try_entry_;
929 }
930
931 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
932
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +0000933 bool IsValidTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +0100934 DCHECK(IsCatchBlock());
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +0000935 return catch_type_index_.IsValid();
David Brazdilec16f792015-08-19 15:04:01 +0100936 }
937
Andreas Gampea5b09a62016-11-17 15:21:22 -0800938 dex::TypeIndex GetCatchTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +0100939 DCHECK(IsCatchBlock());
940 return catch_type_index_;
941 }
942
943 const DexFile& GetCatchDexFile() const {
944 DCHECK(IsCatchBlock());
945 return *catch_dex_file_;
946 }
947
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +0000948 void SetInvalidTypeIndex() {
949 catch_type_index_ = dex::TypeIndex::Invalid();
950 }
951
David Brazdilec16f792015-08-19 15:04:01 +0100952 private:
953 // One of possibly several TryBoundary instructions entering the block's try.
954 // Only set for try blocks.
955 const HTryBoundary* try_entry_;
956
957 // Exception type information. Only set for catch blocks.
958 const DexFile* catch_dex_file_;
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +0000959 dex::TypeIndex catch_type_index_;
David Brazdilec16f792015-08-19 15:04:01 +0100960};
961
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100962static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100963static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100964
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000965// A block in a method. Contains the list of instructions represented
966// as a double linked list. Each block knows its predecessors and
967// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100968
Vladimir Markof9f64412015-09-02 14:05:49 +0100969class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000970 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700971 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000972 : graph_(graph),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100973 predecessors_(graph->GetAllocator()->Adapter(kArenaAllocPredecessors)),
974 successors_(graph->GetAllocator()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000975 loop_information_(nullptr),
976 dominator_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100977 dominated_blocks_(graph->GetAllocator()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100978 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100979 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100980 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000981 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000982 try_catch_information_(nullptr) {
983 predecessors_.reserve(kDefaultNumberOfPredecessors);
984 successors_.reserve(kDefaultNumberOfSuccessors);
985 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
986 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000987
Vladimir Marko60584552015-09-03 13:35:12 +0000988 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100989 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000990 }
991
Vladimir Marko60584552015-09-03 13:35:12 +0000992 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100993 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000994 }
995
David Brazdild26a4112015-11-10 11:07:31 +0000996 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
997 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
998
Vladimir Marko60584552015-09-03 13:35:12 +0000999 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
1000 return ContainsElement(successors_, block, start_from);
1001 }
1002
1003 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001004 return dominated_blocks_;
1005 }
1006
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001007 bool IsEntryBlock() const {
1008 return graph_->GetEntryBlock() == this;
1009 }
1010
1011 bool IsExitBlock() const {
1012 return graph_->GetExitBlock() == this;
1013 }
1014
David Brazdil46e2a392015-03-16 17:31:52 +00001015 bool IsSingleGoto() const;
Mads Ager16e52892017-07-14 13:11:37 +02001016 bool IsSingleReturn() const;
Mingyao Yang46721ef2017-10-05 14:45:17 -07001017 bool IsSingleReturnOrReturnVoidAllowingPhis() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +00001018 bool IsSingleTryBoundary() const;
1019
1020 // Returns true if this block emits nothing but a jump.
1021 bool IsSingleJump() const {
1022 HLoopInformation* loop_info = GetLoopInformation();
1023 return (IsSingleGoto() || IsSingleTryBoundary())
1024 // Back edges generate a suspend check.
1025 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
1026 }
David Brazdil46e2a392015-03-16 17:31:52 +00001027
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001028 void AddBackEdge(HBasicBlock* back_edge) {
1029 if (loop_information_ == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001030 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001031 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001032 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001033 loop_information_->AddBackEdge(back_edge);
1034 }
1035
Artem Serov7f4aff62017-06-21 17:02:18 +01001036 // Registers a back edge; if the block was not a loop header before the call associates a newly
1037 // created loop info with it.
1038 //
1039 // Used in SuperblockCloner to preserve LoopInformation object instead of reseting loop
1040 // info for all blocks during back edges recalculation.
1041 void AddBackEdgeWhileUpdating(HBasicBlock* back_edge) {
1042 if (loop_information_ == nullptr || loop_information_->GetHeader() != this) {
1043 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
1044 }
1045 loop_information_->AddBackEdge(back_edge);
1046 }
1047
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001048 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001049 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001050
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001051 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001052 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001053 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001054
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001055 HBasicBlock* GetDominator() const { return dominator_; }
1056 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +00001057 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
1058
1059 void RemoveDominatedBlock(HBasicBlock* block) {
1060 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +01001061 }
Vladimir Marko60584552015-09-03 13:35:12 +00001062
1063 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
1064 ReplaceElement(dominated_blocks_, existing, new_block);
1065 }
1066
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001067 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001068
1069 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001070 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001071 }
1072
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001073 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
1074 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +01001075 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001076 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +01001077 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
1078 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001079
Nicolas Geoffray09aa1472016-01-19 10:52:54 +00001080 HInstruction* GetFirstInstructionDisregardMoves() const;
1081
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001082 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001083 successors_.push_back(block);
1084 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001085 }
1086
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001087 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
1088 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001089 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001090 new_block->predecessors_.push_back(this);
1091 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001092 }
1093
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001094 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
1095 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001096 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001097 new_block->successors_.push_back(this);
1098 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001099 }
1100
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001101 // Insert `this` between `predecessor` and `successor. This method
1102 // preserves the indicies, and will update the first edge found between
1103 // `predecessor` and `successor`.
1104 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
1105 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001106 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +00001107 successor->predecessors_[predecessor_index] = this;
1108 predecessor->successors_[successor_index] = this;
1109 successors_.push_back(successor);
1110 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001111 }
1112
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001113 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001114 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001115 }
1116
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001117 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001118 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001119 }
1120
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001121 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001122 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001123 }
1124
1125 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001126 predecessors_.push_back(block);
1127 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001128 }
1129
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001130 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001131 DCHECK_EQ(predecessors_.size(), 2u);
1132 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001133 }
1134
David Brazdil769c9e52015-04-27 13:54:09 +01001135 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001136 DCHECK_EQ(successors_.size(), 2u);
1137 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +01001138 }
1139
David Brazdilfc6a86a2015-06-26 10:33:45 +00001140 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001141 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001142 }
1143
David Brazdilfc6a86a2015-06-26 10:33:45 +00001144 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001145 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001146 }
1147
David Brazdilfc6a86a2015-06-26 10:33:45 +00001148 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001149 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001150 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001151 }
1152
1153 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001154 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001155 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001156 }
1157
1158 // Returns whether the first occurrence of `predecessor` in the list of
1159 // predecessors is at index `idx`.
1160 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001161 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001162 return GetPredecessorIndexOf(predecessor) == idx;
1163 }
1164
David Brazdild7558da2015-09-22 13:04:14 +01001165 // Create a new block between this block and its predecessors. The new block
1166 // is added to the graph, all predecessor edges are relinked to it and an edge
1167 // is created to `this`. Returns the new empty block. Reverse post order or
1168 // loop and try/catch information are not updated.
1169 HBasicBlock* CreateImmediateDominator();
1170
David Brazdilfc6a86a2015-06-26 10:33:45 +00001171 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001172 // created, latter block. Note that this method will add the block to the
1173 // graph, create a Goto at the end of the former block and will create an edge
1174 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001175 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001176 HBasicBlock* SplitBefore(HInstruction* cursor);
1177
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001178 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001179 // created block. Note that this method just updates raw block information,
1180 // like predecessors, successors, dominators, and instruction list. It does not
1181 // update the graph, reverse post order, loop information, nor make sure the
1182 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001183 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1184
1185 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1186 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001187
1188 // Merge `other` at the end of `this`. Successors and dominated blocks of
1189 // `other` are changed to be successors and dominated blocks of `this`. Note
1190 // that this method does not update the graph, reverse post order, loop
1191 // information, nor make sure the blocks are consistent (for example ending
1192 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001193 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001194
1195 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1196 // of `this` are moved to `other`.
1197 // Note that this method does not update the graph, reverse post order, loop
1198 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001199 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001200 void ReplaceWith(HBasicBlock* other);
1201
Aart Bik6b69e0a2017-01-11 10:20:43 -08001202 // Merges the instructions of `other` at the end of `this`.
1203 void MergeInstructionsWith(HBasicBlock* other);
1204
David Brazdil2d7352b2015-04-20 14:52:42 +01001205 // Merge `other` at the end of `this`. This method updates loops, reverse post
1206 // order, links to predecessors, successors, dominators and deletes the block
1207 // from the graph. The two blocks must be successive, i.e. `this` the only
1208 // predecessor of `other` and vice versa.
1209 void MergeWith(HBasicBlock* other);
1210
1211 // Disconnects `this` from all its predecessors, successors and dominator,
1212 // removes it from all loops it is included in and eventually from the graph.
1213 // The block must not dominate any other block. Predecessors and successors
1214 // are safely updated.
1215 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001216
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001217 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001218 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001219 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001220 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Artem Serovcced8ba2017-07-19 18:18:09 +01001221 // Replace phi `initial` with `replacement` within this block.
1222 void ReplaceAndRemovePhiWith(HPhi* initial, HPhi* replacement);
Roland Levillainccc07a92014-09-16 14:48:16 +01001223 // Replace instruction `initial` with `replacement` within this block.
1224 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1225 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001226 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001227 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001228 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1229 // instruction list. With 'ensure_safety' set to true, it verifies that the
1230 // instruction is not in use and removes it from the use lists of its inputs.
1231 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1232 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001233 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001234
1235 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001236 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001237 }
1238
Roland Levillain6b879dd2014-09-22 17:13:44 +01001239 bool IsLoopPreHeaderFirstPredecessor() const {
1240 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001241 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001242 }
1243
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001244 bool IsFirstPredecessorBackEdge() const {
1245 DCHECK(IsLoopHeader());
1246 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1247 }
1248
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001249 HLoopInformation* GetLoopInformation() const {
1250 return loop_information_;
1251 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001252
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001253 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001254 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001255 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001256 void SetInLoop(HLoopInformation* info) {
1257 if (IsLoopHeader()) {
1258 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001259 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001260 loop_information_ = info;
1261 } else if (loop_information_->Contains(*info->GetHeader())) {
1262 // Block is currently part of an outer loop. Make it part of this inner loop.
1263 // Note that a non loop header having a loop information means this loop information
1264 // has already been populated
1265 loop_information_ = info;
1266 } else {
1267 // Block is part of an inner loop. Do not update the loop information.
1268 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1269 // at this point, because this method is being called while populating `info`.
1270 }
1271 }
1272
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001273 // Raw update of the loop information.
1274 void SetLoopInformation(HLoopInformation* info) {
1275 loop_information_ = info;
1276 }
1277
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001278 bool IsInLoop() const { return loop_information_ != nullptr; }
1279
David Brazdilec16f792015-08-19 15:04:01 +01001280 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1281
1282 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1283 try_catch_information_ = try_catch_information;
1284 }
1285
1286 bool IsTryBlock() const {
1287 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1288 }
1289
1290 bool IsCatchBlock() const {
1291 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1292 }
David Brazdilffee3d32015-07-06 11:48:53 +01001293
1294 // Returns the try entry that this block's successors should have. They will
1295 // be in the same try, unless the block ends in a try boundary. In that case,
1296 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001297 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001298
Aart Bik75ff2c92018-04-21 01:28:11 +00001299 bool HasThrowingInstructions() const;
1300
David Brazdila4b8c212015-05-07 09:59:30 +01001301 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001302 bool Dominates(HBasicBlock* block) const;
1303
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001304 size_t GetLifetimeStart() const { return lifetime_start_; }
1305 size_t GetLifetimeEnd() const { return lifetime_end_; }
1306
1307 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1308 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1309
David Brazdil8d5b8b22015-03-24 10:51:52 +00001310 bool EndsWithControlFlowInstruction() const;
Aart Bik4dc09e72018-05-11 14:40:31 -07001311 bool EndsWithReturn() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001312 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001313 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001314 bool HasSinglePhi() const;
1315
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001316 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001317 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001318 ArenaVector<HBasicBlock*> predecessors_;
1319 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001320 HInstructionList instructions_;
1321 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001322 HLoopInformation* loop_information_;
1323 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001324 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001325 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001326 // The dex program counter of the first instruction of this block.
1327 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001328 size_t lifetime_start_;
1329 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001330 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001331
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001332 friend class HGraph;
1333 friend class HInstruction;
1334
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001335 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1336};
1337
David Brazdilb2bd1c52015-03-25 11:17:37 +00001338// Iterates over the LoopInformation of all loops which contain 'block'
1339// from the innermost to the outermost.
1340class HLoopInformationOutwardIterator : public ValueObject {
1341 public:
1342 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1343 : current_(block.GetLoopInformation()) {}
1344
1345 bool Done() const { return current_ == nullptr; }
1346
1347 void Advance() {
1348 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001349 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001350 }
1351
1352 HLoopInformation* Current() const {
1353 DCHECK(!Done());
1354 return current_;
1355 }
1356
1357 private:
1358 HLoopInformation* current_;
1359
1360 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1361};
1362
Alexandre Ramesef20f712015-06-09 10:29:30 +01001363#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001364 M(Above, Condition) \
1365 M(AboveOrEqual, Condition) \
Aart Bik3dad3412018-02-28 12:01:46 -08001366 M(Abs, UnaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001367 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001368 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001369 M(ArrayGet, Instruction) \
1370 M(ArrayLength, Instruction) \
1371 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001372 M(Below, Condition) \
1373 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001374 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001375 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001376 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001377 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001378 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001379 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001380 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001381 M(Compare, BinaryOperation) \
Igor Murashkind01745e2017-04-05 16:40:31 -07001382 M(ConstructorFence, Instruction) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001383 M(CurrentMethod, Instruction) \
Mingyao Yang063fc772016-08-02 11:02:54 -07001384 M(ShouldDeoptimizeFlag, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001385 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001386 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001387 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001388 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001389 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001390 M(Exit, Instruction) \
1391 M(FloatConstant, Constant) \
1392 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001393 M(GreaterThan, Condition) \
1394 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001395 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001396 M(InstanceFieldGet, Instruction) \
1397 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001398 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001399 M(IntConstant, Constant) \
xueliang.zhonge0eb4832017-10-30 13:43:14 +00001400 M(IntermediateAddress, Instruction) \
Calin Juravle175dc732015-08-25 15:42:32 +01001401 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001402 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001403 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001404 M(InvokeVirtual, Invoke) \
Orion Hodsonac141392017-01-13 11:53:47 +00001405 M(InvokePolymorphic, Invoke) \
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001406 M(InvokeCustom, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001407 M(LessThan, Condition) \
1408 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001409 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001410 M(LoadException, Instruction) \
Orion Hodsondbaa5c72018-05-10 08:22:46 +01001411 M(LoadMethodHandle, Instruction) \
Orion Hodson18259d72018-04-12 11:18:23 +01001412 M(LoadMethodType, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001413 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001414 M(LongConstant, Constant) \
Aart Bik1f8d51b2018-02-15 10:42:37 -08001415 M(Max, Instruction) \
Calin Juravle27df7582015-04-17 19:12:31 +01001416 M(MemoryBarrier, Instruction) \
Aart Bik1f8d51b2018-02-15 10:42:37 -08001417 M(Min, BinaryOperation) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001418 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001419 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001420 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001421 M(Neg, UnaryOperation) \
1422 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001423 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001424 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001425 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001426 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001427 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001428 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001429 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001430 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001431 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001432 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001433 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001434 M(Return, Instruction) \
1435 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001436 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001437 M(Shl, BinaryOperation) \
1438 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001439 M(StaticFieldGet, Instruction) \
1440 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001441 M(UnresolvedInstanceFieldGet, Instruction) \
1442 M(UnresolvedInstanceFieldSet, Instruction) \
1443 M(UnresolvedStaticFieldGet, Instruction) \
1444 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001445 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001446 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001447 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001448 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001449 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001450 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001451 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001452 M(Xor, BinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001453 M(VecReplicateScalar, VecUnaryOperation) \
Aart Bik0148de42017-09-05 09:25:01 -07001454 M(VecExtractScalar, VecUnaryOperation) \
1455 M(VecReduce, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001456 M(VecCnv, VecUnaryOperation) \
1457 M(VecNeg, VecUnaryOperation) \
Aart Bik6daebeb2017-04-03 14:35:41 -07001458 M(VecAbs, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001459 M(VecNot, VecUnaryOperation) \
1460 M(VecAdd, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001461 M(VecHalvingAdd, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001462 M(VecSub, VecBinaryOperation) \
1463 M(VecMul, VecBinaryOperation) \
1464 M(VecDiv, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001465 M(VecMin, VecBinaryOperation) \
1466 M(VecMax, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001467 M(VecAnd, VecBinaryOperation) \
1468 M(VecAndNot, VecBinaryOperation) \
1469 M(VecOr, VecBinaryOperation) \
1470 M(VecXor, VecBinaryOperation) \
Aart Bik29aa0822018-03-08 11:28:00 -08001471 M(VecSaturationAdd, VecBinaryOperation) \
1472 M(VecSaturationSub, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001473 M(VecShl, VecBinaryOperation) \
1474 M(VecShr, VecBinaryOperation) \
1475 M(VecUShr, VecBinaryOperation) \
Aart Bik8de59162017-04-21 09:42:01 -07001476 M(VecSetScalars, VecOperation) \
Artem Serovf34dd202017-04-10 17:41:46 +01001477 M(VecMultiplyAccumulate, VecOperation) \
Aart Bikdbbac8f2017-09-01 13:06:08 -07001478 M(VecSADAccumulate, VecOperation) \
Artem Serovaaac0e32018-08-07 00:52:22 +01001479 M(VecDotProd, VecOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001480 M(VecLoad, VecMemoryOperation) \
1481 M(VecStore, VecMemoryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001482
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001483/*
1484 * Instructions, shared across several (not all) architectures.
1485 */
1486#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1487#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1488#else
1489#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001490 M(BitwiseNegatedRight, Instruction) \
Anton Kirilov74234da2017-01-13 14:42:47 +00001491 M(DataProcWithShifterOp, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001492 M(MultiplyAccumulate, Instruction) \
Artem Serove1811ed2017-04-27 16:50:47 +01001493 M(IntermediateAddressIndex, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001494#endif
1495
Alexandre Ramesef20f712015-06-09 10:29:30 +01001496#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1497
1498#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1499
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001500#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001501#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001502#else
1503#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1504 M(MipsComputeBaseMethodAddress, Instruction) \
Lena Djokica2901602017-09-21 13:50:52 +02001505 M(MipsPackedSwitch, Instruction) \
1506 M(IntermediateArrayAddressIndex, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001507#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001508
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001509#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1510
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001511#ifndef ART_ENABLE_CODEGEN_x86
1512#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1513#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001514#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1515 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001516 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001517 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001518 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001519#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001520
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05301521#if defined(ART_ENABLE_CODEGEN_x86) || defined(ART_ENABLE_CODEGEN_x86_64)
1522#define FOR_EACH_CONCRETE_INSTRUCTION_X86_COMMON(M) \
1523 M(X86AndNot, Instruction) \
1524 M(X86MaskOrResetLeastSetBit, Instruction)
1525#else
1526#define FOR_EACH_CONCRETE_INSTRUCTION_X86_COMMON(M)
1527#endif
1528
Alexandre Ramesef20f712015-06-09 10:29:30 +01001529#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1530
1531#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1532 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001533 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001534 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1535 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001536 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001537 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001538 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05301539 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M) \
1540 FOR_EACH_CONCRETE_INSTRUCTION_X86_COMMON(M)
Alexandre Ramesef20f712015-06-09 10:29:30 +01001541
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001542#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1543 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001544 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001545 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001546 M(BinaryOperation, Instruction) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001547 M(Invoke, Instruction) \
1548 M(VecOperation, Instruction) \
1549 M(VecUnaryOperation, VecOperation) \
1550 M(VecBinaryOperation, VecOperation) \
1551 M(VecMemoryOperation, VecOperation)
Dave Allison20dfc792014-06-16 20:44:29 -07001552
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001553#define FOR_EACH_INSTRUCTION(M) \
1554 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1555 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1556
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001557#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001558FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1559#undef FORWARD_DECLARATION
1560
Vladimir Marko372f10e2016-05-17 16:30:10 +01001561#define DECLARE_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001562 private: \
1563 H##type& operator=(const H##type&) = delete; \
1564 public: \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001565 const char* DebugName() const override { return #type; } \
1566 HInstruction* Clone(ArenaAllocator* arena) const override { \
Artem Serovcced8ba2017-07-19 18:18:09 +01001567 DCHECK(IsClonable()); \
1568 return new (arena) H##type(*this->As##type()); \
1569 } \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001570 void Accept(HGraphVisitor* visitor) override
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001571
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001572#define DECLARE_ABSTRACT_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001573 private: \
1574 H##type& operator=(const H##type&) = delete; \
Vladimir Markoa90dd512018-05-04 15:04:45 +01001575 public:
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001576
Artem Serovcced8ba2017-07-19 18:18:09 +01001577#define DEFAULT_COPY_CONSTRUCTOR(type) \
1578 explicit H##type(const H##type& other) = default;
1579
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001580template <typename T>
Vladimir Marko82b07402017-03-01 19:02:04 +00001581class HUseListNode : public ArenaObject<kArenaAllocUseListNode>,
1582 public IntrusiveForwardListNode<HUseListNode<T>> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001583 public:
Igor Murashkind01745e2017-04-05 16:40:31 -07001584 // Get the instruction which has this use as one of the inputs.
David Brazdiled596192015-01-23 10:39:45 +00001585 T GetUser() const { return user_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001586 // Get the position of the input record that this use corresponds to.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001587 size_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001588 // Set the position of the input record that this use corresponds to.
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001589 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001590
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001591 private:
David Brazdiled596192015-01-23 10:39:45 +00001592 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001593 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001594
1595 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001596 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001597
Vladimir Marko46817b82016-03-29 12:21:58 +01001598 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001599
1600 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1601};
1602
David Brazdiled596192015-01-23 10:39:45 +00001603template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001604using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001605
David Brazdil1abb4192015-02-17 18:33:36 +00001606// This class is used by HEnvironment and HInstruction classes to record the
1607// instructions they use and pointers to the corresponding HUseListNodes kept
1608// by the used instructions.
1609template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001610class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001611 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001612 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1613 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001614
Vladimir Marko46817b82016-03-29 12:21:58 +01001615 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1616 : HUserRecord(old_record.instruction_, before_use_node) {}
1617 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1618 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001619 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001620 }
1621
1622 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001623 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1624 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001625
1626 private:
1627 // Instruction used by the user.
1628 HInstruction* instruction_;
1629
Vladimir Marko46817b82016-03-29 12:21:58 +01001630 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1631 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001632};
1633
Vladimir Markoe9004912016-06-16 16:50:52 +01001634// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1635// This is used for HInstruction::GetInputs() to return a container wrapper providing
1636// HInstruction* values even though the underlying container has HUserRecord<>s.
1637struct HInputExtractor {
1638 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1639 return record.GetInstruction();
1640 }
1641 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1642 return record.GetInstruction();
1643 }
1644};
1645
1646using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1647using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1648
Aart Bik854a02b2015-07-14 16:07:00 -07001649/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001650 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001651 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001652 * For write/read dependences on fields/arrays, the dependence analysis uses
1653 * type disambiguation (e.g. a float field write cannot modify the value of an
1654 * integer field read) and the access type (e.g. a reference array write cannot
1655 * modify the value of a reference field read [although it may modify the
1656 * reference fetch prior to reading the field, which is represented by its own
1657 * write/read dependence]). The analysis makes conservative points-to
1658 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1659 * the same, and any reference read depends on any reference read without
1660 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001661 *
Artem Serovd1aa7d02018-06-22 11:35:46 +01001662 * kDependsOnGCBit is defined in the following way: instructions with kDependsOnGCBit must not be
1663 * alive across the point where garbage collection might happen.
1664 *
1665 * Note: Instructions with kCanTriggerGCBit do not depend on each other.
1666 *
1667 * kCanTriggerGCBit must be used for instructions for which GC might happen on the path across
1668 * those instructions from the compiler perspective (between this instruction and the next one
1669 * in the IR).
1670 *
1671 * Note: Instructions which can cause GC only on a fatal slow path do not need
1672 * kCanTriggerGCBit as the execution never returns to the instruction next to the exceptional
1673 * one. However the execution may return to compiled code if there is a catch block in the
1674 * current method; for this purpose the TryBoundary exit instruction has kCanTriggerGCBit
1675 * set.
1676 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001677 * The internal representation uses 38-bit and is described in the table below.
1678 * The first line indicates the side effect, and for field/array accesses the
1679 * second line indicates the type of the access (in the order of the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001680 * DataType::Type enum).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001681 * The two numbered lines below indicate the bit position in the bitfield (read
1682 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001683 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001684 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1685 * +-------------+---------+---------+--------------+---------+---------+
1686 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1687 * | 3 |333333322|222222221| 1 |111111110|000000000|
1688 * | 7 |654321098|765432109| 8 |765432109|876543210|
1689 *
1690 * Note that, to ease the implementation, 'changes' bits are least significant
1691 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001692 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001693class SideEffects : public ValueObject {
1694 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001695 SideEffects() : flags_(0) {}
1696
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001697 static SideEffects None() {
1698 return SideEffects(0);
1699 }
1700
1701 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001702 return SideEffects(kAllChangeBits | kAllDependOnBits);
1703 }
1704
1705 static SideEffects AllChanges() {
1706 return SideEffects(kAllChangeBits);
1707 }
1708
1709 static SideEffects AllDependencies() {
1710 return SideEffects(kAllDependOnBits);
1711 }
1712
1713 static SideEffects AllExceptGCDependency() {
1714 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1715 }
1716
1717 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001718 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001719 }
1720
Aart Bik34c3ba92015-07-20 14:08:59 -07001721 static SideEffects AllWrites() {
1722 return SideEffects(kAllWrites);
1723 }
1724
1725 static SideEffects AllReads() {
1726 return SideEffects(kAllReads);
1727 }
1728
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001729 static SideEffects FieldWriteOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001730 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001731 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001732 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001733 }
1734
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001735 static SideEffects ArrayWriteOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001736 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001737 }
1738
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001739 static SideEffects FieldReadOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001740 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001741 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001742 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001743 }
1744
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001745 static SideEffects ArrayReadOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001746 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001747 }
1748
Artem Serovd1aa7d02018-06-22 11:35:46 +01001749 // Returns whether GC might happen across this instruction from the compiler perspective so
1750 // the next instruction in the IR would see that.
1751 //
1752 // See the SideEffect class comments.
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001753 static SideEffects CanTriggerGC() {
1754 return SideEffects(1ULL << kCanTriggerGCBit);
1755 }
1756
Artem Serovd1aa7d02018-06-22 11:35:46 +01001757 // Returns whether the instruction must not be alive across a GC point.
1758 //
1759 // See the SideEffect class comments.
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001760 static SideEffects DependsOnGC() {
1761 return SideEffects(1ULL << kDependsOnGCBit);
1762 }
1763
Aart Bik854a02b2015-07-14 16:07:00 -07001764 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001765 SideEffects Union(SideEffects other) const {
1766 return SideEffects(flags_ | other.flags_);
1767 }
1768
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001769 SideEffects Exclusion(SideEffects other) const {
1770 return SideEffects(flags_ & ~other.flags_);
1771 }
1772
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001773 void Add(SideEffects other) {
1774 flags_ |= other.flags_;
1775 }
1776
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001777 bool Includes(SideEffects other) const {
1778 return (other.flags_ & flags_) == other.flags_;
1779 }
1780
1781 bool HasSideEffects() const {
1782 return (flags_ & kAllChangeBits);
1783 }
1784
1785 bool HasDependencies() const {
1786 return (flags_ & kAllDependOnBits);
1787 }
1788
1789 // Returns true if there are no side effects or dependencies.
1790 bool DoesNothing() const {
1791 return flags_ == 0;
1792 }
1793
Aart Bik854a02b2015-07-14 16:07:00 -07001794 // Returns true if something is written.
1795 bool DoesAnyWrite() const {
1796 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001797 }
1798
Aart Bik854a02b2015-07-14 16:07:00 -07001799 // Returns true if something is read.
1800 bool DoesAnyRead() const {
1801 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001802 }
1803
Aart Bik854a02b2015-07-14 16:07:00 -07001804 // Returns true if potentially everything is written and read
1805 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001806 bool DoesAllReadWrite() const {
1807 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1808 }
1809
Aart Bik854a02b2015-07-14 16:07:00 -07001810 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001811 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001812 }
1813
Roland Levillain0d5a2812015-11-13 10:07:31 +00001814 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001815 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001816 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1817 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001818 }
1819
1820 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001821 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001822 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001823 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001824 for (int s = kLastBit; s >= 0; s--) {
1825 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1826 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1827 // This is a bit for the GC side effect.
1828 if (current_bit_is_set) {
1829 flags += "GC";
1830 }
Aart Bik854a02b2015-07-14 16:07:00 -07001831 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001832 } else {
1833 // This is a bit for the array/field analysis.
1834 // The underscore character stands for the 'can trigger GC' bit.
1835 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1836 if (current_bit_is_set) {
1837 flags += kDebug[s];
1838 }
1839 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1840 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1841 flags += "|";
1842 }
1843 }
Aart Bik854a02b2015-07-14 16:07:00 -07001844 }
1845 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001846 }
1847
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001848 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001849
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001850 private:
1851 static constexpr int kFieldArrayAnalysisBits = 9;
1852
1853 static constexpr int kFieldWriteOffset = 0;
1854 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1855 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1856 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1857
1858 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1859
1860 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1861 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1862 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1863 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1864
1865 static constexpr int kLastBit = kDependsOnGCBit;
1866 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1867
1868 // Aliases.
1869
1870 static_assert(kChangeBits == kDependOnBits,
1871 "the 'change' bits should match the 'depend on' bits.");
1872
1873 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1874 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1875 static constexpr uint64_t kAllWrites =
1876 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1877 static constexpr uint64_t kAllReads =
1878 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001879
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001880 // Translates type to bit flag. The type must correspond to a Java type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001881 static uint64_t TypeFlag(DataType::Type type, int offset) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001882 int shift;
1883 switch (type) {
1884 case DataType::Type::kReference: shift = 0; break;
1885 case DataType::Type::kBool: shift = 1; break;
1886 case DataType::Type::kInt8: shift = 2; break;
1887 case DataType::Type::kUint16: shift = 3; break;
1888 case DataType::Type::kInt16: shift = 4; break;
1889 case DataType::Type::kInt32: shift = 5; break;
1890 case DataType::Type::kInt64: shift = 6; break;
1891 case DataType::Type::kFloat32: shift = 7; break;
1892 case DataType::Type::kFloat64: shift = 8; break;
1893 default:
1894 LOG(FATAL) << "Unexpected data type " << type;
1895 UNREACHABLE();
1896 }
Aart Bik854a02b2015-07-14 16:07:00 -07001897 DCHECK_LE(kFieldWriteOffset, shift);
1898 DCHECK_LT(shift, kArrayWriteOffset);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001899 return UINT64_C(1) << (shift + offset);
Aart Bik854a02b2015-07-14 16:07:00 -07001900 }
1901
1902 // Private constructor on direct flags value.
1903 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1904
1905 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001906};
1907
David Brazdiled596192015-01-23 10:39:45 +00001908// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001909class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001910 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001911 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
Mingyao Yang01b47b02017-02-03 12:09:57 -08001912 size_t number_of_vregs,
1913 ArtMethod* method,
1914 uint32_t dex_pc,
1915 HInstruction* holder)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001916 : vregs_(number_of_vregs, allocator->Adapter(kArenaAllocEnvironmentVRegs)),
1917 locations_(allocator->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001918 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001919 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001920 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001921 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001922 }
1923
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001924 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
1925 const HEnvironment& to_copy,
1926 HInstruction* holder)
1927 : HEnvironment(allocator,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001928 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001929 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001930 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001931 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001932
Vladimir Markoec32f642017-06-02 10:51:55 +01001933 void AllocateLocations() {
1934 DCHECK(locations_.empty());
1935 locations_.resize(vregs_.size());
1936 }
1937
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001938 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001939 if (parent_ != nullptr) {
1940 parent_->SetAndCopyParentChain(allocator, parent);
1941 } else {
1942 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1943 parent_->CopyFrom(parent);
1944 if (parent->GetParent() != nullptr) {
1945 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1946 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001947 }
David Brazdiled596192015-01-23 10:39:45 +00001948 }
1949
Vladimir Marko69d310e2017-10-09 14:12:23 +01001950 void CopyFrom(ArrayRef<HInstruction* const> locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001951 void CopyFrom(HEnvironment* environment);
1952
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001953 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1954 // input to the loop phi instead. This is for inserting instructions that
1955 // require an environment (like HDeoptimization) in the loop pre-header.
1956 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001957
1958 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001959 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001960 }
1961
1962 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001963 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001964 }
1965
David Brazdil1abb4192015-02-17 18:33:36 +00001966 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001967
Artem Serovca210e32017-12-15 13:43:20 +00001968 // Replaces the input at the position 'index' with the replacement; the replacement and old
1969 // input instructions' env_uses_ lists are adjusted. The function works similar to
1970 // HInstruction::ReplaceInput.
1971 void ReplaceInput(HInstruction* replacement, size_t index);
1972
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001973 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001974
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001975 HEnvironment* GetParent() const { return parent_; }
1976
1977 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001978 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001979 }
1980
1981 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001982 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001983 }
1984
1985 uint32_t GetDexPc() const {
1986 return dex_pc_;
1987 }
1988
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001989 ArtMethod* GetMethod() const {
1990 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001991 }
1992
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001993 HInstruction* GetHolder() const {
1994 return holder_;
1995 }
1996
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001997
1998 bool IsFromInlinedInvoke() const {
1999 return GetParent() != nullptr;
2000 }
2001
David Brazdiled596192015-01-23 10:39:45 +00002002 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002003 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
2004 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002005 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00002006 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002007 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00002008
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01002009 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002010 HInstruction* const holder_;
2011
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01002012 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00002013
2014 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
2015};
2016
Vladimir Markof9f64412015-09-02 14:05:49 +01002017class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002018 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302019#define DECLARE_KIND(type, super) k##type,
2020 enum InstructionKind {
Vladimir Markoe3946222018-05-04 14:18:47 +01002021 FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_KIND)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302022 kLastInstructionKind
2023 };
2024#undef DECLARE_KIND
2025
2026 HInstruction(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01002027 : HInstruction(kind, DataType::Type::kVoid, side_effects, dex_pc) {}
2028
2029 HInstruction(InstructionKind kind, DataType::Type type, SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002030 : previous_(nullptr),
2031 next_(nullptr),
2032 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002033 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002034 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002035 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002036 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002037 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002038 locations_(nullptr),
2039 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002040 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00002041 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002042 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302043 SetPackedField<InstructionKindField>(kind);
Vladimir Markobd785672018-05-03 17:09:09 +01002044 SetPackedField<TypeField>(type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002045 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
2046 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002047
Dave Allison20dfc792014-06-16 20:44:29 -07002048 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002049
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002050
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002051 HInstruction* GetNext() const { return next_; }
2052 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002053
Calin Juravle77520bc2015-01-12 18:45:46 +00002054 HInstruction* GetNextDisregardingMoves() const;
2055 HInstruction* GetPreviousDisregardingMoves() const;
2056
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002057 HBasicBlock* GetBlock() const { return block_; }
Vladimir Markoca6fff82017-10-03 14:49:14 +01002058 ArenaAllocator* GetAllocator() const { return block_->GetGraph()->GetAllocator(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002059 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002060 bool IsInBlock() const { return block_ != nullptr; }
2061 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00002062 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
2063 bool IsIrreducibleLoopHeaderPhi() const {
2064 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
2065 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002066
Vladimir Marko372f10e2016-05-17 16:30:10 +01002067 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
2068
2069 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
2070 // One virtual method is enough, just const_cast<> and then re-add the const.
2071 return ArrayRef<const HUserRecord<HInstruction*>>(
2072 const_cast<HInstruction*>(this)->GetInputRecords());
2073 }
2074
Vladimir Markoe9004912016-06-16 16:50:52 +01002075 HInputsRef GetInputs() {
2076 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002077 }
2078
Vladimir Markoe9004912016-06-16 16:50:52 +01002079 HConstInputsRef GetInputs() const {
2080 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002081 }
2082
2083 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00002084 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002085
Aart Bik2767f4b2016-10-28 15:03:53 -07002086 bool HasInput(HInstruction* input) const {
2087 for (const HInstruction* i : GetInputs()) {
2088 if (i == input) {
2089 return true;
2090 }
2091 }
2092 return false;
2093 }
2094
Vladimir Marko372f10e2016-05-17 16:30:10 +01002095 void SetRawInputAt(size_t index, HInstruction* input) {
2096 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
2097 }
2098
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002099 virtual void Accept(HGraphVisitor* visitor) = 0;
2100 virtual const char* DebugName() const = 0;
2101
Vladimir Markobd785672018-05-03 17:09:09 +01002102 DataType::Type GetType() const {
2103 return TypeField::Decode(GetPackedFields());
2104 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002105
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002106 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002107
2108 uint32_t GetDexPc() const { return dex_pc_; }
2109
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002110 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01002111
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002112 // Can the instruction throw?
2113 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
2114 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01002115 virtual bool CanThrow() const { return false; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08002116
2117 // Does the instruction always throw an exception unconditionally?
2118 virtual bool AlwaysThrows() const { return false; }
2119
David Brazdilec16f792015-08-19 15:04:01 +01002120 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002121
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002122 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002123 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002124
Calin Juravle10e244f2015-01-26 18:54:32 +00002125 // Does not apply for all instructions, but having this at top level greatly
2126 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00002127 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002128 virtual bool CanBeNull() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002129 DCHECK_EQ(GetType(), DataType::Type::kReference) << "CanBeNull only applies to reference types";
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002130 return true;
2131 }
Calin Juravle10e244f2015-01-26 18:54:32 +00002132
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002133 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01002134 return false;
2135 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002136
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01002137 // If this instruction will do an implicit null check, return the `HNullCheck` associated
2138 // with it. Otherwise return null.
2139 HNullCheck* GetImplicitNullCheck() const {
2140 // Find the first previous instruction which is not a move.
2141 HInstruction* first_prev_not_move = GetPreviousDisregardingMoves();
2142 if (first_prev_not_move != nullptr &&
2143 first_prev_not_move->IsNullCheck() &&
2144 first_prev_not_move->IsEmittedAtUseSite()) {
2145 return first_prev_not_move->AsNullCheck();
2146 }
2147 return nullptr;
2148 }
2149
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002150 virtual bool IsActualObject() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002151 return GetType() == DataType::Type::kReference;
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002152 }
2153
Calin Juravle2e768302015-07-28 14:41:11 +00002154 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00002155
Calin Juravle61d544b2015-02-23 16:46:57 +00002156 ReferenceTypeInfo GetReferenceTypeInfo() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002157 DCHECK_EQ(GetType(), DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002158 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00002159 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00002160 }
Calin Juravleacf735c2015-02-12 15:25:22 +00002161
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002162 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00002163 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002164 // Note: fixup_end remains valid across push_front().
2165 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
2166 HUseListNode<HInstruction*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002167 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HInstruction*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002168 uses_.push_front(*new_node);
2169 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002170 }
2171
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002172 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01002173 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002174 // Note: env_fixup_end remains valid across push_front().
2175 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
2176 HUseListNode<HEnvironment*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002177 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HEnvironment*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002178 env_uses_.push_front(*new_node);
2179 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002180 }
2181
David Brazdil1abb4192015-02-17 18:33:36 +00002182 void RemoveAsUserOfInput(size_t input) {
2183 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002184 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2185 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2186 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00002187 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002188
Vladimir Marko372f10e2016-05-17 16:30:10 +01002189 void RemoveAsUserOfAllInputs() {
2190 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2191 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2192 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2193 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
2194 }
2195 }
2196
David Brazdil1abb4192015-02-17 18:33:36 +00002197 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2198 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002199
Vladimir Marko46817b82016-03-29 12:21:58 +01002200 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2201 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2202 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002203 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002204 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002205 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002206
Aart Bikcc42be02016-10-20 16:14:16 -07002207 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002208 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002209 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002210 !CanThrow() &&
2211 !IsSuspendCheck() &&
2212 !IsControlFlow() &&
2213 !IsNativeDebugInfo() &&
2214 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002215 // If we added an explicit barrier then we should keep it.
Igor Murashkind01745e2017-04-05 16:40:31 -07002216 !IsMemoryBarrier() &&
2217 !IsConstructorFence();
Aart Bik482095d2016-10-10 15:39:10 -07002218 }
2219
Aart Bikcc42be02016-10-20 16:14:16 -07002220 bool IsDeadAndRemovable() const {
2221 return IsRemovable() && !HasUses();
2222 }
2223
Roland Levillain6c82d402014-10-13 16:10:27 +01002224 // Does this instruction strictly dominate `other_instruction`?
2225 // Returns false if this instruction and `other_instruction` are the same.
2226 // Aborts if this instruction and `other_instruction` are both phis.
2227 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002228
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002229 int GetId() const { return id_; }
2230 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002231
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002232 int GetSsaIndex() const { return ssa_index_; }
2233 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2234 bool HasSsaIndex() const { return ssa_index_ != -1; }
2235
2236 bool HasEnvironment() const { return environment_ != nullptr; }
2237 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002238 // Set the `environment_` field. Raw because this method does not
2239 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002240 void SetRawEnvironment(HEnvironment* environment) {
2241 DCHECK(environment_ == nullptr);
2242 DCHECK_EQ(environment->GetHolder(), this);
2243 environment_ = environment;
2244 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002245
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002246 void InsertRawEnvironment(HEnvironment* environment) {
2247 DCHECK(environment_ != nullptr);
2248 DCHECK_EQ(environment->GetHolder(), this);
2249 DCHECK(environment->GetParent() == nullptr);
2250 environment->parent_ = environment_;
2251 environment_ = environment;
2252 }
2253
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002254 void RemoveEnvironment();
2255
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002256 // Set the environment of this instruction, copying it from `environment`. While
2257 // copying, the uses lists are being updated.
2258 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002259 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002260 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002261 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002262 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002263 if (environment->GetParent() != nullptr) {
2264 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2265 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002266 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002267
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002268 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2269 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002270 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002271 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002272 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002273 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002274 if (environment->GetParent() != nullptr) {
2275 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2276 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002277 }
2278
Nicolas Geoffray39468442014-09-02 15:17:15 +01002279 // Returns the number of entries in the environment. Typically, that is the
2280 // number of dex registers in a method. It could be more in case of inlining.
2281 size_t EnvironmentSize() const;
2282
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002283 LocationSummary* GetLocations() const { return locations_; }
2284 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002285
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002286 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002287 void ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray8a62a4c2018-07-03 09:39:07 +01002288 void ReplaceEnvUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002289 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002290
Alexandre Rames188d4312015-04-09 18:30:21 +01002291 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2292 // uses of this instruction by `other` are *not* updated.
2293 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2294 ReplaceWith(other);
2295 other->ReplaceInput(this, use_index);
2296 }
2297
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002298 // Move `this` instruction before `cursor`
2299 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002300
Vladimir Markofb337ea2015-11-25 15:25:10 +00002301 // Move `this` before its first user and out of any loops. If there is no
2302 // out-of-loop user that dominates all other users, move the instruction
2303 // to the end of the out-of-loop common dominator of the user's blocks.
2304 //
2305 // This can be used only on non-throwing instructions with no side effects that
2306 // have at least one use but no environment uses.
2307 void MoveBeforeFirstUserAndOutOfLoops();
2308
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002309#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01002310 bool Is##type() const;
2311
2312 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2313#undef INSTRUCTION_TYPE_CHECK
2314
2315#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002316 const H##type* As##type() const; \
2317 H##type* As##type();
2318
Vladimir Markoa90dd512018-05-04 15:04:45 +01002319 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
2320#undef INSTRUCTION_TYPE_CAST
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002321
Artem Serovcced8ba2017-07-19 18:18:09 +01002322 // Return a clone of the instruction if it is clonable (shallow copy by default, custom copy
2323 // if a custom copy-constructor is provided for a particular type). If IsClonable() is false for
2324 // the instruction then the behaviour of this function is undefined.
2325 //
2326 // Note: It is semantically valid to create a clone of the instruction only until
2327 // prepare_for_register_allocator phase as lifetime, intervals and codegen info are not
2328 // copied.
2329 //
2330 // Note: HEnvironment and some other fields are not copied and are set to default values, see
2331 // 'explicit HInstruction(const HInstruction& other)' for details.
2332 virtual HInstruction* Clone(ArenaAllocator* arena ATTRIBUTE_UNUSED) const {
2333 LOG(FATAL) << "Cloning is not implemented for the instruction " <<
2334 DebugName() << " " << GetId();
2335 UNREACHABLE();
2336 }
2337
2338 // Return whether instruction can be cloned (copied).
2339 virtual bool IsClonable() const { return false; }
2340
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002341 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002342 // TODO: this method is used by LICM and GVN with possibly different
2343 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002344 virtual bool CanBeMoved() const { return false; }
2345
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002346 // Returns whether any data encoded in the two instructions is equal.
2347 // This method does not look at the inputs. Both instructions must be
2348 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002349 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002350 return false;
2351 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002352
2353 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002354 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002355 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002356 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002357
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302358 InstructionKind GetKind() const { return GetPackedField<InstructionKindField>(); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002359
2360 virtual size_t ComputeHashCode() const {
2361 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002362 for (const HInstruction* input : GetInputs()) {
2363 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002364 }
2365 return result;
2366 }
2367
2368 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002369 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002370 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002371
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002372 size_t GetLifetimePosition() const { return lifetime_position_; }
2373 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2374 LiveInterval* GetLiveInterval() const { return live_interval_; }
2375 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2376 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2377
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002378 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2379
2380 // Returns whether the code generation of the instruction will require to have access
2381 // to the current method. Such instructions are:
2382 // (1): Instructions that require an environment, as calling the runtime requires
2383 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002384 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2385 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002386 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002387 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002388 }
2389
Vladimir Markodc151b22015-10-15 18:02:30 +01002390 // Returns whether the code generation of the instruction will require to have access
2391 // to the dex cache of the current method's declaring class via the current method.
2392 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002393
Mark Mendellc4701932015-04-10 13:18:51 -04002394 // Does this instruction have any use in an environment before
2395 // control flow hits 'other'?
2396 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2397
2398 // Remove all references to environment uses of this instruction.
2399 // The caller must ensure that this is safe to do.
2400 void RemoveEnvironmentUsers();
2401
Vladimir Markoa1de9182016-02-25 11:37:38 +00002402 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2403 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002404
David Brazdil1abb4192015-02-17 18:33:36 +00002405 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002406 // If set, the machine code for this instruction is assumed to be generated by
2407 // its users. Used by liveness analysis to compute use positions accordingly.
2408 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2409 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302410 static constexpr size_t kFieldInstructionKind = kFlagReferenceTypeIsExact + 1;
2411 static constexpr size_t kFieldInstructionKindSize =
2412 MinimumBitsToStore(static_cast<size_t>(InstructionKind::kLastInstructionKind - 1));
Vladimir Markobd785672018-05-03 17:09:09 +01002413 static constexpr size_t kFieldType =
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302414 kFieldInstructionKind + kFieldInstructionKindSize;
Vladimir Markobd785672018-05-03 17:09:09 +01002415 static constexpr size_t kFieldTypeSize =
2416 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
2417 static constexpr size_t kNumberOfGenericPackedBits = kFieldType + kFieldTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00002418 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2419
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302420 static_assert(kNumberOfGenericPackedBits <= kMaxNumberOfPackedBits,
2421 "Too many generic packed fields");
2422
Vladimir Markobd785672018-05-03 17:09:09 +01002423 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
2424
Vladimir Marko372f10e2016-05-17 16:30:10 +01002425 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2426 return GetInputRecords()[i];
2427 }
2428
2429 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2430 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2431 input_records[index] = input;
2432 }
David Brazdil1abb4192015-02-17 18:33:36 +00002433
Vladimir Markoa1de9182016-02-25 11:37:38 +00002434 uint32_t GetPackedFields() const {
2435 return packed_fields_;
2436 }
2437
2438 template <size_t flag>
2439 bool GetPackedFlag() const {
2440 return (packed_fields_ & (1u << flag)) != 0u;
2441 }
2442
2443 template <size_t flag>
2444 void SetPackedFlag(bool value = true) {
2445 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2446 }
2447
2448 template <typename BitFieldType>
2449 typename BitFieldType::value_type GetPackedField() const {
2450 return BitFieldType::Decode(packed_fields_);
2451 }
2452
2453 template <typename BitFieldType>
2454 void SetPackedField(typename BitFieldType::value_type value) {
2455 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2456 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2457 }
2458
Artem Serovcced8ba2017-07-19 18:18:09 +01002459 // Copy construction for the instruction (used for Clone function).
2460 //
2461 // Fields (e.g. lifetime, intervals and codegen info) associated with phases starting from
2462 // prepare_for_register_allocator are not copied (set to default values).
2463 //
2464 // Copy constructors must be provided for every HInstruction type; default copy constructor is
2465 // fine for most of them. However for some of the instructions a custom copy constructor must be
2466 // specified (when instruction has non-trivially copyable fields and must have a special behaviour
2467 // for copying them).
2468 explicit HInstruction(const HInstruction& other)
2469 : previous_(nullptr),
2470 next_(nullptr),
2471 block_(nullptr),
2472 dex_pc_(other.dex_pc_),
2473 id_(-1),
2474 ssa_index_(-1),
2475 packed_fields_(other.packed_fields_),
2476 environment_(nullptr),
2477 locations_(nullptr),
2478 live_interval_(nullptr),
2479 lifetime_position_(kNoLifetime),
2480 side_effects_(other.side_effects_),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302481 reference_type_handle_(other.reference_type_handle_) {
2482 }
Artem Serovcced8ba2017-07-19 18:18:09 +01002483
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002484 private:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302485 using InstructionKindField =
2486 BitField<InstructionKind, kFieldInstructionKind, kFieldInstructionKindSize>;
2487
Vladimir Marko46817b82016-03-29 12:21:58 +01002488 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2489 auto before_use_node = uses_.before_begin();
2490 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2491 HInstruction* user = use_node->GetUser();
2492 size_t input_index = use_node->GetIndex();
2493 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2494 before_use_node = use_node;
2495 }
2496 }
2497
2498 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2499 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2500 if (next != uses_.end()) {
2501 HInstruction* next_user = next->GetUser();
2502 size_t next_index = next->GetIndex();
2503 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2504 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2505 }
2506 }
2507
2508 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2509 auto before_env_use_node = env_uses_.before_begin();
2510 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2511 HEnvironment* user = env_use_node->GetUser();
2512 size_t input_index = env_use_node->GetIndex();
2513 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2514 before_env_use_node = env_use_node;
2515 }
2516 }
2517
2518 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2519 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2520 if (next != env_uses_.end()) {
2521 HEnvironment* next_user = next->GetUser();
2522 size_t next_index = next->GetIndex();
2523 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2524 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2525 }
2526 }
David Brazdil1abb4192015-02-17 18:33:36 +00002527
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002528 HInstruction* previous_;
2529 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002530 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002531 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002532
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002533 // An instruction gets an id when it is added to the graph.
2534 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002535 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002536 int id_;
2537
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002538 // When doing liveness analysis, instructions that have uses get an SSA index.
2539 int ssa_index_;
2540
Vladimir Markoa1de9182016-02-25 11:37:38 +00002541 // Packed fields.
2542 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002543
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002544 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002545 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002546
2547 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002548 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002549
Nicolas Geoffray39468442014-09-02 15:17:15 +01002550 // The environment associated with this instruction. Not null if the instruction
2551 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002552 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002553
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002554 // Set by the code generator.
2555 LocationSummary* locations_;
2556
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002557 // Set by the liveness analysis.
2558 LiveInterval* live_interval_;
2559
2560 // Set by the liveness analysis, this is the position in a linear
2561 // order of blocks where this instruction's live interval start.
2562 size_t lifetime_position_;
2563
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002564 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002565
Vladimir Markoa1de9182016-02-25 11:37:38 +00002566 // The reference handle part of the reference type info.
2567 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002568 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002569 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002570
David Brazdil1abb4192015-02-17 18:33:36 +00002571 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002572 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002573 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002574 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002575 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002576};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002577std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002578
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002579// Iterates over the instructions, while preserving the next instruction
2580// in case the current instruction gets removed from the list by the user
2581// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002582class HInstructionIterator : public ValueObject {
2583 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002584 explicit HInstructionIterator(const HInstructionList& instructions)
2585 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002586 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002587 }
2588
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002589 bool Done() const { return instruction_ == nullptr; }
2590 HInstruction* Current() const { return instruction_; }
2591 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002592 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002593 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002594 }
2595
2596 private:
2597 HInstruction* instruction_;
2598 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002599
2600 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002601};
2602
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002603// Iterates over the instructions without saving the next instruction,
2604// therefore handling changes in the graph potentially made by the user
2605// of this iterator.
2606class HInstructionIteratorHandleChanges : public ValueObject {
2607 public:
2608 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2609 : instruction_(instructions.first_instruction_) {
2610 }
2611
2612 bool Done() const { return instruction_ == nullptr; }
2613 HInstruction* Current() const { return instruction_; }
2614 void Advance() {
2615 instruction_ = instruction_->GetNext();
2616 }
2617
2618 private:
2619 HInstruction* instruction_;
2620
2621 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2622};
2623
2624
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002625class HBackwardInstructionIterator : public ValueObject {
2626 public:
2627 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2628 : instruction_(instructions.last_instruction_) {
2629 next_ = Done() ? nullptr : instruction_->GetPrevious();
2630 }
2631
2632 bool Done() const { return instruction_ == nullptr; }
2633 HInstruction* Current() const { return instruction_; }
2634 void Advance() {
2635 instruction_ = next_;
2636 next_ = Done() ? nullptr : instruction_->GetPrevious();
2637 }
2638
2639 private:
2640 HInstruction* instruction_;
2641 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002642
2643 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002644};
2645
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002646class HVariableInputSizeInstruction : public HInstruction {
2647 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002648 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002649 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() override {
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002650 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2651 }
2652
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002653 void AddInput(HInstruction* input);
2654 void InsertInputAt(size_t index, HInstruction* input);
2655 void RemoveInputAt(size_t index);
2656
Igor Murashkind01745e2017-04-05 16:40:31 -07002657 // Removes all the inputs.
2658 // Also removes this instructions from each input's use list
2659 // (for non-environment uses only).
2660 void RemoveAllInputs();
2661
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002662 protected:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302663 HVariableInputSizeInstruction(InstructionKind inst_kind,
2664 SideEffects side_effects,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002665 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002666 ArenaAllocator* allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002667 size_t number_of_inputs,
2668 ArenaAllocKind kind)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302669 : HInstruction(inst_kind, side_effects, dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002670 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Vladimir Markobd785672018-05-03 17:09:09 +01002671 HVariableInputSizeInstruction(InstructionKind inst_kind,
2672 DataType::Type type,
2673 SideEffects side_effects,
2674 uint32_t dex_pc,
2675 ArenaAllocator* allocator,
2676 size_t number_of_inputs,
2677 ArenaAllocKind kind)
2678 : HInstruction(inst_kind, type, side_effects, dex_pc),
2679 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002680
Artem Serovcced8ba2017-07-19 18:18:09 +01002681 DEFAULT_COPY_CONSTRUCTOR(VariableInputSizeInstruction);
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002682
Artem Serovcced8ba2017-07-19 18:18:09 +01002683 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002684};
2685
Vladimir Markof9f64412015-09-02 14:05:49 +01002686template<size_t N>
Vladimir Markobd785672018-05-03 17:09:09 +01002687class HExpression : public HInstruction {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002688 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002689 HExpression<N>(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302690 : HInstruction(kind, side_effects, dex_pc), inputs_() {}
Vladimir Markobd785672018-05-03 17:09:09 +01002691 HExpression<N>(InstructionKind kind,
2692 DataType::Type type,
2693 SideEffects side_effects,
2694 uint32_t dex_pc)
2695 : HInstruction(kind, type, side_effects, dex_pc), inputs_() {}
2696 virtual ~HExpression() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002697
Vladimir Marko372f10e2016-05-17 16:30:10 +01002698 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01002699 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01002700 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002701 }
2702
Artem Serovcced8ba2017-07-19 18:18:09 +01002703 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002704 DEFAULT_COPY_CONSTRUCTOR(Expression<N>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002705
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002706 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002707 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002708
2709 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002710};
2711
Vladimir Markobd785672018-05-03 17:09:09 +01002712// HExpression specialization for N=0.
Vladimir Markof9f64412015-09-02 14:05:49 +01002713template<>
Vladimir Markobd785672018-05-03 17:09:09 +01002714class HExpression<0> : public HInstruction {
Vladimir Markof9f64412015-09-02 14:05:49 +01002715 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002716 using HInstruction::HInstruction;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002717
Vladimir Markobd785672018-05-03 17:09:09 +01002718 virtual ~HExpression() {}
Vladimir Markof9f64412015-09-02 14:05:49 +01002719
Vladimir Marko372f10e2016-05-17 16:30:10 +01002720 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01002721 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01002722 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002723 }
2724
Artem Serovcced8ba2017-07-19 18:18:09 +01002725 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002726 DEFAULT_COPY_CONSTRUCTOR(Expression<0>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002727
Vladimir Markof9f64412015-09-02 14:05:49 +01002728 private:
2729 friend class SsaBuilder;
2730};
2731
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002732// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2733// instruction that branches to the exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002734class HReturnVoid final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002735 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002736 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002737 : HExpression(kReturnVoid, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302738 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002739
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002740 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002741
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002742 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002743
Artem Serovcced8ba2017-07-19 18:18:09 +01002744 protected:
2745 DEFAULT_COPY_CONSTRUCTOR(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002746};
2747
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002748// Represents dex's RETURN opcodes. A HReturn is a control flow
2749// instruction that branches to the exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002750class HReturn final : public HExpression<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002751 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002752 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002753 : HExpression(kReturn, SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002754 SetRawInputAt(0, value);
2755 }
2756
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002757 bool IsControlFlow() const override { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002758
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002759 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002760
Artem Serovcced8ba2017-07-19 18:18:09 +01002761 protected:
2762 DEFAULT_COPY_CONSTRUCTOR(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002763};
2764
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002765class HPhi final : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002766 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002767 HPhi(ArenaAllocator* allocator,
David Brazdildee58d62016-04-07 09:54:26 +00002768 uint32_t reg_number,
2769 size_t number_of_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002770 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +00002771 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002772 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302773 kPhi,
Vladimir Markobd785672018-05-03 17:09:09 +01002774 ToPhiType(type),
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002775 SideEffects::None(),
2776 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002777 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002778 number_of_inputs,
2779 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002780 reg_number_(reg_number) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002781 DCHECK_NE(GetType(), DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +00002782 // Phis are constructed live and marked dead if conflicting or unused.
2783 // Individual steps of SsaBuilder should assume that if a phi has been
2784 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2785 SetPackedFlag<kFlagIsLive>(true);
2786 SetPackedFlag<kFlagCanBeNull>(true);
2787 }
2788
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002789 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01002790
David Brazdildee58d62016-04-07 09:54:26 +00002791 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002792 static DataType::Type ToPhiType(DataType::Type type) {
2793 return DataType::Kind(type);
David Brazdildee58d62016-04-07 09:54:26 +00002794 }
2795
2796 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2797
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002798 void SetType(DataType::Type new_type) {
David Brazdildee58d62016-04-07 09:54:26 +00002799 // Make sure that only valid type changes occur. The following are allowed:
2800 // (1) int -> float/ref (primitive type propagation),
2801 // (2) long -> double (primitive type propagation).
2802 DCHECK(GetType() == new_type ||
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002803 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kFloat32) ||
2804 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kReference) ||
2805 (GetType() == DataType::Type::kInt64 && new_type == DataType::Type::kFloat64));
David Brazdildee58d62016-04-07 09:54:26 +00002806 SetPackedField<TypeField>(new_type);
2807 }
2808
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002809 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
David Brazdildee58d62016-04-07 09:54:26 +00002810 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2811
2812 uint32_t GetRegNumber() const { return reg_number_; }
2813
2814 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2815 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2816 bool IsDead() const { return !IsLive(); }
2817 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2818
Vladimir Markoe9004912016-06-16 16:50:52 +01002819 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002820 return other != nullptr
2821 && other->IsPhi()
2822 && other->AsPhi()->GetBlock() == GetBlock()
2823 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2824 }
2825
Nicolas Geoffrayf57c1ae2017-06-28 17:40:18 +01002826 bool HasEquivalentPhi() const {
2827 if (GetPrevious() != nullptr && GetPrevious()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2828 return true;
2829 }
2830 if (GetNext() != nullptr && GetNext()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2831 return true;
2832 }
2833 return false;
2834 }
2835
David Brazdildee58d62016-04-07 09:54:26 +00002836 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2837 // An equivalent phi is a phi having the same dex register and type.
2838 // It assumes that phis with the same dex register are adjacent.
2839 HPhi* GetNextEquivalentPhiWithSameType() {
2840 HInstruction* next = GetNext();
2841 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2842 if (next->GetType() == GetType()) {
2843 return next->AsPhi();
2844 }
2845 next = next->GetNext();
2846 }
2847 return nullptr;
2848 }
2849
2850 DECLARE_INSTRUCTION(Phi);
2851
Artem Serovcced8ba2017-07-19 18:18:09 +01002852 protected:
2853 DEFAULT_COPY_CONSTRUCTOR(Phi);
2854
David Brazdildee58d62016-04-07 09:54:26 +00002855 private:
Vladimir Markobd785672018-05-03 17:09:09 +01002856 static constexpr size_t kFlagIsLive = HInstruction::kNumberOfGenericPackedBits;
David Brazdildee58d62016-04-07 09:54:26 +00002857 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2858 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2859 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
David Brazdildee58d62016-04-07 09:54:26 +00002860
David Brazdildee58d62016-04-07 09:54:26 +00002861 const uint32_t reg_number_;
David Brazdildee58d62016-04-07 09:54:26 +00002862};
2863
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002864// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002865// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002866// exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002867class HExit final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002868 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302869 explicit HExit(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002870 : HExpression(kExit, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302871 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002872
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002873 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002874
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002875 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002876
Artem Serovcced8ba2017-07-19 18:18:09 +01002877 protected:
2878 DEFAULT_COPY_CONSTRUCTOR(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002879};
2880
2881// Jumps from one block to another.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002882class HGoto final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002883 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302884 explicit HGoto(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002885 : HExpression(kGoto, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302886 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002887
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002888 bool IsClonable() const override { return true; }
2889 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002890
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002891 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002892 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002893 }
2894
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002895 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002896
Artem Serovcced8ba2017-07-19 18:18:09 +01002897 protected:
2898 DEFAULT_COPY_CONSTRUCTOR(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002899};
2900
Roland Levillain9867bc72015-08-05 10:21:34 +01002901class HConstant : public HExpression<0> {
2902 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302903 explicit HConstant(InstructionKind kind, DataType::Type type, uint32_t dex_pc = kNoDexPc)
2904 : HExpression(kind, type, SideEffects::None(), dex_pc) {
2905 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002906
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002907 bool CanBeMoved() const override { return true; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002908
Roland Levillain1a653882016-03-18 18:05:57 +00002909 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002910 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002911 // Is this constant 0 in the arithmetic sense?
2912 virtual bool IsArithmeticZero() const { return false; }
2913 // Is this constant a 0-bit pattern?
2914 virtual bool IsZeroBitPattern() const { return false; }
2915 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002916 virtual bool IsOne() const { return false; }
2917
David Brazdil77a48ae2015-09-15 12:34:04 +00002918 virtual uint64_t GetValueAsUint64() const = 0;
2919
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002920 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002921
Artem Serovcced8ba2017-07-19 18:18:09 +01002922 protected:
2923 DEFAULT_COPY_CONSTRUCTOR(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002924};
2925
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002926class HNullConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002927 public:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002928 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01002929 return true;
2930 }
2931
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002932 uint64_t GetValueAsUint64() const override { return 0; }
David Brazdil77a48ae2015-09-15 12:34:04 +00002933
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002934 size_t ComputeHashCode() const override { return 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002935
Roland Levillain1a653882016-03-18 18:05:57 +00002936 // The null constant representation is a 0-bit pattern.
Yi Kong39402542019-03-24 02:47:16 -07002937 bool IsZeroBitPattern() const override { return true; }
Roland Levillain1a653882016-03-18 18:05:57 +00002938
Roland Levillain9867bc72015-08-05 10:21:34 +01002939 DECLARE_INSTRUCTION(NullConstant);
2940
Artem Serovcced8ba2017-07-19 18:18:09 +01002941 protected:
2942 DEFAULT_COPY_CONSTRUCTOR(NullConstant);
2943
Roland Levillain9867bc72015-08-05 10:21:34 +01002944 private:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002945 explicit HNullConstant(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302946 : HConstant(kNullConstant, DataType::Type::kReference, dex_pc) {
2947 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002948
2949 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01002950};
2951
2952// Constants of the type int. Those can be from Dex instructions, or
2953// synthesized (for example with the if-eqz instruction).
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002954class HIntConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002955 public:
2956 int32_t GetValue() const { return value_; }
2957
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002958 uint64_t GetValueAsUint64() const override {
David Brazdil9f389d42015-10-01 14:32:56 +01002959 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2960 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002961
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002962 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002963 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002964 return other->AsIntConstant()->value_ == value_;
2965 }
2966
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002967 size_t ComputeHashCode() const override { return GetValue(); }
Roland Levillain9867bc72015-08-05 10:21:34 +01002968
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002969 bool IsMinusOne() const override { return GetValue() == -1; }
2970 bool IsArithmeticZero() const override { return GetValue() == 0; }
2971 bool IsZeroBitPattern() const override { return GetValue() == 0; }
2972 bool IsOne() const override { return GetValue() == 1; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002973
Roland Levillain1a653882016-03-18 18:05:57 +00002974 // Integer constants are used to encode Boolean values as well,
2975 // where 1 means true and 0 means false.
2976 bool IsTrue() const { return GetValue() == 1; }
2977 bool IsFalse() const { return GetValue() == 0; }
2978
Roland Levillain9867bc72015-08-05 10:21:34 +01002979 DECLARE_INSTRUCTION(IntConstant);
2980
Artem Serovcced8ba2017-07-19 18:18:09 +01002981 protected:
2982 DEFAULT_COPY_CONSTRUCTOR(IntConstant);
2983
Roland Levillain9867bc72015-08-05 10:21:34 +01002984 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002985 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302986 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc), value_(value) {
2987 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002988 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302989 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc),
2990 value_(value ? 1 : 0) {
2991 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002992
2993 const int32_t value_;
2994
2995 friend class HGraph;
2996 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2997 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Roland Levillain9867bc72015-08-05 10:21:34 +01002998};
2999
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003000class HLongConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01003001 public:
3002 int64_t GetValue() const { return value_; }
3003
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003004 uint64_t GetValueAsUint64() const override { return value_; }
David Brazdil77a48ae2015-09-15 12:34:04 +00003005
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003006 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003007 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01003008 return other->AsLongConstant()->value_ == value_;
3009 }
3010
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003011 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain9867bc72015-08-05 10:21:34 +01003012
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003013 bool IsMinusOne() const override { return GetValue() == -1; }
3014 bool IsArithmeticZero() const override { return GetValue() == 0; }
3015 bool IsZeroBitPattern() const override { return GetValue() == 0; }
3016 bool IsOne() const override { return GetValue() == 1; }
Roland Levillain9867bc72015-08-05 10:21:34 +01003017
3018 DECLARE_INSTRUCTION(LongConstant);
3019
Artem Serovcced8ba2017-07-19 18:18:09 +01003020 protected:
3021 DEFAULT_COPY_CONSTRUCTOR(LongConstant);
3022
Roland Levillain9867bc72015-08-05 10:21:34 +01003023 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003024 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303025 : HConstant(kLongConstant, DataType::Type::kInt64, dex_pc),
3026 value_(value) {
3027 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003028
3029 const int64_t value_;
3030
3031 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01003032};
Dave Allison20dfc792014-06-16 20:44:29 -07003033
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003034class HFloatConstant final : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003035 public:
3036 float GetValue() const { return value_; }
3037
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003038 uint64_t GetValueAsUint64() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003039 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3040 }
3041
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003042 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003043 DCHECK(other->IsFloatConstant()) << other->DebugName();
3044 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
3045 }
3046
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003047 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003048
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003049 bool IsMinusOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003050 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
3051 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003052 bool IsArithmeticZero() const override {
Roland Levillain1a653882016-03-18 18:05:57 +00003053 return std::fpclassify(value_) == FP_ZERO;
3054 }
3055 bool IsArithmeticPositiveZero() const {
3056 return IsArithmeticZero() && !std::signbit(value_);
3057 }
3058 bool IsArithmeticNegativeZero() const {
3059 return IsArithmeticZero() && std::signbit(value_);
3060 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003061 bool IsZeroBitPattern() const override {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003062 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00003063 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003064 bool IsOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003065 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3066 }
3067 bool IsNaN() const {
3068 return std::isnan(value_);
3069 }
3070
3071 DECLARE_INSTRUCTION(FloatConstant);
3072
Artem Serovcced8ba2017-07-19 18:18:09 +01003073 protected:
3074 DEFAULT_COPY_CONSTRUCTOR(FloatConstant);
3075
Roland Levillain31dd3d62016-02-16 12:21:02 +00003076 private:
3077 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303078 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3079 value_(value) {
3080 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003081 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303082 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3083 value_(bit_cast<float, int32_t>(value)) {
3084 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003085
3086 const float value_;
3087
3088 // Only the SsaBuilder and HGraph can create floating-point constants.
3089 friend class SsaBuilder;
3090 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003091};
3092
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003093class HDoubleConstant final : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003094 public:
3095 double GetValue() const { return value_; }
3096
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003097 uint64_t GetValueAsUint64() const override { return bit_cast<uint64_t, double>(value_); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003098
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003099 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003100 DCHECK(other->IsDoubleConstant()) << other->DebugName();
3101 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
3102 }
3103
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003104 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003105
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003106 bool IsMinusOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003107 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
3108 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003109 bool IsArithmeticZero() const override {
Roland Levillain1a653882016-03-18 18:05:57 +00003110 return std::fpclassify(value_) == FP_ZERO;
3111 }
3112 bool IsArithmeticPositiveZero() const {
3113 return IsArithmeticZero() && !std::signbit(value_);
3114 }
3115 bool IsArithmeticNegativeZero() const {
3116 return IsArithmeticZero() && std::signbit(value_);
3117 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003118 bool IsZeroBitPattern() const override {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003119 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00003120 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003121 bool IsOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003122 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3123 }
3124 bool IsNaN() const {
3125 return std::isnan(value_);
3126 }
3127
3128 DECLARE_INSTRUCTION(DoubleConstant);
3129
Artem Serovcced8ba2017-07-19 18:18:09 +01003130 protected:
3131 DEFAULT_COPY_CONSTRUCTOR(DoubleConstant);
3132
Roland Levillain31dd3d62016-02-16 12:21:02 +00003133 private:
3134 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303135 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3136 value_(value) {
3137 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003138 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303139 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3140 value_(bit_cast<double, int64_t>(value)) {
3141 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003142
3143 const double value_;
3144
3145 // Only the SsaBuilder and HGraph can create floating-point constants.
3146 friend class SsaBuilder;
3147 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003148};
3149
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003150// Conditional branch. A block ending with an HIf instruction must have
3151// two successors.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003152class HIf final : public HExpression<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003153 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003154 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003155 : HExpression(kIf, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003156 SetRawInputAt(0, input);
3157 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003158
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003159 bool IsClonable() const override { return true; }
3160 bool IsControlFlow() const override { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003161
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003162 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003163 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003164 }
3165
3166 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003167 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003168 }
3169
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003170 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003171
Artem Serovcced8ba2017-07-19 18:18:09 +01003172 protected:
3173 DEFAULT_COPY_CONSTRUCTOR(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003174};
3175
David Brazdilfc6a86a2015-06-26 10:33:45 +00003176
3177// Abstract instruction which marks the beginning and/or end of a try block and
3178// links it to the respective exception handlers. Behaves the same as a Goto in
3179// non-exceptional control flow.
3180// Normal-flow successor is stored at index zero, exception handlers under
3181// higher indices in no particular order.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003182class HTryBoundary final : public HExpression<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003183 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003184 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01003185 kEntry,
3186 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003187 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01003188 };
3189
Artem Serovd1aa7d02018-06-22 11:35:46 +01003190 // SideEffects::CanTriggerGC prevents instructions with SideEffects::DependOnGC to be alive
3191 // across the catch block entering edges as GC might happen during throwing an exception.
3192 // TryBoundary with BoundaryKind::kExit is conservatively used for that as there is no
3193 // HInstruction which a catch block must start from.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003194 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01003195 : HExpression(kTryBoundary,
3196 (kind == BoundaryKind::kExit) ? SideEffects::CanTriggerGC()
3197 : SideEffects::None(),
3198 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003199 SetPackedField<BoundaryKindField>(kind);
3200 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003201
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003202 bool IsControlFlow() const override { return true; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003203
3204 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01003205 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003206
David Brazdild26a4112015-11-10 11:07:31 +00003207 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
3208 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
3209 }
3210
David Brazdilfc6a86a2015-06-26 10:33:45 +00003211 // Returns whether `handler` is among its exception handlers (non-zero index
3212 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01003213 bool HasExceptionHandler(const HBasicBlock& handler) const {
3214 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00003215 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00003216 }
3217
3218 // If not present already, adds `handler` to its block's list of exception
3219 // handlers.
3220 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01003221 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003222 GetBlock()->AddSuccessor(handler);
3223 }
3224 }
3225
Vladimir Markoa1de9182016-02-25 11:37:38 +00003226 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
3227 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003228
David Brazdilffee3d32015-07-06 11:48:53 +01003229 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
3230
David Brazdilfc6a86a2015-06-26 10:33:45 +00003231 DECLARE_INSTRUCTION(TryBoundary);
3232
Artem Serovcced8ba2017-07-19 18:18:09 +01003233 protected:
3234 DEFAULT_COPY_CONSTRUCTOR(TryBoundary);
3235
David Brazdilfc6a86a2015-06-26 10:33:45 +00003236 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003237 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
3238 static constexpr size_t kFieldBoundaryKindSize =
3239 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
3240 static constexpr size_t kNumberOfTryBoundaryPackedBits =
3241 kFieldBoundaryKind + kFieldBoundaryKindSize;
3242 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
3243 "Too many packed fields.");
3244 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00003245};
3246
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003247// Deoptimize to interpreter, upon checking a condition.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003248class HDeoptimize final : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003249 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003250 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
3251 // across.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003252 HDeoptimize(ArenaAllocator* allocator,
3253 HInstruction* cond,
3254 DeoptimizationKind kind,
3255 uint32_t dex_pc)
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003256 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303257 kDeoptimize,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003258 SideEffects::All(),
3259 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003260 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08003261 /* number_of_inputs= */ 1,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003262 kArenaAllocMisc) {
3263 SetPackedFlag<kFieldCanBeMoved>(false);
3264 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003265 SetRawInputAt(0, cond);
3266 }
3267
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003268 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003269
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003270 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
3271 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
3272 // instead of `guard`.
3273 // We set CanTriggerGC to prevent any intermediate address to be live
3274 // at the point of the `HDeoptimize`.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003275 HDeoptimize(ArenaAllocator* allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003276 HInstruction* cond,
3277 HInstruction* guard,
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003278 DeoptimizationKind kind,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003279 uint32_t dex_pc)
3280 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303281 kDeoptimize,
Vladimir Markobd785672018-05-03 17:09:09 +01003282 guard->GetType(),
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003283 SideEffects::CanTriggerGC(),
3284 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003285 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08003286 /* number_of_inputs= */ 2,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003287 kArenaAllocMisc) {
3288 SetPackedFlag<kFieldCanBeMoved>(true);
3289 SetPackedField<DeoptimizeKindField>(kind);
3290 SetRawInputAt(0, cond);
3291 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003292 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003293
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003294 bool CanBeMoved() const override { return GetPackedFlag<kFieldCanBeMoved>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003295
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003296 bool InstructionDataEquals(const HInstruction* other) const override {
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003297 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
3298 }
3299
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003300 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003301
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003302 bool CanThrow() const override { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003303
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003304 DeoptimizationKind GetDeoptimizationKind() const { return GetPackedField<DeoptimizeKindField>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003305
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003306 bool GuardsAnInput() const {
3307 return InputCount() == 2;
3308 }
3309
3310 HInstruction* GuardedInput() const {
3311 DCHECK(GuardsAnInput());
3312 return InputAt(1);
3313 }
3314
3315 void RemoveGuard() {
3316 RemoveInputAt(1);
3317 }
3318
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003319 DECLARE_INSTRUCTION(Deoptimize);
3320
Artem Serovcced8ba2017-07-19 18:18:09 +01003321 protected:
3322 DEFAULT_COPY_CONSTRUCTOR(Deoptimize);
3323
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003324 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003325 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3326 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3327 static constexpr size_t kFieldDeoptimizeKindSize =
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003328 MinimumBitsToStore(static_cast<size_t>(DeoptimizationKind::kLast));
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003329 static constexpr size_t kNumberOfDeoptimizePackedBits =
3330 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3331 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3332 "Too many packed fields.");
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003333 using DeoptimizeKindField =
3334 BitField<DeoptimizationKind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003335};
3336
Mingyao Yang063fc772016-08-02 11:02:54 -07003337// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3338// The compiled code checks this flag value in a guard before devirtualized call and
3339// if it's true, starts to do deoptimization.
3340// It has a 4-byte slot on stack.
3341// TODO: allocate a register for this flag.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003342class HShouldDeoptimizeFlag final : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003343 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003344 // CHA guards are only optimized in a separate pass and it has no side effects
3345 // with regard to other passes.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003346 HShouldDeoptimizeFlag(ArenaAllocator* allocator, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303347 : HVariableInputSizeInstruction(kShouldDeoptimizeFlag,
Vladimir Markobd785672018-05-03 17:09:09 +01003348 DataType::Type::kInt32,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303349 SideEffects::None(),
3350 dex_pc,
3351 allocator,
3352 0,
3353 kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003354 }
3355
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003356 // We do all CHA guard elimination/motion in a single pass, after which there is no
3357 // further guard elimination/motion since a guard might have been used for justification
3358 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3359 // to avoid other optimizations trying to move it.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003360 bool CanBeMoved() const override { return false; }
Mingyao Yang063fc772016-08-02 11:02:54 -07003361
3362 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3363
Artem Serovcced8ba2017-07-19 18:18:09 +01003364 protected:
3365 DEFAULT_COPY_CONSTRUCTOR(ShouldDeoptimizeFlag);
Mingyao Yang063fc772016-08-02 11:02:54 -07003366};
3367
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003368// Represents the ArtMethod that was passed as a first argument to
3369// the method. It is used by instructions that depend on it, like
3370// instructions that work with the dex cache.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003371class HCurrentMethod final : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003372 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003373 explicit HCurrentMethod(DataType::Type type, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303374 : HExpression(kCurrentMethod, type, SideEffects::None(), dex_pc) {
3375 }
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003376
3377 DECLARE_INSTRUCTION(CurrentMethod);
3378
Artem Serovcced8ba2017-07-19 18:18:09 +01003379 protected:
3380 DEFAULT_COPY_CONSTRUCTOR(CurrentMethod);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003381};
3382
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003383// Fetches an ArtMethod from the virtual table or the interface method table
3384// of a class.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003385class HClassTableGet final : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003386 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003387 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003388 kVTable,
3389 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003390 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003391 };
3392 HClassTableGet(HInstruction* cls,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003393 DataType::Type type,
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003394 TableKind kind,
3395 size_t index,
3396 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303397 : HExpression(kClassTableGet, type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003398 index_(index) {
3399 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003400 SetRawInputAt(0, cls);
3401 }
3402
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003403 bool IsClonable() const override { return true; }
3404 bool CanBeMoved() const override { return true; }
3405 bool InstructionDataEquals(const HInstruction* other) const override {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003406 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003407 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003408 }
3409
Vladimir Markoa1de9182016-02-25 11:37:38 +00003410 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003411 size_t GetIndex() const { return index_; }
3412
3413 DECLARE_INSTRUCTION(ClassTableGet);
3414
Artem Serovcced8ba2017-07-19 18:18:09 +01003415 protected:
3416 DEFAULT_COPY_CONSTRUCTOR(ClassTableGet);
3417
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003418 private:
Vladimir Markobd785672018-05-03 17:09:09 +01003419 static constexpr size_t kFieldTableKind = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003420 static constexpr size_t kFieldTableKindSize =
3421 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3422 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3423 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3424 "Too many packed fields.");
3425 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3426
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003427 // The index of the ArtMethod in the table.
3428 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003429};
3430
Mark Mendellfe57faa2015-09-18 09:26:15 -04003431// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3432// have one successor for each entry in the switch table, and the final successor
3433// will be the block containing the next Dex opcode.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003434class HPackedSwitch final : public HExpression<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003435 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003436 HPackedSwitch(int32_t start_value,
3437 uint32_t num_entries,
3438 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003439 uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003440 : HExpression(kPackedSwitch, SideEffects::None(), dex_pc),
Mark Mendellfe57faa2015-09-18 09:26:15 -04003441 start_value_(start_value),
3442 num_entries_(num_entries) {
3443 SetRawInputAt(0, input);
3444 }
3445
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003446 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003447
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003448 bool IsControlFlow() const override { return true; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003449
3450 int32_t GetStartValue() const { return start_value_; }
3451
Vladimir Marko211c2112015-09-24 16:52:33 +01003452 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003453
3454 HBasicBlock* GetDefaultBlock() const {
3455 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003456 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003457 }
3458 DECLARE_INSTRUCTION(PackedSwitch);
3459
Artem Serovcced8ba2017-07-19 18:18:09 +01003460 protected:
3461 DEFAULT_COPY_CONSTRUCTOR(PackedSwitch);
3462
Mark Mendellfe57faa2015-09-18 09:26:15 -04003463 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003464 const int32_t start_value_;
3465 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003466};
3467
Roland Levillain88cb1752014-10-20 16:36:47 +01003468class HUnaryOperation : public HExpression<1> {
3469 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303470 HUnaryOperation(InstructionKind kind,
3471 DataType::Type result_type,
3472 HInstruction* input,
3473 uint32_t dex_pc = kNoDexPc)
3474 : HExpression(kind, result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003475 SetRawInputAt(0, input);
3476 }
3477
Artem Serovcced8ba2017-07-19 18:18:09 +01003478 // All of the UnaryOperation instructions are clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003479 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003480
Roland Levillain88cb1752014-10-20 16:36:47 +01003481 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003482 DataType::Type GetResultType() const { return GetType(); }
Roland Levillain88cb1752014-10-20 16:36:47 +01003483
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003484 bool CanBeMoved() const override { return true; }
3485 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003486 return true;
3487 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003488
Roland Levillain31dd3d62016-02-16 12:21:02 +00003489 // Try to statically evaluate `this` and return a HConstant
3490 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003491 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003492 HConstant* TryStaticEvaluation() const;
3493
3494 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003495 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3496 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003497 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3498 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003499
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003500 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003501
Artem Serovcced8ba2017-07-19 18:18:09 +01003502 protected:
3503 DEFAULT_COPY_CONSTRUCTOR(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003504};
3505
Dave Allison20dfc792014-06-16 20:44:29 -07003506class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003507 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303508 HBinaryOperation(InstructionKind kind,
3509 DataType::Type result_type,
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003510 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003511 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003512 SideEffects side_effects = SideEffects::None(),
3513 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303514 : HExpression(kind, result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003515 SetRawInputAt(0, left);
3516 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003517 }
3518
Artem Serovcced8ba2017-07-19 18:18:09 +01003519 // All of the BinaryOperation instructions are clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003520 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003521
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003522 HInstruction* GetLeft() const { return InputAt(0); }
3523 HInstruction* GetRight() const { return InputAt(1); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003524 DataType::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003525
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003526 virtual bool IsCommutative() const { return false; }
3527
3528 // Put constant on the right.
3529 // Returns whether order is changed.
3530 bool OrderInputsWithConstantOnTheRight() {
3531 HInstruction* left = InputAt(0);
3532 HInstruction* right = InputAt(1);
3533 if (left->IsConstant() && !right->IsConstant()) {
3534 ReplaceInput(right, 0);
3535 ReplaceInput(left, 1);
3536 return true;
3537 }
3538 return false;
3539 }
3540
3541 // Order inputs by instruction id, but favor constant on the right side.
3542 // This helps GVN for commutative ops.
3543 void OrderInputs() {
3544 DCHECK(IsCommutative());
3545 HInstruction* left = InputAt(0);
3546 HInstruction* right = InputAt(1);
3547 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3548 return;
3549 }
3550 if (OrderInputsWithConstantOnTheRight()) {
3551 return;
3552 }
3553 // Order according to instruction id.
3554 if (left->GetId() > right->GetId()) {
3555 ReplaceInput(right, 0);
3556 ReplaceInput(left, 1);
3557 }
3558 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003559
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003560 bool CanBeMoved() const override { return true; }
3561 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003562 return true;
3563 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003564
Roland Levillain31dd3d62016-02-16 12:21:02 +00003565 // Try to statically evaluate `this` and return a HConstant
3566 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003567 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003568 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003569
3570 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003571 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3572 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003573 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3574 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003575 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003576 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3577 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003578 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3579 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003580 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3581 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003582 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003583 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3584 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003585
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003586 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003587 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003588 HConstant* GetConstantRight() const;
3589
3590 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003591 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003592 HInstruction* GetLeastConstantLeft() const;
3593
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003594 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003595
Artem Serovcced8ba2017-07-19 18:18:09 +01003596 protected:
3597 DEFAULT_COPY_CONSTRUCTOR(BinaryOperation);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003598};
3599
Mark Mendellc4701932015-04-10 13:18:51 -04003600// The comparison bias applies for floating point operations and indicates how NaN
3601// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003602enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003603 kNoBias, // bias is not applicable (i.e. for long operation)
3604 kGtBias, // return 1 for NaN comparisons
3605 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003606 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003607};
3608
Roland Levillain31dd3d62016-02-16 12:21:02 +00003609std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3610
Dave Allison20dfc792014-06-16 20:44:29 -07003611class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003612 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303613 HCondition(InstructionKind kind,
3614 HInstruction* first,
3615 HInstruction* second,
3616 uint32_t dex_pc = kNoDexPc)
3617 : HBinaryOperation(kind,
3618 DataType::Type::kBool,
3619 first,
3620 second,
3621 SideEffects::None(),
3622 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003623 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3624 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003625
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003626 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003627 // `instruction`, and disregard moves in between.
3628 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003629
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003630 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003631
3632 virtual IfCondition GetCondition() const = 0;
3633
Mark Mendellc4701932015-04-10 13:18:51 -04003634 virtual IfCondition GetOppositeCondition() const = 0;
3635
Vladimir Markoa1de9182016-02-25 11:37:38 +00003636 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003637 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3638
Vladimir Markoa1de9182016-02-25 11:37:38 +00003639 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3640 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003641
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003642 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003643 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003644 }
3645
Roland Levillain4fa13f62015-07-06 18:11:54 +01003646 bool IsFPConditionTrueIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003647 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003648 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003649 if (if_cond == kCondNE) {
3650 return true;
3651 } else if (if_cond == kCondEQ) {
3652 return false;
3653 }
3654 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003655 }
3656
3657 bool IsFPConditionFalseIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003658 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003659 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003660 if (if_cond == kCondEQ) {
3661 return true;
3662 } else if (if_cond == kCondNE) {
3663 return false;
3664 }
3665 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003666 }
3667
Roland Levillain31dd3d62016-02-16 12:21:02 +00003668 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003669 // Needed if we merge a HCompare into a HCondition.
Vladimir Markobd785672018-05-03 17:09:09 +01003670 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003671 static constexpr size_t kFieldComparisonBiasSize =
3672 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3673 static constexpr size_t kNumberOfConditionPackedBits =
3674 kFieldComparisonBias + kFieldComparisonBiasSize;
3675 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3676 using ComparisonBiasField =
3677 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3678
Roland Levillain31dd3d62016-02-16 12:21:02 +00003679 template <typename T>
3680 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3681
3682 template <typename T>
3683 int32_t CompareFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003684 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003685 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3686 // Handle the bias.
3687 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3688 }
3689
3690 // Return an integer constant containing the result of a condition evaluated at compile time.
3691 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3692 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3693 }
3694
Artem Serovcced8ba2017-07-19 18:18:09 +01003695 DEFAULT_COPY_CONSTRUCTOR(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003696};
3697
3698// Instruction to check if two inputs are equal to each other.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003699class HEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003700 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003701 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303702 : HCondition(kEqual, first, second, dex_pc) {
3703 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003704
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003705 bool IsCommutative() const override { return true; }
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003706
Vladimir Marko9e23df52015-11-10 17:14:35 +00003707 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003708 HNullConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003709 return MakeConstantCondition(true, GetDexPc());
3710 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003711 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003712 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3713 }
3714 // In the following Evaluate methods, a HCompare instruction has
3715 // been merged into this HEqual instruction; evaluate it as
3716 // `Compare(x, y) == 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003717 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003718 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3719 GetDexPc());
3720 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003721 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003722 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3723 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003724 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003725 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003726 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003727
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003728 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003729
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003730 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003731 return kCondEQ;
3732 }
3733
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003734 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003735 return kCondNE;
3736 }
3737
Artem Serovcced8ba2017-07-19 18:18:09 +01003738 protected:
3739 DEFAULT_COPY_CONSTRUCTOR(Equal);
3740
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003741 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003742 template <typename T> static bool Compute(T x, T y) { return x == y; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003743};
3744
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003745class HNotEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003746 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303747 HNotEqual(HInstruction* first, HInstruction* second,
3748 uint32_t dex_pc = kNoDexPc)
3749 : HCondition(kNotEqual, first, second, dex_pc) {
3750 }
Dave Allison20dfc792014-06-16 20:44:29 -07003751
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003752 bool IsCommutative() const override { return true; }
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003753
Vladimir Marko9e23df52015-11-10 17:14:35 +00003754 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003755 HNullConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003756 return MakeConstantCondition(false, GetDexPc());
3757 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003758 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003759 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3760 }
3761 // In the following Evaluate methods, a HCompare instruction has
3762 // been merged into this HNotEqual instruction; evaluate it as
3763 // `Compare(x, y) != 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003764 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003765 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3766 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003767 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003768 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3769 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003770 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003771 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003772 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003773
Dave Allison20dfc792014-06-16 20:44:29 -07003774 DECLARE_INSTRUCTION(NotEqual);
3775
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003776 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003777 return kCondNE;
3778 }
3779
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003780 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003781 return kCondEQ;
3782 }
3783
Artem Serovcced8ba2017-07-19 18:18:09 +01003784 protected:
3785 DEFAULT_COPY_CONSTRUCTOR(NotEqual);
3786
Dave Allison20dfc792014-06-16 20:44:29 -07003787 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003788 template <typename T> static bool Compute(T x, T y) { return x != y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003789};
3790
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003791class HLessThan final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003792 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303793 HLessThan(HInstruction* first, HInstruction* second,
3794 uint32_t dex_pc = kNoDexPc)
3795 : HCondition(kLessThan, first, second, dex_pc) {
3796 }
Dave Allison20dfc792014-06-16 20:44:29 -07003797
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003798 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003799 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003800 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003801 // In the following Evaluate methods, a HCompare instruction has
3802 // been merged into this HLessThan instruction; evaluate it as
3803 // `Compare(x, y) < 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003804 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003805 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3806 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003807 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003808 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3809 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003810 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003811 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003812 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003813
Dave Allison20dfc792014-06-16 20:44:29 -07003814 DECLARE_INSTRUCTION(LessThan);
3815
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003816 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003817 return kCondLT;
3818 }
3819
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003820 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003821 return kCondGE;
3822 }
3823
Artem Serovcced8ba2017-07-19 18:18:09 +01003824 protected:
3825 DEFAULT_COPY_CONSTRUCTOR(LessThan);
3826
Dave Allison20dfc792014-06-16 20:44:29 -07003827 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003828 template <typename T> static bool Compute(T x, T y) { return x < y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003829};
3830
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003831class HLessThanOrEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003832 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303833 HLessThanOrEqual(HInstruction* first, HInstruction* second,
3834 uint32_t dex_pc = kNoDexPc)
3835 : HCondition(kLessThanOrEqual, first, second, dex_pc) {
3836 }
Dave Allison20dfc792014-06-16 20:44:29 -07003837
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003838 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003839 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003840 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003841 // In the following Evaluate methods, a HCompare instruction has
3842 // been merged into this HLessThanOrEqual instruction; evaluate it as
3843 // `Compare(x, y) <= 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003844 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003845 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3846 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003847 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003848 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3849 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003850 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003851 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003852 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003853
Dave Allison20dfc792014-06-16 20:44:29 -07003854 DECLARE_INSTRUCTION(LessThanOrEqual);
3855
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003856 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003857 return kCondLE;
3858 }
3859
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003860 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003861 return kCondGT;
3862 }
3863
Artem Serovcced8ba2017-07-19 18:18:09 +01003864 protected:
3865 DEFAULT_COPY_CONSTRUCTOR(LessThanOrEqual);
3866
Dave Allison20dfc792014-06-16 20:44:29 -07003867 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003868 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003869};
3870
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003871class HGreaterThan final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003872 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003873 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303874 : HCondition(kGreaterThan, first, second, dex_pc) {
3875 }
Dave Allison20dfc792014-06-16 20:44:29 -07003876
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003877 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003878 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003879 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003880 // In the following Evaluate methods, a HCompare instruction has
3881 // been merged into this HGreaterThan instruction; evaluate it as
3882 // `Compare(x, y) > 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003883 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003884 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3885 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003886 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003887 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3888 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003889 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003890 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003891 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003892
Dave Allison20dfc792014-06-16 20:44:29 -07003893 DECLARE_INSTRUCTION(GreaterThan);
3894
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003895 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003896 return kCondGT;
3897 }
3898
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003899 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003900 return kCondLE;
3901 }
3902
Artem Serovcced8ba2017-07-19 18:18:09 +01003903 protected:
3904 DEFAULT_COPY_CONSTRUCTOR(GreaterThan);
3905
Dave Allison20dfc792014-06-16 20:44:29 -07003906 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003907 template <typename T> static bool Compute(T x, T y) { return x > y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003908};
3909
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003910class HGreaterThanOrEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003911 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003912 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303913 : HCondition(kGreaterThanOrEqual, first, second, dex_pc) {
3914 }
Dave Allison20dfc792014-06-16 20:44:29 -07003915
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003916 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003917 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003918 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003919 // In the following Evaluate methods, a HCompare instruction has
3920 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3921 // `Compare(x, y) >= 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003922 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003923 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3924 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003925 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003926 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3927 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003928 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003929 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003930 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003931
Dave Allison20dfc792014-06-16 20:44:29 -07003932 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3933
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003934 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003935 return kCondGE;
3936 }
3937
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003938 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003939 return kCondLT;
3940 }
3941
Artem Serovcced8ba2017-07-19 18:18:09 +01003942 protected:
3943 DEFAULT_COPY_CONSTRUCTOR(GreaterThanOrEqual);
3944
Dave Allison20dfc792014-06-16 20:44:29 -07003945 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003946 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003947};
3948
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003949class HBelow final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003950 public:
3951 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303952 : HCondition(kBelow, first, second, dex_pc) {
3953 }
Aart Bike9f37602015-10-09 11:15:55 -07003954
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003955 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003956 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003957 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003958 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003959 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3960 }
3961 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003962 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003963 LOG(FATAL) << DebugName() << " is not defined for float values";
3964 UNREACHABLE();
3965 }
3966 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003967 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003968 LOG(FATAL) << DebugName() << " is not defined for double values";
3969 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003970 }
3971
3972 DECLARE_INSTRUCTION(Below);
3973
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003974 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07003975 return kCondB;
3976 }
3977
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003978 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07003979 return kCondAE;
3980 }
3981
Artem Serovcced8ba2017-07-19 18:18:09 +01003982 protected:
3983 DEFAULT_COPY_CONSTRUCTOR(Below);
3984
Aart Bike9f37602015-10-09 11:15:55 -07003985 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003986 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003987 return MakeUnsigned(x) < MakeUnsigned(y);
3988 }
Aart Bike9f37602015-10-09 11:15:55 -07003989};
3990
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003991class HBelowOrEqual final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003992 public:
3993 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303994 : HCondition(kBelowOrEqual, first, second, dex_pc) {
3995 }
Aart Bike9f37602015-10-09 11:15:55 -07003996
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003997 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003998 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003999 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004000 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004001 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4002 }
4003 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004004 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004005 LOG(FATAL) << DebugName() << " is not defined for float values";
4006 UNREACHABLE();
4007 }
4008 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004009 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004010 LOG(FATAL) << DebugName() << " is not defined for double values";
4011 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004012 }
4013
4014 DECLARE_INSTRUCTION(BelowOrEqual);
4015
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004016 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004017 return kCondBE;
4018 }
4019
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004020 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004021 return kCondA;
4022 }
4023
Artem Serovcced8ba2017-07-19 18:18:09 +01004024 protected:
4025 DEFAULT_COPY_CONSTRUCTOR(BelowOrEqual);
4026
Aart Bike9f37602015-10-09 11:15:55 -07004027 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004028 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004029 return MakeUnsigned(x) <= MakeUnsigned(y);
4030 }
Aart Bike9f37602015-10-09 11:15:55 -07004031};
4032
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004033class HAbove final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004034 public:
4035 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304036 : HCondition(kAbove, first, second, dex_pc) {
4037 }
Aart Bike9f37602015-10-09 11:15:55 -07004038
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004039 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004040 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004041 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004042 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004043 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4044 }
4045 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004046 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004047 LOG(FATAL) << DebugName() << " is not defined for float values";
4048 UNREACHABLE();
4049 }
4050 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004051 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004052 LOG(FATAL) << DebugName() << " is not defined for double values";
4053 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004054 }
4055
4056 DECLARE_INSTRUCTION(Above);
4057
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004058 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004059 return kCondA;
4060 }
4061
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004062 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004063 return kCondBE;
4064 }
4065
Artem Serovcced8ba2017-07-19 18:18:09 +01004066 protected:
4067 DEFAULT_COPY_CONSTRUCTOR(Above);
4068
Aart Bike9f37602015-10-09 11:15:55 -07004069 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004070 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004071 return MakeUnsigned(x) > MakeUnsigned(y);
4072 }
Aart Bike9f37602015-10-09 11:15:55 -07004073};
4074
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004075class HAboveOrEqual final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004076 public:
4077 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304078 : HCondition(kAboveOrEqual, first, second, dex_pc) {
4079 }
Aart Bike9f37602015-10-09 11:15:55 -07004080
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004081 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004082 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004083 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004084 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004085 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4086 }
4087 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004088 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004089 LOG(FATAL) << DebugName() << " is not defined for float values";
4090 UNREACHABLE();
4091 }
4092 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004093 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004094 LOG(FATAL) << DebugName() << " is not defined for double values";
4095 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004096 }
4097
4098 DECLARE_INSTRUCTION(AboveOrEqual);
4099
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004100 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004101 return kCondAE;
4102 }
4103
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004104 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004105 return kCondB;
4106 }
4107
Artem Serovcced8ba2017-07-19 18:18:09 +01004108 protected:
4109 DEFAULT_COPY_CONSTRUCTOR(AboveOrEqual);
4110
Aart Bike9f37602015-10-09 11:15:55 -07004111 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004112 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004113 return MakeUnsigned(x) >= MakeUnsigned(y);
4114 }
Aart Bike9f37602015-10-09 11:15:55 -07004115};
Dave Allison20dfc792014-06-16 20:44:29 -07004116
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004117// Instruction to check how two inputs compare to each other.
4118// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004119class HCompare final : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004120 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00004121 // Note that `comparison_type` is the type of comparison performed
4122 // between the comparison's inputs, not the type of the instantiated
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004123 // HCompare instruction (which is always DataType::Type::kInt).
4124 HCompare(DataType::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004125 HInstruction* first,
4126 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04004127 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004128 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304129 : HBinaryOperation(kCompare,
4130 DataType::Type::kInt32,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004131 first,
4132 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00004133 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004134 dex_pc) {
4135 SetPackedField<ComparisonBiasField>(bias);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004136 DCHECK_EQ(comparison_type, DataType::Kind(first->GetType()));
4137 DCHECK_EQ(comparison_type, DataType::Kind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004138 }
4139
Roland Levillain9867bc72015-08-05 10:21:34 +01004140 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004141 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
4142
4143 template <typename T>
4144 int32_t ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004145 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004146 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
4147 // Handle the bias.
4148 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
4149 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004150
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004151 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004152 // Note that there is no "cmp-int" Dex instruction so we shouldn't
4153 // reach this code path when processing a freshly built HIR
4154 // graph. However HCompare integer instructions can be synthesized
4155 // by the instruction simplifier to implement IntegerCompare and
4156 // IntegerSignum intrinsics, so we have to handle this case.
4157 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004158 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004159 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004160 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4161 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004162 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004163 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4164 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004165 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004166 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01004167 }
4168
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004169 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004170 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00004171 }
4172
Vladimir Markoa1de9182016-02-25 11:37:38 +00004173 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04004174
Roland Levillain31dd3d62016-02-16 12:21:02 +00004175 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00004176 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00004177 bool IsGtBias() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004178 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004179 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00004180 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004181
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004182 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type type ATTRIBUTE_UNUSED) {
Roland Levillain1693a1f2016-03-15 14:57:31 +00004183 // Comparisons do not require a runtime call in any back end.
4184 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004185 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004186
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004187 DECLARE_INSTRUCTION(Compare);
4188
Roland Levillain31dd3d62016-02-16 12:21:02 +00004189 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01004190 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004191 static constexpr size_t kFieldComparisonBiasSize =
4192 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
4193 static constexpr size_t kNumberOfComparePackedBits =
4194 kFieldComparisonBias + kFieldComparisonBiasSize;
4195 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4196 using ComparisonBiasField =
4197 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
4198
Roland Levillain31dd3d62016-02-16 12:21:02 +00004199 // Return an integer constant containing the result of a comparison evaluated at compile time.
4200 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
4201 DCHECK(value == -1 || value == 0 || value == 1) << value;
4202 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
4203 }
4204
Artem Serovcced8ba2017-07-19 18:18:09 +01004205 DEFAULT_COPY_CONSTRUCTOR(Compare);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004206};
4207
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004208class HNewInstance final : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00004209 public:
4210 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00004211 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004212 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00004213 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00004214 bool finalizable,
4215 QuickEntrypointEnum entrypoint)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304216 : HExpression(kNewInstance,
4217 DataType::Type::kReference,
4218 SideEffects::CanTriggerGC(),
4219 dex_pc),
David Brazdil6de19382016-01-08 17:37:10 +00004220 type_index_(type_index),
4221 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00004222 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004223 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00004224 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00004225 }
4226
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004227 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004228
Andreas Gampea5b09a62016-11-17 15:21:22 -08004229 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00004230 const DexFile& GetDexFile() const { return dex_file_; }
4231
4232 // Calls runtime so needs an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004233 bool NeedsEnvironment() const override { return true; }
David Brazdil6de19382016-01-08 17:37:10 +00004234
Mingyao Yang062157f2016-03-02 10:15:36 -08004235 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004236 bool CanThrow() const override { return true; }
Mingyao Yang062157f2016-03-02 10:15:36 -08004237
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004238 bool NeedsChecks() const {
4239 return entrypoint_ == kQuickAllocObjectWithChecks;
4240 }
David Brazdil6de19382016-01-08 17:37:10 +00004241
Vladimir Markoa1de9182016-02-25 11:37:38 +00004242 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00004243
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004244 bool CanBeNull() const override { return false; }
David Brazdil6de19382016-01-08 17:37:10 +00004245
4246 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4247
4248 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
4249 entrypoint_ = entrypoint;
4250 }
4251
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004252 HLoadClass* GetLoadClass() const {
4253 HInstruction* input = InputAt(0);
4254 if (input->IsClinitCheck()) {
4255 input = input->InputAt(0);
4256 }
4257 DCHECK(input->IsLoadClass());
4258 return input->AsLoadClass();
4259 }
4260
David Brazdil6de19382016-01-08 17:37:10 +00004261 bool IsStringAlloc() const;
4262
4263 DECLARE_INSTRUCTION(NewInstance);
4264
Artem Serovcced8ba2017-07-19 18:18:09 +01004265 protected:
4266 DEFAULT_COPY_CONSTRUCTOR(NewInstance);
4267
David Brazdil6de19382016-01-08 17:37:10 +00004268 private:
Vladimir Markobd785672018-05-03 17:09:09 +01004269 static constexpr size_t kFlagFinalizable = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004270 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
4271 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
4272 "Too many packed fields.");
4273
Andreas Gampea5b09a62016-11-17 15:21:22 -08004274 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00004275 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00004276 QuickEntrypointEnum entrypoint_;
David Brazdil6de19382016-01-08 17:37:10 +00004277};
4278
Agi Csaki05f20562015-08-19 14:58:14 -07004279enum IntrinsicNeedsEnvironmentOrCache {
4280 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
4281 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07004282};
4283
Aart Bik5d75afe2015-12-14 11:57:01 -08004284enum IntrinsicSideEffects {
4285 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
4286 kReadSideEffects, // Intrinsic may read heap memory.
4287 kWriteSideEffects, // Intrinsic may write heap memory.
4288 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
4289};
4290
4291enum IntrinsicExceptions {
4292 kNoThrow, // Intrinsic does not throw any exceptions.
4293 kCanThrow // Intrinsic may throw exceptions.
4294};
4295
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004296class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004297 public:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004298 bool NeedsEnvironment() const override;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004299
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01004300 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004301 SetRawInputAt(index, argument);
4302 }
4303
Roland Levillain3e3d7332015-04-28 11:00:54 +01004304 // Return the number of arguments. This number can be lower than
4305 // the number of inputs returned by InputCount(), as some invoke
4306 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
4307 // inputs at the end of their list of inputs.
4308 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
4309
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004310 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4311
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004312 InvokeType GetInvokeType() const {
4313 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004314 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004315
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01004316 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004317 return intrinsic_;
4318 }
4319
Aart Bik5d75afe2015-12-14 11:57:01 -08004320 void SetIntrinsic(Intrinsics intrinsic,
4321 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
4322 IntrinsicSideEffects side_effects,
4323 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004324
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004325 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00004326 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004327 }
4328
Aart Bikff7d89c2016-11-07 08:49:28 -08004329 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4330
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004331 bool CanThrow() const override { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004332
Aart Bika8b8e9b2018-01-09 11:01:02 -08004333 void SetAlwaysThrows(bool always_throws) { SetPackedFlag<kFlagAlwaysThrows>(always_throws); }
4334
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004335 bool AlwaysThrows() const override { return GetPackedFlag<kFlagAlwaysThrows>(); }
Aart Bika8b8e9b2018-01-09 11:01:02 -08004336
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004337 bool CanBeMoved() const override { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004338
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004339 bool InstructionDataEquals(const HInstruction* other) const override {
Aart Bik5d75afe2015-12-14 11:57:01 -08004340 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4341 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004342
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004343 uint32_t* GetIntrinsicOptimizations() {
4344 return &intrinsic_optimizations_;
4345 }
4346
4347 const uint32_t* GetIntrinsicOptimizations() const {
4348 return &intrinsic_optimizations_;
4349 }
4350
4351 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4352
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004353 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffray76d4bb0f32018-09-21 12:58:45 +01004354 void SetResolvedMethod(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004355
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004356 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004357
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004358 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004359 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4360 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004361 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
Vladimir Markobd785672018-05-03 17:09:09 +01004362 static constexpr size_t kFlagCanThrow = kFieldInvokeType + kFieldInvokeTypeSize;
Aart Bika8b8e9b2018-01-09 11:01:02 -08004363 static constexpr size_t kFlagAlwaysThrows = kFlagCanThrow + 1;
4364 static constexpr size_t kNumberOfInvokePackedBits = kFlagAlwaysThrows + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004365 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004366 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004367
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304368 HInvoke(InstructionKind kind,
4369 ArenaAllocator* allocator,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004370 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004371 uint32_t number_of_other_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004372 DataType::Type return_type,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004373 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004374 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004375 ArtMethod* resolved_method,
4376 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004377 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304378 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01004379 return_type,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004380 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4381 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004382 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004383 number_of_arguments + number_of_other_inputs,
4384 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004385 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004386 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07004387 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004388 intrinsic_optimizations_(0) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004389 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004390 SetPackedFlag<kFlagCanThrow>(true);
Nicolas Geoffray76d4bb0f32018-09-21 12:58:45 +01004391 // Check mutator lock, constructors lack annotalysis support.
4392 Locks::mutator_lock_->AssertNotExclusiveHeld(Thread::Current());
4393 SetResolvedMethod(resolved_method);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004394 }
4395
Artem Serovcced8ba2017-07-19 18:18:09 +01004396 DEFAULT_COPY_CONSTRUCTOR(Invoke);
4397
Roland Levillain3e3d7332015-04-28 11:00:54 +01004398 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004399 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004400 const uint32_t dex_method_index_;
4401 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004402
4403 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4404 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004405};
4406
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004407class HInvokeUnresolved final : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004408 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004409 HInvokeUnresolved(ArenaAllocator* allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004410 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004411 DataType::Type return_type,
Calin Juravle175dc732015-08-25 15:42:32 +01004412 uint32_t dex_pc,
4413 uint32_t dex_method_index,
4414 InvokeType invoke_type)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304415 : HInvoke(kInvokeUnresolved,
4416 allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004417 number_of_arguments,
Andreas Gampe3db70682018-12-26 15:12:03 -08004418 /* number_of_other_inputs= */ 0u,
Calin Juravle175dc732015-08-25 15:42:32 +01004419 return_type,
4420 dex_pc,
4421 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004422 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01004423 invoke_type) {
4424 }
4425
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004426 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004427
Calin Juravle175dc732015-08-25 15:42:32 +01004428 DECLARE_INSTRUCTION(InvokeUnresolved);
4429
Artem Serovcced8ba2017-07-19 18:18:09 +01004430 protected:
4431 DEFAULT_COPY_CONSTRUCTOR(InvokeUnresolved);
Calin Juravle175dc732015-08-25 15:42:32 +01004432};
4433
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004434class HInvokePolymorphic final : public HInvoke {
Orion Hodsonac141392017-01-13 11:53:47 +00004435 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004436 HInvokePolymorphic(ArenaAllocator* allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004437 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004438 DataType::Type return_type,
Orion Hodsonac141392017-01-13 11:53:47 +00004439 uint32_t dex_pc,
4440 uint32_t dex_method_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304441 : HInvoke(kInvokePolymorphic,
4442 allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004443 number_of_arguments,
Andreas Gampe3db70682018-12-26 15:12:03 -08004444 /* number_of_other_inputs= */ 0u,
Orion Hodsonac141392017-01-13 11:53:47 +00004445 return_type,
4446 dex_pc,
4447 dex_method_index,
4448 nullptr,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304449 kVirtual) {
4450 }
Orion Hodsonac141392017-01-13 11:53:47 +00004451
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004452 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004453
Orion Hodsonac141392017-01-13 11:53:47 +00004454 DECLARE_INSTRUCTION(InvokePolymorphic);
4455
Artem Serovcced8ba2017-07-19 18:18:09 +01004456 protected:
4457 DEFAULT_COPY_CONSTRUCTOR(InvokePolymorphic);
Orion Hodsonac141392017-01-13 11:53:47 +00004458};
4459
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004460class HInvokeCustom final : public HInvoke {
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004461 public:
4462 HInvokeCustom(ArenaAllocator* allocator,
4463 uint32_t number_of_arguments,
4464 uint32_t call_site_index,
4465 DataType::Type return_type,
4466 uint32_t dex_pc)
4467 : HInvoke(kInvokeCustom,
4468 allocator,
4469 number_of_arguments,
Andreas Gampe3db70682018-12-26 15:12:03 -08004470 /* number_of_other_inputs= */ 0u,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004471 return_type,
4472 dex_pc,
Andreas Gampe3db70682018-12-26 15:12:03 -08004473 /* dex_method_index= */ dex::kDexNoIndex,
4474 /* resolved_method= */ nullptr,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004475 kStatic),
4476 call_site_index_(call_site_index) {
4477 }
4478
4479 uint32_t GetCallSiteIndex() const { return call_site_index_; }
4480
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004481 bool IsClonable() const override { return true; }
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004482
4483 DECLARE_INSTRUCTION(InvokeCustom);
4484
4485 protected:
4486 DEFAULT_COPY_CONSTRUCTOR(InvokeCustom);
4487
4488 private:
4489 uint32_t call_site_index_;
4490};
4491
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004492class HInvokeStaticOrDirect final : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004493 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004494 // Requirements of this method call regarding the class
4495 // initialization (clinit) check of its declaring class.
4496 enum class ClinitCheckRequirement {
4497 kNone, // Class already initialized.
4498 kExplicit, // Static call having explicit clinit check as last input.
4499 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004500 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004501 };
4502
Vladimir Marko58155012015-08-19 12:49:41 +00004503 // Determines how to load the target ArtMethod*.
4504 enum class MethodLoadKind {
4505 // Use a String init ArtMethod* loaded from Thread entrypoints.
4506 kStringInit,
4507
4508 // Use the method's own ArtMethod* loaded by the register allocator.
4509 kRecursive,
4510
Vladimir Marko65979462017-05-19 17:25:12 +01004511 // Use PC-relative boot image ArtMethod* address that will be known at link time.
4512 // Used for boot image methods referenced by boot image code.
4513 kBootImageLinkTimePcRelative,
4514
Vladimir Markob066d432018-01-03 13:14:37 +00004515 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
4516 // Used for app->boot calls with relocatable image.
4517 kBootImageRelRo,
4518
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004519 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004520 // Used for methods outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004521 kBssEntry,
Vladimir Marko58155012015-08-19 12:49:41 +00004522
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004523 // Use ArtMethod* at a known address, embed the direct address in the code.
4524 // Used for for JIT-compiled calls.
4525 kJitDirectAddress,
4526
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004527 // Make a runtime call to resolve and call the method. This is the last-resort-kind
4528 // used when other kinds are unimplemented on a particular architecture.
4529 kRuntimeCall,
Vladimir Marko58155012015-08-19 12:49:41 +00004530 };
4531
4532 // Determines the location of the code pointer.
4533 enum class CodePtrLocation {
4534 // Recursive call, use local PC-relative call instruction.
4535 kCallSelf,
4536
Vladimir Marko58155012015-08-19 12:49:41 +00004537 // Use code pointer from the ArtMethod*.
4538 // Used when we don't know the target code. This is also the last-resort-kind used when
4539 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4540 kCallArtMethod,
4541 };
4542
4543 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004544 MethodLoadKind method_load_kind;
4545 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004546 // The method load data holds
4547 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4548 // Note that there are multiple string init methods, each having its own offset.
4549 // - the method address for kDirectAddress
Vladimir Markodc151b22015-10-15 18:02:30 +01004550 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004551 };
4552
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004553 HInvokeStaticOrDirect(ArenaAllocator* allocator,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004554 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004555 DataType::Type return_type,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004556 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004557 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004558 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004559 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004560 InvokeType invoke_type,
4561 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004562 ClinitCheckRequirement clinit_check_requirement)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304563 : HInvoke(kInvokeStaticOrDirect,
4564 allocator,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004565 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004566 // There is potentially one extra argument for the HCurrentMethod node, and
Nicolas Geoffray0846a8f2018-09-12 15:21:07 +01004567 // potentially one other if the clinit check is explicit.
Vladimir Markob554b5a2015-11-06 12:57:55 +00004568 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004569 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004570 return_type,
4571 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004572 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004573 resolved_method,
4574 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004575 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004576 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004577 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4578 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004579
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004580 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004581
Vladimir Markodc151b22015-10-15 18:02:30 +01004582 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004583 bool had_current_method_input = HasCurrentMethodInput();
4584 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4585
4586 // Using the current method is the default and once we find a better
4587 // method load kind, we should not go back to using the current method.
4588 DCHECK(had_current_method_input || !needs_current_method_input);
4589
4590 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004591 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4592 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004593 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004594 dispatch_info_ = dispatch_info;
4595 }
4596
Aart Bik6daebeb2017-04-03 14:35:41 -07004597 DispatchInfo GetDispatchInfo() const {
4598 return dispatch_info_;
4599 }
4600
Vladimir Markoc53c0792015-11-19 15:48:33 +00004601 void AddSpecialInput(HInstruction* input) {
4602 // We allow only one special input.
4603 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4604 DCHECK(InputCount() == GetSpecialInputIndex() ||
4605 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4606 InsertInputAt(GetSpecialInputIndex(), input);
4607 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004608
Vladimir Marko372f10e2016-05-17 16:30:10 +01004609 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004610 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01004611 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4612 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4613 DCHECK(!input_records.empty());
4614 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4615 HInstruction* last_input = input_records.back().GetInstruction();
4616 // Note: `last_input` may be null during arguments setup.
4617 if (last_input != nullptr) {
4618 // `last_input` is the last input of a static invoke marked as having
4619 // an explicit clinit check. It must either be:
4620 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4621 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4622 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4623 }
4624 }
4625 return input_records;
4626 }
4627
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004628 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004629 // We access the method via the dex cache so we can't do an implicit null check.
4630 // TODO: for intrinsics we can generate implicit null checks.
4631 return false;
4632 }
4633
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004634 bool CanBeNull() const override {
Vladimir Markobd785672018-05-03 17:09:09 +01004635 return GetType() == DataType::Type::kReference && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004636 }
4637
Vladimir Markoc53c0792015-11-19 15:48:33 +00004638 // Get the index of the special input, if any.
4639 //
David Brazdil6de19382016-01-08 17:37:10 +00004640 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4641 // method pointer; otherwise there may be one platform-specific special input,
4642 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004643 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004644 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004645
Vladimir Marko58155012015-08-19 12:49:41 +00004646 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4647 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4648 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004649 bool NeedsDexCacheOfDeclaringClass() const override;
Vladimir Marko58155012015-08-19 12:49:41 +00004650 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004651 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kJitDirectAddress; }
Vladimir Marko65979462017-05-19 17:25:12 +01004652 bool HasPcRelativeMethodLoadKind() const {
4653 return GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markob066d432018-01-03 13:14:37 +00004654 GetMethodLoadKind() == MethodLoadKind::kBootImageRelRo ||
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004655 GetMethodLoadKind() == MethodLoadKind::kBssEntry;
Vladimir Marko65979462017-05-19 17:25:12 +01004656 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004657 bool HasCurrentMethodInput() const {
4658 // This function can be called only after the invoke has been fully initialized by the builder.
4659 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004660 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004661 return true;
4662 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004663 DCHECK(InputCount() == GetSpecialInputIndex() ||
4664 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004665 return false;
4666 }
4667 }
Vladimir Marko58155012015-08-19 12:49:41 +00004668
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004669 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004670 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004671 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004672 }
4673
4674 uint64_t GetMethodAddress() const {
4675 DCHECK(HasMethodAddress());
4676 return dispatch_info_.method_load_data;
4677 }
4678
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004679 const DexFile& GetDexFileForPcRelativeDexCache() const;
4680
Vladimir Markoa1de9182016-02-25 11:37:38 +00004681 ClinitCheckRequirement GetClinitCheckRequirement() const {
4682 return GetPackedField<ClinitCheckRequirementField>();
4683 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004684
Roland Levillain4c0eb422015-04-24 16:43:49 +01004685 // Is this instruction a call to a static method?
4686 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004687 return GetInvokeType() == kStatic;
4688 }
4689
4690 MethodReference GetTargetMethod() const {
4691 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004692 }
4693
Vladimir Markofbb184a2015-11-13 14:47:00 +00004694 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4695 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4696 // instruction; only relevant for static calls with explicit clinit check.
4697 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004698 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004699 size_t last_input_index = inputs_.size() - 1u;
4700 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004701 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004702 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004703 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004704 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004705 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004706 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004707 }
4708
4709 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004710 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004711 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004712 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004713 }
4714
4715 // Is this a call to a static method whose declaring class has an
4716 // implicit intialization check requirement?
4717 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004718 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004719 }
4720
Vladimir Markob554b5a2015-11-06 12:57:55 +00004721 // Does this method load kind need the current method as an input?
4722 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004723 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kRuntimeCall;
Vladimir Markob554b5a2015-11-06 12:57:55 +00004724 }
4725
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004726 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004727
Artem Serovcced8ba2017-07-19 18:18:09 +01004728 protected:
4729 DEFAULT_COPY_CONSTRUCTOR(InvokeStaticOrDirect);
4730
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004731 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004732 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004733 static constexpr size_t kFieldClinitCheckRequirementSize =
4734 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4735 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4736 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4737 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4738 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004739 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4740 kFieldClinitCheckRequirement,
4741 kFieldClinitCheckRequirementSize>;
4742
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004743 // Cached values of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffrayea179f42018-02-08 22:30:18 +00004744 const MethodReference target_method_;
Vladimir Marko58155012015-08-19 12:49:41 +00004745 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004746};
Vladimir Markof64242a2015-12-01 14:58:23 +00004747std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004748std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004749
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004750class HInvokeVirtual final : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004751 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004752 HInvokeVirtual(ArenaAllocator* allocator,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004753 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004754 DataType::Type return_type,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004755 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004756 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004757 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004758 uint32_t vtable_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304759 : HInvoke(kInvokeVirtual,
4760 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004761 number_of_arguments,
4762 0u,
4763 return_type,
4764 dex_pc,
4765 dex_method_index,
4766 resolved_method,
4767 kVirtual),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304768 vtable_index_(vtable_index) {
4769 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004770
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004771 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004772
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004773 bool CanBeNull() const override {
Aart Bik71bf7b42016-11-16 10:17:46 -08004774 switch (GetIntrinsic()) {
4775 case Intrinsics::kThreadCurrentThread:
4776 case Intrinsics::kStringBufferAppend:
4777 case Intrinsics::kStringBufferToString:
4778 case Intrinsics::kStringBuilderAppend:
4779 case Intrinsics::kStringBuilderToString:
4780 return false;
4781 default:
4782 return HInvoke::CanBeNull();
4783 }
4784 }
4785
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004786 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004787 // TODO: Add implicit null checks in intrinsics.
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01004788 return (obj == InputAt(0)) && !IsIntrinsic();
Calin Juravle77520bc2015-01-12 18:45:46 +00004789 }
4790
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004791 uint32_t GetVTableIndex() const { return vtable_index_; }
4792
4793 DECLARE_INSTRUCTION(InvokeVirtual);
4794
Artem Serovcced8ba2017-07-19 18:18:09 +01004795 protected:
4796 DEFAULT_COPY_CONSTRUCTOR(InvokeVirtual);
4797
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004798 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004799 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004800 const uint32_t vtable_index_;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004801};
4802
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004803class HInvokeInterface final : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004804 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004805 HInvokeInterface(ArenaAllocator* allocator,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004806 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004807 DataType::Type return_type,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004808 uint32_t dex_pc,
4809 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004810 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004811 uint32_t imt_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304812 : HInvoke(kInvokeInterface,
4813 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004814 number_of_arguments,
4815 0u,
4816 return_type,
4817 dex_pc,
4818 dex_method_index,
4819 resolved_method,
4820 kInterface),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304821 imt_index_(imt_index) {
4822 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004823
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004824 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004825
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004826 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004827 // TODO: Add implicit null checks in intrinsics.
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01004828 return (obj == InputAt(0)) && !IsIntrinsic();
Calin Juravle77520bc2015-01-12 18:45:46 +00004829 }
4830
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004831 bool NeedsDexCacheOfDeclaringClass() const override {
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004832 // The assembly stub currently needs it.
4833 return true;
4834 }
4835
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004836 uint32_t GetImtIndex() const { return imt_index_; }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004837
4838 DECLARE_INSTRUCTION(InvokeInterface);
4839
Artem Serovcced8ba2017-07-19 18:18:09 +01004840 protected:
4841 DEFAULT_COPY_CONSTRUCTOR(InvokeInterface);
4842
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004843 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004844 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004845 const uint32_t imt_index_;
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004846};
4847
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004848class HNeg final : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004849 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004850 HNeg(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304851 : HUnaryOperation(kNeg, result_type, input, dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004852 DCHECK_EQ(result_type, DataType::Kind(input->GetType()));
Roland Levillain937e6cd2016-03-22 11:54:37 +00004853 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004854
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004855 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004856
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004857 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004858 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004859 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004860 HConstant* Evaluate(HLongConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004861 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004862 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004863 HConstant* Evaluate(HFloatConstant* x) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004864 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4865 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004866 HConstant* Evaluate(HDoubleConstant* x) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004867 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4868 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004869
Roland Levillain88cb1752014-10-20 16:36:47 +01004870 DECLARE_INSTRUCTION(Neg);
4871
Artem Serovcced8ba2017-07-19 18:18:09 +01004872 protected:
4873 DEFAULT_COPY_CONSTRUCTOR(Neg);
Roland Levillain88cb1752014-10-20 16:36:47 +01004874};
4875
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004876class HNewArray final : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004877 public:
Vladimir Markob5461632018-10-15 14:24:21 +01004878 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc, size_t component_size_shift)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304879 : HExpression(kNewArray, DataType::Type::kReference, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004880 SetRawInputAt(0, cls);
4881 SetRawInputAt(1, length);
Vladimir Markob5461632018-10-15 14:24:21 +01004882 SetPackedField<ComponentSizeShiftField>(component_size_shift);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004883 }
4884
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004885 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004886
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004887 // Calls runtime so needs an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004888 bool NeedsEnvironment() const override { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004889
Mingyao Yang0c365e62015-03-31 15:09:29 -07004890 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004891 bool CanThrow() const override { return true; }
Mingyao Yang0c365e62015-03-31 15:09:29 -07004892
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004893 bool CanBeNull() const override { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004894
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004895 HLoadClass* GetLoadClass() const {
4896 DCHECK(InputAt(0)->IsLoadClass());
4897 return InputAt(0)->AsLoadClass();
4898 }
4899
4900 HInstruction* GetLength() const {
4901 return InputAt(1);
4902 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004903
Vladimir Markob5461632018-10-15 14:24:21 +01004904 size_t GetComponentSizeShift() {
4905 return GetPackedField<ComponentSizeShiftField>();
4906 }
4907
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004908 DECLARE_INSTRUCTION(NewArray);
4909
Artem Serovcced8ba2017-07-19 18:18:09 +01004910 protected:
4911 DEFAULT_COPY_CONSTRUCTOR(NewArray);
Vladimir Markob5461632018-10-15 14:24:21 +01004912
4913 private:
4914 static constexpr size_t kFieldComponentSizeShift = kNumberOfGenericPackedBits;
4915 static constexpr size_t kFieldComponentSizeShiftSize = MinimumBitsToStore(3u);
4916 static constexpr size_t kNumberOfNewArrayPackedBits =
4917 kFieldComponentSizeShift + kFieldComponentSizeShiftSize;
4918 static_assert(kNumberOfNewArrayPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4919 using ComponentSizeShiftField =
4920 BitField<size_t, kFieldComponentSizeShift, kFieldComponentSizeShift>;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004921};
4922
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004923class HAdd final : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004924 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004925 HAdd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004926 HInstruction* left,
4927 HInstruction* right,
4928 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304929 : HBinaryOperation(kAdd, result_type, left, right, SideEffects::None(), dex_pc) {
4930 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004931
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004932 bool IsCommutative() const override { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004933
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004934 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004935
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004936 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004937 return GetBlock()->GetGraph()->GetIntConstant(
4938 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004939 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004940 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004941 return GetBlock()->GetGraph()->GetLongConstant(
4942 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004943 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004944 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004945 return GetBlock()->GetGraph()->GetFloatConstant(
4946 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4947 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004948 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004949 return GetBlock()->GetGraph()->GetDoubleConstant(
4950 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4951 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004952
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004953 DECLARE_INSTRUCTION(Add);
4954
Artem Serovcced8ba2017-07-19 18:18:09 +01004955 protected:
4956 DEFAULT_COPY_CONSTRUCTOR(Add);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004957};
4958
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004959class HSub final : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004960 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004961 HSub(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004962 HInstruction* left,
4963 HInstruction* right,
4964 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304965 : HBinaryOperation(kSub, result_type, left, right, SideEffects::None(), dex_pc) {
4966 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004967
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004968 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004969
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004970 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004971 return GetBlock()->GetGraph()->GetIntConstant(
4972 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004973 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004974 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004975 return GetBlock()->GetGraph()->GetLongConstant(
4976 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004977 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004978 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004979 return GetBlock()->GetGraph()->GetFloatConstant(
4980 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4981 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004982 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004983 return GetBlock()->GetGraph()->GetDoubleConstant(
4984 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4985 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004986
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004987 DECLARE_INSTRUCTION(Sub);
4988
Artem Serovcced8ba2017-07-19 18:18:09 +01004989 protected:
4990 DEFAULT_COPY_CONSTRUCTOR(Sub);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004991};
4992
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004993class HMul final : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004994 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004995 HMul(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004996 HInstruction* left,
4997 HInstruction* right,
4998 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304999 : HBinaryOperation(kMul, result_type, left, right, SideEffects::None(), dex_pc) {
5000 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005001
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005002 bool IsCommutative() const override { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005003
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005004 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005005
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005006 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005007 return GetBlock()->GetGraph()->GetIntConstant(
5008 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005009 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005010 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005011 return GetBlock()->GetGraph()->GetLongConstant(
5012 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005013 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005014 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005015 return GetBlock()->GetGraph()->GetFloatConstant(
5016 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5017 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005018 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005019 return GetBlock()->GetGraph()->GetDoubleConstant(
5020 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5021 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005022
5023 DECLARE_INSTRUCTION(Mul);
5024
Artem Serovcced8ba2017-07-19 18:18:09 +01005025 protected:
5026 DEFAULT_COPY_CONSTRUCTOR(Mul);
Calin Juravle34bacdf2014-10-07 20:23:36 +01005027};
5028
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005029class HDiv final : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00005030 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005031 HDiv(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005032 HInstruction* left,
5033 HInstruction* right,
5034 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305035 : HBinaryOperation(kDiv, result_type, left, right, SideEffects::None(), dex_pc) {
5036 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00005037
Roland Levillain9867bc72015-08-05 10:21:34 +01005038 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00005039 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005040 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01005041 // Our graph structure ensures we never have 0 for `y` during
5042 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00005043 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00005044 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00005045 return (y == -1) ? -x : x / y;
5046 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005047
Roland Levillain31dd3d62016-02-16 12:21:02 +00005048 template <typename T>
5049 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005050 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00005051 return x / y;
5052 }
5053
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005054 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005055 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005056 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005057 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005058 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005059 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005060 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5061 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005062 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005063 return GetBlock()->GetGraph()->GetFloatConstant(
5064 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5065 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005066 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005067 return GetBlock()->GetGraph()->GetDoubleConstant(
5068 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005069 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00005070
5071 DECLARE_INSTRUCTION(Div);
5072
Artem Serovcced8ba2017-07-19 18:18:09 +01005073 protected:
5074 DEFAULT_COPY_CONSTRUCTOR(Div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00005075};
5076
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005077class HRem final : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00005078 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005079 HRem(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005080 HInstruction* left,
5081 HInstruction* right,
5082 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305083 : HBinaryOperation(kRem, result_type, left, right, SideEffects::None(), dex_pc) {
5084 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005085
Roland Levillain9867bc72015-08-05 10:21:34 +01005086 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00005087 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005088 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01005089 // Our graph structure ensures we never have 0 for `y` during
5090 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00005091 DCHECK_NE(y, 0);
5092 // Special case -1 to avoid getting a SIGFPE on x86(_64).
5093 return (y == -1) ? 0 : x % y;
5094 }
5095
Roland Levillain31dd3d62016-02-16 12:21:02 +00005096 template <typename T>
5097 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005098 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00005099 return std::fmod(x, y);
5100 }
5101
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005102 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005103 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005104 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005105 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005106 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005107 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005108 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005109 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005110 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005111 return GetBlock()->GetGraph()->GetFloatConstant(
5112 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5113 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005114 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005115 return GetBlock()->GetGraph()->GetDoubleConstant(
5116 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5117 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005118
5119 DECLARE_INSTRUCTION(Rem);
5120
Artem Serovcced8ba2017-07-19 18:18:09 +01005121 protected:
5122 DEFAULT_COPY_CONSTRUCTOR(Rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00005123};
5124
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005125class HMin final : public HBinaryOperation {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005126 public:
5127 HMin(DataType::Type result_type,
5128 HInstruction* left,
5129 HInstruction* right,
5130 uint32_t dex_pc)
5131 : HBinaryOperation(kMin, result_type, left, right, SideEffects::None(), dex_pc) {}
5132
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005133 bool IsCommutative() const override { return true; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005134
5135 // Evaluation for integral values.
5136 template <typename T> static T ComputeIntegral(T x, T y) {
5137 return (x <= y) ? x : y;
5138 }
5139
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005140 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005141 return GetBlock()->GetGraph()->GetIntConstant(
5142 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5143 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005144 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005145 return GetBlock()->GetGraph()->GetLongConstant(
5146 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5147 }
5148 // TODO: Evaluation for floating-point values.
5149 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005150 HFloatConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005151 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005152 HDoubleConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005153
5154 DECLARE_INSTRUCTION(Min);
5155
5156 protected:
5157 DEFAULT_COPY_CONSTRUCTOR(Min);
5158};
5159
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005160class HMax final : public HBinaryOperation {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005161 public:
5162 HMax(DataType::Type result_type,
5163 HInstruction* left,
5164 HInstruction* right,
5165 uint32_t dex_pc)
5166 : HBinaryOperation(kMax, result_type, left, right, SideEffects::None(), dex_pc) {}
5167
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005168 bool IsCommutative() const override { return true; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005169
5170 // Evaluation for integral values.
5171 template <typename T> static T ComputeIntegral(T x, T y) {
5172 return (x >= y) ? x : y;
5173 }
5174
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005175 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005176 return GetBlock()->GetGraph()->GetIntConstant(
5177 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5178 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005179 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005180 return GetBlock()->GetGraph()->GetLongConstant(
5181 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5182 }
5183 // TODO: Evaluation for floating-point values.
5184 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005185 HFloatConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005186 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005187 HDoubleConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005188
5189 DECLARE_INSTRUCTION(Max);
5190
5191 protected:
5192 DEFAULT_COPY_CONSTRUCTOR(Max);
5193};
5194
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005195class HAbs final : public HUnaryOperation {
Aart Bik3dad3412018-02-28 12:01:46 -08005196 public:
5197 HAbs(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
5198 : HUnaryOperation(kAbs, result_type, input, dex_pc) {}
5199
5200 // Evaluation for integral values.
5201 template <typename T> static T ComputeIntegral(T x) {
5202 return x < 0 ? -x : x;
5203 }
5204
5205 // Evaluation for floating-point values.
5206 // Note, as a "quality of implementation", rather than pure "spec compliance",
5207 // we require that Math.abs() clears the sign bit (but changes nothing else)
5208 // for all floating-point numbers, including NaN (signaling NaN may become quiet though).
5209 // http://b/30758343
5210 template <typename T, typename S> static T ComputeFP(T x) {
5211 S bits = bit_cast<S, T>(x);
5212 return bit_cast<T, S>(bits & std::numeric_limits<S>::max());
5213 }
5214
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005215 HConstant* Evaluate(HIntConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005216 return GetBlock()->GetGraph()->GetIntConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5217 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005218 HConstant* Evaluate(HLongConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005219 return GetBlock()->GetGraph()->GetLongConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5220 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005221 HConstant* Evaluate(HFloatConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005222 return GetBlock()->GetGraph()->GetFloatConstant(
5223 ComputeFP<float, int32_t>(x->GetValue()), GetDexPc());
5224 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005225 HConstant* Evaluate(HDoubleConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005226 return GetBlock()->GetGraph()->GetDoubleConstant(
5227 ComputeFP<double, int64_t>(x->GetValue()), GetDexPc());
5228 }
5229
5230 DECLARE_INSTRUCTION(Abs);
5231
5232 protected:
5233 DEFAULT_COPY_CONSTRUCTOR(Abs);
5234};
5235
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005236class HDivZeroCheck final : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00005237 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00005238 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01005239 // constructor. However it can only do it on a fatal slow path so execution never returns to the
5240 // instruction following the current one; thus 'SideEffects::None()' is used.
Calin Juravled0d48522014-11-04 16:40:20 +00005241 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01005242 : HExpression(kDivZeroCheck, value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00005243 SetRawInputAt(0, value);
5244 }
5245
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005246 bool IsClonable() const override { return true; }
5247 bool CanBeMoved() const override { return true; }
Calin Juravled0d48522014-11-04 16:40:20 +00005248
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005249 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Calin Juravled0d48522014-11-04 16:40:20 +00005250 return true;
5251 }
5252
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005253 bool NeedsEnvironment() const override { return true; }
5254 bool CanThrow() const override { return true; }
Calin Juravled0d48522014-11-04 16:40:20 +00005255
Calin Juravled0d48522014-11-04 16:40:20 +00005256 DECLARE_INSTRUCTION(DivZeroCheck);
5257
Artem Serovcced8ba2017-07-19 18:18:09 +01005258 protected:
5259 DEFAULT_COPY_CONSTRUCTOR(DivZeroCheck);
Calin Juravled0d48522014-11-04 16:40:20 +00005260};
5261
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005262class HShl final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005263 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005264 HShl(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005265 HInstruction* value,
5266 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005267 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305268 : HBinaryOperation(kShl, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005269 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5270 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005271 }
5272
Roland Levillain5b5b9312016-03-22 14:57:31 +00005273 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005274 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005275 return value << (distance & max_shift_distance);
5276 }
5277
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005278 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005279 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005280 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005281 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005282 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005283 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005284 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005285 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005286 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005287 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005288 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5289 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005290 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005291 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005292 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005293 LOG(FATAL) << DebugName() << " is not defined for float values";
5294 UNREACHABLE();
5295 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005296 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005297 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005298 LOG(FATAL) << DebugName() << " is not defined for double values";
5299 UNREACHABLE();
5300 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005301
5302 DECLARE_INSTRUCTION(Shl);
5303
Artem Serovcced8ba2017-07-19 18:18:09 +01005304 protected:
5305 DEFAULT_COPY_CONSTRUCTOR(Shl);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005306};
5307
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005308class HShr final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005309 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005310 HShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005311 HInstruction* value,
5312 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005313 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305314 : HBinaryOperation(kShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005315 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5316 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005317 }
5318
Roland Levillain5b5b9312016-03-22 14:57:31 +00005319 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005320 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005321 return value >> (distance & max_shift_distance);
5322 }
5323
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005324 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005325 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005326 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005327 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005328 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005329 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005330 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005331 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005332 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005333 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005334 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5335 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005336 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005337 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005338 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005339 LOG(FATAL) << DebugName() << " is not defined for float values";
5340 UNREACHABLE();
5341 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005342 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005343 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005344 LOG(FATAL) << DebugName() << " is not defined for double values";
5345 UNREACHABLE();
5346 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005347
5348 DECLARE_INSTRUCTION(Shr);
5349
Artem Serovcced8ba2017-07-19 18:18:09 +01005350 protected:
5351 DEFAULT_COPY_CONSTRUCTOR(Shr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005352};
5353
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005354class HUShr final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005355 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005356 HUShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005357 HInstruction* value,
5358 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005359 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305360 : HBinaryOperation(kUShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005361 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5362 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00005363 }
5364
Roland Levillain5b5b9312016-03-22 14:57:31 +00005365 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005366 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005367 typedef typename std::make_unsigned<T>::type V;
5368 V ux = static_cast<V>(value);
5369 return static_cast<T>(ux >> (distance & max_shift_distance));
5370 }
5371
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005372 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005373 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005374 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005375 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005376 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005377 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005378 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005379 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005380 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005381 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005382 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5383 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00005384 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005385 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005386 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005387 LOG(FATAL) << DebugName() << " is not defined for float values";
5388 UNREACHABLE();
5389 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005390 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005391 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005392 LOG(FATAL) << DebugName() << " is not defined for double values";
5393 UNREACHABLE();
5394 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005395
5396 DECLARE_INSTRUCTION(UShr);
5397
Artem Serovcced8ba2017-07-19 18:18:09 +01005398 protected:
5399 DEFAULT_COPY_CONSTRUCTOR(UShr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005400};
5401
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005402class HAnd final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005403 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005404 HAnd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005405 HInstruction* left,
5406 HInstruction* right,
5407 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305408 : HBinaryOperation(kAnd, result_type, left, right, SideEffects::None(), dex_pc) {
5409 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005410
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005411 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005412
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005413 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005414
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005415 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005416 return GetBlock()->GetGraph()->GetIntConstant(
5417 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005418 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005419 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005420 return GetBlock()->GetGraph()->GetLongConstant(
5421 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005422 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005423 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005424 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005425 LOG(FATAL) << DebugName() << " is not defined for float values";
5426 UNREACHABLE();
5427 }
5428 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005429 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005430 LOG(FATAL) << DebugName() << " is not defined for double values";
5431 UNREACHABLE();
5432 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005433
5434 DECLARE_INSTRUCTION(And);
5435
Artem Serovcced8ba2017-07-19 18:18:09 +01005436 protected:
5437 DEFAULT_COPY_CONSTRUCTOR(And);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005438};
5439
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005440class HOr final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005441 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005442 HOr(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005443 HInstruction* left,
5444 HInstruction* right,
5445 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305446 : HBinaryOperation(kOr, result_type, left, right, SideEffects::None(), dex_pc) {
5447 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005448
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005449 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005450
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005451 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005452
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005453 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005454 return GetBlock()->GetGraph()->GetIntConstant(
5455 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005456 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005457 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005458 return GetBlock()->GetGraph()->GetLongConstant(
5459 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005460 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005461 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005462 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005463 LOG(FATAL) << DebugName() << " is not defined for float values";
5464 UNREACHABLE();
5465 }
5466 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005467 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005468 LOG(FATAL) << DebugName() << " is not defined for double values";
5469 UNREACHABLE();
5470 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005471
5472 DECLARE_INSTRUCTION(Or);
5473
Artem Serovcced8ba2017-07-19 18:18:09 +01005474 protected:
5475 DEFAULT_COPY_CONSTRUCTOR(Or);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005476};
5477
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005478class HXor final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005479 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005480 HXor(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005481 HInstruction* left,
5482 HInstruction* right,
5483 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305484 : HBinaryOperation(kXor, result_type, left, right, SideEffects::None(), dex_pc) {
5485 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005486
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005487 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005488
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005489 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005490
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005491 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005492 return GetBlock()->GetGraph()->GetIntConstant(
5493 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005494 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005495 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005496 return GetBlock()->GetGraph()->GetLongConstant(
5497 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005498 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005499 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005500 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005501 LOG(FATAL) << DebugName() << " is not defined for float values";
5502 UNREACHABLE();
5503 }
5504 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005505 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005506 LOG(FATAL) << DebugName() << " is not defined for double values";
5507 UNREACHABLE();
5508 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005509
5510 DECLARE_INSTRUCTION(Xor);
5511
Artem Serovcced8ba2017-07-19 18:18:09 +01005512 protected:
5513 DEFAULT_COPY_CONSTRUCTOR(Xor);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005514};
5515
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005516class HRor final : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005517 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005518 HRor(DataType::Type result_type, HInstruction* value, HInstruction* distance)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305519 : HBinaryOperation(kRor, result_type, value, distance) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005520 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5521 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00005522 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005523
Roland Levillain5b5b9312016-03-22 14:57:31 +00005524 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005525 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005526 typedef typename std::make_unsigned<T>::type V;
5527 V ux = static_cast<V>(value);
5528 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005529 return static_cast<T>(ux);
5530 } else {
5531 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005532 return static_cast<T>(ux >> (distance & max_shift_value)) |
5533 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005534 }
5535 }
5536
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005537 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005538 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005539 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005540 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005541 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005542 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005543 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005544 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005545 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005546 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005547 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5548 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005549 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005550 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005551 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005552 LOG(FATAL) << DebugName() << " is not defined for float values";
5553 UNREACHABLE();
5554 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005555 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005556 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005557 LOG(FATAL) << DebugName() << " is not defined for double values";
5558 UNREACHABLE();
5559 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005560
5561 DECLARE_INSTRUCTION(Ror);
5562
Artem Serovcced8ba2017-07-19 18:18:09 +01005563 protected:
5564 DEFAULT_COPY_CONSTRUCTOR(Ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005565};
5566
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005567// The value of a parameter in this method. Its location depends on
5568// the calling convention.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005569class HParameterValue final : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005570 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005571 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005572 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005573 uint8_t index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005574 DataType::Type parameter_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005575 bool is_this = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305576 : HExpression(kParameterValue, parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005577 dex_file_(dex_file),
5578 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005579 index_(index) {
5580 SetPackedFlag<kFlagIsThis>(is_this);
5581 SetPackedFlag<kFlagCanBeNull>(!is_this);
5582 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005583
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005584 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005585 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005586 uint8_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07005587 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005588
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005589 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005590 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005591
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005592 DECLARE_INSTRUCTION(ParameterValue);
5593
Artem Serovcced8ba2017-07-19 18:18:09 +01005594 protected:
5595 DEFAULT_COPY_CONSTRUCTOR(ParameterValue);
5596
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005597 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005598 // Whether or not the parameter value corresponds to 'this' argument.
Vladimir Markobd785672018-05-03 17:09:09 +01005599 static constexpr size_t kFlagIsThis = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005600 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5601 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5602 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5603 "Too many packed fields.");
5604
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005605 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005606 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005607 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005608 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005609 const uint8_t index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005610};
5611
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005612class HNot final : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005613 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005614 HNot(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305615 : HUnaryOperation(kNot, result_type, input, dex_pc) {
5616 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005617
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005618 bool CanBeMoved() const override { return true; }
5619 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005620 return true;
5621 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005622
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005623 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005624
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005625 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005626 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005627 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005628 HConstant* Evaluate(HLongConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005629 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005630 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005631 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005632 LOG(FATAL) << DebugName() << " is not defined for float values";
5633 UNREACHABLE();
5634 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005635 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005636 LOG(FATAL) << DebugName() << " is not defined for double values";
5637 UNREACHABLE();
5638 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005639
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005640 DECLARE_INSTRUCTION(Not);
5641
Artem Serovcced8ba2017-07-19 18:18:09 +01005642 protected:
5643 DEFAULT_COPY_CONSTRUCTOR(Not);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005644};
5645
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005646class HBooleanNot final : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005647 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005648 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305649 : HUnaryOperation(kBooleanNot, DataType::Type::kBool, input, dex_pc) {
5650 }
David Brazdil66d126e2015-04-03 16:02:44 +01005651
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005652 bool CanBeMoved() const override { return true; }
5653 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
David Brazdil66d126e2015-04-03 16:02:44 +01005654 return true;
5655 }
5656
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005657 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005658 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005659 return !x;
5660 }
5661
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005662 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005663 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005664 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005665 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005666 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005667 UNREACHABLE();
5668 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005669 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005670 LOG(FATAL) << DebugName() << " is not defined for float values";
5671 UNREACHABLE();
5672 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005673 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005674 LOG(FATAL) << DebugName() << " is not defined for double values";
5675 UNREACHABLE();
5676 }
David Brazdil66d126e2015-04-03 16:02:44 +01005677
5678 DECLARE_INSTRUCTION(BooleanNot);
5679
Artem Serovcced8ba2017-07-19 18:18:09 +01005680 protected:
5681 DEFAULT_COPY_CONSTRUCTOR(BooleanNot);
David Brazdil66d126e2015-04-03 16:02:44 +01005682};
5683
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005684class HTypeConversion final : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005685 public:
5686 // Instantiate a type conversion of `input` to `result_type`.
Vladimir Markoc8fb2112017-10-03 11:37:52 +01005687 HTypeConversion(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305688 : HExpression(kTypeConversion, result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005689 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005690 // Invariant: We should never generate a conversion to a Boolean value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005691 DCHECK_NE(DataType::Type::kBool, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005692 }
5693
5694 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005695 DataType::Type GetInputType() const { return GetInput()->GetType(); }
5696 DataType::Type GetResultType() const { return GetType(); }
Roland Levillaindff1f282014-11-05 14:15:05 +00005697
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005698 bool IsClonable() const override { return true; }
5699 bool CanBeMoved() const override { return true; }
5700 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01005701 return true;
5702 }
Nicolas Geoffrayacc56ac2018-10-09 08:45:24 +01005703 // Return whether the conversion is implicit. This includes conversion to the same type.
5704 bool IsImplicitConversion() const {
5705 return DataType::IsTypeConversionImplicit(GetInputType(), GetResultType());
5706 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005707
Mark Mendelle82549b2015-05-06 10:55:34 -04005708 // Try to statically evaluate the conversion and return a HConstant
5709 // containing the result. If the input cannot be converted, return nullptr.
5710 HConstant* TryStaticEvaluation() const;
5711
Roland Levillaindff1f282014-11-05 14:15:05 +00005712 DECLARE_INSTRUCTION(TypeConversion);
5713
Artem Serovcced8ba2017-07-19 18:18:09 +01005714 protected:
5715 DEFAULT_COPY_CONSTRUCTOR(TypeConversion);
Roland Levillaindff1f282014-11-05 14:15:05 +00005716};
5717
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005718static constexpr uint32_t kNoRegNumber = -1;
5719
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005720class HNullCheck final : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005721 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005722 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01005723 // constructor. However it can only do it on a fatal slow path so execution never returns to the
5724 // instruction following the current one; thus 'SideEffects::None()' is used.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005725 HNullCheck(HInstruction* value, uint32_t dex_pc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01005726 : HExpression(kNullCheck, value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005727 SetRawInputAt(0, value);
5728 }
5729
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005730 bool IsClonable() const override { return true; }
5731 bool CanBeMoved() const override { return true; }
5732 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005733 return true;
5734 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005735
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005736 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005737
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005738 bool CanThrow() const override { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00005739
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005740 bool CanBeNull() const override { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005741
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005742 DECLARE_INSTRUCTION(NullCheck);
5743
Artem Serovcced8ba2017-07-19 18:18:09 +01005744 protected:
5745 DEFAULT_COPY_CONSTRUCTOR(NullCheck);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005746};
5747
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005748// Embeds an ArtField and all the information required by the compiler. We cache
5749// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005750class FieldInfo : public ValueObject {
5751 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005752 FieldInfo(ArtField* field,
5753 MemberOffset field_offset,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005754 DataType::Type field_type,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005755 bool is_volatile,
5756 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005757 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005758 const DexFile& dex_file)
5759 : field_(field),
5760 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005761 field_type_(field_type),
5762 is_volatile_(is_volatile),
5763 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005764 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005765 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005766
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005767 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005768 MemberOffset GetFieldOffset() const { return field_offset_; }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005769 DataType::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005770 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005771 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005772 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005773 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005774
5775 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005776 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005777 const MemberOffset field_offset_;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005778 const DataType::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005779 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005780 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005781 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005782 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005783};
5784
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005785class HInstanceFieldGet final : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005786 public:
5787 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005788 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005789 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005790 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005791 bool is_volatile,
5792 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005793 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005794 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005795 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305796 : HExpression(kInstanceFieldGet,
5797 field_type,
5798 SideEffects::FieldReadOfType(field_type, is_volatile),
5799 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005800 field_info_(field,
5801 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005802 field_type,
5803 is_volatile,
5804 field_idx,
5805 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005806 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005807 SetRawInputAt(0, value);
5808 }
5809
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005810 bool IsClonable() const override { return true; }
5811 bool CanBeMoved() const override { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005812
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005813 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01005814 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005815 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005816 }
5817
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005818 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005819 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005820 }
5821
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005822 size_t ComputeHashCode() const override {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005823 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5824 }
5825
Calin Juravle52c48962014-12-16 17:02:57 +00005826 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005827 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005828 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005829 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005830
Vladimir Marko61b92282017-10-11 13:23:17 +01005831 void SetType(DataType::Type new_type) {
5832 DCHECK(DataType::IsIntegralType(GetType()));
5833 DCHECK(DataType::IsIntegralType(new_type));
5834 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5835 SetPackedField<TypeField>(new_type);
5836 }
5837
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005838 DECLARE_INSTRUCTION(InstanceFieldGet);
5839
Artem Serovcced8ba2017-07-19 18:18:09 +01005840 protected:
5841 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldGet);
5842
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005843 private:
5844 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005845};
5846
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005847class HInstanceFieldSet final : public HExpression<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005848 public:
5849 HInstanceFieldSet(HInstruction* object,
5850 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005851 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005852 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005853 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005854 bool is_volatile,
5855 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005856 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005857 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005858 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01005859 : HExpression(kInstanceFieldSet,
5860 SideEffects::FieldWriteOfType(field_type, is_volatile),
5861 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005862 field_info_(field,
5863 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005864 field_type,
5865 is_volatile,
5866 field_idx,
5867 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005868 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005869 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005870 SetRawInputAt(0, object);
5871 SetRawInputAt(1, value);
5872 }
5873
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005874 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01005875
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005876 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005877 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005878 }
5879
Calin Juravle52c48962014-12-16 17:02:57 +00005880 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005881 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005882 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005883 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005884 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005885 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5886 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005887
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005888 DECLARE_INSTRUCTION(InstanceFieldSet);
5889
Artem Serovcced8ba2017-07-19 18:18:09 +01005890 protected:
5891 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldSet);
5892
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005893 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005894 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5895 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5896 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5897 "Too many packed fields.");
5898
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005899 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005900};
5901
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005902class HArrayGet final : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005903 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005904 HArrayGet(HInstruction* array,
5905 HInstruction* index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005906 DataType::Type type,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005907 uint32_t dex_pc)
5908 : HArrayGet(array,
5909 index,
5910 type,
5911 SideEffects::ArrayReadOfType(type),
5912 dex_pc,
Andreas Gampe3db70682018-12-26 15:12:03 -08005913 /* is_string_char_at= */ false) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305914 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005915
5916 HArrayGet(HInstruction* array,
5917 HInstruction* index,
5918 DataType::Type type,
5919 SideEffects side_effects,
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005920 uint32_t dex_pc,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005921 bool is_string_char_at)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305922 : HExpression(kArrayGet, type, side_effects, dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005923 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005924 SetRawInputAt(0, array);
5925 SetRawInputAt(1, index);
5926 }
5927
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005928 bool IsClonable() const override { return true; }
5929 bool CanBeMoved() const override { return true; }
5930 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005931 return true;
5932 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005933 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00005934 // TODO: We can be smarter here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01005935 // Currently, unless the array is the result of NewArray, the array access is always
5936 // preceded by some form of null NullCheck necessary for the bounds check, usually
5937 // implicit null check on the ArrayLength input to BoundsCheck or Deoptimize for
5938 // dynamic BCE. There are cases when these could be removed to produce better code.
5939 // If we ever add optimizations to do so we should allow an implicit check here
5940 // (as long as the address falls in the first page).
5941 //
5942 // As an example of such fancy optimization, we could eliminate BoundsCheck for
5943 // a = cond ? new int[1] : null;
5944 // a[0]; // The Phi does not need bounds check for either input.
Calin Juravle77520bc2015-01-12 18:45:46 +00005945 return false;
5946 }
5947
David Brazdil4833f5a2015-12-16 10:37:39 +00005948 bool IsEquivalentOf(HArrayGet* other) const {
5949 bool result = (GetDexPc() == other->GetDexPc());
5950 if (kIsDebugBuild && result) {
5951 DCHECK_EQ(GetBlock(), other->GetBlock());
5952 DCHECK_EQ(GetArray(), other->GetArray());
5953 DCHECK_EQ(GetIndex(), other->GetIndex());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005954 if (DataType::IsIntOrLongType(GetType())) {
5955 DCHECK(DataType::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005956 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005957 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
5958 DCHECK(DataType::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005959 }
5960 }
5961 return result;
5962 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005963
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005964 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5965
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005966 HInstruction* GetArray() const { return InputAt(0); }
5967 HInstruction* GetIndex() const { return InputAt(1); }
5968
Vladimir Marko61b92282017-10-11 13:23:17 +01005969 void SetType(DataType::Type new_type) {
5970 DCHECK(DataType::IsIntegralType(GetType()));
5971 DCHECK(DataType::IsIntegralType(new_type));
5972 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5973 SetPackedField<TypeField>(new_type);
5974 }
5975
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005976 DECLARE_INSTRUCTION(ArrayGet);
5977
Artem Serovcced8ba2017-07-19 18:18:09 +01005978 protected:
5979 DEFAULT_COPY_CONSTRUCTOR(ArrayGet);
5980
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005981 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005982 // We treat a String as an array, creating the HArrayGet from String.charAt()
5983 // intrinsic in the instruction simplifier. We can always determine whether
5984 // a particular HArrayGet is actually a String.charAt() by looking at the type
5985 // of the input but that requires holding the mutator lock, so we prefer to use
5986 // a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01005987 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005988 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5989 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5990 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005991};
5992
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005993class HArraySet final : public HExpression<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005994 public:
5995 HArraySet(HInstruction* array,
5996 HInstruction* index,
5997 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005998 DataType::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005999 uint32_t dex_pc)
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006000 : HArraySet(array,
6001 index,
6002 value,
6003 expected_component_type,
6004 // Make a best guess for side effects now, may be refined during SSA building.
6005 ComputeSideEffects(GetComponentType(value->GetType(), expected_component_type)),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306006 dex_pc) {
6007 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006008
6009 HArraySet(HInstruction* array,
6010 HInstruction* index,
6011 HInstruction* value,
6012 DataType::Type expected_component_type,
6013 SideEffects side_effects,
6014 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006015 : HExpression(kArraySet, side_effects, dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006016 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006017 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006018 SetPackedFlag<kFlagValueCanBeNull>(true);
6019 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006020 SetRawInputAt(0, array);
6021 SetRawInputAt(1, index);
6022 SetRawInputAt(2, value);
6023 }
6024
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006025 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006026
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006027 bool NeedsEnvironment() const override {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006028 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006029 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006030 }
6031
Mingyao Yang81014cb2015-06-02 03:16:27 -07006032 // Can throw ArrayStoreException.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006033 bool CanThrow() const override { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07006034
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006035 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00006036 // TODO: Same as for ArrayGet.
6037 return false;
6038 }
6039
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006040 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006041 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006042 }
6043
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006044 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006045 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006046 }
6047
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006048 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006049 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006050 }
6051
Vladimir Markoa1de9182016-02-25 11:37:38 +00006052 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6053 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
6054 bool StaticTypeOfArrayIsObjectArray() const {
6055 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
6056 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006057
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006058 HInstruction* GetArray() const { return InputAt(0); }
6059 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006060 HInstruction* GetValue() const { return InputAt(2); }
6061
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006062 DataType::Type GetComponentType() const {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006063 return GetComponentType(GetValue()->GetType(), GetRawExpectedComponentType());
6064 }
6065
6066 static DataType::Type GetComponentType(DataType::Type value_type,
6067 DataType::Type expected_component_type) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006068 // The Dex format does not type floating point index operations. Since the
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006069 // `expected_component_type` comes from SSA building and can therefore not
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006070 // be correct, we also check what is the value type. If it is a floating
6071 // point type, we must use that type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006072 return ((value_type == DataType::Type::kFloat32) || (value_type == DataType::Type::kFloat64))
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006073 ? value_type
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006074 : expected_component_type;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006075 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01006076
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006077 DataType::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006078 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006079 }
6080
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006081 static SideEffects ComputeSideEffects(DataType::Type type) {
6082 return SideEffects::ArrayWriteOfType(type).Union(SideEffectsForArchRuntimeCalls(type));
Aart Bik18b36ab2016-04-13 16:41:35 -07006083 }
6084
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006085 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type value_type) {
6086 return (value_type == DataType::Type::kReference) ? SideEffects::CanTriggerGC()
6087 : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006088 }
6089
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006090 DECLARE_INSTRUCTION(ArraySet);
6091
Artem Serovcced8ba2017-07-19 18:18:09 +01006092 protected:
6093 DEFAULT_COPY_CONSTRUCTOR(ArraySet);
6094
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006095 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006096 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
6097 static constexpr size_t kFieldExpectedComponentTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006098 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006099 static constexpr size_t kFlagNeedsTypeCheck =
6100 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
6101 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006102 // Cached information for the reference_type_info_ so that codegen
6103 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006104 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
6105 static constexpr size_t kNumberOfArraySetPackedBits =
6106 kFlagStaticTypeOfArrayIsObjectArray + 1;
6107 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6108 using ExpectedComponentTypeField =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006109 BitField<DataType::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006110};
6111
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006112class HArrayLength final : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006113 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006114 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306115 : HExpression(kArrayLength, DataType::Type::kInt32, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006116 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006117 // Note that arrays do not change length, so the instruction does not
6118 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006119 SetRawInputAt(0, array);
6120 }
6121
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006122 bool IsClonable() const override { return true; }
6123 bool CanBeMoved() const override { return true; }
6124 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006125 return true;
6126 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006127 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle641547a2015-04-21 22:08:51 +01006128 return obj == InputAt(0);
6129 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006130
Vladimir Markodce016e2016-04-28 13:10:02 +01006131 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
6132
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006133 DECLARE_INSTRUCTION(ArrayLength);
6134
Artem Serovcced8ba2017-07-19 18:18:09 +01006135 protected:
6136 DEFAULT_COPY_CONSTRUCTOR(ArrayLength);
6137
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006138 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01006139 // We treat a String as an array, creating the HArrayLength from String.length()
6140 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
6141 // determine whether a particular HArrayLength is actually a String.length() by
6142 // looking at the type of the input but that requires holding the mutator lock, so
6143 // we prefer to use a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01006144 static constexpr size_t kFlagIsStringLength = kNumberOfGenericPackedBits;
Vladimir Markodce016e2016-04-28 13:10:02 +01006145 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
6146 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6147 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006148};
6149
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006150class HBoundsCheck final : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006151 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00006152 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01006153 // constructor. However it can only do it on a fatal slow path so execution never returns to the
6154 // instruction following the current one; thus 'SideEffects::None()' is used.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006155 HBoundsCheck(HInstruction* index,
6156 HInstruction* length,
6157 uint32_t dex_pc,
Vladimir Marko0259c242017-12-04 11:27:47 +00006158 bool is_string_char_at = false)
Artem Serovd1aa7d02018-06-22 11:35:46 +01006159 : HExpression(kBoundsCheck, index->GetType(), SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006160 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(index->GetType()));
Vladimir Marko0259c242017-12-04 11:27:47 +00006161 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006162 SetRawInputAt(0, index);
6163 SetRawInputAt(1, length);
6164 }
6165
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006166 bool IsClonable() const override { return true; }
6167 bool CanBeMoved() const override { return true; }
6168 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006169 return true;
6170 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006171
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006172 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006173
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006174 bool CanThrow() const override { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01006175
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00006176 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006177
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006178 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006179
6180 DECLARE_INSTRUCTION(BoundsCheck);
6181
Artem Serovcced8ba2017-07-19 18:18:09 +01006182 protected:
6183 DEFAULT_COPY_CONSTRUCTOR(BoundsCheck);
6184
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006185 private:
Vladimir Markobd785672018-05-03 17:09:09 +01006186 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Markof02046e2018-10-02 15:31:32 +01006187 static constexpr size_t kNumberOfBoundsCheckPackedBits = kFlagIsStringCharAt + 1;
6188 static_assert(kNumberOfBoundsCheckPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6189 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006190};
6191
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006192class HSuspendCheck final : public HExpression<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006193 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00006194 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01006195 : HExpression(kSuspendCheck, SideEffects::CanTriggerGC(), dex_pc),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306196 slow_path_(nullptr) {
6197 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006198
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006199 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006200
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006201 bool NeedsEnvironment() const override {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006202 return true;
6203 }
6204
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006205 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
6206 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006207
6208 DECLARE_INSTRUCTION(SuspendCheck);
6209
Artem Serovcced8ba2017-07-19 18:18:09 +01006210 protected:
6211 DEFAULT_COPY_CONSTRUCTOR(SuspendCheck);
6212
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006213 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006214 // Only used for code generation, in order to share the same slow path between back edges
6215 // of a same loop.
6216 SlowPathCode* slow_path_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006217};
6218
David Srbecky0cf44932015-12-09 14:09:59 +00006219// Pseudo-instruction which provides the native debugger with mapping information.
6220// It ensures that we can generate line number and local variables at this point.
Vladimir Markobd785672018-05-03 17:09:09 +01006221class HNativeDebugInfo : public HExpression<0> {
David Srbecky0cf44932015-12-09 14:09:59 +00006222 public:
6223 explicit HNativeDebugInfo(uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006224 : HExpression<0>(kNativeDebugInfo, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306225 }
David Srbecky0cf44932015-12-09 14:09:59 +00006226
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006227 bool NeedsEnvironment() const override {
David Srbecky0cf44932015-12-09 14:09:59 +00006228 return true;
6229 }
6230
6231 DECLARE_INSTRUCTION(NativeDebugInfo);
6232
Artem Serovcced8ba2017-07-19 18:18:09 +01006233 protected:
6234 DEFAULT_COPY_CONSTRUCTOR(NativeDebugInfo);
David Srbecky0cf44932015-12-09 14:09:59 +00006235};
6236
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006237/**
6238 * Instruction to load a Class object.
6239 */
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006240class HLoadClass final : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006241 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006242 // Determines how to load the Class.
6243 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006244 // We cannot load this class. See HSharpening::SharpenLoadClass.
6245 kInvalid = -1,
6246
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006247 // Use the Class* from the method's own ArtMethod*.
6248 kReferrersClass,
6249
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006250 // Use PC-relative boot image Class* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006251 // Used for boot image classes referenced by boot image code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006252 kBootImageLinkTimePcRelative,
6253
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006254 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006255 // Used for boot image classes referenced by apps in AOT-compiled code.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006256 kBootImageRelRo,
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006257
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006258 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006259 // Used for classes outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006260 kBssEntry,
6261
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006262 // Use a known boot image Class* address, embedded in the code by the codegen.
6263 // Used for boot image classes referenced by apps in JIT-compiled code.
6264 kJitBootImageAddress,
6265
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006266 // Load from the root table associated with the JIT compiled method.
6267 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006268
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006269 // Load using a simple runtime call. This is the fall-back load kind when
6270 // the codegen is unable to use another appropriate kind.
6271 kRuntimeCall,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006272
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006273 kLast = kRuntimeCall
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006274 };
6275
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006276 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08006277 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006278 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006279 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006280 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01006281 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00006282 bool needs_access_check)
Vladimir Markobd785672018-05-03 17:09:09 +01006283 : HInstruction(kLoadClass,
6284 DataType::Type::kReference,
6285 SideEffectsForArchRuntimeCalls(),
6286 dex_pc),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006287 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006288 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006289 dex_file_(dex_file),
Vladimir Marko175e7862018-03-27 09:03:13 +00006290 klass_(klass) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01006291 // Referrers class should not need access check. We never inline unverified
6292 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006293 DCHECK(!is_referrers_class || !needs_access_check);
6294
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006295 SetPackedField<LoadKindField>(
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006296 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kRuntimeCall);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006297 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00006298 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006299 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Marko175e7862018-03-27 09:03:13 +00006300 SetPackedFlag<kFlagValidLoadedClassRTI>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006301 }
6302
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006303 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006304
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006305 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006306
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006307 LoadKind GetLoadKind() const {
6308 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006309 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006310
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006311 bool HasPcRelativeLoadKind() const {
6312 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6313 GetLoadKind() == LoadKind::kBootImageRelRo ||
6314 GetLoadKind() == LoadKind::kBssEntry;
6315 }
6316
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006317 bool CanBeMoved() const override { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006318
Yi Kong39402542019-03-24 02:47:16 -07006319 bool InstructionDataEquals(const HInstruction* other) const override;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006320
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006321 size_t ComputeHashCode() const override { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006322
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006323 bool CanBeNull() const override { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006324
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006325 bool NeedsEnvironment() const override {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006326 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006327 }
6328
Calin Juravle0ba218d2015-05-19 18:46:01 +01006329 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00006330 // The entrypoint the code generator is going to call does not do
6331 // clinit of the class.
6332 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00006333 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006334 }
6335
6336 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00006337 return NeedsAccessCheck() ||
6338 MustGenerateClinitCheck() ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006339 GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006340 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01006341 }
6342
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006343 bool CanThrow() const override {
Vladimir Marko41559982017-01-06 14:04:23 +00006344 return NeedsAccessCheck() ||
6345 MustGenerateClinitCheck() ||
6346 // If the class is in the boot image, the lookup in the runtime call cannot throw.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006347 ((GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006348 GetLoadKind() == LoadKind::kBssEntry) &&
6349 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006350 }
6351
Calin Juravleacf735c2015-02-12 15:25:22 +00006352 ReferenceTypeInfo GetLoadedClassRTI() {
Vladimir Marko175e7862018-03-27 09:03:13 +00006353 if (GetPackedFlag<kFlagValidLoadedClassRTI>()) {
6354 // Note: The is_exact flag from the return value should not be used.
Andreas Gampe3db70682018-12-26 15:12:03 -08006355 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact= */ true);
Vladimir Marko175e7862018-03-27 09:03:13 +00006356 } else {
6357 return ReferenceTypeInfo::CreateInvalid();
6358 }
Calin Juravleacf735c2015-02-12 15:25:22 +00006359 }
6360
Vladimir Marko175e7862018-03-27 09:03:13 +00006361 // Loaded class RTI is marked as valid by RTP if the klass_ is admissible.
6362 void SetValidLoadedClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
6363 DCHECK(klass_ != nullptr);
6364 SetPackedFlag<kFlagValidLoadedClassRTI>(true);
Calin Juravleacf735c2015-02-12 15:25:22 +00006365 }
6366
Andreas Gampea5b09a62016-11-17 15:21:22 -08006367 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006368 const DexFile& GetDexFile() const { return dex_file_; }
6369
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006370 bool NeedsDexCacheOfDeclaringClass() const override {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006371 return GetLoadKind() == LoadKind::kRuntimeCall;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006372 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00006373
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006374 static SideEffects SideEffectsForArchRuntimeCalls() {
6375 return SideEffects::CanTriggerGC();
6376 }
6377
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006378 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006379 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006380 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006381 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006382
Vladimir Markoa9f303c2018-07-20 16:43:56 +01006383 bool MustResolveTypeOnSlowPath() const {
6384 // Check that this instruction has a slow path.
6385 DCHECK(GetLoadKind() != LoadKind::kRuntimeCall); // kRuntimeCall calls on main path.
6386 DCHECK(GetLoadKind() == LoadKind::kBssEntry || MustGenerateClinitCheck());
6387 return GetLoadKind() == LoadKind::kBssEntry;
6388 }
6389
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006390 void MarkInBootImage() {
6391 SetPackedFlag<kFlagIsInBootImage>(true);
6392 }
6393
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006394 void AddSpecialInput(HInstruction* special_input);
6395
6396 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006397 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006398 return ArrayRef<HUserRecord<HInstruction*>>(
6399 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6400 }
6401
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006402 Handle<mirror::Class> GetClass() const {
6403 return klass_;
6404 }
6405
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006406 DECLARE_INSTRUCTION(LoadClass);
6407
Artem Serovcced8ba2017-07-19 18:18:09 +01006408 protected:
6409 DEFAULT_COPY_CONSTRUCTOR(LoadClass);
6410
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006411 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006412 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00006413 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006414 // Whether this instruction must generate the initialization check.
6415 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006416 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006417 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
6418 static constexpr size_t kFieldLoadKindSize =
6419 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
Vladimir Marko175e7862018-03-27 09:03:13 +00006420 static constexpr size_t kFlagValidLoadedClassRTI = kFieldLoadKind + kFieldLoadKindSize;
6421 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagValidLoadedClassRTI + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006422 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006423 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
6424
6425 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006426 return load_kind == LoadKind::kReferrersClass ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006427 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006428 load_kind == LoadKind::kBssEntry ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006429 load_kind == LoadKind::kRuntimeCall;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006430 }
6431
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006432 void SetLoadKindInternal(LoadKind load_kind);
6433
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006434 // The special input is the HCurrentMethod for kRuntimeCall or kReferrersClass.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006435 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006436 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006437 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006438
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006439 // A type index and dex file where the class can be accessed. The dex file can be:
6440 // - The compiling method's dex file if the class is defined there too.
6441 // - The compiling method's dex file if the class is referenced there.
6442 // - The dex file where the class is defined. When the load kind can only be
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006443 // kBssEntry or kRuntimeCall, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08006444 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006445 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006446
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006447 Handle<mirror::Class> klass_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006448};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006449std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
6450
6451// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006452inline void HLoadClass::SetLoadKind(LoadKind load_kind) {
6453 // The load kind should be determined before inserting the instruction to the graph.
6454 DCHECK(GetBlock() == nullptr);
6455 DCHECK(GetEnvironment() == nullptr);
6456 SetPackedField<LoadKindField>(load_kind);
6457 if (load_kind != LoadKind::kRuntimeCall && load_kind != LoadKind::kReferrersClass) {
6458 special_input_ = HUserRecord<HInstruction*>(nullptr);
6459 }
6460 if (!NeedsEnvironment()) {
6461 SetSideEffects(SideEffects::None());
6462 }
6463}
6464
6465// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006466inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006467 // The special input is used for PC-relative loads on some architectures,
6468 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006469 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006470 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006471 GetLoadKind() == LoadKind::kBssEntry ||
6472 GetLoadKind() == LoadKind::kJitBootImageAddress) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006473 DCHECK(special_input_.GetInstruction() == nullptr);
6474 special_input_ = HUserRecord<HInstruction*>(special_input);
6475 special_input->AddUseAt(this, 0);
6476}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006477
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006478class HLoadString final : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006479 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006480 // Determines how to load the String.
6481 enum class LoadKind {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006482 // Use PC-relative boot image String* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006483 // Used for boot image strings referenced by boot image code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006484 kBootImageLinkTimePcRelative,
6485
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006486 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006487 // Used for boot image strings referenced by apps in AOT-compiled code.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006488 kBootImageRelRo,
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006489
Vladimir Markoaad75c62016-10-03 08:46:48 +00006490 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006491 // Used for strings outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Markoaad75c62016-10-03 08:46:48 +00006492 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006493
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006494 // Use a known boot image String* address, embedded in the code by the codegen.
6495 // Used for boot image strings referenced by apps in JIT-compiled code.
6496 kJitBootImageAddress,
6497
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006498 // Load from the root table associated with the JIT compiled method.
6499 kJitTableAddress,
6500
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006501 // Load using a simple runtime call. This is the fall-back load kind when
6502 // the codegen is unable to use another appropriate kind.
6503 kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006504
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006505 kLast = kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006506 };
6507
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006508 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006509 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006510 const DexFile& dex_file,
6511 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006512 : HInstruction(kLoadString,
6513 DataType::Type::kReference,
6514 SideEffectsForArchRuntimeCalls(),
6515 dex_pc),
Vladimir Marko372f10e2016-05-17 16:30:10 +01006516 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006517 string_index_(string_index),
6518 dex_file_(dex_file) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006519 SetPackedField<LoadKindField>(LoadKind::kRuntimeCall);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006520 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006521
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006522 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006523
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006524 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006525
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006526 LoadKind GetLoadKind() const {
6527 return GetPackedField<LoadKindField>();
6528 }
6529
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006530 bool HasPcRelativeLoadKind() const {
6531 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6532 GetLoadKind() == LoadKind::kBootImageRelRo ||
6533 GetLoadKind() == LoadKind::kBssEntry;
6534 }
6535
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006536 const DexFile& GetDexFile() const {
6537 return dex_file_;
6538 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006539
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006540 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006541 return string_index_;
6542 }
6543
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006544 Handle<mirror::String> GetString() const {
6545 return string_;
6546 }
6547
6548 void SetString(Handle<mirror::String> str) {
6549 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006550 }
6551
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006552 bool CanBeMoved() const override { return true; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006553
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006554 bool InstructionDataEquals(const HInstruction* other) const override;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006555
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006556 size_t ComputeHashCode() const override { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006557
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006558 // Will call the runtime if we need to load the string through
6559 // the dex cache and the string is not guaranteed to be there yet.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006560 bool NeedsEnvironment() const override {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006561 LoadKind load_kind = GetLoadKind();
Vladimir Marko764d4542017-05-16 10:31:41 +01006562 if (load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006563 load_kind == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006564 load_kind == LoadKind::kJitBootImageAddress ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006565 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006566 return false;
6567 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006568 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006569 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006570
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006571 bool NeedsDexCacheOfDeclaringClass() const override {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006572 return GetLoadKind() == LoadKind::kRuntimeCall;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006573 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006574
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006575 bool CanBeNull() const override { return false; }
6576 bool CanThrow() const override { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006577
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006578 static SideEffects SideEffectsForArchRuntimeCalls() {
6579 return SideEffects::CanTriggerGC();
6580 }
6581
Vladimir Marko372f10e2016-05-17 16:30:10 +01006582 void AddSpecialInput(HInstruction* special_input);
6583
6584 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006585 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01006586 return ArrayRef<HUserRecord<HInstruction*>>(
6587 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006588 }
6589
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006590 DECLARE_INSTRUCTION(LoadString);
6591
Artem Serovcced8ba2017-07-19 18:18:09 +01006592 protected:
6593 DEFAULT_COPY_CONSTRUCTOR(LoadString);
6594
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006595 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006596 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006597 static constexpr size_t kFieldLoadKindSize =
6598 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
6599 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006600 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006601 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006602
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006603 void SetLoadKindInternal(LoadKind load_kind);
6604
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006605 // The special input is the HCurrentMethod for kRuntimeCall.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006606 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00006607 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01006608 HUserRecord<HInstruction*> special_input_;
6609
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006610 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006611 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006612
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006613 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006614};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006615std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
6616
6617// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006618inline void HLoadString::SetLoadKind(LoadKind load_kind) {
6619 // The load kind should be determined before inserting the instruction to the graph.
6620 DCHECK(GetBlock() == nullptr);
6621 DCHECK(GetEnvironment() == nullptr);
6622 DCHECK_EQ(GetLoadKind(), LoadKind::kRuntimeCall);
6623 SetPackedField<LoadKindField>(load_kind);
6624 if (load_kind != LoadKind::kRuntimeCall) {
6625 special_input_ = HUserRecord<HInstruction*>(nullptr);
6626 }
6627 if (!NeedsEnvironment()) {
6628 SetSideEffects(SideEffects::None());
6629 }
6630}
6631
6632// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006633inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006634 // The special input is used for PC-relative loads on some architectures,
6635 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006636 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006637 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006638 GetLoadKind() == LoadKind::kBssEntry ||
6639 GetLoadKind() == LoadKind::kJitBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01006640 // HLoadString::GetInputRecords() returns an empty array at this point,
6641 // so use the GetInputRecords() from the base class to set the input record.
6642 DCHECK(special_input_.GetInstruction() == nullptr);
6643 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006644 special_input->AddUseAt(this, 0);
6645}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006646
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006647class HLoadMethodHandle final : public HInstruction {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006648 public:
6649 HLoadMethodHandle(HCurrentMethod* current_method,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01006650 uint16_t method_handle_idx,
6651 const DexFile& dex_file,
6652 uint32_t dex_pc)
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006653 : HInstruction(kLoadMethodHandle,
6654 DataType::Type::kReference,
6655 SideEffectsForArchRuntimeCalls(),
6656 dex_pc),
6657 special_input_(HUserRecord<HInstruction*>(current_method)),
6658 method_handle_idx_(method_handle_idx),
6659 dex_file_(dex_file) {
6660 }
6661
6662 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006663 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006664 return ArrayRef<HUserRecord<HInstruction*>>(
6665 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6666 }
6667
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006668 bool IsClonable() const override { return true; }
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006669
6670 uint16_t GetMethodHandleIndex() const { return method_handle_idx_; }
6671
6672 const DexFile& GetDexFile() const { return dex_file_; }
6673
6674 static SideEffects SideEffectsForArchRuntimeCalls() {
6675 return SideEffects::CanTriggerGC();
6676 }
6677
6678 DECLARE_INSTRUCTION(LoadMethodHandle);
6679
6680 protected:
6681 DEFAULT_COPY_CONSTRUCTOR(LoadMethodHandle);
6682
6683 private:
6684 // The special input is the HCurrentMethod for kRuntimeCall.
6685 HUserRecord<HInstruction*> special_input_;
6686
6687 const uint16_t method_handle_idx_;
6688 const DexFile& dex_file_;
6689};
6690
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006691class HLoadMethodType final : public HInstruction {
Orion Hodson18259d72018-04-12 11:18:23 +01006692 public:
6693 HLoadMethodType(HCurrentMethod* current_method,
Orion Hodson06d10a72018-05-14 08:53:38 +01006694 dex::ProtoIndex proto_index,
Orion Hodson18259d72018-04-12 11:18:23 +01006695 const DexFile& dex_file,
6696 uint32_t dex_pc)
6697 : HInstruction(kLoadMethodType,
6698 DataType::Type::kReference,
6699 SideEffectsForArchRuntimeCalls(),
6700 dex_pc),
6701 special_input_(HUserRecord<HInstruction*>(current_method)),
Orion Hodson06d10a72018-05-14 08:53:38 +01006702 proto_index_(proto_index),
Orion Hodson18259d72018-04-12 11:18:23 +01006703 dex_file_(dex_file) {
6704 }
6705
6706 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006707 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Orion Hodson18259d72018-04-12 11:18:23 +01006708 return ArrayRef<HUserRecord<HInstruction*>>(
6709 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6710 }
6711
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006712 bool IsClonable() const override { return true; }
Orion Hodson18259d72018-04-12 11:18:23 +01006713
Orion Hodson06d10a72018-05-14 08:53:38 +01006714 dex::ProtoIndex GetProtoIndex() const { return proto_index_; }
Orion Hodson18259d72018-04-12 11:18:23 +01006715
6716 const DexFile& GetDexFile() const { return dex_file_; }
6717
6718 static SideEffects SideEffectsForArchRuntimeCalls() {
6719 return SideEffects::CanTriggerGC();
6720 }
6721
6722 DECLARE_INSTRUCTION(LoadMethodType);
6723
6724 protected:
6725 DEFAULT_COPY_CONSTRUCTOR(LoadMethodType);
6726
6727 private:
6728 // The special input is the HCurrentMethod for kRuntimeCall.
6729 HUserRecord<HInstruction*> special_input_;
6730
Orion Hodson06d10a72018-05-14 08:53:38 +01006731 const dex::ProtoIndex proto_index_;
Orion Hodson18259d72018-04-12 11:18:23 +01006732 const DexFile& dex_file_;
6733};
6734
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006735/**
6736 * Performs an initialization check on its Class object input.
6737 */
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006738class HClinitCheck final : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006739 public:
Roland Levillain3887c462015-08-12 18:15:42 +01006740 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07006741 : HExpression(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306742 kClinitCheck,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006743 DataType::Type::kReference,
Mingyao Yang217eb062017-12-11 15:20:07 -08006744 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006745 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006746 SetRawInputAt(0, constant);
6747 }
Artem Serova6e26142018-06-19 14:55:17 +01006748 // TODO: Make ClinitCheck clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006749 bool CanBeMoved() const override { return true; }
6750 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006751 return true;
6752 }
6753
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006754 bool NeedsEnvironment() const override {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006755 // May call runtime to initialize the class.
6756 return true;
6757 }
6758
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006759 bool CanThrow() const override { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006760
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006761 HLoadClass* GetLoadClass() const {
6762 DCHECK(InputAt(0)->IsLoadClass());
6763 return InputAt(0)->AsLoadClass();
6764 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006765
6766 DECLARE_INSTRUCTION(ClinitCheck);
6767
Artem Serovcced8ba2017-07-19 18:18:09 +01006768
6769 protected:
6770 DEFAULT_COPY_CONSTRUCTOR(ClinitCheck);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006771};
6772
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006773class HStaticFieldGet final : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006774 public:
6775 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006776 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006777 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006778 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006779 bool is_volatile,
6780 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006781 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006782 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006783 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306784 : HExpression(kStaticFieldGet,
6785 field_type,
6786 SideEffects::FieldReadOfType(field_type, is_volatile),
6787 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006788 field_info_(field,
6789 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006790 field_type,
6791 is_volatile,
6792 field_idx,
6793 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006794 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006795 SetRawInputAt(0, cls);
6796 }
6797
Calin Juravle52c48962014-12-16 17:02:57 +00006798
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006799 bool IsClonable() const override { return true; }
6800 bool CanBeMoved() const override { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006801
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006802 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01006803 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006804 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006805 }
6806
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006807 size_t ComputeHashCode() const override {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006808 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6809 }
6810
Calin Juravle52c48962014-12-16 17:02:57 +00006811 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006812 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006813 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006814 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006815
Vladimir Marko61b92282017-10-11 13:23:17 +01006816 void SetType(DataType::Type new_type) {
6817 DCHECK(DataType::IsIntegralType(GetType()));
6818 DCHECK(DataType::IsIntegralType(new_type));
6819 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6820 SetPackedField<TypeField>(new_type);
6821 }
6822
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006823 DECLARE_INSTRUCTION(StaticFieldGet);
6824
Artem Serovcced8ba2017-07-19 18:18:09 +01006825 protected:
6826 DEFAULT_COPY_CONSTRUCTOR(StaticFieldGet);
6827
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006828 private:
6829 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006830};
6831
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006832class HStaticFieldSet final : public HExpression<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006833 public:
6834 HStaticFieldSet(HInstruction* cls,
6835 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006836 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006837 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006838 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006839 bool is_volatile,
6840 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006841 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006842 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006843 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006844 : HExpression(kStaticFieldSet,
6845 SideEffects::FieldWriteOfType(field_type, is_volatile),
6846 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006847 field_info_(field,
6848 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006849 field_type,
6850 is_volatile,
6851 field_idx,
6852 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006853 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006854 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006855 SetRawInputAt(0, cls);
6856 SetRawInputAt(1, value);
6857 }
6858
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006859 bool IsClonable() const override { return true; }
Calin Juravle52c48962014-12-16 17:02:57 +00006860 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006861 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006862 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006863 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006864
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006865 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006866 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6867 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006868
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006869 DECLARE_INSTRUCTION(StaticFieldSet);
6870
Artem Serovcced8ba2017-07-19 18:18:09 +01006871 protected:
6872 DEFAULT_COPY_CONSTRUCTOR(StaticFieldSet);
6873
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006874 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006875 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6876 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6877 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6878 "Too many packed fields.");
6879
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006880 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006881};
6882
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006883class HUnresolvedInstanceFieldGet final : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006884 public:
6885 HUnresolvedInstanceFieldGet(HInstruction* obj,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006886 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006887 uint32_t field_index,
6888 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306889 : HExpression(kUnresolvedInstanceFieldGet,
6890 field_type,
6891 SideEffects::AllExceptGCDependency(),
6892 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006893 field_index_(field_index) {
6894 SetRawInputAt(0, obj);
6895 }
6896
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006897 bool IsClonable() const override { return true; }
6898 bool NeedsEnvironment() const override { return true; }
6899 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006900
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006901 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006902 uint32_t GetFieldIndex() const { return field_index_; }
6903
6904 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6905
Artem Serovcced8ba2017-07-19 18:18:09 +01006906 protected:
6907 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldGet);
6908
Calin Juravlee460d1d2015-09-29 04:52:17 +01006909 private:
6910 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006911};
6912
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006913class HUnresolvedInstanceFieldSet final : public HExpression<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006914 public:
6915 HUnresolvedInstanceFieldSet(HInstruction* obj,
6916 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006917 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006918 uint32_t field_index,
6919 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006920 : HExpression(kUnresolvedInstanceFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006921 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006922 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006923 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006924 SetRawInputAt(0, obj);
6925 SetRawInputAt(1, value);
6926 }
6927
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006928 bool IsClonable() const override { return true; }
6929 bool NeedsEnvironment() const override { return true; }
6930 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006931
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006932 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006933 uint32_t GetFieldIndex() const { return field_index_; }
6934
6935 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6936
Artem Serovcced8ba2017-07-19 18:18:09 +01006937 protected:
6938 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldSet);
6939
Calin Juravlee460d1d2015-09-29 04:52:17 +01006940 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006941 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6942 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006943 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006944 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6945 kFieldFieldType + kFieldFieldTypeSize;
6946 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6947 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006948 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006949
Calin Juravlee460d1d2015-09-29 04:52:17 +01006950 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006951};
6952
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006953class HUnresolvedStaticFieldGet final : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006954 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006955 HUnresolvedStaticFieldGet(DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006956 uint32_t field_index,
6957 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306958 : HExpression(kUnresolvedStaticFieldGet,
6959 field_type,
6960 SideEffects::AllExceptGCDependency(),
6961 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006962 field_index_(field_index) {
6963 }
6964
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006965 bool IsClonable() const override { return true; }
6966 bool NeedsEnvironment() const override { return true; }
6967 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006968
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006969 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006970 uint32_t GetFieldIndex() const { return field_index_; }
6971
6972 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6973
Artem Serovcced8ba2017-07-19 18:18:09 +01006974 protected:
6975 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldGet);
6976
Calin Juravlee460d1d2015-09-29 04:52:17 +01006977 private:
6978 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006979};
6980
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006981class HUnresolvedStaticFieldSet final : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006982 public:
6983 HUnresolvedStaticFieldSet(HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006984 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006985 uint32_t field_index,
6986 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006987 : HExpression(kUnresolvedStaticFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006988 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006989 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006990 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006991 SetRawInputAt(0, value);
6992 }
6993
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006994 bool IsClonable() const override { return true; }
6995 bool NeedsEnvironment() const override { return true; }
6996 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006997
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006998 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006999 uint32_t GetFieldIndex() const { return field_index_; }
7000
7001 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
7002
Artem Serovcced8ba2017-07-19 18:18:09 +01007003 protected:
7004 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldSet);
7005
Calin Juravlee460d1d2015-09-29 04:52:17 +01007006 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007007 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
7008 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007009 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00007010 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
7011 kFieldFieldType + kFieldFieldTypeSize;
7012 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7013 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007014 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007015
Calin Juravlee460d1d2015-09-29 04:52:17 +01007016 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007017};
7018
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007019// Implement the move-exception DEX instruction.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007020class HLoadException final : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007021 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007022 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307023 : HExpression(kLoadException, DataType::Type::kReference, SideEffects::None(), dex_pc) {
7024 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007025
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007026 bool CanBeNull() const override { return false; }
David Brazdilbbd733e2015-08-18 17:48:17 +01007027
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007028 DECLARE_INSTRUCTION(LoadException);
7029
Artem Serovcced8ba2017-07-19 18:18:09 +01007030 protected:
7031 DEFAULT_COPY_CONSTRUCTOR(LoadException);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007032};
7033
David Brazdilcb1c0552015-08-04 16:22:25 +01007034// Implicit part of move-exception which clears thread-local exception storage.
7035// Must not be removed because the runtime expects the TLS to get cleared.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007036class HClearException final : public HExpression<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01007037 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007038 explicit HClearException(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007039 : HExpression(kClearException, SideEffects::AllWrites(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307040 }
David Brazdilcb1c0552015-08-04 16:22:25 +01007041
7042 DECLARE_INSTRUCTION(ClearException);
7043
Artem Serovcced8ba2017-07-19 18:18:09 +01007044 protected:
7045 DEFAULT_COPY_CONSTRUCTOR(ClearException);
David Brazdilcb1c0552015-08-04 16:22:25 +01007046};
7047
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007048class HThrow final : public HExpression<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007049 public:
7050 HThrow(HInstruction* exception, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007051 : HExpression(kThrow, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007052 SetRawInputAt(0, exception);
7053 }
7054
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007055 bool IsControlFlow() const override { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007056
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007057 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007058
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007059 bool CanThrow() const override { return true; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007060
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007061 bool AlwaysThrows() const override { return true; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08007062
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007063 DECLARE_INSTRUCTION(Throw);
7064
Artem Serovcced8ba2017-07-19 18:18:09 +01007065 protected:
7066 DEFAULT_COPY_CONSTRUCTOR(Throw);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007067};
7068
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007069/**
7070 * Implementation strategies for the code generator of a HInstanceOf
7071 * or `HCheckCast`.
7072 */
7073enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01007074 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007075 kExactCheck, // Can do a single class compare.
7076 kClassHierarchyCheck, // Can just walk the super class chain.
7077 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
7078 kInterfaceCheck, // No optimization yet when checking against an interface.
7079 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00007080 kArrayCheck, // No optimization yet when checking against a generic array.
Vladimir Marko175e7862018-03-27 09:03:13 +00007081 kBitstringCheck, // Compare the type check bitstring.
Vladimir Markoa1de9182016-02-25 11:37:38 +00007082 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007083};
7084
Roland Levillain86503782016-02-11 19:07:30 +00007085std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
7086
Vladimir Marko175e7862018-03-27 09:03:13 +00007087// Note: HTypeCheckInstruction is just a helper class, not an abstract instruction with an
7088// `IsTypeCheckInstruction()`. (New virtual methods in the HInstruction class have a high cost.)
7089class HTypeCheckInstruction : public HVariableInputSizeInstruction {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007090 public:
Vladimir Marko175e7862018-03-27 09:03:13 +00007091 HTypeCheckInstruction(InstructionKind kind,
Vladimir Markobd785672018-05-03 17:09:09 +01007092 DataType::Type type,
Vladimir Marko175e7862018-03-27 09:03:13 +00007093 HInstruction* object,
7094 HInstruction* target_class_or_null,
7095 TypeCheckKind check_kind,
7096 Handle<mirror::Class> klass,
7097 uint32_t dex_pc,
7098 ArenaAllocator* allocator,
7099 HIntConstant* bitstring_path_to_root,
7100 HIntConstant* bitstring_mask,
7101 SideEffects side_effects)
7102 : HVariableInputSizeInstruction(
7103 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01007104 type,
Vladimir Marko175e7862018-03-27 09:03:13 +00007105 side_effects,
7106 dex_pc,
7107 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08007108 /* number_of_inputs= */ check_kind == TypeCheckKind::kBitstringCheck ? 4u : 2u,
Vladimir Marko175e7862018-03-27 09:03:13 +00007109 kArenaAllocTypeCheckInputs),
7110 klass_(klass) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007111 SetPackedField<TypeCheckKindField>(check_kind);
7112 SetPackedFlag<kFlagMustDoNullCheck>(true);
Vladimir Marko175e7862018-03-27 09:03:13 +00007113 SetPackedFlag<kFlagValidTargetClassRTI>(false);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007114 SetRawInputAt(0, object);
Vladimir Marko175e7862018-03-27 09:03:13 +00007115 SetRawInputAt(1, target_class_or_null);
7116 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_path_to_root != nullptr);
7117 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_mask != nullptr);
7118 if (check_kind == TypeCheckKind::kBitstringCheck) {
7119 DCHECK(target_class_or_null->IsNullConstant());
7120 SetRawInputAt(2, bitstring_path_to_root);
7121 SetRawInputAt(3, bitstring_mask);
7122 } else {
7123 DCHECK(target_class_or_null->IsLoadClass());
7124 }
Vladimir Marko87584542017-12-12 17:47:52 +00007125 }
7126
7127 HLoadClass* GetTargetClass() const {
Vladimir Marko175e7862018-03-27 09:03:13 +00007128 DCHECK_NE(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
Vladimir Marko87584542017-12-12 17:47:52 +00007129 HInstruction* load_class = InputAt(1);
7130 DCHECK(load_class->IsLoadClass());
7131 return load_class->AsLoadClass();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007132 }
7133
Vladimir Marko175e7862018-03-27 09:03:13 +00007134 uint32_t GetBitstringPathToRoot() const {
7135 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7136 HInstruction* path_to_root = InputAt(2);
7137 DCHECK(path_to_root->IsIntConstant());
7138 return static_cast<uint32_t>(path_to_root->AsIntConstant()->GetValue());
7139 }
7140
7141 uint32_t GetBitstringMask() const {
7142 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7143 HInstruction* mask = InputAt(3);
7144 DCHECK(mask->IsIntConstant());
7145 return static_cast<uint32_t>(mask->AsIntConstant()->GetValue());
7146 }
7147
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007148 bool IsClonable() const override { return true; }
7149 bool CanBeMoved() const override { return true; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007150
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007151 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko175e7862018-03-27 09:03:13 +00007152 DCHECK(other->IsInstanceOf() || other->IsCheckCast()) << other->DebugName();
7153 return GetPackedFields() == down_cast<const HTypeCheckInstruction*>(other)->GetPackedFields();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007154 }
7155
Andreas Gampe3fbd3ad2018-03-26 21:14:46 +00007156 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
7157 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
7158 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
7159 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
7160
Vladimir Marko175e7862018-03-27 09:03:13 +00007161 ReferenceTypeInfo GetTargetClassRTI() {
7162 if (GetPackedFlag<kFlagValidTargetClassRTI>()) {
7163 // Note: The is_exact flag from the return value should not be used.
Andreas Gampe3db70682018-12-26 15:12:03 -08007164 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact= */ true);
Vladimir Marko175e7862018-03-27 09:03:13 +00007165 } else {
7166 return ReferenceTypeInfo::CreateInvalid();
7167 }
7168 }
7169
7170 // Target class RTI is marked as valid by RTP if the klass_ is admissible.
7171 void SetValidTargetClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
7172 DCHECK(klass_ != nullptr);
7173 SetPackedFlag<kFlagValidTargetClassRTI>(true);
7174 }
7175
7176 Handle<mirror::Class> GetClass() const {
7177 return klass_;
7178 }
7179
7180 protected:
7181 DEFAULT_COPY_CONSTRUCTOR(TypeCheckInstruction);
7182
7183 private:
7184 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
7185 static constexpr size_t kFieldTypeCheckKindSize =
7186 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
7187 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
7188 static constexpr size_t kFlagValidTargetClassRTI = kFlagMustDoNullCheck + 1;
7189 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagValidTargetClassRTI + 1;
7190 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7191 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
7192
7193 Handle<mirror::Class> klass_;
7194};
7195
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007196class HInstanceOf final : public HTypeCheckInstruction {
Vladimir Marko175e7862018-03-27 09:03:13 +00007197 public:
7198 HInstanceOf(HInstruction* object,
7199 HInstruction* target_class_or_null,
7200 TypeCheckKind check_kind,
7201 Handle<mirror::Class> klass,
7202 uint32_t dex_pc,
7203 ArenaAllocator* allocator,
7204 HIntConstant* bitstring_path_to_root,
7205 HIntConstant* bitstring_mask)
7206 : HTypeCheckInstruction(kInstanceOf,
Vladimir Markobd785672018-05-03 17:09:09 +01007207 DataType::Type::kBool,
Vladimir Marko175e7862018-03-27 09:03:13 +00007208 object,
7209 target_class_or_null,
7210 check_kind,
7211 klass,
7212 dex_pc,
7213 allocator,
7214 bitstring_path_to_root,
7215 bitstring_mask,
7216 SideEffectsForArchRuntimeCalls(check_kind)) {}
7217
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007218 bool IsClonable() const override { return true; }
Artem Serova6e26142018-06-19 14:55:17 +01007219
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007220 bool NeedsEnvironment() const override {
Vladimir Marko175e7862018-03-27 09:03:13 +00007221 return CanCallRuntime(GetTypeCheckKind());
7222 }
7223
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007224 static bool CanCallRuntime(TypeCheckKind check_kind) {
7225 // Mips currently does runtime calls for any other checks.
7226 return check_kind != TypeCheckKind::kExactCheck;
7227 }
7228
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007229 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007230 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01007231 }
7232
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007233 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007234
Artem Serovcced8ba2017-07-19 18:18:09 +01007235 protected:
7236 DEFAULT_COPY_CONSTRUCTOR(InstanceOf);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007237};
7238
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007239class HBoundType final : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00007240 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07007241 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307242 : HExpression(kBoundType, DataType::Type::kReference, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00007243 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
7244 SetPackedFlag<kFlagUpperCanBeNull>(true);
7245 SetPackedFlag<kFlagCanBeNull>(true);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007246 DCHECK_EQ(input->GetType(), DataType::Type::kReference);
Calin Juravleb1498f62015-02-16 13:13:29 +00007247 SetRawInputAt(0, input);
7248 }
7249
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007250 bool InstructionDataEquals(const HInstruction* other) const override;
7251 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007252
David Brazdilf5552582015-12-27 13:36:12 +00007253 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007254 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00007255 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00007256 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007257
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007258 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007259 DCHECK(GetUpperCanBeNull() || !can_be_null);
7260 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007261 }
7262
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007263 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007264
Calin Juravleb1498f62015-02-16 13:13:29 +00007265 DECLARE_INSTRUCTION(BoundType);
7266
Artem Serovcced8ba2017-07-19 18:18:09 +01007267 protected:
7268 DEFAULT_COPY_CONSTRUCTOR(BoundType);
7269
Calin Juravleb1498f62015-02-16 13:13:29 +00007270 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007271 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
7272 // is false then CanBeNull() cannot be true).
Vladimir Markobd785672018-05-03 17:09:09 +01007273 static constexpr size_t kFlagUpperCanBeNull = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007274 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
7275 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
7276 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7277
Calin Juravleb1498f62015-02-16 13:13:29 +00007278 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007279 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
7280 // It is used to bound the type in cases like:
7281 // if (x instanceof ClassX) {
7282 // // uper_bound_ will be ClassX
7283 // }
David Brazdilf5552582015-12-27 13:36:12 +00007284 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00007285};
7286
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007287class HCheckCast final : public HTypeCheckInstruction {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007288 public:
7289 HCheckCast(HInstruction* object,
Vladimir Marko175e7862018-03-27 09:03:13 +00007290 HInstruction* target_class_or_null,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007291 TypeCheckKind check_kind,
Vladimir Marko175e7862018-03-27 09:03:13 +00007292 Handle<mirror::Class> klass,
7293 uint32_t dex_pc,
7294 ArenaAllocator* allocator,
7295 HIntConstant* bitstring_path_to_root,
7296 HIntConstant* bitstring_mask)
7297 : HTypeCheckInstruction(kCheckCast,
Vladimir Markobd785672018-05-03 17:09:09 +01007298 DataType::Type::kVoid,
Vladimir Marko175e7862018-03-27 09:03:13 +00007299 object,
7300 target_class_or_null,
7301 check_kind,
7302 klass,
7303 dex_pc,
7304 allocator,
7305 bitstring_path_to_root,
7306 bitstring_mask,
7307 SideEffects::CanTriggerGC()) {}
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007308
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007309 bool IsClonable() const override { return true; }
7310 bool NeedsEnvironment() const override {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007311 // Instruction may throw a CheckCastError.
7312 return true;
7313 }
7314
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007315 bool CanThrow() const override { return true; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007316
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007317 DECLARE_INSTRUCTION(CheckCast);
7318
Artem Serovcced8ba2017-07-19 18:18:09 +01007319 protected:
7320 DEFAULT_COPY_CONSTRUCTOR(CheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007321};
7322
Andreas Gampe26de38b2016-07-27 17:53:11 -07007323/**
7324 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
7325 * @details We define the combined barrier types that are actually required
7326 * by the Java Memory Model, rather than using exactly the terminology from
7327 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
7328 * primitives. Note that the JSR-133 cookbook generally does not deal with
7329 * store atomicity issues, and the recipes there are not always entirely sufficient.
7330 * The current recipe is as follows:
7331 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
7332 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
7333 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
7334 * -# Use StoreStore barrier after all stores but before return from any constructor whose
7335 * class has final fields.
7336 * -# Use NTStoreStore to order non-temporal stores with respect to all later
7337 * store-to-memory instructions. Only generated together with non-temporal stores.
7338 */
7339enum MemBarrierKind {
7340 kAnyStore,
7341 kLoadAny,
7342 kStoreStore,
7343 kAnyAny,
7344 kNTStoreStore,
7345 kLastBarrierKind = kNTStoreStore
7346};
7347std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
7348
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007349class HMemoryBarrier final : public HExpression<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01007350 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007351 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007352 : HExpression(kMemoryBarrier,
7353 SideEffects::AllWritesAndReads(), // Assume write/read on all fields/arrays.
7354 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007355 SetPackedField<BarrierKindField>(barrier_kind);
7356 }
Calin Juravle27df7582015-04-17 19:12:31 +01007357
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007358 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007359
Vladimir Markoa1de9182016-02-25 11:37:38 +00007360 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01007361
7362 DECLARE_INSTRUCTION(MemoryBarrier);
7363
Artem Serovcced8ba2017-07-19 18:18:09 +01007364 protected:
7365 DEFAULT_COPY_CONSTRUCTOR(MemoryBarrier);
7366
Calin Juravle27df7582015-04-17 19:12:31 +01007367 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007368 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
7369 static constexpr size_t kFieldBarrierKindSize =
7370 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
7371 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
7372 kFieldBarrierKind + kFieldBarrierKindSize;
7373 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
7374 "Too many packed fields.");
7375 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01007376};
7377
Igor Murashkind01745e2017-04-05 16:40:31 -07007378// A constructor fence orders all prior stores to fields that could be accessed via a final field of
7379// the specified object(s), with respect to any subsequent store that might "publish"
7380// (i.e. make visible) the specified object to another thread.
7381//
7382// JLS 17.5.1 "Semantics of final fields" states that a freeze action happens
7383// for all final fields (that were set) at the end of the invoked constructor.
7384//
7385// The constructor fence models the freeze actions for the final fields of an object
7386// being constructed (semantically at the end of the constructor). Constructor fences
7387// have a per-object affinity; two separate objects being constructed get two separate
7388// constructor fences.
7389//
7390// (Note: that if calling a super-constructor or forwarding to another constructor,
7391// the freezes would happen at the end of *that* constructor being invoked).
7392//
7393// The memory model guarantees that when the object being constructed is "published" after
7394// constructor completion (i.e. escapes the current thread via a store), then any final field
7395// writes must be observable on other threads (once they observe that publication).
7396//
7397// Further, anything written before the freeze, and read by dereferencing through the final field,
7398// must also be visible (so final object field could itself have an object with non-final fields;
7399// yet the freeze must also extend to them).
7400//
7401// Constructor example:
7402//
7403// class HasFinal {
7404// final int field; Optimizing IR for <init>()V:
7405// HasFinal() {
7406// field = 123; HInstanceFieldSet(this, HasFinal.field, 123)
7407// // freeze(this.field); HConstructorFence(this)
7408// } HReturn
7409// }
7410//
7411// HConstructorFence can serve double duty as a fence for new-instance/new-array allocations of
7412// already-initialized classes; in that case the allocation must act as a "default-initializer"
7413// of the object which effectively writes the class pointer "final field".
7414//
7415// For example, we can model default-initialiation as roughly the equivalent of the following:
7416//
7417// class Object {
7418// private final Class header;
7419// }
7420//
7421// Java code: Optimizing IR:
7422//
7423// T new_instance<T>() {
7424// Object obj = allocate_memory(T.class.size); obj = HInvoke(art_quick_alloc_object, T)
7425// obj.header = T.class; // header write is done by above call.
7426// // freeze(obj.header) HConstructorFence(obj)
7427// return (T)obj;
7428// }
7429//
7430// See also:
Vladimir Markoc1c34522018-10-31 13:56:49 +00007431// * DexCompilationUnit::RequiresConstructorBarrier
Igor Murashkind01745e2017-04-05 16:40:31 -07007432// * QuasiAtomic::ThreadFenceForConstructor
7433//
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007434class HConstructorFence final : public HVariableInputSizeInstruction {
Igor Murashkind01745e2017-04-05 16:40:31 -07007435 // A fence has variable inputs because the inputs can be removed
7436 // after prepare_for_register_allocation phase.
7437 // (TODO: In the future a fence could freeze multiple objects
7438 // after merging two fences together.)
7439 public:
7440 // `fence_object` is the reference that needs to be protected for correct publication.
7441 //
7442 // It makes sense in the following situations:
7443 // * <init> constructors, it's the "this" parameter (i.e. HParameterValue, s.t. IsThis() == true).
7444 // * new-instance-like instructions, it's the return value (i.e. HNewInstance).
7445 //
7446 // After construction the `fence_object` becomes the 0th input.
7447 // This is not an input in a real sense, but just a convenient place to stash the information
7448 // about the associated object.
7449 HConstructorFence(HInstruction* fence_object,
7450 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007451 ArenaAllocator* allocator)
Igor Murashkind01745e2017-04-05 16:40:31 -07007452 // We strongly suspect there is not a more accurate way to describe the fine-grained reordering
7453 // constraints described in the class header. We claim that these SideEffects constraints
7454 // enforce a superset of the real constraints.
7455 //
7456 // The ordering described above is conservatively modeled with SideEffects as follows:
7457 //
7458 // * To prevent reordering of the publication stores:
7459 // ----> "Reads of objects" is the initial SideEffect.
7460 // * For every primitive final field store in the constructor:
7461 // ----> Union that field's type as a read (e.g. "Read of T") into the SideEffect.
7462 // * If there are any stores to reference final fields in the constructor:
7463 // ----> Use a more conservative "AllReads" SideEffect because any stores to any references
7464 // that are reachable from `fence_object` also need to be prevented for reordering
7465 // (and we do not want to do alias analysis to figure out what those stores are).
7466 //
7467 // In the implementation, this initially starts out as an "all reads" side effect; this is an
7468 // even more conservative approach than the one described above, and prevents all of the
7469 // above reordering without analyzing any of the instructions in the constructor.
7470 //
7471 // If in a later phase we discover that there are no writes to reference final fields,
7472 // we can refine the side effect to a smaller set of type reads (see above constraints).
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307473 : HVariableInputSizeInstruction(kConstructorFence,
7474 SideEffects::AllReads(),
Igor Murashkind01745e2017-04-05 16:40:31 -07007475 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007476 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08007477 /* number_of_inputs= */ 1,
Igor Murashkind01745e2017-04-05 16:40:31 -07007478 kArenaAllocConstructorFenceInputs) {
7479 DCHECK(fence_object != nullptr);
7480 SetRawInputAt(0, fence_object);
7481 }
7482
7483 // The object associated with this constructor fence.
7484 //
7485 // (Note: This will be null after the prepare_for_register_allocation phase,
7486 // as all constructor fence inputs are removed there).
7487 HInstruction* GetFenceObject() const {
7488 return InputAt(0);
7489 }
7490
7491 // Find all the HConstructorFence uses (`fence_use`) for `this` and:
7492 // - Delete `fence_use` from `this`'s use list.
7493 // - Delete `this` from `fence_use`'s inputs list.
7494 // - If the `fence_use` is dead, remove it from the graph.
7495 //
7496 // A fence is considered dead once it no longer has any uses
7497 // and all of the inputs are dead.
7498 //
7499 // This must *not* be called during/after prepare_for_register_allocation,
7500 // because that removes all the inputs to the fences but the fence is actually
7501 // still considered live.
Igor Murashkin6ef45672017-08-08 13:59:55 -07007502 //
7503 // Returns how many HConstructorFence instructions were removed from graph.
7504 static size_t RemoveConstructorFences(HInstruction* instruction);
Igor Murashkind01745e2017-04-05 16:40:31 -07007505
Igor Murashkindd018df2017-08-09 10:38:31 -07007506 // Combine all inputs of `this` and `other` instruction and remove
7507 // `other` from the graph.
7508 //
7509 // Inputs are unique after the merge.
7510 //
7511 // Requirement: `this` must not be the same as `other.
7512 void Merge(HConstructorFence* other);
7513
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007514 // Check if this constructor fence is protecting
7515 // an HNewInstance or HNewArray that is also the immediate
7516 // predecessor of `this`.
7517 //
Igor Murashkindd018df2017-08-09 10:38:31 -07007518 // If `ignore_inputs` is true, then the immediate predecessor doesn't need
7519 // to be one of the inputs of `this`.
7520 //
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007521 // Returns the associated HNewArray or HNewInstance,
7522 // or null otherwise.
Igor Murashkindd018df2017-08-09 10:38:31 -07007523 HInstruction* GetAssociatedAllocation(bool ignore_inputs = false);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007524
Igor Murashkind01745e2017-04-05 16:40:31 -07007525 DECLARE_INSTRUCTION(ConstructorFence);
7526
Artem Serovcced8ba2017-07-19 18:18:09 +01007527 protected:
7528 DEFAULT_COPY_CONSTRUCTOR(ConstructorFence);
Igor Murashkind01745e2017-04-05 16:40:31 -07007529};
7530
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007531class HMonitorOperation final : public HExpression<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007532 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007533 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007534 kEnter,
7535 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00007536 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007537 };
7538
7539 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007540 : HExpression(kMonitorOperation,
7541 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
7542 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007543 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007544 SetRawInputAt(0, object);
7545 }
7546
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007547 // Instruction may go into runtime, so we need an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007548 bool NeedsEnvironment() const override { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00007549
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007550 bool CanThrow() const override {
David Brazdilbff75032015-07-08 17:26:51 +00007551 // Verifier guarantees that monitor-exit cannot throw.
7552 // This is important because it allows the HGraphBuilder to remove
7553 // a dead throw-catch loop generated for `synchronized` blocks/methods.
7554 return IsEnter();
7555 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007556
Vladimir Markoa1de9182016-02-25 11:37:38 +00007557 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
7558 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007559
7560 DECLARE_INSTRUCTION(MonitorOperation);
7561
Artem Serovcced8ba2017-07-19 18:18:09 +01007562 protected:
7563 DEFAULT_COPY_CONSTRUCTOR(MonitorOperation);
7564
Calin Juravle52c48962014-12-16 17:02:57 +00007565 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007566 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
7567 static constexpr size_t kFieldOperationKindSize =
7568 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
7569 static constexpr size_t kNumberOfMonitorOperationPackedBits =
7570 kFieldOperationKind + kFieldOperationKindSize;
7571 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7572 "Too many packed fields.");
7573 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007574};
7575
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007576class HSelect final : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00007577 public:
7578 HSelect(HInstruction* condition,
7579 HInstruction* true_value,
7580 HInstruction* false_value,
7581 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307582 : HExpression(kSelect, HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
David Brazdil74eb1b22015-12-14 11:44:01 +00007583 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
7584
7585 // First input must be `true_value` or `false_value` to allow codegens to
7586 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
7587 // that architectures which implement HSelect as a conditional move also
7588 // will not need to invert the condition.
7589 SetRawInputAt(0, false_value);
7590 SetRawInputAt(1, true_value);
7591 SetRawInputAt(2, condition);
7592 }
7593
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007594 bool IsClonable() const override { return true; }
David Brazdil74eb1b22015-12-14 11:44:01 +00007595 HInstruction* GetFalseValue() const { return InputAt(0); }
7596 HInstruction* GetTrueValue() const { return InputAt(1); }
7597 HInstruction* GetCondition() const { return InputAt(2); }
7598
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007599 bool CanBeMoved() const override { return true; }
7600 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01007601 return true;
7602 }
David Brazdil74eb1b22015-12-14 11:44:01 +00007603
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007604 bool CanBeNull() const override {
David Brazdil74eb1b22015-12-14 11:44:01 +00007605 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
7606 }
7607
7608 DECLARE_INSTRUCTION(Select);
7609
Artem Serovcced8ba2017-07-19 18:18:09 +01007610 protected:
7611 DEFAULT_COPY_CONSTRUCTOR(Select);
David Brazdil74eb1b22015-12-14 11:44:01 +00007612};
7613
Vladimir Markof9f64412015-09-02 14:05:49 +01007614class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007615 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01007616 MoveOperands(Location source,
7617 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007618 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007619 HInstruction* instruction)
7620 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007621
7622 Location GetSource() const { return source_; }
7623 Location GetDestination() const { return destination_; }
7624
7625 void SetSource(Location value) { source_ = value; }
7626 void SetDestination(Location value) { destination_ = value; }
7627
7628 // The parallel move resolver marks moves as "in-progress" by clearing the
7629 // destination (but not the source).
7630 Location MarkPending() {
7631 DCHECK(!IsPending());
7632 Location dest = destination_;
7633 destination_ = Location::NoLocation();
7634 return dest;
7635 }
7636
7637 void ClearPending(Location dest) {
7638 DCHECK(IsPending());
7639 destination_ = dest;
7640 }
7641
7642 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00007643 DCHECK(source_.IsValid() || destination_.IsInvalid());
7644 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007645 }
7646
7647 // True if this blocks a move from the given location.
7648 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08007649 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007650 }
7651
7652 // A move is redundant if it's been eliminated, if its source and
7653 // destination are the same, or if its destination is unneeded.
7654 bool IsRedundant() const {
7655 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
7656 }
7657
7658 // We clear both operands to indicate move that's been eliminated.
7659 void Eliminate() {
7660 source_ = destination_ = Location::NoLocation();
7661 }
7662
7663 bool IsEliminated() const {
7664 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
7665 return source_.IsInvalid();
7666 }
7667
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007668 DataType::Type GetType() const { return type_; }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007669
Nicolas Geoffray90218252015-04-15 11:56:51 +01007670 bool Is64BitMove() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007671 return DataType::Is64BitType(type_);
Nicolas Geoffray90218252015-04-15 11:56:51 +01007672 }
7673
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007674 HInstruction* GetInstruction() const { return instruction_; }
7675
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007676 private:
7677 Location source_;
7678 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01007679 // The type this move is for.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007680 DataType::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007681 // The instruction this move is assocatied with. Null when this move is
7682 // for moving an input in the expected locations of user (including a phi user).
7683 // This is only used in debug mode, to ensure we do not connect interval siblings
7684 // in the same parallel move.
7685 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007686};
7687
Roland Levillainc9285912015-12-18 10:38:42 +00007688std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
7689
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007690static constexpr size_t kDefaultNumberOfMoves = 4;
7691
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007692class HParallelMove final : public HExpression<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007693 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007694 explicit HParallelMove(ArenaAllocator* allocator, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007695 : HExpression(kParallelMove, SideEffects::None(), dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007696 moves_(allocator->Adapter(kArenaAllocMoveOperands)) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007697 moves_.reserve(kDefaultNumberOfMoves);
7698 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007699
Nicolas Geoffray90218252015-04-15 11:56:51 +01007700 void AddMove(Location source,
7701 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007702 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007703 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00007704 DCHECK(source.IsValid());
7705 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007706 if (kIsDebugBuild) {
7707 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007708 for (const MoveOperands& move : moves_) {
7709 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007710 // Special case the situation where the move is for the spill slot
7711 // of the instruction.
7712 if ((GetPrevious() == instruction)
7713 || ((GetPrevious() == nullptr)
7714 && instruction->IsPhi()
7715 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007716 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007717 << "Doing parallel moves for the same instruction.";
7718 } else {
7719 DCHECK(false) << "Doing parallel moves for the same instruction.";
7720 }
7721 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00007722 }
7723 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007724 for (const MoveOperands& move : moves_) {
7725 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007726 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01007727 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007728 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007729 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007730 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007731 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007732 }
7733
Vladimir Marko225b6462015-09-28 12:17:40 +01007734 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007735 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007736 }
7737
Vladimir Marko225b6462015-09-28 12:17:40 +01007738 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007739
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01007740 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007741
Artem Serovcced8ba2017-07-19 18:18:09 +01007742 protected:
7743 DEFAULT_COPY_CONSTRUCTOR(ParallelMove);
7744
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007745 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01007746 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007747};
7748
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007749// This instruction computes an intermediate address pointing in the 'middle' of an object. The
7750// result pointer cannot be handled by GC, so extra care is taken to make sure that this value is
7751// never used across anything that can trigger GC.
7752// The result of this instruction is not a pointer in the sense of `DataType::Type::kreference`.
7753// So we represent it by the type `DataType::Type::kInt`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007754class HIntermediateAddress final : public HExpression<2> {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007755 public:
7756 HIntermediateAddress(HInstruction* base_address, HInstruction* offset, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307757 : HExpression(kIntermediateAddress,
7758 DataType::Type::kInt32,
7759 SideEffects::DependsOnGC(),
7760 dex_pc) {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007761 DCHECK_EQ(DataType::Size(DataType::Type::kInt32),
7762 DataType::Size(DataType::Type::kReference))
7763 << "kPrimInt and kPrimNot have different sizes.";
7764 SetRawInputAt(0, base_address);
7765 SetRawInputAt(1, offset);
7766 }
7767
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007768 bool IsClonable() const override { return true; }
7769 bool CanBeMoved() const override { return true; }
7770 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007771 return true;
7772 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007773 bool IsActualObject() const override { return false; }
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007774
7775 HInstruction* GetBaseAddress() const { return InputAt(0); }
7776 HInstruction* GetOffset() const { return InputAt(1); }
7777
7778 DECLARE_INSTRUCTION(IntermediateAddress);
7779
Artem Serovcced8ba2017-07-19 18:18:09 +01007780 protected:
7781 DEFAULT_COPY_CONSTRUCTOR(IntermediateAddress);
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007782};
7783
7784
Mark Mendell0616ae02015-04-17 12:49:27 -04007785} // namespace art
7786
Aart Bikf8f5a162017-02-06 15:35:29 -08007787#include "nodes_vector.h"
7788
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007789#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
7790#include "nodes_shared.h"
7791#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007792#ifdef ART_ENABLE_CODEGEN_mips
7793#include "nodes_mips.h"
7794#endif
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05307795#if defined(ART_ENABLE_CODEGEN_x86) || defined(ART_ENABLE_CODEGEN_x86_64)
Mark Mendell0616ae02015-04-17 12:49:27 -04007796#include "nodes_x86.h"
7797#endif
7798
7799namespace art {
7800
Igor Murashkin6ef45672017-08-08 13:59:55 -07007801class OptimizingCompilerStats;
7802
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007803class HGraphVisitor : public ValueObject {
7804 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007805 explicit HGraphVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7806 : stats_(stats),
7807 graph_(graph) {}
Dave Allison20dfc792014-06-16 20:44:29 -07007808 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007809
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007810 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007811 virtual void VisitBasicBlock(HBasicBlock* block);
7812
Roland Levillain633021e2014-10-01 14:12:25 +01007813 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007814 void VisitInsertionOrder();
7815
Roland Levillain633021e2014-10-01 14:12:25 +01007816 // Visit the graph following dominator tree reverse post-order.
7817 void VisitReversePostOrder();
7818
Nicolas Geoffray787c3072014-03-17 10:20:19 +00007819 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007820
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007821 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007822#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007823 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
7824
7825 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7826
7827#undef DECLARE_VISIT_INSTRUCTION
7828
Igor Murashkin6ef45672017-08-08 13:59:55 -07007829 protected:
7830 OptimizingCompilerStats* stats_;
7831
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007832 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07007833 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007834
7835 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
7836};
7837
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007838class HGraphDelegateVisitor : public HGraphVisitor {
7839 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007840 explicit HGraphDelegateVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7841 : HGraphVisitor(graph, stats) {}
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007842 virtual ~HGraphDelegateVisitor() {}
7843
7844 // Visit functions that delegate to to super class.
7845#define DECLARE_VISIT_INSTRUCTION(name, super) \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007846 void Visit##name(H##name* instr) override { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007847
7848 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7849
7850#undef DECLARE_VISIT_INSTRUCTION
7851
7852 private:
7853 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
7854};
7855
Artem Serovcced8ba2017-07-19 18:18:09 +01007856// Create a clone of the instruction, insert it into the graph; replace the old one with a new
7857// and remove the old instruction.
7858HInstruction* ReplaceInstrOrPhiByClone(HInstruction* instr);
7859
7860// Create a clone for each clonable instructions/phis and replace the original with the clone.
7861//
7862// Used for testing individual instruction cloner.
7863class CloneAndReplaceInstructionVisitor : public HGraphDelegateVisitor {
7864 public:
7865 explicit CloneAndReplaceInstructionVisitor(HGraph* graph)
Artem Serov7f4aff62017-06-21 17:02:18 +01007866 : HGraphDelegateVisitor(graph), instr_replaced_by_clones_count_(0) {}
Artem Serovcced8ba2017-07-19 18:18:09 +01007867
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007868 void VisitInstruction(HInstruction* instruction) override {
Artem Serovcced8ba2017-07-19 18:18:09 +01007869 if (instruction->IsClonable()) {
7870 ReplaceInstrOrPhiByClone(instruction);
Artem Serov7f4aff62017-06-21 17:02:18 +01007871 instr_replaced_by_clones_count_++;
Artem Serovcced8ba2017-07-19 18:18:09 +01007872 }
7873 }
7874
Artem Serov7f4aff62017-06-21 17:02:18 +01007875 size_t GetInstrReplacedByClonesCount() const { return instr_replaced_by_clones_count_; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007876
7877 private:
Artem Serov7f4aff62017-06-21 17:02:18 +01007878 size_t instr_replaced_by_clones_count_;
Artem Serovcced8ba2017-07-19 18:18:09 +01007879
7880 DISALLOW_COPY_AND_ASSIGN(CloneAndReplaceInstructionVisitor);
7881};
7882
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007883// Iterator over the blocks that art part of the loop. Includes blocks part
7884// of an inner loop. The order in which the blocks are iterated is on their
7885// block id.
7886class HBlocksInLoopIterator : public ValueObject {
7887 public:
7888 explicit HBlocksInLoopIterator(const HLoopInformation& info)
7889 : blocks_in_loop_(info.GetBlocks()),
7890 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
7891 index_(0) {
7892 if (!blocks_in_loop_.IsBitSet(index_)) {
7893 Advance();
7894 }
7895 }
7896
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007897 bool Done() const { return index_ == blocks_.size(); }
7898 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007899 void Advance() {
7900 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007901 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007902 if (blocks_in_loop_.IsBitSet(index_)) {
7903 break;
7904 }
7905 }
7906 }
7907
7908 private:
7909 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007910 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007911 size_t index_;
7912
7913 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
7914};
7915
Mingyao Yang3584bce2015-05-19 16:01:59 -07007916// Iterator over the blocks that art part of the loop. Includes blocks part
7917// of an inner loop. The order in which the blocks are iterated is reverse
7918// post order.
7919class HBlocksInLoopReversePostOrderIterator : public ValueObject {
7920 public:
7921 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
7922 : blocks_in_loop_(info.GetBlocks()),
7923 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
7924 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007925 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007926 Advance();
7927 }
7928 }
7929
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007930 bool Done() const { return index_ == blocks_.size(); }
7931 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07007932 void Advance() {
7933 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007934 for (size_t e = blocks_.size(); index_ < e; ++index_) {
7935 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007936 break;
7937 }
7938 }
7939 }
7940
7941 private:
7942 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007943 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07007944 size_t index_;
7945
7946 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
7947};
7948
Aart Bikf3e61ee2017-04-12 17:09:20 -07007949// Returns int64_t value of a properly typed constant.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007950inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00007951 if (constant->IsIntConstant()) {
7952 return constant->AsIntConstant()->GetValue();
7953 } else if (constant->IsLongConstant()) {
7954 return constant->AsLongConstant()->GetValue();
7955 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00007956 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00007957 return 0;
7958 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007959}
7960
Aart Bikf3e61ee2017-04-12 17:09:20 -07007961// Returns true iff instruction is an integral constant (and sets value on success).
7962inline bool IsInt64AndGet(HInstruction* instruction, /*out*/ int64_t* value) {
7963 if (instruction->IsIntConstant()) {
7964 *value = instruction->AsIntConstant()->GetValue();
7965 return true;
7966 } else if (instruction->IsLongConstant()) {
7967 *value = instruction->AsLongConstant()->GetValue();
7968 return true;
7969 } else if (instruction->IsNullConstant()) {
7970 *value = 0;
7971 return true;
7972 }
7973 return false;
7974}
7975
Aart Bik0148de42017-09-05 09:25:01 -07007976// Returns true iff instruction is the given integral constant.
7977inline bool IsInt64Value(HInstruction* instruction, int64_t value) {
7978 int64_t val = 0;
7979 return IsInt64AndGet(instruction, &val) && val == value;
7980}
7981
7982// Returns true iff instruction is a zero bit pattern.
7983inline bool IsZeroBitPattern(HInstruction* instruction) {
7984 return instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern();
7985}
7986
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01007987// Implement HInstruction::Is##type() for concrete instructions.
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007988#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01007989 inline bool HInstruction::Is##type() const { return GetKind() == k##type; }
7990 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
7991#undef INSTRUCTION_TYPE_CHECK
7992
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01007993// Implement HInstruction::Is##type() for abstract instructions.
Vladimir Markoa90dd512018-05-04 15:04:45 +01007994#define INSTRUCTION_TYPE_CHECK_RESULT(type, super) \
7995 std::is_base_of<BaseType, H##type>::value,
7996#define INSTRUCTION_TYPE_CHECK(type, super) \
7997 inline bool HInstruction::Is##type() const { \
7998 DCHECK_LT(GetKind(), kLastInstructionKind); \
7999 using BaseType = H##type; \
8000 static constexpr bool results[] = { \
8001 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK_RESULT) \
8002 }; \
8003 return results[static_cast<size_t>(GetKind())]; \
8004 }
8005
8006 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
8007#undef INSTRUCTION_TYPE_CHECK
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01008008#undef INSTRUCTION_TYPE_CHECK_RESULT
Vladimir Markoa90dd512018-05-04 15:04:45 +01008009
8010#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008011 inline const H##type* HInstruction::As##type() const { \
8012 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
8013 } \
8014 inline H##type* HInstruction::As##type() { \
8015 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
8016 }
8017
Vladimir Markoa90dd512018-05-04 15:04:45 +01008018 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
8019#undef INSTRUCTION_TYPE_CAST
8020
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008021
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00008022// Create space in `blocks` for adding `number_of_new_blocks` entries
8023// starting at location `at`. Blocks after `at` are moved accordingly.
8024inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
8025 size_t number_of_new_blocks,
8026 size_t after) {
8027 DCHECK_LT(after, blocks->size());
8028 size_t old_size = blocks->size();
8029 size_t new_size = old_size + number_of_new_blocks;
8030 blocks->resize(new_size);
8031 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
8032}
8033
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008034/*
8035 * Hunt "under the hood" of array lengths (leading to array references),
8036 * null checks (also leading to array references), and new arrays
8037 * (leading to the actual length). This makes it more likely related
8038 * instructions become actually comparable.
8039 */
8040inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
8041 while (instruction->IsArrayLength() ||
8042 instruction->IsNullCheck() ||
8043 instruction->IsNewArray()) {
8044 instruction = instruction->IsNewArray()
8045 ? instruction->AsNewArray()->GetLength()
8046 : instruction->InputAt(0);
8047 }
8048 return instruction;
8049}
8050
Artem Serov21c7e6f2017-07-27 16:04:42 +01008051void RemoveEnvironmentUses(HInstruction* instruction);
8052bool HasEnvironmentUsedByOthers(HInstruction* instruction);
8053void ResetEnvironmentInputRecords(HInstruction* instruction);
8054
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008055} // namespace art
8056
8057#endif // ART_COMPILER_OPTIMIZING_NODES_H_