blob: 7aeab722f0f135deb8c6b8e6f0483181c1fb418a [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 Marko3fae1292019-06-07 11:26:25 +010028#include "base/intrusive_forward_list.h"
Vladimir Marko2c45bc92016-10-25 16:54:12 +010029#include "base/iteration_range.h"
Andreas Gampe7fbc4a52018-11-28 08:26:47 -080030#include "base/mutex.h"
David Sehrc431b9d2018-03-02 12:01:51 -080031#include "base/quasi_atomic.h"
Vladimir Marko60584552015-09-03 13:35:12 +000032#include "base/stl_util.h"
David Brazdild9c90372016-09-14 16:53:55 +010033#include "base/transform_array_ref.h"
Nicolas Geoffray76d4bb0f32018-09-21 12:58:45 +010034#include "art_method.h"
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010035#include "data_type.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070036#include "deoptimization_kind.h"
David Sehr9e734c72018-01-04 17:56:19 -080037#include "dex/dex_file.h"
38#include "dex/dex_file_types.h"
David Sehr8c0961f2018-01-23 16:11:38 -080039#include "dex/invoke_type.h"
David Sehr312f3b22018-03-19 08:39:26 -070040#include "dex/method_reference.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000041#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000042#include "handle.h"
43#include "handle_scope.h"
Nicolas Geoffray762869d2016-07-15 15:28:35 +010044#include "intrinsics_enum.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010045#include "locations.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000046#include "mirror/class.h"
Orion Hodson18259d72018-04-12 11:18:23 +010047#include "mirror/method_type.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010048#include "offsets.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000049
Vladimir Marko0a516052019-10-14 13:00:44 +000050namespace art {
Nicolas Geoffray818f2102014-02-18 16:43:35 +000051
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 Geoffray7cfc8f52019-08-07 10:41:09 +0100134 kAnalysisFailPhiEquivalentInOsr,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000135 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000136};
137
Andreas Gampe9186ced2016-12-12 14:28:21 -0800138template <typename T>
139static inline typename std::make_unsigned<T>::type MakeUnsigned(T x) {
140 return static_cast<typename std::make_unsigned<T>::type>(x);
141}
142
Vladimir Markof9f64412015-09-02 14:05:49 +0100143class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100144 public:
145 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
146
147 void AddInstruction(HInstruction* instruction);
148 void RemoveInstruction(HInstruction* instruction);
149
David Brazdilc3d743f2015-04-22 13:40:50 +0100150 // Insert `instruction` before/after an existing instruction `cursor`.
151 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
152 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
153
Roland Levillain6b469232014-09-25 10:10:38 +0100154 // Return true if this list contains `instruction`.
155 bool Contains(HInstruction* instruction) const;
156
Roland Levillainccc07a92014-09-16 14:48:16 +0100157 // Return true if `instruction1` is found before `instruction2` in
158 // this instruction list and false otherwise. Abort if none
159 // of these instructions is found.
160 bool FoundBefore(const HInstruction* instruction1,
161 const HInstruction* instruction2) const;
162
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000163 bool IsEmpty() const { return first_instruction_ == nullptr; }
164 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
165
166 // Update the block of all instructions to be `block`.
167 void SetBlockOfInstructions(HBasicBlock* block) const;
168
169 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000170 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000171 void Add(const HInstructionList& instruction_list);
172
David Brazdil2d7352b2015-04-20 14:52:42 +0100173 // Return the number of instructions in the list. This is an expensive operation.
174 size_t CountSize() const;
175
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100176 private:
177 HInstruction* first_instruction_;
178 HInstruction* last_instruction_;
179
180 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000181 friend class HGraph;
182 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100183 friend class HInstructionIterator;
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +0000184 friend class HInstructionIteratorHandleChanges;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100185 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100186
187 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
188};
189
David Brazdil4833f5a2015-12-16 10:37:39 +0000190class ReferenceTypeInfo : ValueObject {
191 public:
192 typedef Handle<mirror::Class> TypeHandle;
193
Vladimir Markoa1de9182016-02-25 11:37:38 +0000194 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
195
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700196 static ReferenceTypeInfo Create(TypeHandle type_handle) REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil94ab38f2016-06-21 17:48:19 +0100197 return Create(type_handle, type_handle->CannotBeAssignedFromOtherTypes());
198 }
199
Vladimir Markoa1de9182016-02-25 11:37:38 +0000200 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000201 return ReferenceTypeInfo(type_handle, is_exact);
202 }
203
204 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
205
Vladimir Markof39745e2016-01-26 12:16:55 +0000206 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000207 return handle.GetReference() != nullptr;
208 }
209
Vladimir Marko456307a2016-04-19 14:12:13 +0000210 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000211 return IsValidHandle(type_handle_);
212 }
213
214 bool IsExact() const { return is_exact_; }
215
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700216 bool IsObjectClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000217 DCHECK(IsValid());
218 return GetTypeHandle()->IsObjectClass();
219 }
220
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700221 bool IsStringClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000222 DCHECK(IsValid());
223 return GetTypeHandle()->IsStringClass();
224 }
225
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700226 bool IsObjectArray() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000227 DCHECK(IsValid());
228 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
229 }
230
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700231 bool IsInterface() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000232 DCHECK(IsValid());
233 return GetTypeHandle()->IsInterface();
234 }
235
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700236 bool IsArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000237 DCHECK(IsValid());
238 return GetTypeHandle()->IsArrayClass();
239 }
240
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700241 bool IsPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000242 DCHECK(IsValid());
243 return GetTypeHandle()->IsPrimitiveArray();
244 }
245
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700246 bool IsNonPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000247 DCHECK(IsValid());
248 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
249 }
250
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700251 bool CanArrayHold(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000252 DCHECK(IsValid());
253 if (!IsExact()) return false;
254 if (!IsArrayClass()) return false;
255 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
256 }
257
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700258 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000259 DCHECK(IsValid());
260 if (!IsExact()) return false;
261 if (!IsArrayClass()) return false;
262 if (!rti.IsArrayClass()) return false;
263 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
264 rti.GetTypeHandle()->GetComponentType());
265 }
266
267 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
268
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700269 bool IsSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000270 DCHECK(IsValid());
271 DCHECK(rti.IsValid());
272 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
273 }
274
David Brazdil4833f5a2015-12-16 10:37:39 +0000275 // Returns true if the type information provide the same amount of details.
276 // Note that it does not mean that the instructions have the same actual type
277 // (because the type can be the result of a merge).
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700278 bool IsEqual(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000279 if (!IsValid() && !rti.IsValid()) {
280 // Invalid types are equal.
281 return true;
282 }
283 if (!IsValid() || !rti.IsValid()) {
284 // One is valid, the other not.
285 return false;
286 }
287 return IsExact() == rti.IsExact()
288 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
289 }
290
291 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000292 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
293 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
294 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000295
296 // The class of the object.
297 TypeHandle type_handle_;
298 // Whether or not the type is exact or a superclass of the actual type.
299 // Whether or not we have any information about this type.
300 bool is_exact_;
301};
302
303std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
304
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000305// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100306class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000307 public:
Vladimir Markoca6fff82017-10-03 14:49:14 +0100308 HGraph(ArenaAllocator* allocator,
309 ArenaStack* arena_stack,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100310 const DexFile& dex_file,
311 uint32_t method_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700312 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100313 InvokeType invoke_type = kInvalidInvokeType,
Hans Boehm206348c2018-12-05 11:11:33 -0800314 bool dead_reference_safe = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100315 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000316 bool osr = false,
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +0000317 bool is_shared_jit_code = false,
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +0000318 bool baseline = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100319 int start_instruction_id = 0)
Vladimir Markoca6fff82017-10-03 14:49:14 +0100320 : allocator_(allocator),
321 arena_stack_(arena_stack),
322 blocks_(allocator->Adapter(kArenaAllocBlockList)),
323 reverse_post_order_(allocator->Adapter(kArenaAllocReversePostOrder)),
324 linear_order_(allocator->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700325 entry_block_(nullptr),
326 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100327 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100328 number_of_vregs_(0),
329 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000330 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400331 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000332 has_try_catch_(false),
Artem Serov2808be82018-12-20 19:15:11 +0000333 has_monitor_operations_(false),
Aart Bikb13c65b2017-03-21 20:14:07 -0700334 has_simd_(false),
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800335 has_loops_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000336 has_irreducible_loops_(false),
Hans Boehm206348c2018-12-05 11:11:33 -0800337 dead_reference_safe_(dead_reference_safe),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000338 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000339 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100340 dex_file_(dex_file),
341 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100342 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100343 in_ssa_form_(false),
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800344 number_of_cha_guards_(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700345 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000346 cached_null_constant_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100347 cached_int_constants_(std::less<int32_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
348 cached_float_constants_(std::less<int32_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
349 cached_long_constants_(std::less<int64_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
350 cached_double_constants_(std::less<int64_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000351 cached_current_method_(nullptr),
Nicolas Geoffray53fec082017-03-27 12:56:16 +0100352 art_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000353 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
Mingyao Yang063fc772016-08-02 11:02:54 -0700354 osr_(osr),
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +0000355 baseline_(baseline),
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +0000356 cha_single_implementation_list_(allocator->Adapter(kArenaAllocCHA)),
357 is_shared_jit_code_(is_shared_jit_code) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100358 blocks_.reserve(kDefaultNumberOfBlocks);
359 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000360
David Brazdilbadd8262016-02-02 16:28:56 +0000361 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700362 void InitializeInexactObjectRTI(VariableSizedHandleScope* handles);
David Brazdilbadd8262016-02-02 16:28:56 +0000363
Vladimir Markoca6fff82017-10-03 14:49:14 +0100364 ArenaAllocator* GetAllocator() const { return allocator_; }
365 ArenaStack* GetArenaStack() const { return arena_stack_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100366 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
367
David Brazdil69ba7b72015-06-23 18:27:30 +0100368 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000369 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100370
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000371 HBasicBlock* GetEntryBlock() const { return entry_block_; }
372 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100373 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000374
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000375 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
376 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000377
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000378 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100379
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100380 void ComputeDominanceInformation();
381 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000382 void ClearLoopInformation();
383 void FindBackEdges(ArenaBitVector* visited);
384 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100385 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100386 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000387
David Brazdil4833f5a2015-12-16 10:37:39 +0000388 // Analyze all natural loops in this graph. Returns a code specifying that it
389 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000390 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000391 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100392
David Brazdilffee3d32015-07-06 11:48:53 +0100393 // Iterate over blocks to compute try block membership. Needs reverse post
394 // order and loop information.
395 void ComputeTryBlockInformation();
396
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000397 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000398 // Returns the instruction to replace the invoke expression or null if the
399 // invoke is for a void method. Note that the caller is responsible for replacing
400 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000401 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000402
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000403 // Update the loop and try membership of `block`, which was spawned from `reference`.
404 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
405 // should be the new back edge.
406 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
407 HBasicBlock* reference,
408 bool replace_if_back_edge);
409
Mingyao Yang3584bce2015-05-19 16:01:59 -0700410 // Need to add a couple of blocks to test if the loop body is entered and
411 // put deoptimization instructions, etc.
412 void TransformLoopHeaderForBCE(HBasicBlock* header);
413
Aart Bikf8f5a162017-02-06 15:35:29 -0800414 // Adds a new loop directly after the loop with the given header and exit.
415 // Returns the new preheader.
416 HBasicBlock* TransformLoopForVectorization(HBasicBlock* header,
417 HBasicBlock* body,
418 HBasicBlock* exit);
419
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000420 // Removes `block` from the graph. Assumes `block` has been disconnected from
421 // other blocks and has no instructions or phis.
422 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000423
David Brazdilfc6a86a2015-06-26 10:33:45 +0000424 // Splits the edge between `block` and `successor` while preserving the
425 // indices in the predecessor/successor lists. If there are multiple edges
426 // between the blocks, the lowest indices are used.
427 // Returns the new block which is empty and has the same dex pc as `successor`.
428 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
429
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100430 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
Artem Serovc73ee372017-07-31 15:08:40 +0100431 void OrderLoopHeaderPredecessors(HBasicBlock* header);
Artem Serov09faaea2017-12-07 14:36:01 +0000432
433 // Transform a loop into a format with a single preheader.
434 //
435 // Each phi in the header should be split: original one in the header should only hold
436 // inputs reachable from the back edges and a single input from the preheader. The newly created
437 // phi in the preheader should collate the inputs from the original multiple incoming blocks.
438 //
439 // Loops in the graph typically have a single preheader, so this method is used to "repair" loops
440 // that no longer have this property.
441 void TransformLoopToSinglePreheaderFormat(HBasicBlock* header);
442
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100443 void SimplifyLoop(HBasicBlock* header);
444
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000445 int32_t GetNextInstructionId() {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100446 CHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000447 return current_instruction_id_++;
448 }
449
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000450 int32_t GetCurrentInstructionId() const {
451 return current_instruction_id_;
452 }
453
454 void SetCurrentInstructionId(int32_t id) {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100455 CHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000456 current_instruction_id_ = id;
457 }
458
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100459 uint16_t GetMaximumNumberOfOutVRegs() const {
460 return maximum_number_of_out_vregs_;
461 }
462
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000463 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
464 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100465 }
466
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100467 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
468 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
469 }
470
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000471 void UpdateTemporariesVRegSlots(size_t slots) {
472 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100473 }
474
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000475 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100476 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000477 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100478 }
479
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100480 void SetNumberOfVRegs(uint16_t number_of_vregs) {
481 number_of_vregs_ = number_of_vregs;
482 }
483
484 uint16_t GetNumberOfVRegs() const {
485 return number_of_vregs_;
486 }
487
488 void SetNumberOfInVRegs(uint16_t value) {
489 number_of_in_vregs_ = value;
490 }
491
David Brazdildee58d62016-04-07 09:54:26 +0000492 uint16_t GetNumberOfInVRegs() const {
493 return number_of_in_vregs_;
494 }
495
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100496 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100497 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100498 return number_of_vregs_ - number_of_in_vregs_;
499 }
500
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100501 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100502 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100503 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100504
Eric Holk1868de92020-02-12 09:10:21 -0800505 ArrayRef<HBasicBlock* const> GetReversePostOrderSkipEntryBlock() const {
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100506 DCHECK(GetReversePostOrder()[0] == entry_block_);
507 return ArrayRef<HBasicBlock* const>(GetReversePostOrder()).SubArray(1);
508 }
509
510 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetPostOrder() const {
511 return ReverseRange(GetReversePostOrder());
512 }
513
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100514 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100515 return linear_order_;
516 }
517
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100518 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetLinearPostOrder() const {
519 return ReverseRange(GetLinearOrder());
520 }
521
Mark Mendell1152c922015-04-24 17:06:35 -0400522 bool HasBoundsChecks() const {
523 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800524 }
525
Mark Mendell1152c922015-04-24 17:06:35 -0400526 void SetHasBoundsChecks(bool value) {
527 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800528 }
529
Hans Boehm206348c2018-12-05 11:11:33 -0800530 // Is the code known to be robust against eliminating dead references
531 // and the effects of early finalization?
532 bool IsDeadReferenceSafe() const { return dead_reference_safe_; }
533
534 void MarkDeadReferenceUnsafe() { dead_reference_safe_ = false; }
535
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000536 bool IsDebuggable() const { return debuggable_; }
537
David Brazdil8d5b8b22015-03-24 10:51:52 +0000538 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000539 // already, it is created and inserted into the graph. This method is only for
540 // integral types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100541 HConstant* GetConstant(DataType::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000542
543 // TODO: This is problematic for the consistency of reference type propagation
544 // because it can be created anytime after the pass and thus it will be left
545 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600546 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000547
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600548 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
549 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000550 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600551 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
552 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000553 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600554 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
555 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000556 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600557 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
558 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000559 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000560
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100561 HCurrentMethod* GetCurrentMethod();
562
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100563 const DexFile& GetDexFile() const {
564 return dex_file_;
565 }
566
567 uint32_t GetMethodIdx() const {
568 return method_idx_;
569 }
570
Igor Murashkind01745e2017-04-05 16:40:31 -0700571 // Get the method name (without the signature), e.g. "<init>"
572 const char* GetMethodName() const;
573
574 // Get the pretty method name (class + name + optionally signature).
575 std::string PrettyMethod(bool with_signature = true) const;
576
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100577 InvokeType GetInvokeType() const {
578 return invoke_type_;
579 }
580
Mark Mendellc4701932015-04-10 13:18:51 -0400581 InstructionSet GetInstructionSet() const {
582 return instruction_set_;
583 }
584
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000585 bool IsCompilingOsr() const { return osr_; }
586
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +0000587 bool IsCompilingBaseline() const { return baseline_; }
588
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +0000589 bool IsCompilingForSharedJitCode() const {
590 return is_shared_jit_code_;
591 }
592
Mingyao Yang063fc772016-08-02 11:02:54 -0700593 ArenaSet<ArtMethod*>& GetCHASingleImplementationList() {
594 return cha_single_implementation_list_;
595 }
596
597 void AddCHASingleImplementationDependency(ArtMethod* method) {
598 cha_single_implementation_list_.insert(method);
599 }
600
601 bool HasShouldDeoptimizeFlag() const {
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800602 return number_of_cha_guards_ != 0;
Mingyao Yang063fc772016-08-02 11:02:54 -0700603 }
604
David Brazdil77a48ae2015-09-15 12:34:04 +0000605 bool HasTryCatch() const { return has_try_catch_; }
606 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100607
Artem Serov2808be82018-12-20 19:15:11 +0000608 bool HasMonitorOperations() const { return has_monitor_operations_; }
609 void SetHasMonitorOperations(bool value) { has_monitor_operations_ = value; }
610
Aart Bikb13c65b2017-03-21 20:14:07 -0700611 bool HasSIMD() const { return has_simd_; }
612 void SetHasSIMD(bool value) { has_simd_ = value; }
613
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800614 bool HasLoops() const { return has_loops_; }
615 void SetHasLoops(bool value) { has_loops_ = value; }
616
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000617 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
618 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
619
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100620 ArtMethod* GetArtMethod() const { return art_method_; }
621 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
622
Roland Levillain5e8d5f02016-10-18 18:03:43 +0100623 // Returns an instruction with the opposite Boolean value from 'cond'.
Mark Mendellf6529172015-11-17 11:16:56 -0500624 // The instruction has been inserted into the graph, either as a constant, or
625 // before cursor.
626 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
627
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000628 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
629
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800630 uint32_t GetNumberOfCHAGuards() { return number_of_cha_guards_; }
631 void SetNumberOfCHAGuards(uint32_t num) { number_of_cha_guards_ = num; }
632 void IncrementNumberOfCHAGuards() { number_of_cha_guards_++; }
633
David Brazdil2d7352b2015-04-20 14:52:42 +0100634 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000635 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100636 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000637
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000638 template <class InstructionType, typename ValueType>
639 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600640 ArenaSafeMap<ValueType, InstructionType*>* cache,
641 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000642 // Try to find an existing constant of the given value.
643 InstructionType* constant = nullptr;
644 auto cached_constant = cache->find(value);
645 if (cached_constant != cache->end()) {
646 constant = cached_constant->second;
647 }
648
649 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100650 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000651 if (constant == nullptr || constant->GetBlock() == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100652 constant = new (allocator_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000653 cache->Overwrite(value, constant);
654 InsertConstant(constant);
655 }
656 return constant;
657 }
658
David Brazdil8d5b8b22015-03-24 10:51:52 +0000659 void InsertConstant(HConstant* instruction);
660
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000661 // Cache a float constant into the graph. This method should only be
662 // called by the SsaBuilder when creating "equivalent" instructions.
663 void CacheFloatConstant(HFloatConstant* constant);
664
665 // See CacheFloatConstant comment.
666 void CacheDoubleConstant(HDoubleConstant* constant);
667
Vladimir Markoca6fff82017-10-03 14:49:14 +0100668 ArenaAllocator* const allocator_;
669 ArenaStack* const arena_stack_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000670
671 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100672 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000673
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100674 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100675 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000676
Aart Bik281c6812016-08-26 11:31:48 -0700677 // List of blocks to perform a linear order tree traversal. Unlike the reverse
678 // post order, this order is not incrementally kept up-to-date.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100679 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100680
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000681 HBasicBlock* entry_block_;
682 HBasicBlock* exit_block_;
683
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100684 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100685 uint16_t maximum_number_of_out_vregs_;
686
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100687 // The number of virtual registers in this method. Contains the parameters.
688 uint16_t number_of_vregs_;
689
690 // The number of virtual registers used by parameters of this method.
691 uint16_t number_of_in_vregs_;
692
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000693 // Number of vreg size slots that the temporaries use (used in baseline compiler).
694 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100695
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800696 // Flag whether there are bounds checks in the graph. We can skip
697 // BCE if it's false. It's only best effort to keep it up to date in
698 // the presence of code elimination so there might be false positives.
Mark Mendell1152c922015-04-24 17:06:35 -0400699 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800700
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800701 // Flag whether there are try/catch blocks in the graph. We will skip
702 // try/catch-related passes if it's false. It's only best effort to keep
703 // it up to date in the presence of code elimination so there might be
704 // false positives.
David Brazdil77a48ae2015-09-15 12:34:04 +0000705 bool has_try_catch_;
706
Artem Serov2808be82018-12-20 19:15:11 +0000707 // Flag whether there are any HMonitorOperation in the graph. If yes this will mandate
708 // DexRegisterMap to be present to allow deadlock analysis for non-debuggable code.
709 bool has_monitor_operations_;
710
Aart Bikb13c65b2017-03-21 20:14:07 -0700711 // Flag whether SIMD instructions appear in the graph. If true, the
712 // code generators may have to be more careful spilling the wider
713 // contents of SIMD registers.
714 bool has_simd_;
715
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800716 // Flag whether there are any loops in the graph. We can skip loop
717 // optimization if it's false. It's only best effort to keep it up
718 // to date in the presence of code elimination so there might be false
719 // positives.
720 bool has_loops_;
721
722 // Flag whether there are any irreducible loops in the graph. It's only
723 // best effort to keep it up to date in the presence of code elimination
724 // so there might be false positives.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000725 bool has_irreducible_loops_;
726
Hans Boehm206348c2018-12-05 11:11:33 -0800727 // Is the code known to be robust against eliminating dead references
728 // and the effects of early finalization? If false, dead reference variables
729 // are kept if they might be visible to the garbage collector.
730 // Currently this means that the class was declared to be dead-reference-safe,
731 // the method accesses no reachability-sensitive fields or data, and the same
732 // is true for any methods that were inlined into the current one.
733 bool dead_reference_safe_;
734
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000735 // Indicates whether the graph should be compiled in a way that
736 // ensures full debuggability. If false, we can apply more
737 // aggressive optimizations that may limit the level of debugging.
738 const bool debuggable_;
739
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000740 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000741 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000742
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100743 // The dex file from which the method is from.
744 const DexFile& dex_file_;
745
746 // The method index in the dex file.
747 const uint32_t method_idx_;
748
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100749 // If inlined, this encodes how the callee is being invoked.
750 const InvokeType invoke_type_;
751
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100752 // Whether the graph has been transformed to SSA form. Only used
753 // in debug mode to ensure we are not using properties only valid
754 // for non-SSA form (like the number of temporaries).
755 bool in_ssa_form_;
756
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800757 // Number of CHA guards in the graph. Used to short-circuit the
758 // CHA guard optimization pass when there is no CHA guard left.
759 uint32_t number_of_cha_guards_;
760
Mathieu Chartiere401d142015-04-22 13:56:20 -0700761 const InstructionSet instruction_set_;
762
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000763 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000764 HNullConstant* cached_null_constant_;
765 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000766 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000767 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000768 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000769
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100770 HCurrentMethod* cached_current_method_;
771
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100772 // The ArtMethod this graph is for. Note that for AOT, it may be null,
773 // for example for methods whose declaring class could not be resolved
774 // (such as when the superclass could not be found).
775 ArtMethod* art_method_;
776
David Brazdil4833f5a2015-12-16 10:37:39 +0000777 // Keep the RTI of inexact Object to avoid having to pass stack handle
778 // collection pointer to passes which may create NullConstant.
779 ReferenceTypeInfo inexact_object_rti_;
780
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000781 // Whether we are compiling this graph for on stack replacement: this will
782 // make all loops seen as irreducible and emit special stack maps to mark
783 // compiled code entries which the interpreter can directly jump to.
784 const bool osr_;
785
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +0000786 // Whether we are compiling baseline (not running optimizations). This affects
787 // the code being generated.
788 const bool baseline_;
789
Mingyao Yang063fc772016-08-02 11:02:54 -0700790 // List of methods that are assumed to have single implementation.
791 ArenaSet<ArtMethod*> cha_single_implementation_list_;
792
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +0000793 // Whether we are JIT compiling in the shared region area, putting
794 // restrictions on, for example, how literals are being generated.
795 bool is_shared_jit_code_;
796
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000797 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100798 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000799 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000800 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000801 DISALLOW_COPY_AND_ASSIGN(HGraph);
802};
803
Vladimir Markof9f64412015-09-02 14:05:49 +0100804class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000805 public:
806 HLoopInformation(HBasicBlock* header, HGraph* graph)
807 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100808 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000809 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100810 contains_irreducible_loop_(false),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100811 back_edges_(graph->GetAllocator()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100812 // Make bit vector growable, as the number of blocks may change.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100813 blocks_(graph->GetAllocator(),
814 graph->GetBlocks().size(),
815 true,
816 kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100817 back_edges_.reserve(kDefaultNumberOfBackEdges);
818 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100819
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000820 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100821 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000822
823 void Dump(std::ostream& os);
824
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100825 HBasicBlock* GetHeader() const {
826 return header_;
827 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000828
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000829 void SetHeader(HBasicBlock* block) {
830 header_ = block;
831 }
832
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100833 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
834 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
835 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
836
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000837 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100838 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000839 }
840
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100841 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100842 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100843 }
844
David Brazdil46e2a392015-03-16 17:31:52 +0000845 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100846 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100847 }
848
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000849 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100850 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000851 }
852
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100853 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100854
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100855 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100856 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100857 }
858
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100859 // Returns the lifetime position of the back edge that has the
860 // greatest lifetime position.
861 size_t GetLifetimeEnd() const;
862
863 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100864 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100865 }
866
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000867 // Finds blocks that are part of this loop.
868 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100869
Artem Serov7f4aff62017-06-21 17:02:18 +0100870 // Updates blocks population of the loop and all of its outer' ones recursively after the
871 // population of the inner loop is updated.
872 void PopulateInnerLoopUpwards(HLoopInformation* inner_loop);
873
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100874 // Returns whether this loop information contains `block`.
875 // Note that this loop information *must* be populated before entering this function.
876 bool Contains(const HBasicBlock& block) const;
877
878 // Returns whether this loop information is an inner loop of `other`.
879 // Note that `other` *must* be populated before entering this function.
880 bool IsIn(const HLoopInformation& other) const;
881
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800882 // Returns true if instruction is not defined within this loop.
883 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700884
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100885 const ArenaBitVector& GetBlocks() const { return blocks_; }
886
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000887 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000888 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000889
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000890 void ClearAllBlocks() {
891 blocks_.ClearAllBits();
892 }
893
David Brazdil3f4a5222016-05-06 12:46:21 +0100894 bool HasBackEdgeNotDominatedByHeader() const;
895
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100896 bool IsPopulated() const {
897 return blocks_.GetHighestBitSet() != -1;
898 }
899
Anton Shaminf89381f2016-05-16 16:44:13 +0600900 bool DominatesAllBackEdges(HBasicBlock* block);
901
David Sehrc757dec2016-11-04 15:48:34 -0700902 bool HasExitEdge() const;
903
Artem Serov7f4aff62017-06-21 17:02:18 +0100904 // Resets back edge and blocks-in-loop data.
905 void ResetBasicBlockData() {
906 back_edges_.clear();
907 ClearAllBlocks();
908 }
909
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000910 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100911 // Internal recursive implementation of `Populate`.
912 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100913 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100914
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000915 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100916 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000917 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100918 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100919 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100920 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000921
922 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
923};
924
David Brazdilec16f792015-08-19 15:04:01 +0100925// Stores try/catch information for basic blocks.
926// Note that HGraph is constructed so that catch blocks cannot simultaneously
927// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100928class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100929 public:
930 // Try block information constructor.
931 explicit TryCatchInformation(const HTryBoundary& try_entry)
932 : try_entry_(&try_entry),
933 catch_dex_file_(nullptr),
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +0000934 catch_type_index_(dex::TypeIndex::Invalid()) {
David Brazdilec16f792015-08-19 15:04:01 +0100935 DCHECK(try_entry_ != nullptr);
936 }
937
938 // Catch block information constructor.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800939 TryCatchInformation(dex::TypeIndex catch_type_index, const DexFile& dex_file)
David Brazdilec16f792015-08-19 15:04:01 +0100940 : try_entry_(nullptr),
941 catch_dex_file_(&dex_file),
942 catch_type_index_(catch_type_index) {}
943
944 bool IsTryBlock() const { return try_entry_ != nullptr; }
945
946 const HTryBoundary& GetTryEntry() const {
947 DCHECK(IsTryBlock());
948 return *try_entry_;
949 }
950
951 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
952
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +0000953 bool IsValidTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +0100954 DCHECK(IsCatchBlock());
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +0000955 return catch_type_index_.IsValid();
David Brazdilec16f792015-08-19 15:04:01 +0100956 }
957
Andreas Gampea5b09a62016-11-17 15:21:22 -0800958 dex::TypeIndex GetCatchTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +0100959 DCHECK(IsCatchBlock());
960 return catch_type_index_;
961 }
962
963 const DexFile& GetCatchDexFile() const {
964 DCHECK(IsCatchBlock());
965 return *catch_dex_file_;
966 }
967
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +0000968 void SetInvalidTypeIndex() {
969 catch_type_index_ = dex::TypeIndex::Invalid();
970 }
971
David Brazdilec16f792015-08-19 15:04:01 +0100972 private:
973 // One of possibly several TryBoundary instructions entering the block's try.
974 // Only set for try blocks.
975 const HTryBoundary* try_entry_;
976
977 // Exception type information. Only set for catch blocks.
978 const DexFile* catch_dex_file_;
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +0000979 dex::TypeIndex catch_type_index_;
David Brazdilec16f792015-08-19 15:04:01 +0100980};
981
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100982static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100983static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100984
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000985// A block in a method. Contains the list of instructions represented
986// as a double linked list. Each block knows its predecessors and
987// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100988
Vladimir Markof9f64412015-09-02 14:05:49 +0100989class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000990 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700991 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000992 : graph_(graph),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100993 predecessors_(graph->GetAllocator()->Adapter(kArenaAllocPredecessors)),
994 successors_(graph->GetAllocator()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000995 loop_information_(nullptr),
996 dominator_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100997 dominated_blocks_(graph->GetAllocator()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100998 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100999 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001000 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001001 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +00001002 try_catch_information_(nullptr) {
1003 predecessors_.reserve(kDefaultNumberOfPredecessors);
1004 successors_.reserve(kDefaultNumberOfSuccessors);
1005 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
1006 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001007
Vladimir Marko60584552015-09-03 13:35:12 +00001008 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001009 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001010 }
1011
Vladimir Marko60584552015-09-03 13:35:12 +00001012 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001013 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001014 }
1015
David Brazdild26a4112015-11-10 11:07:31 +00001016 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
1017 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
1018
Vladimir Marko60584552015-09-03 13:35:12 +00001019 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
1020 return ContainsElement(successors_, block, start_from);
1021 }
1022
1023 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001024 return dominated_blocks_;
1025 }
1026
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001027 bool IsEntryBlock() const {
1028 return graph_->GetEntryBlock() == this;
1029 }
1030
1031 bool IsExitBlock() const {
1032 return graph_->GetExitBlock() == this;
1033 }
1034
David Brazdil46e2a392015-03-16 17:31:52 +00001035 bool IsSingleGoto() const;
Mads Ager16e52892017-07-14 13:11:37 +02001036 bool IsSingleReturn() const;
Mingyao Yang46721ef2017-10-05 14:45:17 -07001037 bool IsSingleReturnOrReturnVoidAllowingPhis() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +00001038 bool IsSingleTryBoundary() const;
1039
1040 // Returns true if this block emits nothing but a jump.
1041 bool IsSingleJump() const {
1042 HLoopInformation* loop_info = GetLoopInformation();
1043 return (IsSingleGoto() || IsSingleTryBoundary())
1044 // Back edges generate a suspend check.
1045 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
1046 }
David Brazdil46e2a392015-03-16 17:31:52 +00001047
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001048 void AddBackEdge(HBasicBlock* back_edge) {
1049 if (loop_information_ == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001050 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001051 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001052 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001053 loop_information_->AddBackEdge(back_edge);
1054 }
1055
Artem Serov7f4aff62017-06-21 17:02:18 +01001056 // Registers a back edge; if the block was not a loop header before the call associates a newly
1057 // created loop info with it.
1058 //
1059 // Used in SuperblockCloner to preserve LoopInformation object instead of reseting loop
1060 // info for all blocks during back edges recalculation.
1061 void AddBackEdgeWhileUpdating(HBasicBlock* back_edge) {
1062 if (loop_information_ == nullptr || loop_information_->GetHeader() != this) {
1063 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
1064 }
1065 loop_information_->AddBackEdge(back_edge);
1066 }
1067
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001068 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001069 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001070
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001071 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001072 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001073 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001074
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001075 HBasicBlock* GetDominator() const { return dominator_; }
1076 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +00001077 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
1078
1079 void RemoveDominatedBlock(HBasicBlock* block) {
1080 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +01001081 }
Vladimir Marko60584552015-09-03 13:35:12 +00001082
1083 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
1084 ReplaceElement(dominated_blocks_, existing, new_block);
1085 }
1086
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001087 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001088
1089 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001090 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001091 }
1092
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001093 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
1094 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +01001095 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001096 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +01001097 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
1098 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001099
Nicolas Geoffray09aa1472016-01-19 10:52:54 +00001100 HInstruction* GetFirstInstructionDisregardMoves() const;
1101
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001102 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001103 successors_.push_back(block);
1104 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001105 }
1106
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001107 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
1108 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001109 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001110 new_block->predecessors_.push_back(this);
1111 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001112 }
1113
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001114 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
1115 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001116 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001117 new_block->successors_.push_back(this);
1118 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001119 }
1120
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001121 // Insert `this` between `predecessor` and `successor. This method
Alex Light1e52a072019-06-25 09:12:04 -07001122 // preserves the indices, and will update the first edge found between
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001123 // `predecessor` and `successor`.
1124 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
1125 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001126 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +00001127 successor->predecessors_[predecessor_index] = this;
1128 predecessor->successors_[successor_index] = this;
1129 successors_.push_back(successor);
1130 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001131 }
1132
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001133 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001134 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001135 }
1136
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001137 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001138 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001139 }
1140
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001141 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001142 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001143 }
1144
1145 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001146 predecessors_.push_back(block);
1147 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001148 }
1149
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001150 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001151 DCHECK_EQ(predecessors_.size(), 2u);
1152 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001153 }
1154
David Brazdil769c9e52015-04-27 13:54:09 +01001155 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001156 DCHECK_EQ(successors_.size(), 2u);
1157 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +01001158 }
1159
David Brazdilfc6a86a2015-06-26 10:33:45 +00001160 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001161 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001162 }
1163
David Brazdilfc6a86a2015-06-26 10:33:45 +00001164 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001165 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001166 }
1167
David Brazdilfc6a86a2015-06-26 10:33:45 +00001168 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001169 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001170 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001171 }
1172
1173 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001174 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001175 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001176 }
1177
1178 // Returns whether the first occurrence of `predecessor` in the list of
1179 // predecessors is at index `idx`.
1180 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001181 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001182 return GetPredecessorIndexOf(predecessor) == idx;
1183 }
1184
David Brazdild7558da2015-09-22 13:04:14 +01001185 // Create a new block between this block and its predecessors. The new block
1186 // is added to the graph, all predecessor edges are relinked to it and an edge
1187 // is created to `this`. Returns the new empty block. Reverse post order or
1188 // loop and try/catch information are not updated.
1189 HBasicBlock* CreateImmediateDominator();
1190
David Brazdilfc6a86a2015-06-26 10:33:45 +00001191 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001192 // created, latter block. Note that this method will add the block to the
1193 // graph, create a Goto at the end of the former block and will create an edge
1194 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001195 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001196 HBasicBlock* SplitBefore(HInstruction* cursor);
1197
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001198 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001199 // created block. Note that this method just updates raw block information,
1200 // like predecessors, successors, dominators, and instruction list. It does not
1201 // update the graph, reverse post order, loop information, nor make sure the
1202 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001203 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1204
1205 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1206 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001207
1208 // Merge `other` at the end of `this`. Successors and dominated blocks of
1209 // `other` are changed to be successors and dominated blocks of `this`. Note
1210 // that this method does not update the graph, reverse post order, loop
1211 // information, nor make sure the blocks are consistent (for example ending
1212 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001213 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001214
1215 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1216 // of `this` are moved to `other`.
1217 // Note that this method does not update the graph, reverse post order, loop
1218 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001219 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001220 void ReplaceWith(HBasicBlock* other);
1221
Aart Bik6b69e0a2017-01-11 10:20:43 -08001222 // Merges the instructions of `other` at the end of `this`.
1223 void MergeInstructionsWith(HBasicBlock* other);
1224
David Brazdil2d7352b2015-04-20 14:52:42 +01001225 // Merge `other` at the end of `this`. This method updates loops, reverse post
1226 // order, links to predecessors, successors, dominators and deletes the block
1227 // from the graph. The two blocks must be successive, i.e. `this` the only
1228 // predecessor of `other` and vice versa.
1229 void MergeWith(HBasicBlock* other);
1230
1231 // Disconnects `this` from all its predecessors, successors and dominator,
1232 // removes it from all loops it is included in and eventually from the graph.
1233 // The block must not dominate any other block. Predecessors and successors
1234 // are safely updated.
1235 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001236
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001237 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001238 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001239 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001240 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Artem Serovcced8ba2017-07-19 18:18:09 +01001241 // Replace phi `initial` with `replacement` within this block.
1242 void ReplaceAndRemovePhiWith(HPhi* initial, HPhi* replacement);
Roland Levillainccc07a92014-09-16 14:48:16 +01001243 // Replace instruction `initial` with `replacement` within this block.
1244 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1245 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001246 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001247 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001248 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1249 // instruction list. With 'ensure_safety' set to true, it verifies that the
1250 // instruction is not in use and removes it from the use lists of its inputs.
1251 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1252 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001253 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001254
1255 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001256 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001257 }
1258
Roland Levillain6b879dd2014-09-22 17:13:44 +01001259 bool IsLoopPreHeaderFirstPredecessor() const {
1260 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001261 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001262 }
1263
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001264 bool IsFirstPredecessorBackEdge() const {
1265 DCHECK(IsLoopHeader());
1266 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1267 }
1268
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001269 HLoopInformation* GetLoopInformation() const {
1270 return loop_information_;
1271 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001272
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001273 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001274 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001275 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001276 void SetInLoop(HLoopInformation* info) {
1277 if (IsLoopHeader()) {
1278 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001279 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001280 loop_information_ = info;
1281 } else if (loop_information_->Contains(*info->GetHeader())) {
1282 // Block is currently part of an outer loop. Make it part of this inner loop.
1283 // Note that a non loop header having a loop information means this loop information
1284 // has already been populated
1285 loop_information_ = info;
1286 } else {
1287 // Block is part of an inner loop. Do not update the loop information.
1288 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1289 // at this point, because this method is being called while populating `info`.
1290 }
1291 }
1292
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001293 // Raw update of the loop information.
1294 void SetLoopInformation(HLoopInformation* info) {
1295 loop_information_ = info;
1296 }
1297
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001298 bool IsInLoop() const { return loop_information_ != nullptr; }
1299
David Brazdilec16f792015-08-19 15:04:01 +01001300 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1301
1302 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1303 try_catch_information_ = try_catch_information;
1304 }
1305
1306 bool IsTryBlock() const {
1307 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1308 }
1309
1310 bool IsCatchBlock() const {
1311 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1312 }
David Brazdilffee3d32015-07-06 11:48:53 +01001313
1314 // Returns the try entry that this block's successors should have. They will
1315 // be in the same try, unless the block ends in a try boundary. In that case,
1316 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001317 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001318
Aart Bik75ff2c92018-04-21 01:28:11 +00001319 bool HasThrowingInstructions() const;
1320
David Brazdila4b8c212015-05-07 09:59:30 +01001321 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001322 bool Dominates(HBasicBlock* block) const;
1323
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001324 size_t GetLifetimeStart() const { return lifetime_start_; }
1325 size_t GetLifetimeEnd() const { return lifetime_end_; }
1326
1327 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1328 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1329
David Brazdil8d5b8b22015-03-24 10:51:52 +00001330 bool EndsWithControlFlowInstruction() const;
Aart Bik4dc09e72018-05-11 14:40:31 -07001331 bool EndsWithReturn() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001332 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001333 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001334 bool HasSinglePhi() const;
1335
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001336 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001337 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001338 ArenaVector<HBasicBlock*> predecessors_;
1339 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001340 HInstructionList instructions_;
1341 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001342 HLoopInformation* loop_information_;
1343 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001344 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001345 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001346 // The dex program counter of the first instruction of this block.
1347 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001348 size_t lifetime_start_;
1349 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001350 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001351
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001352 friend class HGraph;
1353 friend class HInstruction;
1354
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001355 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1356};
1357
David Brazdilb2bd1c52015-03-25 11:17:37 +00001358// Iterates over the LoopInformation of all loops which contain 'block'
1359// from the innermost to the outermost.
1360class HLoopInformationOutwardIterator : public ValueObject {
1361 public:
1362 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1363 : current_(block.GetLoopInformation()) {}
1364
1365 bool Done() const { return current_ == nullptr; }
1366
1367 void Advance() {
1368 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001369 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001370 }
1371
1372 HLoopInformation* Current() const {
1373 DCHECK(!Done());
1374 return current_;
1375 }
1376
1377 private:
1378 HLoopInformation* current_;
1379
1380 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1381};
1382
Artem Serov1a719e42019-07-18 14:24:55 +01001383#define FOR_EACH_CONCRETE_INSTRUCTION_SCALAR_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001384 M(Above, Condition) \
1385 M(AboveOrEqual, Condition) \
Aart Bik3dad3412018-02-28 12:01:46 -08001386 M(Abs, UnaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001387 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001388 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001389 M(ArrayGet, Instruction) \
1390 M(ArrayLength, Instruction) \
1391 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001392 M(Below, Condition) \
1393 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001394 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001395 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001396 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001397 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001398 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001399 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001400 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001401 M(Compare, BinaryOperation) \
Igor Murashkind01745e2017-04-05 16:40:31 -07001402 M(ConstructorFence, Instruction) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001403 M(CurrentMethod, Instruction) \
Mingyao Yang063fc772016-08-02 11:02:54 -07001404 M(ShouldDeoptimizeFlag, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001405 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001406 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001407 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001408 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001409 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001410 M(Exit, Instruction) \
1411 M(FloatConstant, Constant) \
1412 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001413 M(GreaterThan, Condition) \
1414 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001415 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001416 M(InstanceFieldGet, Instruction) \
1417 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001418 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001419 M(IntConstant, Constant) \
xueliang.zhonge0eb4832017-10-30 13:43:14 +00001420 M(IntermediateAddress, Instruction) \
Calin Juravle175dc732015-08-25 15:42:32 +01001421 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001422 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001423 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001424 M(InvokeVirtual, Invoke) \
Orion Hodsonac141392017-01-13 11:53:47 +00001425 M(InvokePolymorphic, Invoke) \
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001426 M(InvokeCustom, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001427 M(LessThan, Condition) \
1428 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001429 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001430 M(LoadException, Instruction) \
Orion Hodsondbaa5c72018-05-10 08:22:46 +01001431 M(LoadMethodHandle, Instruction) \
Orion Hodson18259d72018-04-12 11:18:23 +01001432 M(LoadMethodType, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001433 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001434 M(LongConstant, Constant) \
Aart Bik1f8d51b2018-02-15 10:42:37 -08001435 M(Max, Instruction) \
Calin Juravle27df7582015-04-17 19:12:31 +01001436 M(MemoryBarrier, Instruction) \
Aart Bik1f8d51b2018-02-15 10:42:37 -08001437 M(Min, BinaryOperation) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001438 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001439 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001440 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001441 M(Neg, UnaryOperation) \
1442 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001443 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001444 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001445 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001446 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001447 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001448 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001449 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001450 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001451 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001452 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001453 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001454 M(Return, Instruction) \
1455 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001456 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001457 M(Shl, BinaryOperation) \
1458 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001459 M(StaticFieldGet, Instruction) \
1460 M(StaticFieldSet, Instruction) \
Vladimir Marko552a1342017-10-31 10:56:47 +00001461 M(StringBuilderAppend, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001462 M(UnresolvedInstanceFieldGet, Instruction) \
1463 M(UnresolvedInstanceFieldSet, Instruction) \
1464 M(UnresolvedStaticFieldGet, Instruction) \
1465 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001466 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001467 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001468 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001469 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001470 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001471 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001472 M(UShr, BinaryOperation) \
Artem Serov1a719e42019-07-18 14:24:55 +01001473 M(Xor, BinaryOperation)
1474
1475#define FOR_EACH_CONCRETE_INSTRUCTION_VECTOR_COMMON(M) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001476 M(VecReplicateScalar, VecUnaryOperation) \
Aart Bik0148de42017-09-05 09:25:01 -07001477 M(VecExtractScalar, VecUnaryOperation) \
1478 M(VecReduce, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001479 M(VecCnv, VecUnaryOperation) \
1480 M(VecNeg, VecUnaryOperation) \
Aart Bik6daebeb2017-04-03 14:35:41 -07001481 M(VecAbs, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001482 M(VecNot, VecUnaryOperation) \
1483 M(VecAdd, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001484 M(VecHalvingAdd, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001485 M(VecSub, VecBinaryOperation) \
1486 M(VecMul, VecBinaryOperation) \
1487 M(VecDiv, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001488 M(VecMin, VecBinaryOperation) \
1489 M(VecMax, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001490 M(VecAnd, VecBinaryOperation) \
1491 M(VecAndNot, VecBinaryOperation) \
1492 M(VecOr, VecBinaryOperation) \
1493 M(VecXor, VecBinaryOperation) \
Aart Bik29aa0822018-03-08 11:28:00 -08001494 M(VecSaturationAdd, VecBinaryOperation) \
1495 M(VecSaturationSub, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001496 M(VecShl, VecBinaryOperation) \
1497 M(VecShr, VecBinaryOperation) \
1498 M(VecUShr, VecBinaryOperation) \
Aart Bik8de59162017-04-21 09:42:01 -07001499 M(VecSetScalars, VecOperation) \
Artem Serovf34dd202017-04-10 17:41:46 +01001500 M(VecMultiplyAccumulate, VecOperation) \
Aart Bikdbbac8f2017-09-01 13:06:08 -07001501 M(VecSADAccumulate, VecOperation) \
Artem Serovaaac0e32018-08-07 00:52:22 +01001502 M(VecDotProd, VecOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001503 M(VecLoad, VecMemoryOperation) \
1504 M(VecStore, VecMemoryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001505
Artem Serov1a719e42019-07-18 14:24:55 +01001506#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1507 FOR_EACH_CONCRETE_INSTRUCTION_SCALAR_COMMON(M) \
1508 FOR_EACH_CONCRETE_INSTRUCTION_VECTOR_COMMON(M)
1509
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001510/*
1511 * Instructions, shared across several (not all) architectures.
1512 */
1513#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1514#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1515#else
1516#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001517 M(BitwiseNegatedRight, Instruction) \
Anton Kirilov74234da2017-01-13 14:42:47 +00001518 M(DataProcWithShifterOp, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001519 M(MultiplyAccumulate, Instruction) \
Artem Serove1811ed2017-04-27 16:50:47 +01001520 M(IntermediateAddressIndex, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001521#endif
1522
Alexandre Ramesef20f712015-06-09 10:29:30 +01001523#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1524
1525#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1526
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001527#ifndef ART_ENABLE_CODEGEN_x86
1528#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1529#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001530#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1531 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001532 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001533 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001534 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001535#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001536
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05301537#if defined(ART_ENABLE_CODEGEN_x86) || defined(ART_ENABLE_CODEGEN_x86_64)
1538#define FOR_EACH_CONCRETE_INSTRUCTION_X86_COMMON(M) \
Shalini Salomi Bodapati81d15be2019-05-30 11:00:42 +05301539 M(X86AndNot, Instruction) \
Shalini Salomi Bodapatib45a4352019-07-10 16:09:41 +05301540 M(X86MaskOrResetLeastSetBit, Instruction)
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05301541#else
1542#define FOR_EACH_CONCRETE_INSTRUCTION_X86_COMMON(M)
1543#endif
1544
Alexandre Ramesef20f712015-06-09 10:29:30 +01001545#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1546
1547#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1548 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001549 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001550 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1551 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
1552 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05301553 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M) \
1554 FOR_EACH_CONCRETE_INSTRUCTION_X86_COMMON(M)
Alexandre Ramesef20f712015-06-09 10:29:30 +01001555
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001556#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1557 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001558 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001559 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001560 M(BinaryOperation, Instruction) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001561 M(Invoke, Instruction) \
1562 M(VecOperation, Instruction) \
1563 M(VecUnaryOperation, VecOperation) \
1564 M(VecBinaryOperation, VecOperation) \
1565 M(VecMemoryOperation, VecOperation)
Dave Allison20dfc792014-06-16 20:44:29 -07001566
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001567#define FOR_EACH_INSTRUCTION(M) \
1568 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1569 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1570
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001571#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001572FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1573#undef FORWARD_DECLARATION
1574
Vladimir Marko372f10e2016-05-17 16:30:10 +01001575#define DECLARE_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001576 private: \
1577 H##type& operator=(const H##type&) = delete; \
1578 public: \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001579 const char* DebugName() const override { return #type; } \
1580 HInstruction* Clone(ArenaAllocator* arena) const override { \
Artem Serovcced8ba2017-07-19 18:18:09 +01001581 DCHECK(IsClonable()); \
1582 return new (arena) H##type(*this->As##type()); \
1583 } \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001584 void Accept(HGraphVisitor* visitor) override
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001585
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001586#define DECLARE_ABSTRACT_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001587 private: \
1588 H##type& operator=(const H##type&) = delete; \
Vladimir Markoa90dd512018-05-04 15:04:45 +01001589 public:
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001590
Artem Serovcced8ba2017-07-19 18:18:09 +01001591#define DEFAULT_COPY_CONSTRUCTOR(type) \
1592 explicit H##type(const H##type& other) = default;
1593
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001594template <typename T>
Vladimir Marko82b07402017-03-01 19:02:04 +00001595class HUseListNode : public ArenaObject<kArenaAllocUseListNode>,
1596 public IntrusiveForwardListNode<HUseListNode<T>> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001597 public:
Igor Murashkind01745e2017-04-05 16:40:31 -07001598 // Get the instruction which has this use as one of the inputs.
David Brazdiled596192015-01-23 10:39:45 +00001599 T GetUser() const { return user_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001600 // Get the position of the input record that this use corresponds to.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001601 size_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001602 // Set the position of the input record that this use corresponds to.
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001603 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001604
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001605 private:
David Brazdiled596192015-01-23 10:39:45 +00001606 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001607 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001608
1609 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001610 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001611
Vladimir Marko46817b82016-03-29 12:21:58 +01001612 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001613
1614 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1615};
1616
David Brazdiled596192015-01-23 10:39:45 +00001617template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001618using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001619
David Brazdil1abb4192015-02-17 18:33:36 +00001620// This class is used by HEnvironment and HInstruction classes to record the
1621// instructions they use and pointers to the corresponding HUseListNodes kept
1622// by the used instructions.
1623template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001624class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001625 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001626 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1627 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001628
Vladimir Marko46817b82016-03-29 12:21:58 +01001629 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1630 : HUserRecord(old_record.instruction_, before_use_node) {}
1631 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1632 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001633 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001634 }
1635
1636 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001637 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1638 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001639
1640 private:
1641 // Instruction used by the user.
1642 HInstruction* instruction_;
1643
Vladimir Marko46817b82016-03-29 12:21:58 +01001644 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1645 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001646};
1647
Vladimir Markoe9004912016-06-16 16:50:52 +01001648// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1649// This is used for HInstruction::GetInputs() to return a container wrapper providing
1650// HInstruction* values even though the underlying container has HUserRecord<>s.
1651struct HInputExtractor {
1652 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1653 return record.GetInstruction();
1654 }
1655 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1656 return record.GetInstruction();
1657 }
1658};
1659
1660using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1661using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1662
Aart Bik854a02b2015-07-14 16:07:00 -07001663/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001664 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001665 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001666 * For write/read dependences on fields/arrays, the dependence analysis uses
1667 * type disambiguation (e.g. a float field write cannot modify the value of an
1668 * integer field read) and the access type (e.g. a reference array write cannot
1669 * modify the value of a reference field read [although it may modify the
1670 * reference fetch prior to reading the field, which is represented by its own
1671 * write/read dependence]). The analysis makes conservative points-to
1672 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1673 * the same, and any reference read depends on any reference read without
1674 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001675 *
Artem Serovd1aa7d02018-06-22 11:35:46 +01001676 * kDependsOnGCBit is defined in the following way: instructions with kDependsOnGCBit must not be
1677 * alive across the point where garbage collection might happen.
1678 *
1679 * Note: Instructions with kCanTriggerGCBit do not depend on each other.
1680 *
1681 * kCanTriggerGCBit must be used for instructions for which GC might happen on the path across
1682 * those instructions from the compiler perspective (between this instruction and the next one
1683 * in the IR).
1684 *
1685 * Note: Instructions which can cause GC only on a fatal slow path do not need
1686 * kCanTriggerGCBit as the execution never returns to the instruction next to the exceptional
1687 * one. However the execution may return to compiled code if there is a catch block in the
1688 * current method; for this purpose the TryBoundary exit instruction has kCanTriggerGCBit
1689 * set.
1690 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001691 * The internal representation uses 38-bit and is described in the table below.
1692 * The first line indicates the side effect, and for field/array accesses the
1693 * second line indicates the type of the access (in the order of the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001694 * DataType::Type enum).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001695 * The two numbered lines below indicate the bit position in the bitfield (read
1696 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001697 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001698 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1699 * +-------------+---------+---------+--------------+---------+---------+
1700 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1701 * | 3 |333333322|222222221| 1 |111111110|000000000|
1702 * | 7 |654321098|765432109| 8 |765432109|876543210|
1703 *
1704 * Note that, to ease the implementation, 'changes' bits are least significant
1705 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001706 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001707class SideEffects : public ValueObject {
1708 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001709 SideEffects() : flags_(0) {}
1710
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001711 static SideEffects None() {
1712 return SideEffects(0);
1713 }
1714
1715 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001716 return SideEffects(kAllChangeBits | kAllDependOnBits);
1717 }
1718
1719 static SideEffects AllChanges() {
1720 return SideEffects(kAllChangeBits);
1721 }
1722
1723 static SideEffects AllDependencies() {
1724 return SideEffects(kAllDependOnBits);
1725 }
1726
1727 static SideEffects AllExceptGCDependency() {
1728 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1729 }
1730
1731 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001732 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001733 }
1734
Aart Bik34c3ba92015-07-20 14:08:59 -07001735 static SideEffects AllWrites() {
1736 return SideEffects(kAllWrites);
1737 }
1738
1739 static SideEffects AllReads() {
1740 return SideEffects(kAllReads);
1741 }
1742
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001743 static SideEffects FieldWriteOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001744 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001745 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001746 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001747 }
1748
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001749 static SideEffects ArrayWriteOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001750 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001751 }
1752
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001753 static SideEffects FieldReadOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001754 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001755 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001756 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001757 }
1758
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001759 static SideEffects ArrayReadOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001760 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001761 }
1762
Artem Serovd1aa7d02018-06-22 11:35:46 +01001763 // Returns whether GC might happen across this instruction from the compiler perspective so
1764 // the next instruction in the IR would see that.
1765 //
1766 // See the SideEffect class comments.
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001767 static SideEffects CanTriggerGC() {
1768 return SideEffects(1ULL << kCanTriggerGCBit);
1769 }
1770
Artem Serovd1aa7d02018-06-22 11:35:46 +01001771 // Returns whether the instruction must not be alive across a GC point.
1772 //
1773 // See the SideEffect class comments.
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001774 static SideEffects DependsOnGC() {
1775 return SideEffects(1ULL << kDependsOnGCBit);
1776 }
1777
Aart Bik854a02b2015-07-14 16:07:00 -07001778 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001779 SideEffects Union(SideEffects other) const {
1780 return SideEffects(flags_ | other.flags_);
1781 }
1782
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001783 SideEffects Exclusion(SideEffects other) const {
1784 return SideEffects(flags_ & ~other.flags_);
1785 }
1786
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001787 void Add(SideEffects other) {
1788 flags_ |= other.flags_;
1789 }
1790
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001791 bool Includes(SideEffects other) const {
1792 return (other.flags_ & flags_) == other.flags_;
1793 }
1794
1795 bool HasSideEffects() const {
1796 return (flags_ & kAllChangeBits);
1797 }
1798
1799 bool HasDependencies() const {
1800 return (flags_ & kAllDependOnBits);
1801 }
1802
1803 // Returns true if there are no side effects or dependencies.
1804 bool DoesNothing() const {
1805 return flags_ == 0;
1806 }
1807
Aart Bik854a02b2015-07-14 16:07:00 -07001808 // Returns true if something is written.
1809 bool DoesAnyWrite() const {
1810 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001811 }
1812
Aart Bik854a02b2015-07-14 16:07:00 -07001813 // Returns true if something is read.
1814 bool DoesAnyRead() const {
1815 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001816 }
1817
Aart Bik854a02b2015-07-14 16:07:00 -07001818 // Returns true if potentially everything is written and read
1819 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001820 bool DoesAllReadWrite() const {
1821 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1822 }
1823
Aart Bik854a02b2015-07-14 16:07:00 -07001824 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001825 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001826 }
1827
Roland Levillain0d5a2812015-11-13 10:07:31 +00001828 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001829 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001830 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1831 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001832 }
1833
1834 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001835 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001836 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001837 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001838 for (int s = kLastBit; s >= 0; s--) {
1839 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1840 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1841 // This is a bit for the GC side effect.
1842 if (current_bit_is_set) {
1843 flags += "GC";
1844 }
Aart Bik854a02b2015-07-14 16:07:00 -07001845 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001846 } else {
1847 // This is a bit for the array/field analysis.
1848 // The underscore character stands for the 'can trigger GC' bit.
1849 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1850 if (current_bit_is_set) {
1851 flags += kDebug[s];
1852 }
1853 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1854 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1855 flags += "|";
1856 }
1857 }
Aart Bik854a02b2015-07-14 16:07:00 -07001858 }
1859 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001860 }
1861
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001862 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001863
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001864 private:
1865 static constexpr int kFieldArrayAnalysisBits = 9;
1866
1867 static constexpr int kFieldWriteOffset = 0;
1868 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1869 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1870 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1871
1872 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1873
1874 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1875 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1876 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1877 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1878
1879 static constexpr int kLastBit = kDependsOnGCBit;
1880 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1881
1882 // Aliases.
1883
1884 static_assert(kChangeBits == kDependOnBits,
1885 "the 'change' bits should match the 'depend on' bits.");
1886
1887 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1888 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1889 static constexpr uint64_t kAllWrites =
1890 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1891 static constexpr uint64_t kAllReads =
1892 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001893
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001894 // Translates type to bit flag. The type must correspond to a Java type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001895 static uint64_t TypeFlag(DataType::Type type, int offset) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001896 int shift;
1897 switch (type) {
1898 case DataType::Type::kReference: shift = 0; break;
1899 case DataType::Type::kBool: shift = 1; break;
1900 case DataType::Type::kInt8: shift = 2; break;
1901 case DataType::Type::kUint16: shift = 3; break;
1902 case DataType::Type::kInt16: shift = 4; break;
1903 case DataType::Type::kInt32: shift = 5; break;
1904 case DataType::Type::kInt64: shift = 6; break;
1905 case DataType::Type::kFloat32: shift = 7; break;
1906 case DataType::Type::kFloat64: shift = 8; break;
1907 default:
1908 LOG(FATAL) << "Unexpected data type " << type;
1909 UNREACHABLE();
1910 }
Aart Bik854a02b2015-07-14 16:07:00 -07001911 DCHECK_LE(kFieldWriteOffset, shift);
1912 DCHECK_LT(shift, kArrayWriteOffset);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001913 return UINT64_C(1) << (shift + offset);
Aart Bik854a02b2015-07-14 16:07:00 -07001914 }
1915
1916 // Private constructor on direct flags value.
1917 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1918
1919 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001920};
1921
David Brazdiled596192015-01-23 10:39:45 +00001922// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001923class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001924 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001925 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
Mingyao Yang01b47b02017-02-03 12:09:57 -08001926 size_t number_of_vregs,
1927 ArtMethod* method,
1928 uint32_t dex_pc,
1929 HInstruction* holder)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001930 : vregs_(number_of_vregs, allocator->Adapter(kArenaAllocEnvironmentVRegs)),
1931 locations_(allocator->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001932 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001933 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001934 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001935 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001936 }
1937
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001938 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
1939 const HEnvironment& to_copy,
1940 HInstruction* holder)
1941 : HEnvironment(allocator,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001942 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001943 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001944 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001945 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001946
Vladimir Markoec32f642017-06-02 10:51:55 +01001947 void AllocateLocations() {
1948 DCHECK(locations_.empty());
1949 locations_.resize(vregs_.size());
1950 }
1951
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001952 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001953 if (parent_ != nullptr) {
1954 parent_->SetAndCopyParentChain(allocator, parent);
1955 } else {
1956 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1957 parent_->CopyFrom(parent);
1958 if (parent->GetParent() != nullptr) {
1959 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1960 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001961 }
David Brazdiled596192015-01-23 10:39:45 +00001962 }
1963
Vladimir Marko69d310e2017-10-09 14:12:23 +01001964 void CopyFrom(ArrayRef<HInstruction* const> locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001965 void CopyFrom(HEnvironment* environment);
1966
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001967 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1968 // input to the loop phi instead. This is for inserting instructions that
1969 // require an environment (like HDeoptimization) in the loop pre-header.
1970 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001971
1972 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001973 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001974 }
1975
1976 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001977 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001978 }
1979
David Brazdil1abb4192015-02-17 18:33:36 +00001980 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001981
Artem Serovca210e32017-12-15 13:43:20 +00001982 // Replaces the input at the position 'index' with the replacement; the replacement and old
1983 // input instructions' env_uses_ lists are adjusted. The function works similar to
1984 // HInstruction::ReplaceInput.
1985 void ReplaceInput(HInstruction* replacement, size_t index);
1986
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001987 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001988
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001989 HEnvironment* GetParent() const { return parent_; }
1990
1991 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001992 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001993 }
1994
1995 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001996 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001997 }
1998
1999 uint32_t GetDexPc() const {
2000 return dex_pc_;
2001 }
2002
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00002003 ArtMethod* GetMethod() const {
2004 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002005 }
2006
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002007 HInstruction* GetHolder() const {
2008 return holder_;
2009 }
2010
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00002011
2012 bool IsFromInlinedInvoke() const {
2013 return GetParent() != nullptr;
2014 }
2015
David Brazdiled596192015-01-23 10:39:45 +00002016 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002017 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
2018 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002019 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00002020 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002021 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00002022
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01002023 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002024 HInstruction* const holder_;
2025
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01002026 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00002027
2028 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
2029};
2030
Vladimir Markof9f64412015-09-02 14:05:49 +01002031class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002032 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302033#define DECLARE_KIND(type, super) k##type,
2034 enum InstructionKind {
Vladimir Markoe3946222018-05-04 14:18:47 +01002035 FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_KIND)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302036 kLastInstructionKind
2037 };
2038#undef DECLARE_KIND
2039
2040 HInstruction(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01002041 : HInstruction(kind, DataType::Type::kVoid, side_effects, dex_pc) {}
2042
2043 HInstruction(InstructionKind kind, DataType::Type type, SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002044 : previous_(nullptr),
2045 next_(nullptr),
2046 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002047 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002048 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002049 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002050 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002051 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002052 locations_(nullptr),
2053 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002054 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00002055 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002056 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302057 SetPackedField<InstructionKindField>(kind);
Vladimir Markobd785672018-05-03 17:09:09 +01002058 SetPackedField<TypeField>(type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002059 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
2060 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002061
Dave Allison20dfc792014-06-16 20:44:29 -07002062 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002063
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002064
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002065 HInstruction* GetNext() const { return next_; }
2066 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002067
Calin Juravle77520bc2015-01-12 18:45:46 +00002068 HInstruction* GetNextDisregardingMoves() const;
2069 HInstruction* GetPreviousDisregardingMoves() const;
2070
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002071 HBasicBlock* GetBlock() const { return block_; }
Vladimir Markoca6fff82017-10-03 14:49:14 +01002072 ArenaAllocator* GetAllocator() const { return block_->GetGraph()->GetAllocator(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002073 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002074 bool IsInBlock() const { return block_ != nullptr; }
2075 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00002076 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
2077 bool IsIrreducibleLoopHeaderPhi() const {
2078 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
2079 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002080
Vladimir Marko372f10e2016-05-17 16:30:10 +01002081 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
2082
2083 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
2084 // One virtual method is enough, just const_cast<> and then re-add the const.
2085 return ArrayRef<const HUserRecord<HInstruction*>>(
2086 const_cast<HInstruction*>(this)->GetInputRecords());
2087 }
2088
Vladimir Markoe9004912016-06-16 16:50:52 +01002089 HInputsRef GetInputs() {
2090 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002091 }
2092
Vladimir Markoe9004912016-06-16 16:50:52 +01002093 HConstInputsRef GetInputs() const {
2094 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002095 }
2096
2097 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00002098 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002099
Aart Bik2767f4b2016-10-28 15:03:53 -07002100 bool HasInput(HInstruction* input) const {
2101 for (const HInstruction* i : GetInputs()) {
2102 if (i == input) {
2103 return true;
2104 }
2105 }
2106 return false;
2107 }
2108
Vladimir Marko372f10e2016-05-17 16:30:10 +01002109 void SetRawInputAt(size_t index, HInstruction* input) {
2110 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
2111 }
2112
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002113 virtual void Accept(HGraphVisitor* visitor) = 0;
2114 virtual const char* DebugName() const = 0;
2115
Vladimir Markobd785672018-05-03 17:09:09 +01002116 DataType::Type GetType() const {
2117 return TypeField::Decode(GetPackedFields());
2118 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002119
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002120 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002121
2122 uint32_t GetDexPc() const { return dex_pc_; }
2123
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002124 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01002125
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002126 // Can the instruction throw?
2127 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
2128 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01002129 virtual bool CanThrow() const { return false; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08002130
2131 // Does the instruction always throw an exception unconditionally?
2132 virtual bool AlwaysThrows() const { return false; }
2133
David Brazdilec16f792015-08-19 15:04:01 +01002134 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002135
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002136 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002137 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002138
Calin Juravle10e244f2015-01-26 18:54:32 +00002139 // Does not apply for all instructions, but having this at top level greatly
2140 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00002141 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002142 virtual bool CanBeNull() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002143 DCHECK_EQ(GetType(), DataType::Type::kReference) << "CanBeNull only applies to reference types";
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002144 return true;
2145 }
Calin Juravle10e244f2015-01-26 18:54:32 +00002146
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002147 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01002148 return false;
2149 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002150
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01002151 // If this instruction will do an implicit null check, return the `HNullCheck` associated
2152 // with it. Otherwise return null.
2153 HNullCheck* GetImplicitNullCheck() const {
Vladimir Markoee1f1262019-06-26 14:37:17 +01002154 // Go over previous non-move instructions that are emitted at use site.
2155 HInstruction* prev_not_move = GetPreviousDisregardingMoves();
2156 while (prev_not_move != nullptr && prev_not_move->IsEmittedAtUseSite()) {
2157 if (prev_not_move->IsNullCheck()) {
2158 return prev_not_move->AsNullCheck();
2159 }
2160 prev_not_move = prev_not_move->GetPreviousDisregardingMoves();
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01002161 }
2162 return nullptr;
2163 }
2164
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002165 virtual bool IsActualObject() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002166 return GetType() == DataType::Type::kReference;
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002167 }
2168
Calin Juravle2e768302015-07-28 14:41:11 +00002169 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00002170
Calin Juravle61d544b2015-02-23 16:46:57 +00002171 ReferenceTypeInfo GetReferenceTypeInfo() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002172 DCHECK_EQ(GetType(), DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002173 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00002174 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00002175 }
Calin Juravleacf735c2015-02-12 15:25:22 +00002176
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002177 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00002178 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002179 // Note: fixup_end remains valid across push_front().
2180 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
2181 HUseListNode<HInstruction*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002182 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HInstruction*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002183 uses_.push_front(*new_node);
2184 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002185 }
2186
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002187 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01002188 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002189 // Note: env_fixup_end remains valid across push_front().
2190 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
2191 HUseListNode<HEnvironment*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002192 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HEnvironment*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002193 env_uses_.push_front(*new_node);
2194 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002195 }
2196
David Brazdil1abb4192015-02-17 18:33:36 +00002197 void RemoveAsUserOfInput(size_t input) {
2198 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002199 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2200 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2201 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00002202 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002203
Vladimir Marko372f10e2016-05-17 16:30:10 +01002204 void RemoveAsUserOfAllInputs() {
2205 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2206 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2207 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2208 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
2209 }
2210 }
2211
David Brazdil1abb4192015-02-17 18:33:36 +00002212 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2213 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002214
Vladimir Marko46817b82016-03-29 12:21:58 +01002215 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2216 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2217 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002218 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002219 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002220 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002221
Aart Bikcc42be02016-10-20 16:14:16 -07002222 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002223 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002224 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002225 !CanThrow() &&
2226 !IsSuspendCheck() &&
2227 !IsControlFlow() &&
2228 !IsNativeDebugInfo() &&
2229 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002230 // If we added an explicit barrier then we should keep it.
Igor Murashkind01745e2017-04-05 16:40:31 -07002231 !IsMemoryBarrier() &&
2232 !IsConstructorFence();
Aart Bik482095d2016-10-10 15:39:10 -07002233 }
2234
Aart Bikcc42be02016-10-20 16:14:16 -07002235 bool IsDeadAndRemovable() const {
2236 return IsRemovable() && !HasUses();
2237 }
2238
Roland Levillain6c82d402014-10-13 16:10:27 +01002239 // Does this instruction strictly dominate `other_instruction`?
2240 // Returns false if this instruction and `other_instruction` are the same.
2241 // Aborts if this instruction and `other_instruction` are both phis.
2242 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002243
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002244 int GetId() const { return id_; }
2245 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002246
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002247 int GetSsaIndex() const { return ssa_index_; }
2248 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2249 bool HasSsaIndex() const { return ssa_index_ != -1; }
2250
2251 bool HasEnvironment() const { return environment_ != nullptr; }
2252 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002253 // Set the `environment_` field. Raw because this method does not
2254 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002255 void SetRawEnvironment(HEnvironment* environment) {
2256 DCHECK(environment_ == nullptr);
2257 DCHECK_EQ(environment->GetHolder(), this);
2258 environment_ = environment;
2259 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002260
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002261 void InsertRawEnvironment(HEnvironment* environment) {
2262 DCHECK(environment_ != nullptr);
2263 DCHECK_EQ(environment->GetHolder(), this);
2264 DCHECK(environment->GetParent() == nullptr);
2265 environment->parent_ = environment_;
2266 environment_ = environment;
2267 }
2268
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002269 void RemoveEnvironment();
2270
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002271 // Set the environment of this instruction, copying it from `environment`. While
2272 // copying, the uses lists are being updated.
2273 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002274 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002275 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002276 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002277 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002278 if (environment->GetParent() != nullptr) {
2279 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2280 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002281 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002282
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002283 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2284 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002285 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002286 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002287 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002288 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002289 if (environment->GetParent() != nullptr) {
2290 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2291 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002292 }
2293
Nicolas Geoffray39468442014-09-02 15:17:15 +01002294 // Returns the number of entries in the environment. Typically, that is the
2295 // number of dex registers in a method. It could be more in case of inlining.
2296 size_t EnvironmentSize() const;
2297
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002298 LocationSummary* GetLocations() const { return locations_; }
2299 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002300
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002301 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002302 void ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray8a62a4c2018-07-03 09:39:07 +01002303 void ReplaceEnvUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002304 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002305
Alexandre Rames188d4312015-04-09 18:30:21 +01002306 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2307 // uses of this instruction by `other` are *not* updated.
2308 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2309 ReplaceWith(other);
2310 other->ReplaceInput(this, use_index);
2311 }
2312
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002313 // Move `this` instruction before `cursor`
2314 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002315
Vladimir Markofb337ea2015-11-25 15:25:10 +00002316 // Move `this` before its first user and out of any loops. If there is no
2317 // out-of-loop user that dominates all other users, move the instruction
2318 // to the end of the out-of-loop common dominator of the user's blocks.
2319 //
2320 // This can be used only on non-throwing instructions with no side effects that
2321 // have at least one use but no environment uses.
2322 void MoveBeforeFirstUserAndOutOfLoops();
2323
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002324#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01002325 bool Is##type() const;
2326
2327 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2328#undef INSTRUCTION_TYPE_CHECK
2329
2330#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002331 const H##type* As##type() const; \
2332 H##type* As##type();
2333
Vladimir Markoa90dd512018-05-04 15:04:45 +01002334 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
2335#undef INSTRUCTION_TYPE_CAST
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002336
Artem Serovcced8ba2017-07-19 18:18:09 +01002337 // Return a clone of the instruction if it is clonable (shallow copy by default, custom copy
2338 // if a custom copy-constructor is provided for a particular type). If IsClonable() is false for
2339 // the instruction then the behaviour of this function is undefined.
2340 //
2341 // Note: It is semantically valid to create a clone of the instruction only until
2342 // prepare_for_register_allocator phase as lifetime, intervals and codegen info are not
2343 // copied.
2344 //
2345 // Note: HEnvironment and some other fields are not copied and are set to default values, see
2346 // 'explicit HInstruction(const HInstruction& other)' for details.
2347 virtual HInstruction* Clone(ArenaAllocator* arena ATTRIBUTE_UNUSED) const {
2348 LOG(FATAL) << "Cloning is not implemented for the instruction " <<
2349 DebugName() << " " << GetId();
2350 UNREACHABLE();
2351 }
2352
2353 // Return whether instruction can be cloned (copied).
2354 virtual bool IsClonable() const { return false; }
2355
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002356 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002357 // TODO: this method is used by LICM and GVN with possibly different
2358 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002359 virtual bool CanBeMoved() const { return false; }
2360
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002361 // Returns whether any data encoded in the two instructions is equal.
2362 // This method does not look at the inputs. Both instructions must be
2363 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002364 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002365 return false;
2366 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002367
2368 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002369 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002370 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002371 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002372
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302373 InstructionKind GetKind() const { return GetPackedField<InstructionKindField>(); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002374
2375 virtual size_t ComputeHashCode() const {
2376 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002377 for (const HInstruction* input : GetInputs()) {
2378 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002379 }
2380 return result;
2381 }
2382
2383 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002384 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002385 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002386
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002387 size_t GetLifetimePosition() const { return lifetime_position_; }
2388 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2389 LiveInterval* GetLiveInterval() const { return live_interval_; }
2390 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2391 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2392
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002393 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2394
2395 // Returns whether the code generation of the instruction will require to have access
2396 // to the current method. Such instructions are:
2397 // (1): Instructions that require an environment, as calling the runtime requires
2398 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002399 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2400 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002401 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002402 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002403 }
2404
Vladimir Markodc151b22015-10-15 18:02:30 +01002405 // Returns whether the code generation of the instruction will require to have access
2406 // to the dex cache of the current method's declaring class via the current method.
2407 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002408
Mark Mendellc4701932015-04-10 13:18:51 -04002409 // Does this instruction have any use in an environment before
2410 // control flow hits 'other'?
2411 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2412
2413 // Remove all references to environment uses of this instruction.
2414 // The caller must ensure that this is safe to do.
2415 void RemoveEnvironmentUsers();
2416
Vladimir Markoa1de9182016-02-25 11:37:38 +00002417 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2418 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002419
David Brazdil1abb4192015-02-17 18:33:36 +00002420 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002421 // If set, the machine code for this instruction is assumed to be generated by
2422 // its users. Used by liveness analysis to compute use positions accordingly.
2423 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2424 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302425 static constexpr size_t kFieldInstructionKind = kFlagReferenceTypeIsExact + 1;
2426 static constexpr size_t kFieldInstructionKindSize =
2427 MinimumBitsToStore(static_cast<size_t>(InstructionKind::kLastInstructionKind - 1));
Vladimir Markobd785672018-05-03 17:09:09 +01002428 static constexpr size_t kFieldType =
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302429 kFieldInstructionKind + kFieldInstructionKindSize;
Vladimir Markobd785672018-05-03 17:09:09 +01002430 static constexpr size_t kFieldTypeSize =
2431 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
2432 static constexpr size_t kNumberOfGenericPackedBits = kFieldType + kFieldTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00002433 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2434
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302435 static_assert(kNumberOfGenericPackedBits <= kMaxNumberOfPackedBits,
2436 "Too many generic packed fields");
2437
Vladimir Markobd785672018-05-03 17:09:09 +01002438 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
2439
Vladimir Marko372f10e2016-05-17 16:30:10 +01002440 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2441 return GetInputRecords()[i];
2442 }
2443
2444 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2445 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2446 input_records[index] = input;
2447 }
David Brazdil1abb4192015-02-17 18:33:36 +00002448
Vladimir Markoa1de9182016-02-25 11:37:38 +00002449 uint32_t GetPackedFields() const {
2450 return packed_fields_;
2451 }
2452
2453 template <size_t flag>
2454 bool GetPackedFlag() const {
2455 return (packed_fields_ & (1u << flag)) != 0u;
2456 }
2457
2458 template <size_t flag>
2459 void SetPackedFlag(bool value = true) {
2460 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2461 }
2462
2463 template <typename BitFieldType>
2464 typename BitFieldType::value_type GetPackedField() const {
2465 return BitFieldType::Decode(packed_fields_);
2466 }
2467
2468 template <typename BitFieldType>
2469 void SetPackedField(typename BitFieldType::value_type value) {
2470 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2471 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2472 }
2473
Artem Serovcced8ba2017-07-19 18:18:09 +01002474 // Copy construction for the instruction (used for Clone function).
2475 //
2476 // Fields (e.g. lifetime, intervals and codegen info) associated with phases starting from
2477 // prepare_for_register_allocator are not copied (set to default values).
2478 //
2479 // Copy constructors must be provided for every HInstruction type; default copy constructor is
2480 // fine for most of them. However for some of the instructions a custom copy constructor must be
2481 // specified (when instruction has non-trivially copyable fields and must have a special behaviour
2482 // for copying them).
2483 explicit HInstruction(const HInstruction& other)
2484 : previous_(nullptr),
2485 next_(nullptr),
2486 block_(nullptr),
2487 dex_pc_(other.dex_pc_),
2488 id_(-1),
2489 ssa_index_(-1),
2490 packed_fields_(other.packed_fields_),
2491 environment_(nullptr),
2492 locations_(nullptr),
2493 live_interval_(nullptr),
2494 lifetime_position_(kNoLifetime),
2495 side_effects_(other.side_effects_),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302496 reference_type_handle_(other.reference_type_handle_) {
2497 }
Artem Serovcced8ba2017-07-19 18:18:09 +01002498
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002499 private:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302500 using InstructionKindField =
2501 BitField<InstructionKind, kFieldInstructionKind, kFieldInstructionKindSize>;
2502
Vladimir Marko46817b82016-03-29 12:21:58 +01002503 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2504 auto before_use_node = uses_.before_begin();
2505 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2506 HInstruction* user = use_node->GetUser();
2507 size_t input_index = use_node->GetIndex();
2508 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2509 before_use_node = use_node;
2510 }
2511 }
2512
2513 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2514 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2515 if (next != uses_.end()) {
2516 HInstruction* next_user = next->GetUser();
2517 size_t next_index = next->GetIndex();
2518 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2519 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2520 }
2521 }
2522
2523 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2524 auto before_env_use_node = env_uses_.before_begin();
2525 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2526 HEnvironment* user = env_use_node->GetUser();
2527 size_t input_index = env_use_node->GetIndex();
2528 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2529 before_env_use_node = env_use_node;
2530 }
2531 }
2532
2533 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2534 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2535 if (next != env_uses_.end()) {
2536 HEnvironment* next_user = next->GetUser();
2537 size_t next_index = next->GetIndex();
2538 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2539 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2540 }
2541 }
David Brazdil1abb4192015-02-17 18:33:36 +00002542
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002543 HInstruction* previous_;
2544 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002545 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002546 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002547
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002548 // An instruction gets an id when it is added to the graph.
2549 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002550 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002551 int id_;
2552
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002553 // When doing liveness analysis, instructions that have uses get an SSA index.
2554 int ssa_index_;
2555
Vladimir Markoa1de9182016-02-25 11:37:38 +00002556 // Packed fields.
2557 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002558
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002559 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002560 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002561
2562 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002563 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002564
Nicolas Geoffray39468442014-09-02 15:17:15 +01002565 // The environment associated with this instruction. Not null if the instruction
2566 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002567 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002568
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002569 // Set by the code generator.
2570 LocationSummary* locations_;
2571
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002572 // Set by the liveness analysis.
2573 LiveInterval* live_interval_;
2574
2575 // Set by the liveness analysis, this is the position in a linear
2576 // order of blocks where this instruction's live interval start.
2577 size_t lifetime_position_;
2578
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002579 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002580
Vladimir Markoa1de9182016-02-25 11:37:38 +00002581 // The reference handle part of the reference type info.
2582 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002583 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002584 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002585
David Brazdil1abb4192015-02-17 18:33:36 +00002586 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002587 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002588 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002589 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002590 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002591};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002592std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002593
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002594// Iterates over the instructions, while preserving the next instruction
2595// in case the current instruction gets removed from the list by the user
2596// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002597class HInstructionIterator : public ValueObject {
2598 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002599 explicit HInstructionIterator(const HInstructionList& instructions)
2600 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002601 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002602 }
2603
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002604 bool Done() const { return instruction_ == nullptr; }
2605 HInstruction* Current() const { return instruction_; }
2606 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002607 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002608 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002609 }
2610
2611 private:
2612 HInstruction* instruction_;
2613 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002614
2615 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002616};
2617
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002618// Iterates over the instructions without saving the next instruction,
2619// therefore handling changes in the graph potentially made by the user
2620// of this iterator.
2621class HInstructionIteratorHandleChanges : public ValueObject {
2622 public:
2623 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2624 : instruction_(instructions.first_instruction_) {
2625 }
2626
2627 bool Done() const { return instruction_ == nullptr; }
2628 HInstruction* Current() const { return instruction_; }
2629 void Advance() {
2630 instruction_ = instruction_->GetNext();
2631 }
2632
2633 private:
2634 HInstruction* instruction_;
2635
2636 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2637};
2638
2639
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002640class HBackwardInstructionIterator : public ValueObject {
2641 public:
2642 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2643 : instruction_(instructions.last_instruction_) {
2644 next_ = Done() ? nullptr : instruction_->GetPrevious();
2645 }
2646
2647 bool Done() const { return instruction_ == nullptr; }
2648 HInstruction* Current() const { return instruction_; }
2649 void Advance() {
2650 instruction_ = next_;
2651 next_ = Done() ? nullptr : instruction_->GetPrevious();
2652 }
2653
2654 private:
2655 HInstruction* instruction_;
2656 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002657
2658 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002659};
2660
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002661class HVariableInputSizeInstruction : public HInstruction {
2662 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002663 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002664 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() override {
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002665 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2666 }
2667
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002668 void AddInput(HInstruction* input);
2669 void InsertInputAt(size_t index, HInstruction* input);
2670 void RemoveInputAt(size_t index);
2671
Igor Murashkind01745e2017-04-05 16:40:31 -07002672 // Removes all the inputs.
2673 // Also removes this instructions from each input's use list
2674 // (for non-environment uses only).
2675 void RemoveAllInputs();
2676
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002677 protected:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302678 HVariableInputSizeInstruction(InstructionKind inst_kind,
2679 SideEffects side_effects,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002680 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002681 ArenaAllocator* allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002682 size_t number_of_inputs,
2683 ArenaAllocKind kind)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302684 : HInstruction(inst_kind, side_effects, dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002685 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Vladimir Markobd785672018-05-03 17:09:09 +01002686 HVariableInputSizeInstruction(InstructionKind inst_kind,
2687 DataType::Type type,
2688 SideEffects side_effects,
2689 uint32_t dex_pc,
2690 ArenaAllocator* allocator,
2691 size_t number_of_inputs,
2692 ArenaAllocKind kind)
2693 : HInstruction(inst_kind, type, side_effects, dex_pc),
2694 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002695
Artem Serovcced8ba2017-07-19 18:18:09 +01002696 DEFAULT_COPY_CONSTRUCTOR(VariableInputSizeInstruction);
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002697
Artem Serovcced8ba2017-07-19 18:18:09 +01002698 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002699};
2700
Vladimir Markof9f64412015-09-02 14:05:49 +01002701template<size_t N>
Vladimir Markobd785672018-05-03 17:09:09 +01002702class HExpression : public HInstruction {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002703 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002704 HExpression<N>(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302705 : HInstruction(kind, side_effects, dex_pc), inputs_() {}
Vladimir Markobd785672018-05-03 17:09:09 +01002706 HExpression<N>(InstructionKind kind,
2707 DataType::Type type,
2708 SideEffects side_effects,
2709 uint32_t dex_pc)
2710 : HInstruction(kind, type, side_effects, dex_pc), inputs_() {}
2711 virtual ~HExpression() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002712
Vladimir Marko372f10e2016-05-17 16:30:10 +01002713 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01002714 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01002715 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002716 }
2717
Artem Serovcced8ba2017-07-19 18:18:09 +01002718 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002719 DEFAULT_COPY_CONSTRUCTOR(Expression<N>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002720
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002721 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002722 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002723
2724 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002725};
2726
Vladimir Markobd785672018-05-03 17:09:09 +01002727// HExpression specialization for N=0.
Vladimir Markof9f64412015-09-02 14:05:49 +01002728template<>
Vladimir Markobd785672018-05-03 17:09:09 +01002729class HExpression<0> : public HInstruction {
Vladimir Markof9f64412015-09-02 14:05:49 +01002730 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002731 using HInstruction::HInstruction;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002732
Vladimir Markobd785672018-05-03 17:09:09 +01002733 virtual ~HExpression() {}
Vladimir Markof9f64412015-09-02 14:05:49 +01002734
Vladimir Marko372f10e2016-05-17 16:30:10 +01002735 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01002736 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01002737 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002738 }
2739
Artem Serovcced8ba2017-07-19 18:18:09 +01002740 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002741 DEFAULT_COPY_CONSTRUCTOR(Expression<0>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002742
Vladimir Markof9f64412015-09-02 14:05:49 +01002743 private:
2744 friend class SsaBuilder;
2745};
2746
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002747// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2748// instruction that branches to the exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002749class HReturnVoid final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002750 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002751 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002752 : HExpression(kReturnVoid, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302753 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002754
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002755 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002756
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002757 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002758
Artem Serovcced8ba2017-07-19 18:18:09 +01002759 protected:
2760 DEFAULT_COPY_CONSTRUCTOR(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002761};
2762
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002763// Represents dex's RETURN opcodes. A HReturn is a control flow
2764// instruction that branches to the exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002765class HReturn final : public HExpression<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002766 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002767 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002768 : HExpression(kReturn, SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002769 SetRawInputAt(0, value);
2770 }
2771
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002772 bool IsControlFlow() const override { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002773
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002774 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002775
Artem Serovcced8ba2017-07-19 18:18:09 +01002776 protected:
2777 DEFAULT_COPY_CONSTRUCTOR(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002778};
2779
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002780class HPhi final : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002781 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002782 HPhi(ArenaAllocator* allocator,
David Brazdildee58d62016-04-07 09:54:26 +00002783 uint32_t reg_number,
2784 size_t number_of_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002785 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +00002786 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002787 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302788 kPhi,
Vladimir Markobd785672018-05-03 17:09:09 +01002789 ToPhiType(type),
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002790 SideEffects::None(),
2791 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002792 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002793 number_of_inputs,
2794 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002795 reg_number_(reg_number) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002796 DCHECK_NE(GetType(), DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +00002797 // Phis are constructed live and marked dead if conflicting or unused.
2798 // Individual steps of SsaBuilder should assume that if a phi has been
2799 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2800 SetPackedFlag<kFlagIsLive>(true);
2801 SetPackedFlag<kFlagCanBeNull>(true);
2802 }
2803
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002804 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01002805
David Brazdildee58d62016-04-07 09:54:26 +00002806 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002807 static DataType::Type ToPhiType(DataType::Type type) {
2808 return DataType::Kind(type);
David Brazdildee58d62016-04-07 09:54:26 +00002809 }
2810
2811 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2812
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002813 void SetType(DataType::Type new_type) {
David Brazdildee58d62016-04-07 09:54:26 +00002814 // Make sure that only valid type changes occur. The following are allowed:
2815 // (1) int -> float/ref (primitive type propagation),
2816 // (2) long -> double (primitive type propagation).
2817 DCHECK(GetType() == new_type ||
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002818 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kFloat32) ||
2819 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kReference) ||
2820 (GetType() == DataType::Type::kInt64 && new_type == DataType::Type::kFloat64));
David Brazdildee58d62016-04-07 09:54:26 +00002821 SetPackedField<TypeField>(new_type);
2822 }
2823
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002824 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
David Brazdildee58d62016-04-07 09:54:26 +00002825 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2826
2827 uint32_t GetRegNumber() const { return reg_number_; }
2828
2829 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2830 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2831 bool IsDead() const { return !IsLive(); }
2832 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2833
Vladimir Markoe9004912016-06-16 16:50:52 +01002834 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002835 return other != nullptr
2836 && other->IsPhi()
2837 && other->AsPhi()->GetBlock() == GetBlock()
2838 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2839 }
2840
Nicolas Geoffrayf57c1ae2017-06-28 17:40:18 +01002841 bool HasEquivalentPhi() const {
2842 if (GetPrevious() != nullptr && GetPrevious()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2843 return true;
2844 }
2845 if (GetNext() != nullptr && GetNext()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2846 return true;
2847 }
2848 return false;
2849 }
2850
David Brazdildee58d62016-04-07 09:54:26 +00002851 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2852 // An equivalent phi is a phi having the same dex register and type.
2853 // It assumes that phis with the same dex register are adjacent.
2854 HPhi* GetNextEquivalentPhiWithSameType() {
2855 HInstruction* next = GetNext();
2856 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2857 if (next->GetType() == GetType()) {
2858 return next->AsPhi();
2859 }
2860 next = next->GetNext();
2861 }
2862 return nullptr;
2863 }
2864
2865 DECLARE_INSTRUCTION(Phi);
2866
Artem Serovcced8ba2017-07-19 18:18:09 +01002867 protected:
2868 DEFAULT_COPY_CONSTRUCTOR(Phi);
2869
David Brazdildee58d62016-04-07 09:54:26 +00002870 private:
Vladimir Markobd785672018-05-03 17:09:09 +01002871 static constexpr size_t kFlagIsLive = HInstruction::kNumberOfGenericPackedBits;
David Brazdildee58d62016-04-07 09:54:26 +00002872 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2873 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2874 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
David Brazdildee58d62016-04-07 09:54:26 +00002875
David Brazdildee58d62016-04-07 09:54:26 +00002876 const uint32_t reg_number_;
David Brazdildee58d62016-04-07 09:54:26 +00002877};
2878
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002879// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002880// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002881// exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002882class HExit final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002883 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302884 explicit HExit(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002885 : HExpression(kExit, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302886 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002887
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002888 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002889
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002890 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002891
Artem Serovcced8ba2017-07-19 18:18:09 +01002892 protected:
2893 DEFAULT_COPY_CONSTRUCTOR(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002894};
2895
2896// Jumps from one block to another.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002897class HGoto final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002898 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302899 explicit HGoto(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002900 : HExpression(kGoto, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302901 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002902
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002903 bool IsClonable() const override { return true; }
2904 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002905
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002906 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002907 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002908 }
2909
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002910 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002911
Artem Serovcced8ba2017-07-19 18:18:09 +01002912 protected:
2913 DEFAULT_COPY_CONSTRUCTOR(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002914};
2915
Roland Levillain9867bc72015-08-05 10:21:34 +01002916class HConstant : public HExpression<0> {
2917 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302918 explicit HConstant(InstructionKind kind, DataType::Type type, uint32_t dex_pc = kNoDexPc)
2919 : HExpression(kind, type, SideEffects::None(), dex_pc) {
2920 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002921
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002922 bool CanBeMoved() const override { return true; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002923
Roland Levillain1a653882016-03-18 18:05:57 +00002924 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002925 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002926 // Is this constant 0 in the arithmetic sense?
2927 virtual bool IsArithmeticZero() const { return false; }
2928 // Is this constant a 0-bit pattern?
2929 virtual bool IsZeroBitPattern() const { return false; }
2930 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002931 virtual bool IsOne() const { return false; }
2932
David Brazdil77a48ae2015-09-15 12:34:04 +00002933 virtual uint64_t GetValueAsUint64() const = 0;
2934
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002935 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002936
Artem Serovcced8ba2017-07-19 18:18:09 +01002937 protected:
2938 DEFAULT_COPY_CONSTRUCTOR(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002939};
2940
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002941class HNullConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002942 public:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002943 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01002944 return true;
2945 }
2946
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002947 uint64_t GetValueAsUint64() const override { return 0; }
David Brazdil77a48ae2015-09-15 12:34:04 +00002948
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002949 size_t ComputeHashCode() const override { return 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002950
Roland Levillain1a653882016-03-18 18:05:57 +00002951 // The null constant representation is a 0-bit pattern.
Yi Kong39402542019-03-24 02:47:16 -07002952 bool IsZeroBitPattern() const override { return true; }
Roland Levillain1a653882016-03-18 18:05:57 +00002953
Roland Levillain9867bc72015-08-05 10:21:34 +01002954 DECLARE_INSTRUCTION(NullConstant);
2955
Artem Serovcced8ba2017-07-19 18:18:09 +01002956 protected:
2957 DEFAULT_COPY_CONSTRUCTOR(NullConstant);
2958
Roland Levillain9867bc72015-08-05 10:21:34 +01002959 private:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002960 explicit HNullConstant(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302961 : HConstant(kNullConstant, DataType::Type::kReference, dex_pc) {
2962 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002963
2964 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01002965};
2966
2967// Constants of the type int. Those can be from Dex instructions, or
2968// synthesized (for example with the if-eqz instruction).
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002969class HIntConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002970 public:
2971 int32_t GetValue() const { return value_; }
2972
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002973 uint64_t GetValueAsUint64() const override {
David Brazdil9f389d42015-10-01 14:32:56 +01002974 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2975 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002976
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002977 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002978 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002979 return other->AsIntConstant()->value_ == value_;
2980 }
2981
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002982 size_t ComputeHashCode() const override { return GetValue(); }
Roland Levillain9867bc72015-08-05 10:21:34 +01002983
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002984 bool IsMinusOne() const override { return GetValue() == -1; }
2985 bool IsArithmeticZero() const override { return GetValue() == 0; }
2986 bool IsZeroBitPattern() const override { return GetValue() == 0; }
2987 bool IsOne() const override { return GetValue() == 1; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002988
Roland Levillain1a653882016-03-18 18:05:57 +00002989 // Integer constants are used to encode Boolean values as well,
2990 // where 1 means true and 0 means false.
2991 bool IsTrue() const { return GetValue() == 1; }
2992 bool IsFalse() const { return GetValue() == 0; }
2993
Roland Levillain9867bc72015-08-05 10:21:34 +01002994 DECLARE_INSTRUCTION(IntConstant);
2995
Artem Serovcced8ba2017-07-19 18:18:09 +01002996 protected:
2997 DEFAULT_COPY_CONSTRUCTOR(IntConstant);
2998
Roland Levillain9867bc72015-08-05 10:21:34 +01002999 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003000 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303001 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc), value_(value) {
3002 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003003 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303004 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc),
3005 value_(value ? 1 : 0) {
3006 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003007
3008 const int32_t value_;
3009
3010 friend class HGraph;
3011 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
3012 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Roland Levillain9867bc72015-08-05 10:21:34 +01003013};
3014
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003015class HLongConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01003016 public:
3017 int64_t GetValue() const { return value_; }
3018
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003019 uint64_t GetValueAsUint64() const override { return value_; }
David Brazdil77a48ae2015-09-15 12:34:04 +00003020
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003021 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003022 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01003023 return other->AsLongConstant()->value_ == value_;
3024 }
3025
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003026 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain9867bc72015-08-05 10:21:34 +01003027
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003028 bool IsMinusOne() const override { return GetValue() == -1; }
3029 bool IsArithmeticZero() const override { return GetValue() == 0; }
3030 bool IsZeroBitPattern() const override { return GetValue() == 0; }
3031 bool IsOne() const override { return GetValue() == 1; }
Roland Levillain9867bc72015-08-05 10:21:34 +01003032
3033 DECLARE_INSTRUCTION(LongConstant);
3034
Artem Serovcced8ba2017-07-19 18:18:09 +01003035 protected:
3036 DEFAULT_COPY_CONSTRUCTOR(LongConstant);
3037
Roland Levillain9867bc72015-08-05 10:21:34 +01003038 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003039 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303040 : HConstant(kLongConstant, DataType::Type::kInt64, dex_pc),
3041 value_(value) {
3042 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003043
3044 const int64_t value_;
3045
3046 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01003047};
Dave Allison20dfc792014-06-16 20:44:29 -07003048
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003049class HFloatConstant final : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003050 public:
3051 float GetValue() const { return value_; }
3052
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003053 uint64_t GetValueAsUint64() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003054 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3055 }
3056
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003057 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003058 DCHECK(other->IsFloatConstant()) << other->DebugName();
3059 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
3060 }
3061
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003062 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003063
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003064 bool IsMinusOne() 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 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003067 bool IsArithmeticZero() const override {
Roland Levillain1a653882016-03-18 18:05:57 +00003068 return std::fpclassify(value_) == FP_ZERO;
3069 }
3070 bool IsArithmeticPositiveZero() const {
3071 return IsArithmeticZero() && !std::signbit(value_);
3072 }
3073 bool IsArithmeticNegativeZero() const {
3074 return IsArithmeticZero() && std::signbit(value_);
3075 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003076 bool IsZeroBitPattern() const override {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003077 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00003078 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003079 bool IsOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003080 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3081 }
3082 bool IsNaN() const {
3083 return std::isnan(value_);
3084 }
3085
3086 DECLARE_INSTRUCTION(FloatConstant);
3087
Artem Serovcced8ba2017-07-19 18:18:09 +01003088 protected:
3089 DEFAULT_COPY_CONSTRUCTOR(FloatConstant);
3090
Roland Levillain31dd3d62016-02-16 12:21:02 +00003091 private:
3092 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303093 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3094 value_(value) {
3095 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003096 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303097 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3098 value_(bit_cast<float, int32_t>(value)) {
3099 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003100
3101 const float value_;
3102
3103 // Only the SsaBuilder and HGraph can create floating-point constants.
3104 friend class SsaBuilder;
3105 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003106};
3107
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003108class HDoubleConstant final : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003109 public:
3110 double GetValue() const { return value_; }
3111
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003112 uint64_t GetValueAsUint64() const override { return bit_cast<uint64_t, double>(value_); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003113
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003114 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003115 DCHECK(other->IsDoubleConstant()) << other->DebugName();
3116 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
3117 }
3118
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003119 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003120
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003121 bool IsMinusOne() 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 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003124 bool IsArithmeticZero() const override {
Roland Levillain1a653882016-03-18 18:05:57 +00003125 return std::fpclassify(value_) == FP_ZERO;
3126 }
3127 bool IsArithmeticPositiveZero() const {
3128 return IsArithmeticZero() && !std::signbit(value_);
3129 }
3130 bool IsArithmeticNegativeZero() const {
3131 return IsArithmeticZero() && std::signbit(value_);
3132 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003133 bool IsZeroBitPattern() const override {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003134 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00003135 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003136 bool IsOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003137 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3138 }
3139 bool IsNaN() const {
3140 return std::isnan(value_);
3141 }
3142
3143 DECLARE_INSTRUCTION(DoubleConstant);
3144
Artem Serovcced8ba2017-07-19 18:18:09 +01003145 protected:
3146 DEFAULT_COPY_CONSTRUCTOR(DoubleConstant);
3147
Roland Levillain31dd3d62016-02-16 12:21:02 +00003148 private:
3149 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303150 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3151 value_(value) {
3152 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003153 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303154 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3155 value_(bit_cast<double, int64_t>(value)) {
3156 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003157
3158 const double value_;
3159
3160 // Only the SsaBuilder and HGraph can create floating-point constants.
3161 friend class SsaBuilder;
3162 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003163};
3164
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003165// Conditional branch. A block ending with an HIf instruction must have
3166// two successors.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003167class HIf final : public HExpression<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003168 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003169 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003170 : HExpression(kIf, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003171 SetRawInputAt(0, input);
3172 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003173
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003174 bool IsClonable() const override { return true; }
3175 bool IsControlFlow() const override { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003176
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003177 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003178 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003179 }
3180
3181 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003182 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003183 }
3184
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003185 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003186
Artem Serovcced8ba2017-07-19 18:18:09 +01003187 protected:
3188 DEFAULT_COPY_CONSTRUCTOR(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003189};
3190
David Brazdilfc6a86a2015-06-26 10:33:45 +00003191
3192// Abstract instruction which marks the beginning and/or end of a try block and
3193// links it to the respective exception handlers. Behaves the same as a Goto in
3194// non-exceptional control flow.
3195// Normal-flow successor is stored at index zero, exception handlers under
3196// higher indices in no particular order.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003197class HTryBoundary final : public HExpression<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003198 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003199 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01003200 kEntry,
3201 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003202 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01003203 };
3204
Artem Serovd1aa7d02018-06-22 11:35:46 +01003205 // SideEffects::CanTriggerGC prevents instructions with SideEffects::DependOnGC to be alive
3206 // across the catch block entering edges as GC might happen during throwing an exception.
3207 // TryBoundary with BoundaryKind::kExit is conservatively used for that as there is no
3208 // HInstruction which a catch block must start from.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003209 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01003210 : HExpression(kTryBoundary,
3211 (kind == BoundaryKind::kExit) ? SideEffects::CanTriggerGC()
3212 : SideEffects::None(),
3213 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003214 SetPackedField<BoundaryKindField>(kind);
3215 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003216
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003217 bool IsControlFlow() const override { return true; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003218
3219 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01003220 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003221
David Brazdild26a4112015-11-10 11:07:31 +00003222 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
3223 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
3224 }
3225
David Brazdilfc6a86a2015-06-26 10:33:45 +00003226 // Returns whether `handler` is among its exception handlers (non-zero index
3227 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01003228 bool HasExceptionHandler(const HBasicBlock& handler) const {
3229 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00003230 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00003231 }
3232
3233 // If not present already, adds `handler` to its block's list of exception
3234 // handlers.
3235 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01003236 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003237 GetBlock()->AddSuccessor(handler);
3238 }
3239 }
3240
Vladimir Markoa1de9182016-02-25 11:37:38 +00003241 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
3242 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003243
David Brazdilffee3d32015-07-06 11:48:53 +01003244 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
3245
David Brazdilfc6a86a2015-06-26 10:33:45 +00003246 DECLARE_INSTRUCTION(TryBoundary);
3247
Artem Serovcced8ba2017-07-19 18:18:09 +01003248 protected:
3249 DEFAULT_COPY_CONSTRUCTOR(TryBoundary);
3250
David Brazdilfc6a86a2015-06-26 10:33:45 +00003251 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003252 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
3253 static constexpr size_t kFieldBoundaryKindSize =
3254 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
3255 static constexpr size_t kNumberOfTryBoundaryPackedBits =
3256 kFieldBoundaryKind + kFieldBoundaryKindSize;
3257 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
3258 "Too many packed fields.");
3259 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00003260};
3261
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003262// Deoptimize to interpreter, upon checking a condition.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003263class HDeoptimize final : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003264 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003265 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
3266 // across.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003267 HDeoptimize(ArenaAllocator* allocator,
3268 HInstruction* cond,
3269 DeoptimizationKind kind,
3270 uint32_t dex_pc)
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003271 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303272 kDeoptimize,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003273 SideEffects::All(),
3274 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003275 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08003276 /* number_of_inputs= */ 1,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003277 kArenaAllocMisc) {
3278 SetPackedFlag<kFieldCanBeMoved>(false);
3279 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003280 SetRawInputAt(0, cond);
3281 }
3282
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003283 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003284
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003285 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
3286 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
3287 // instead of `guard`.
3288 // We set CanTriggerGC to prevent any intermediate address to be live
3289 // at the point of the `HDeoptimize`.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003290 HDeoptimize(ArenaAllocator* allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003291 HInstruction* cond,
3292 HInstruction* guard,
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003293 DeoptimizationKind kind,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003294 uint32_t dex_pc)
3295 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303296 kDeoptimize,
Vladimir Markobd785672018-05-03 17:09:09 +01003297 guard->GetType(),
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003298 SideEffects::CanTriggerGC(),
3299 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003300 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08003301 /* number_of_inputs= */ 2,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003302 kArenaAllocMisc) {
3303 SetPackedFlag<kFieldCanBeMoved>(true);
3304 SetPackedField<DeoptimizeKindField>(kind);
3305 SetRawInputAt(0, cond);
3306 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003307 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003308
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003309 bool CanBeMoved() const override { return GetPackedFlag<kFieldCanBeMoved>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003310
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003311 bool InstructionDataEquals(const HInstruction* other) const override {
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003312 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
3313 }
3314
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003315 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003316
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003317 bool CanThrow() const override { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003318
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003319 DeoptimizationKind GetDeoptimizationKind() const { return GetPackedField<DeoptimizeKindField>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003320
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003321 bool GuardsAnInput() const {
3322 return InputCount() == 2;
3323 }
3324
3325 HInstruction* GuardedInput() const {
3326 DCHECK(GuardsAnInput());
3327 return InputAt(1);
3328 }
3329
3330 void RemoveGuard() {
3331 RemoveInputAt(1);
3332 }
3333
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003334 DECLARE_INSTRUCTION(Deoptimize);
3335
Artem Serovcced8ba2017-07-19 18:18:09 +01003336 protected:
3337 DEFAULT_COPY_CONSTRUCTOR(Deoptimize);
3338
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003339 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003340 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3341 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3342 static constexpr size_t kFieldDeoptimizeKindSize =
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003343 MinimumBitsToStore(static_cast<size_t>(DeoptimizationKind::kLast));
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003344 static constexpr size_t kNumberOfDeoptimizePackedBits =
3345 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3346 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3347 "Too many packed fields.");
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003348 using DeoptimizeKindField =
3349 BitField<DeoptimizationKind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003350};
3351
Mingyao Yang063fc772016-08-02 11:02:54 -07003352// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3353// The compiled code checks this flag value in a guard before devirtualized call and
3354// if it's true, starts to do deoptimization.
3355// It has a 4-byte slot on stack.
3356// TODO: allocate a register for this flag.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003357class HShouldDeoptimizeFlag final : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003358 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003359 // CHA guards are only optimized in a separate pass and it has no side effects
3360 // with regard to other passes.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003361 HShouldDeoptimizeFlag(ArenaAllocator* allocator, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303362 : HVariableInputSizeInstruction(kShouldDeoptimizeFlag,
Vladimir Markobd785672018-05-03 17:09:09 +01003363 DataType::Type::kInt32,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303364 SideEffects::None(),
3365 dex_pc,
3366 allocator,
3367 0,
3368 kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003369 }
3370
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003371 // We do all CHA guard elimination/motion in a single pass, after which there is no
3372 // further guard elimination/motion since a guard might have been used for justification
3373 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3374 // to avoid other optimizations trying to move it.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003375 bool CanBeMoved() const override { return false; }
Mingyao Yang063fc772016-08-02 11:02:54 -07003376
3377 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3378
Artem Serovcced8ba2017-07-19 18:18:09 +01003379 protected:
3380 DEFAULT_COPY_CONSTRUCTOR(ShouldDeoptimizeFlag);
Mingyao Yang063fc772016-08-02 11:02:54 -07003381};
3382
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003383// Represents the ArtMethod that was passed as a first argument to
3384// the method. It is used by instructions that depend on it, like
3385// instructions that work with the dex cache.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003386class HCurrentMethod final : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003387 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003388 explicit HCurrentMethod(DataType::Type type, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303389 : HExpression(kCurrentMethod, type, SideEffects::None(), dex_pc) {
3390 }
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003391
3392 DECLARE_INSTRUCTION(CurrentMethod);
3393
Artem Serovcced8ba2017-07-19 18:18:09 +01003394 protected:
3395 DEFAULT_COPY_CONSTRUCTOR(CurrentMethod);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003396};
3397
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003398// Fetches an ArtMethod from the virtual table or the interface method table
3399// of a class.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003400class HClassTableGet final : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003401 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003402 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003403 kVTable,
3404 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003405 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003406 };
3407 HClassTableGet(HInstruction* cls,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003408 DataType::Type type,
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003409 TableKind kind,
3410 size_t index,
3411 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303412 : HExpression(kClassTableGet, type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003413 index_(index) {
3414 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003415 SetRawInputAt(0, cls);
3416 }
3417
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003418 bool IsClonable() const override { return true; }
3419 bool CanBeMoved() const override { return true; }
3420 bool InstructionDataEquals(const HInstruction* other) const override {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003421 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003422 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003423 }
3424
Vladimir Markoa1de9182016-02-25 11:37:38 +00003425 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003426 size_t GetIndex() const { return index_; }
3427
3428 DECLARE_INSTRUCTION(ClassTableGet);
3429
Artem Serovcced8ba2017-07-19 18:18:09 +01003430 protected:
3431 DEFAULT_COPY_CONSTRUCTOR(ClassTableGet);
3432
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003433 private:
Vladimir Markobd785672018-05-03 17:09:09 +01003434 static constexpr size_t kFieldTableKind = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003435 static constexpr size_t kFieldTableKindSize =
3436 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3437 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3438 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3439 "Too many packed fields.");
3440 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3441
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003442 // The index of the ArtMethod in the table.
3443 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003444};
3445
Mark Mendellfe57faa2015-09-18 09:26:15 -04003446// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3447// have one successor for each entry in the switch table, and the final successor
3448// will be the block containing the next Dex opcode.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003449class HPackedSwitch final : public HExpression<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003450 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003451 HPackedSwitch(int32_t start_value,
3452 uint32_t num_entries,
3453 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003454 uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003455 : HExpression(kPackedSwitch, SideEffects::None(), dex_pc),
Mark Mendellfe57faa2015-09-18 09:26:15 -04003456 start_value_(start_value),
3457 num_entries_(num_entries) {
3458 SetRawInputAt(0, input);
3459 }
3460
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003461 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003462
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003463 bool IsControlFlow() const override { return true; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003464
3465 int32_t GetStartValue() const { return start_value_; }
3466
Vladimir Marko211c2112015-09-24 16:52:33 +01003467 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003468
3469 HBasicBlock* GetDefaultBlock() const {
3470 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003471 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003472 }
3473 DECLARE_INSTRUCTION(PackedSwitch);
3474
Artem Serovcced8ba2017-07-19 18:18:09 +01003475 protected:
3476 DEFAULT_COPY_CONSTRUCTOR(PackedSwitch);
3477
Mark Mendellfe57faa2015-09-18 09:26:15 -04003478 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003479 const int32_t start_value_;
3480 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003481};
3482
Roland Levillain88cb1752014-10-20 16:36:47 +01003483class HUnaryOperation : public HExpression<1> {
3484 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303485 HUnaryOperation(InstructionKind kind,
3486 DataType::Type result_type,
3487 HInstruction* input,
3488 uint32_t dex_pc = kNoDexPc)
3489 : HExpression(kind, result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003490 SetRawInputAt(0, input);
3491 }
3492
Artem Serovcced8ba2017-07-19 18:18:09 +01003493 // All of the UnaryOperation instructions are clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003494 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003495
Roland Levillain88cb1752014-10-20 16:36:47 +01003496 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003497 DataType::Type GetResultType() const { return GetType(); }
Roland Levillain88cb1752014-10-20 16:36:47 +01003498
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003499 bool CanBeMoved() const override { return true; }
3500 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003501 return true;
3502 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003503
Roland Levillain31dd3d62016-02-16 12:21:02 +00003504 // Try to statically evaluate `this` and return a HConstant
3505 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003506 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003507 HConstant* TryStaticEvaluation() const;
3508
3509 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003510 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3511 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003512 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3513 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003514
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003515 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003516
Artem Serovcced8ba2017-07-19 18:18:09 +01003517 protected:
3518 DEFAULT_COPY_CONSTRUCTOR(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003519};
3520
Dave Allison20dfc792014-06-16 20:44:29 -07003521class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003522 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303523 HBinaryOperation(InstructionKind kind,
3524 DataType::Type result_type,
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003525 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003526 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003527 SideEffects side_effects = SideEffects::None(),
3528 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303529 : HExpression(kind, result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003530 SetRawInputAt(0, left);
3531 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003532 }
3533
Artem Serovcced8ba2017-07-19 18:18:09 +01003534 // All of the BinaryOperation instructions are clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003535 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003536
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003537 HInstruction* GetLeft() const { return InputAt(0); }
3538 HInstruction* GetRight() const { return InputAt(1); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003539 DataType::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003540
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003541 virtual bool IsCommutative() const { return false; }
3542
3543 // Put constant on the right.
3544 // Returns whether order is changed.
3545 bool OrderInputsWithConstantOnTheRight() {
3546 HInstruction* left = InputAt(0);
3547 HInstruction* right = InputAt(1);
3548 if (left->IsConstant() && !right->IsConstant()) {
3549 ReplaceInput(right, 0);
3550 ReplaceInput(left, 1);
3551 return true;
3552 }
3553 return false;
3554 }
3555
3556 // Order inputs by instruction id, but favor constant on the right side.
3557 // This helps GVN for commutative ops.
3558 void OrderInputs() {
3559 DCHECK(IsCommutative());
3560 HInstruction* left = InputAt(0);
3561 HInstruction* right = InputAt(1);
3562 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3563 return;
3564 }
3565 if (OrderInputsWithConstantOnTheRight()) {
3566 return;
3567 }
3568 // Order according to instruction id.
3569 if (left->GetId() > right->GetId()) {
3570 ReplaceInput(right, 0);
3571 ReplaceInput(left, 1);
3572 }
3573 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003574
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003575 bool CanBeMoved() const override { return true; }
3576 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003577 return true;
3578 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003579
Roland Levillain31dd3d62016-02-16 12:21:02 +00003580 // Try to statically evaluate `this` and return a HConstant
3581 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003582 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003583 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003584
3585 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003586 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3587 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003588 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3589 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003590 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003591 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3592 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003593 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3594 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003595 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3596 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003597 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003598 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3599 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003600
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003601 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003602 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003603 HConstant* GetConstantRight() const;
3604
3605 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003606 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003607 HInstruction* GetLeastConstantLeft() const;
3608
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003609 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003610
Artem Serovcced8ba2017-07-19 18:18:09 +01003611 protected:
3612 DEFAULT_COPY_CONSTRUCTOR(BinaryOperation);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003613};
3614
Mark Mendellc4701932015-04-10 13:18:51 -04003615// The comparison bias applies for floating point operations and indicates how NaN
3616// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003617enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003618 kNoBias, // bias is not applicable (i.e. for long operation)
3619 kGtBias, // return 1 for NaN comparisons
3620 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003621 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003622};
3623
Roland Levillain31dd3d62016-02-16 12:21:02 +00003624std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3625
Dave Allison20dfc792014-06-16 20:44:29 -07003626class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003627 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303628 HCondition(InstructionKind kind,
3629 HInstruction* first,
3630 HInstruction* second,
3631 uint32_t dex_pc = kNoDexPc)
3632 : HBinaryOperation(kind,
3633 DataType::Type::kBool,
3634 first,
3635 second,
3636 SideEffects::None(),
3637 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003638 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3639 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003640
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003641 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003642 // `instruction`, and disregard moves in between.
3643 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003644
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003645 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003646
3647 virtual IfCondition GetCondition() const = 0;
3648
Mark Mendellc4701932015-04-10 13:18:51 -04003649 virtual IfCondition GetOppositeCondition() const = 0;
3650
Vladimir Markoa1de9182016-02-25 11:37:38 +00003651 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003652 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3653
Vladimir Markoa1de9182016-02-25 11:37:38 +00003654 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3655 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003656
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003657 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003658 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003659 }
3660
Roland Levillain4fa13f62015-07-06 18:11:54 +01003661 bool IsFPConditionTrueIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003662 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003663 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003664 if (if_cond == kCondNE) {
3665 return true;
3666 } else if (if_cond == kCondEQ) {
3667 return false;
3668 }
3669 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003670 }
3671
3672 bool IsFPConditionFalseIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003673 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003674 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003675 if (if_cond == kCondEQ) {
3676 return true;
3677 } else if (if_cond == kCondNE) {
3678 return false;
3679 }
3680 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003681 }
3682
Roland Levillain31dd3d62016-02-16 12:21:02 +00003683 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003684 // Needed if we merge a HCompare into a HCondition.
Vladimir Markobd785672018-05-03 17:09:09 +01003685 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003686 static constexpr size_t kFieldComparisonBiasSize =
3687 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3688 static constexpr size_t kNumberOfConditionPackedBits =
3689 kFieldComparisonBias + kFieldComparisonBiasSize;
3690 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3691 using ComparisonBiasField =
3692 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3693
Roland Levillain31dd3d62016-02-16 12:21:02 +00003694 template <typename T>
3695 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3696
3697 template <typename T>
3698 int32_t CompareFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003699 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003700 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3701 // Handle the bias.
3702 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3703 }
3704
3705 // Return an integer constant containing the result of a condition evaluated at compile time.
3706 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3707 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3708 }
3709
Artem Serovcced8ba2017-07-19 18:18:09 +01003710 DEFAULT_COPY_CONSTRUCTOR(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003711};
3712
3713// Instruction to check if two inputs are equal to each other.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003714class HEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003715 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003716 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303717 : HCondition(kEqual, first, second, dex_pc) {
3718 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003719
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003720 bool IsCommutative() const override { return true; }
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003721
Vladimir Marko9e23df52015-11-10 17:14:35 +00003722 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003723 HNullConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003724 return MakeConstantCondition(true, GetDexPc());
3725 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003726 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003727 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3728 }
3729 // In the following Evaluate methods, a HCompare instruction has
3730 // been merged into this HEqual instruction; evaluate it as
3731 // `Compare(x, y) == 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003732 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003733 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3734 GetDexPc());
3735 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003736 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003737 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3738 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003739 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003740 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003741 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003742
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003743 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003744
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003745 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003746 return kCondEQ;
3747 }
3748
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003749 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003750 return kCondNE;
3751 }
3752
Artem Serovcced8ba2017-07-19 18:18:09 +01003753 protected:
3754 DEFAULT_COPY_CONSTRUCTOR(Equal);
3755
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003756 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003757 template <typename T> static bool Compute(T x, T y) { return x == y; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003758};
3759
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003760class HNotEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003761 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303762 HNotEqual(HInstruction* first, HInstruction* second,
3763 uint32_t dex_pc = kNoDexPc)
3764 : HCondition(kNotEqual, first, second, dex_pc) {
3765 }
Dave Allison20dfc792014-06-16 20:44:29 -07003766
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003767 bool IsCommutative() const override { return true; }
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003768
Vladimir Marko9e23df52015-11-10 17:14:35 +00003769 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003770 HNullConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003771 return MakeConstantCondition(false, GetDexPc());
3772 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003773 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003774 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3775 }
3776 // In the following Evaluate methods, a HCompare instruction has
3777 // been merged into this HNotEqual instruction; evaluate it as
3778 // `Compare(x, y) != 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003779 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003780 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3781 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003782 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003783 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3784 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003785 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003786 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003787 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003788
Dave Allison20dfc792014-06-16 20:44:29 -07003789 DECLARE_INSTRUCTION(NotEqual);
3790
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003791 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003792 return kCondNE;
3793 }
3794
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003795 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003796 return kCondEQ;
3797 }
3798
Artem Serovcced8ba2017-07-19 18:18:09 +01003799 protected:
3800 DEFAULT_COPY_CONSTRUCTOR(NotEqual);
3801
Dave Allison20dfc792014-06-16 20:44:29 -07003802 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003803 template <typename T> static bool Compute(T x, T y) { return x != y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003804};
3805
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003806class HLessThan final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003807 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303808 HLessThan(HInstruction* first, HInstruction* second,
3809 uint32_t dex_pc = kNoDexPc)
3810 : HCondition(kLessThan, first, second, dex_pc) {
3811 }
Dave Allison20dfc792014-06-16 20:44:29 -07003812
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003813 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003814 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003815 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003816 // In the following Evaluate methods, a HCompare instruction has
3817 // been merged into this HLessThan instruction; evaluate it as
3818 // `Compare(x, y) < 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003819 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003820 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3821 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003822 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003823 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3824 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003825 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003826 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003827 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003828
Dave Allison20dfc792014-06-16 20:44:29 -07003829 DECLARE_INSTRUCTION(LessThan);
3830
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003831 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003832 return kCondLT;
3833 }
3834
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003835 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003836 return kCondGE;
3837 }
3838
Artem Serovcced8ba2017-07-19 18:18:09 +01003839 protected:
3840 DEFAULT_COPY_CONSTRUCTOR(LessThan);
3841
Dave Allison20dfc792014-06-16 20:44:29 -07003842 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003843 template <typename T> static bool Compute(T x, T y) { return x < y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003844};
3845
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003846class HLessThanOrEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003847 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303848 HLessThanOrEqual(HInstruction* first, HInstruction* second,
3849 uint32_t dex_pc = kNoDexPc)
3850 : HCondition(kLessThanOrEqual, first, second, dex_pc) {
3851 }
Dave Allison20dfc792014-06-16 20:44:29 -07003852
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003853 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003854 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003855 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003856 // In the following Evaluate methods, a HCompare instruction has
3857 // been merged into this HLessThanOrEqual instruction; evaluate it as
3858 // `Compare(x, y) <= 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003859 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003860 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3861 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003862 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003863 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3864 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003865 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003866 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003867 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003868
Dave Allison20dfc792014-06-16 20:44:29 -07003869 DECLARE_INSTRUCTION(LessThanOrEqual);
3870
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003871 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003872 return kCondLE;
3873 }
3874
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003875 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003876 return kCondGT;
3877 }
3878
Artem Serovcced8ba2017-07-19 18:18:09 +01003879 protected:
3880 DEFAULT_COPY_CONSTRUCTOR(LessThanOrEqual);
3881
Dave Allison20dfc792014-06-16 20:44:29 -07003882 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003883 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003884};
3885
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003886class HGreaterThan final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003887 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003888 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303889 : HCondition(kGreaterThan, first, second, dex_pc) {
3890 }
Dave Allison20dfc792014-06-16 20:44:29 -07003891
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003892 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003893 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003894 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003895 // In the following Evaluate methods, a HCompare instruction has
3896 // been merged into this HGreaterThan instruction; evaluate it as
3897 // `Compare(x, y) > 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003898 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003899 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3900 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003901 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003902 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3903 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003904 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003905 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003906 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003907
Dave Allison20dfc792014-06-16 20:44:29 -07003908 DECLARE_INSTRUCTION(GreaterThan);
3909
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003910 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003911 return kCondGT;
3912 }
3913
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003914 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003915 return kCondLE;
3916 }
3917
Artem Serovcced8ba2017-07-19 18:18:09 +01003918 protected:
3919 DEFAULT_COPY_CONSTRUCTOR(GreaterThan);
3920
Dave Allison20dfc792014-06-16 20:44:29 -07003921 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003922 template <typename T> static bool Compute(T x, T y) { return x > y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003923};
3924
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003925class HGreaterThanOrEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003926 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003927 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303928 : HCondition(kGreaterThanOrEqual, first, second, dex_pc) {
3929 }
Dave Allison20dfc792014-06-16 20:44:29 -07003930
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003931 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003932 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003933 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003934 // In the following Evaluate methods, a HCompare instruction has
3935 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3936 // `Compare(x, y) >= 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003937 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003938 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3939 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003940 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003941 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3942 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003943 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003944 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003945 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003946
Dave Allison20dfc792014-06-16 20:44:29 -07003947 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3948
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003949 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003950 return kCondGE;
3951 }
3952
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003953 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003954 return kCondLT;
3955 }
3956
Artem Serovcced8ba2017-07-19 18:18:09 +01003957 protected:
3958 DEFAULT_COPY_CONSTRUCTOR(GreaterThanOrEqual);
3959
Dave Allison20dfc792014-06-16 20:44:29 -07003960 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003961 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003962};
3963
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003964class HBelow final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003965 public:
3966 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303967 : HCondition(kBelow, first, second, dex_pc) {
3968 }
Aart Bike9f37602015-10-09 11:15:55 -07003969
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003970 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003971 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003972 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003973 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003974 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3975 }
3976 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003977 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003978 LOG(FATAL) << DebugName() << " is not defined for float values";
3979 UNREACHABLE();
3980 }
3981 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003982 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003983 LOG(FATAL) << DebugName() << " is not defined for double values";
3984 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003985 }
3986
3987 DECLARE_INSTRUCTION(Below);
3988
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003989 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07003990 return kCondB;
3991 }
3992
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003993 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07003994 return kCondAE;
3995 }
3996
Artem Serovcced8ba2017-07-19 18:18:09 +01003997 protected:
3998 DEFAULT_COPY_CONSTRUCTOR(Below);
3999
Aart Bike9f37602015-10-09 11:15:55 -07004000 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004001 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004002 return MakeUnsigned(x) < MakeUnsigned(y);
4003 }
Aart Bike9f37602015-10-09 11:15:55 -07004004};
4005
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004006class HBelowOrEqual final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004007 public:
4008 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304009 : HCondition(kBelowOrEqual, first, second, dex_pc) {
4010 }
Aart Bike9f37602015-10-09 11:15:55 -07004011
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004012 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004013 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004014 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004015 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004016 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4017 }
4018 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004019 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004020 LOG(FATAL) << DebugName() << " is not defined for float values";
4021 UNREACHABLE();
4022 }
4023 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004024 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004025 LOG(FATAL) << DebugName() << " is not defined for double values";
4026 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004027 }
4028
4029 DECLARE_INSTRUCTION(BelowOrEqual);
4030
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004031 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004032 return kCondBE;
4033 }
4034
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004035 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004036 return kCondA;
4037 }
4038
Artem Serovcced8ba2017-07-19 18:18:09 +01004039 protected:
4040 DEFAULT_COPY_CONSTRUCTOR(BelowOrEqual);
4041
Aart Bike9f37602015-10-09 11:15:55 -07004042 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004043 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004044 return MakeUnsigned(x) <= MakeUnsigned(y);
4045 }
Aart Bike9f37602015-10-09 11:15:55 -07004046};
4047
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004048class HAbove final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004049 public:
4050 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304051 : HCondition(kAbove, first, second, dex_pc) {
4052 }
Aart Bike9f37602015-10-09 11:15:55 -07004053
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004054 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004055 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004056 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004057 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004058 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4059 }
4060 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004061 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004062 LOG(FATAL) << DebugName() << " is not defined for float values";
4063 UNREACHABLE();
4064 }
4065 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004066 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004067 LOG(FATAL) << DebugName() << " is not defined for double values";
4068 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004069 }
4070
4071 DECLARE_INSTRUCTION(Above);
4072
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004073 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004074 return kCondA;
4075 }
4076
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004077 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004078 return kCondBE;
4079 }
4080
Artem Serovcced8ba2017-07-19 18:18:09 +01004081 protected:
4082 DEFAULT_COPY_CONSTRUCTOR(Above);
4083
Aart Bike9f37602015-10-09 11:15:55 -07004084 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004085 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004086 return MakeUnsigned(x) > MakeUnsigned(y);
4087 }
Aart Bike9f37602015-10-09 11:15:55 -07004088};
4089
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004090class HAboveOrEqual final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004091 public:
4092 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304093 : HCondition(kAboveOrEqual, first, second, dex_pc) {
4094 }
Aart Bike9f37602015-10-09 11:15:55 -07004095
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004096 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004097 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004098 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004099 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004100 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4101 }
4102 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004103 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004104 LOG(FATAL) << DebugName() << " is not defined for float values";
4105 UNREACHABLE();
4106 }
4107 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004108 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004109 LOG(FATAL) << DebugName() << " is not defined for double values";
4110 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004111 }
4112
4113 DECLARE_INSTRUCTION(AboveOrEqual);
4114
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004115 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004116 return kCondAE;
4117 }
4118
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004119 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004120 return kCondB;
4121 }
4122
Artem Serovcced8ba2017-07-19 18:18:09 +01004123 protected:
4124 DEFAULT_COPY_CONSTRUCTOR(AboveOrEqual);
4125
Aart Bike9f37602015-10-09 11:15:55 -07004126 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004127 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004128 return MakeUnsigned(x) >= MakeUnsigned(y);
4129 }
Aart Bike9f37602015-10-09 11:15:55 -07004130};
Dave Allison20dfc792014-06-16 20:44:29 -07004131
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004132// Instruction to check how two inputs compare to each other.
4133// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004134class HCompare final : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004135 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00004136 // Note that `comparison_type` is the type of comparison performed
4137 // between the comparison's inputs, not the type of the instantiated
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004138 // HCompare instruction (which is always DataType::Type::kInt).
4139 HCompare(DataType::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004140 HInstruction* first,
4141 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04004142 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004143 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304144 : HBinaryOperation(kCompare,
4145 DataType::Type::kInt32,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004146 first,
4147 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00004148 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004149 dex_pc) {
4150 SetPackedField<ComparisonBiasField>(bias);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004151 }
4152
Roland Levillain9867bc72015-08-05 10:21:34 +01004153 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004154 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
4155
4156 template <typename T>
4157 int32_t ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004158 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004159 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
4160 // Handle the bias.
4161 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
4162 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004163
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004164 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004165 // Note that there is no "cmp-int" Dex instruction so we shouldn't
4166 // reach this code path when processing a freshly built HIR
4167 // graph. However HCompare integer instructions can be synthesized
4168 // by the instruction simplifier to implement IntegerCompare and
4169 // IntegerSignum intrinsics, so we have to handle this case.
4170 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004171 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004172 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004173 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4174 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004175 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004176 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4177 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004178 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004179 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01004180 }
4181
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004182 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004183 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00004184 }
4185
Vladimir Markoa1de9182016-02-25 11:37:38 +00004186 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04004187
Roland Levillain31dd3d62016-02-16 12:21:02 +00004188 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00004189 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00004190 bool IsGtBias() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004191 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004192 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00004193 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004194
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004195 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type type ATTRIBUTE_UNUSED) {
Roland Levillain1693a1f2016-03-15 14:57:31 +00004196 // Comparisons do not require a runtime call in any back end.
4197 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004198 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004199
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004200 DECLARE_INSTRUCTION(Compare);
4201
Roland Levillain31dd3d62016-02-16 12:21:02 +00004202 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01004203 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004204 static constexpr size_t kFieldComparisonBiasSize =
4205 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
4206 static constexpr size_t kNumberOfComparePackedBits =
4207 kFieldComparisonBias + kFieldComparisonBiasSize;
4208 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4209 using ComparisonBiasField =
4210 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
4211
Roland Levillain31dd3d62016-02-16 12:21:02 +00004212 // Return an integer constant containing the result of a comparison evaluated at compile time.
4213 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
4214 DCHECK(value == -1 || value == 0 || value == 1) << value;
4215 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
4216 }
4217
Artem Serovcced8ba2017-07-19 18:18:09 +01004218 DEFAULT_COPY_CONSTRUCTOR(Compare);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004219};
4220
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004221class HNewInstance final : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00004222 public:
4223 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00004224 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004225 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00004226 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00004227 bool finalizable,
4228 QuickEntrypointEnum entrypoint)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304229 : HExpression(kNewInstance,
4230 DataType::Type::kReference,
4231 SideEffects::CanTriggerGC(),
4232 dex_pc),
David Brazdil6de19382016-01-08 17:37:10 +00004233 type_index_(type_index),
4234 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00004235 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004236 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00004237 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00004238 }
4239
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004240 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004241
Andreas Gampea5b09a62016-11-17 15:21:22 -08004242 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00004243 const DexFile& GetDexFile() const { return dex_file_; }
4244
4245 // Calls runtime so needs an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004246 bool NeedsEnvironment() const override { return true; }
David Brazdil6de19382016-01-08 17:37:10 +00004247
Mingyao Yang062157f2016-03-02 10:15:36 -08004248 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004249 bool CanThrow() const override { return true; }
Mingyao Yang062157f2016-03-02 10:15:36 -08004250
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004251 bool NeedsChecks() const {
4252 return entrypoint_ == kQuickAllocObjectWithChecks;
4253 }
David Brazdil6de19382016-01-08 17:37:10 +00004254
Vladimir Markoa1de9182016-02-25 11:37:38 +00004255 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00004256
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004257 bool CanBeNull() const override { return false; }
David Brazdil6de19382016-01-08 17:37:10 +00004258
4259 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4260
4261 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
4262 entrypoint_ = entrypoint;
4263 }
4264
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004265 HLoadClass* GetLoadClass() const {
4266 HInstruction* input = InputAt(0);
4267 if (input->IsClinitCheck()) {
4268 input = input->InputAt(0);
4269 }
4270 DCHECK(input->IsLoadClass());
4271 return input->AsLoadClass();
4272 }
4273
David Brazdil6de19382016-01-08 17:37:10 +00004274 bool IsStringAlloc() const;
4275
4276 DECLARE_INSTRUCTION(NewInstance);
4277
Artem Serovcced8ba2017-07-19 18:18:09 +01004278 protected:
4279 DEFAULT_COPY_CONSTRUCTOR(NewInstance);
4280
David Brazdil6de19382016-01-08 17:37:10 +00004281 private:
Vladimir Markobd785672018-05-03 17:09:09 +01004282 static constexpr size_t kFlagFinalizable = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004283 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
4284 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
4285 "Too many packed fields.");
4286
Andreas Gampea5b09a62016-11-17 15:21:22 -08004287 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00004288 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00004289 QuickEntrypointEnum entrypoint_;
David Brazdil6de19382016-01-08 17:37:10 +00004290};
4291
Agi Csaki05f20562015-08-19 14:58:14 -07004292enum IntrinsicNeedsEnvironmentOrCache {
4293 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
4294 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07004295};
4296
Aart Bik5d75afe2015-12-14 11:57:01 -08004297enum IntrinsicSideEffects {
4298 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
4299 kReadSideEffects, // Intrinsic may read heap memory.
4300 kWriteSideEffects, // Intrinsic may write heap memory.
4301 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
4302};
4303
4304enum IntrinsicExceptions {
4305 kNoThrow, // Intrinsic does not throw any exceptions.
4306 kCanThrow // Intrinsic may throw exceptions.
4307};
4308
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004309class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004310 public:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004311 bool NeedsEnvironment() const override;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004312
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01004313 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004314 SetRawInputAt(index, argument);
4315 }
4316
Roland Levillain3e3d7332015-04-28 11:00:54 +01004317 // Return the number of arguments. This number can be lower than
4318 // the number of inputs returned by InputCount(), as some invoke
4319 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
4320 // inputs at the end of their list of inputs.
4321 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
4322
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004323 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4324
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004325 InvokeType GetInvokeType() const {
4326 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004327 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004328
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01004329 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004330 return intrinsic_;
4331 }
4332
Aart Bik5d75afe2015-12-14 11:57:01 -08004333 void SetIntrinsic(Intrinsics intrinsic,
4334 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
4335 IntrinsicSideEffects side_effects,
4336 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004337
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004338 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00004339 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004340 }
4341
Aart Bikff7d89c2016-11-07 08:49:28 -08004342 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4343
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004344 bool CanThrow() const override { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004345
Aart Bika8b8e9b2018-01-09 11:01:02 -08004346 void SetAlwaysThrows(bool always_throws) { SetPackedFlag<kFlagAlwaysThrows>(always_throws); }
4347
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004348 bool AlwaysThrows() const override { return GetPackedFlag<kFlagAlwaysThrows>(); }
Aart Bika8b8e9b2018-01-09 11:01:02 -08004349
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004350 bool CanBeMoved() const override { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004351
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004352 bool InstructionDataEquals(const HInstruction* other) const override {
Aart Bik5d75afe2015-12-14 11:57:01 -08004353 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4354 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004355
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004356 uint32_t* GetIntrinsicOptimizations() {
4357 return &intrinsic_optimizations_;
4358 }
4359
4360 const uint32_t* GetIntrinsicOptimizations() const {
4361 return &intrinsic_optimizations_;
4362 }
4363
4364 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4365
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004366 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffray76d4bb0f32018-09-21 12:58:45 +01004367 void SetResolvedMethod(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004368
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004369 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004370
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004371 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004372 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4373 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004374 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
Vladimir Markobd785672018-05-03 17:09:09 +01004375 static constexpr size_t kFlagCanThrow = kFieldInvokeType + kFieldInvokeTypeSize;
Aart Bika8b8e9b2018-01-09 11:01:02 -08004376 static constexpr size_t kFlagAlwaysThrows = kFlagCanThrow + 1;
4377 static constexpr size_t kNumberOfInvokePackedBits = kFlagAlwaysThrows + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004378 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004379 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004380
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304381 HInvoke(InstructionKind kind,
4382 ArenaAllocator* allocator,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004383 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004384 uint32_t number_of_other_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004385 DataType::Type return_type,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004386 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004387 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004388 ArtMethod* resolved_method,
4389 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004390 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304391 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01004392 return_type,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004393 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4394 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004395 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004396 number_of_arguments + number_of_other_inputs,
4397 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004398 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004399 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07004400 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004401 intrinsic_optimizations_(0) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004402 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004403 SetPackedFlag<kFlagCanThrow>(true);
Nicolas Geoffray76d4bb0f32018-09-21 12:58:45 +01004404 // Check mutator lock, constructors lack annotalysis support.
4405 Locks::mutator_lock_->AssertNotExclusiveHeld(Thread::Current());
4406 SetResolvedMethod(resolved_method);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004407 }
4408
Artem Serovcced8ba2017-07-19 18:18:09 +01004409 DEFAULT_COPY_CONSTRUCTOR(Invoke);
4410
Roland Levillain3e3d7332015-04-28 11:00:54 +01004411 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004412 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004413 const uint32_t dex_method_index_;
4414 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004415
4416 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4417 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004418};
4419
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004420class HInvokeUnresolved final : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004421 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004422 HInvokeUnresolved(ArenaAllocator* allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004423 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004424 DataType::Type return_type,
Calin Juravle175dc732015-08-25 15:42:32 +01004425 uint32_t dex_pc,
4426 uint32_t dex_method_index,
4427 InvokeType invoke_type)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304428 : HInvoke(kInvokeUnresolved,
4429 allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004430 number_of_arguments,
Andreas Gampe3db70682018-12-26 15:12:03 -08004431 /* number_of_other_inputs= */ 0u,
Calin Juravle175dc732015-08-25 15:42:32 +01004432 return_type,
4433 dex_pc,
4434 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004435 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01004436 invoke_type) {
4437 }
4438
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004439 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004440
Calin Juravle175dc732015-08-25 15:42:32 +01004441 DECLARE_INSTRUCTION(InvokeUnresolved);
4442
Artem Serovcced8ba2017-07-19 18:18:09 +01004443 protected:
4444 DEFAULT_COPY_CONSTRUCTOR(InvokeUnresolved);
Calin Juravle175dc732015-08-25 15:42:32 +01004445};
4446
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004447class HInvokePolymorphic final : public HInvoke {
Orion Hodsonac141392017-01-13 11:53:47 +00004448 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004449 HInvokePolymorphic(ArenaAllocator* allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004450 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004451 DataType::Type return_type,
Orion Hodsonac141392017-01-13 11:53:47 +00004452 uint32_t dex_pc,
4453 uint32_t dex_method_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304454 : HInvoke(kInvokePolymorphic,
4455 allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004456 number_of_arguments,
Andreas Gampe3db70682018-12-26 15:12:03 -08004457 /* number_of_other_inputs= */ 0u,
Orion Hodsonac141392017-01-13 11:53:47 +00004458 return_type,
4459 dex_pc,
4460 dex_method_index,
4461 nullptr,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304462 kVirtual) {
4463 }
Orion Hodsonac141392017-01-13 11:53:47 +00004464
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004465 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004466
Orion Hodsonac141392017-01-13 11:53:47 +00004467 DECLARE_INSTRUCTION(InvokePolymorphic);
4468
Artem Serovcced8ba2017-07-19 18:18:09 +01004469 protected:
4470 DEFAULT_COPY_CONSTRUCTOR(InvokePolymorphic);
Orion Hodsonac141392017-01-13 11:53:47 +00004471};
4472
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004473class HInvokeCustom final : public HInvoke {
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004474 public:
4475 HInvokeCustom(ArenaAllocator* allocator,
4476 uint32_t number_of_arguments,
4477 uint32_t call_site_index,
4478 DataType::Type return_type,
4479 uint32_t dex_pc)
4480 : HInvoke(kInvokeCustom,
4481 allocator,
4482 number_of_arguments,
Andreas Gampe3db70682018-12-26 15:12:03 -08004483 /* number_of_other_inputs= */ 0u,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004484 return_type,
4485 dex_pc,
Andreas Gampe3db70682018-12-26 15:12:03 -08004486 /* dex_method_index= */ dex::kDexNoIndex,
4487 /* resolved_method= */ nullptr,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004488 kStatic),
4489 call_site_index_(call_site_index) {
4490 }
4491
4492 uint32_t GetCallSiteIndex() const { return call_site_index_; }
4493
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004494 bool IsClonable() const override { return true; }
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004495
4496 DECLARE_INSTRUCTION(InvokeCustom);
4497
4498 protected:
4499 DEFAULT_COPY_CONSTRUCTOR(InvokeCustom);
4500
4501 private:
4502 uint32_t call_site_index_;
4503};
4504
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004505class HInvokeStaticOrDirect final : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004506 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004507 // Requirements of this method call regarding the class
4508 // initialization (clinit) check of its declaring class.
4509 enum class ClinitCheckRequirement {
4510 kNone, // Class already initialized.
4511 kExplicit, // Static call having explicit clinit check as last input.
4512 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004513 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004514 };
4515
Vladimir Marko58155012015-08-19 12:49:41 +00004516 // Determines how to load the target ArtMethod*.
4517 enum class MethodLoadKind {
4518 // Use a String init ArtMethod* loaded from Thread entrypoints.
4519 kStringInit,
4520
4521 // Use the method's own ArtMethod* loaded by the register allocator.
4522 kRecursive,
4523
Vladimir Marko65979462017-05-19 17:25:12 +01004524 // Use PC-relative boot image ArtMethod* address that will be known at link time.
4525 // Used for boot image methods referenced by boot image code.
4526 kBootImageLinkTimePcRelative,
4527
Vladimir Markob066d432018-01-03 13:14:37 +00004528 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
4529 // Used for app->boot calls with relocatable image.
4530 kBootImageRelRo,
4531
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004532 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004533 // Used for methods outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004534 kBssEntry,
Vladimir Marko58155012015-08-19 12:49:41 +00004535
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004536 // Use ArtMethod* at a known address, embed the direct address in the code.
4537 // Used for for JIT-compiled calls.
4538 kJitDirectAddress,
4539
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004540 // Make a runtime call to resolve and call the method. This is the last-resort-kind
4541 // used when other kinds are unimplemented on a particular architecture.
4542 kRuntimeCall,
Vladimir Marko58155012015-08-19 12:49:41 +00004543 };
4544
4545 // Determines the location of the code pointer.
4546 enum class CodePtrLocation {
4547 // Recursive call, use local PC-relative call instruction.
4548 kCallSelf,
4549
Vladimir Marko58155012015-08-19 12:49:41 +00004550 // Use code pointer from the ArtMethod*.
4551 // Used when we don't know the target code. This is also the last-resort-kind used when
4552 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4553 kCallArtMethod,
4554 };
4555
4556 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004557 MethodLoadKind method_load_kind;
4558 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004559 // The method load data holds
4560 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4561 // Note that there are multiple string init methods, each having its own offset.
4562 // - the method address for kDirectAddress
Vladimir Markodc151b22015-10-15 18:02:30 +01004563 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004564 };
4565
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004566 HInvokeStaticOrDirect(ArenaAllocator* allocator,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004567 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004568 DataType::Type return_type,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004569 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004570 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004571 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004572 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004573 InvokeType invoke_type,
4574 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004575 ClinitCheckRequirement clinit_check_requirement)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304576 : HInvoke(kInvokeStaticOrDirect,
4577 allocator,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004578 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004579 // There is potentially one extra argument for the HCurrentMethod node, and
Nicolas Geoffray0846a8f2018-09-12 15:21:07 +01004580 // potentially one other if the clinit check is explicit.
Vladimir Markob554b5a2015-11-06 12:57:55 +00004581 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004582 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004583 return_type,
4584 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004585 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004586 resolved_method,
4587 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004588 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004589 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004590 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4591 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004592
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004593 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004594
Vladimir Markodc151b22015-10-15 18:02:30 +01004595 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004596 bool had_current_method_input = HasCurrentMethodInput();
4597 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4598
4599 // Using the current method is the default and once we find a better
4600 // method load kind, we should not go back to using the current method.
4601 DCHECK(had_current_method_input || !needs_current_method_input);
4602
4603 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004604 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4605 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004606 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004607 dispatch_info_ = dispatch_info;
4608 }
4609
Aart Bik6daebeb2017-04-03 14:35:41 -07004610 DispatchInfo GetDispatchInfo() const {
4611 return dispatch_info_;
4612 }
4613
Vladimir Markoc53c0792015-11-19 15:48:33 +00004614 void AddSpecialInput(HInstruction* input) {
4615 // We allow only one special input.
4616 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4617 DCHECK(InputCount() == GetSpecialInputIndex() ||
4618 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4619 InsertInputAt(GetSpecialInputIndex(), input);
4620 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004621
Vladimir Marko372f10e2016-05-17 16:30:10 +01004622 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004623 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01004624 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4625 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4626 DCHECK(!input_records.empty());
4627 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4628 HInstruction* last_input = input_records.back().GetInstruction();
4629 // Note: `last_input` may be null during arguments setup.
4630 if (last_input != nullptr) {
4631 // `last_input` is the last input of a static invoke marked as having
4632 // an explicit clinit check. It must either be:
4633 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4634 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4635 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4636 }
4637 }
4638 return input_records;
4639 }
4640
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004641 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004642 // We access the method via the dex cache so we can't do an implicit null check.
4643 // TODO: for intrinsics we can generate implicit null checks.
4644 return false;
4645 }
4646
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004647 bool CanBeNull() const override {
Vladimir Markobd785672018-05-03 17:09:09 +01004648 return GetType() == DataType::Type::kReference && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004649 }
4650
Vladimir Markoc53c0792015-11-19 15:48:33 +00004651 // Get the index of the special input, if any.
4652 //
David Brazdil6de19382016-01-08 17:37:10 +00004653 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4654 // method pointer; otherwise there may be one platform-specific special input,
4655 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004656 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004657 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004658
Vladimir Marko58155012015-08-19 12:49:41 +00004659 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4660 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4661 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004662 bool NeedsDexCacheOfDeclaringClass() const override;
Vladimir Marko58155012015-08-19 12:49:41 +00004663 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004664 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kJitDirectAddress; }
Vladimir Marko65979462017-05-19 17:25:12 +01004665 bool HasPcRelativeMethodLoadKind() const {
4666 return GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markob066d432018-01-03 13:14:37 +00004667 GetMethodLoadKind() == MethodLoadKind::kBootImageRelRo ||
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004668 GetMethodLoadKind() == MethodLoadKind::kBssEntry;
Vladimir Marko65979462017-05-19 17:25:12 +01004669 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004670 bool HasCurrentMethodInput() const {
4671 // This function can be called only after the invoke has been fully initialized by the builder.
4672 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004673 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004674 return true;
4675 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004676 DCHECK(InputCount() == GetSpecialInputIndex() ||
4677 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004678 return false;
4679 }
4680 }
Vladimir Marko58155012015-08-19 12:49:41 +00004681
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004682 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004683 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004684 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004685 }
4686
4687 uint64_t GetMethodAddress() const {
4688 DCHECK(HasMethodAddress());
4689 return dispatch_info_.method_load_data;
4690 }
4691
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004692 const DexFile& GetDexFileForPcRelativeDexCache() const;
4693
Vladimir Markoa1de9182016-02-25 11:37:38 +00004694 ClinitCheckRequirement GetClinitCheckRequirement() const {
4695 return GetPackedField<ClinitCheckRequirementField>();
4696 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004697
Roland Levillain4c0eb422015-04-24 16:43:49 +01004698 // Is this instruction a call to a static method?
4699 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004700 return GetInvokeType() == kStatic;
4701 }
4702
4703 MethodReference GetTargetMethod() const {
4704 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004705 }
4706
Vladimir Markofbb184a2015-11-13 14:47:00 +00004707 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4708 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4709 // instruction; only relevant for static calls with explicit clinit check.
4710 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004711 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004712 size_t last_input_index = inputs_.size() - 1u;
4713 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004714 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004715 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004716 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004717 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004718 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004719 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004720 }
4721
4722 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004723 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004724 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004725 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004726 }
4727
4728 // Is this a call to a static method whose declaring class has an
4729 // implicit intialization check requirement?
4730 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004731 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004732 }
4733
Vladimir Markob554b5a2015-11-06 12:57:55 +00004734 // Does this method load kind need the current method as an input?
4735 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004736 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kRuntimeCall;
Vladimir Markob554b5a2015-11-06 12:57:55 +00004737 }
4738
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004739 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004740
Artem Serovcced8ba2017-07-19 18:18:09 +01004741 protected:
4742 DEFAULT_COPY_CONSTRUCTOR(InvokeStaticOrDirect);
4743
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004744 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004745 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004746 static constexpr size_t kFieldClinitCheckRequirementSize =
4747 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4748 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4749 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4750 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4751 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004752 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4753 kFieldClinitCheckRequirement,
4754 kFieldClinitCheckRequirementSize>;
4755
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004756 // Cached values of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffrayea179f42018-02-08 22:30:18 +00004757 const MethodReference target_method_;
Vladimir Marko58155012015-08-19 12:49:41 +00004758 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004759};
Vladimir Markof64242a2015-12-01 14:58:23 +00004760std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004761std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004762
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004763class HInvokeVirtual final : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004764 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004765 HInvokeVirtual(ArenaAllocator* allocator,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004766 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004767 DataType::Type return_type,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004768 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004769 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004770 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004771 uint32_t vtable_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304772 : HInvoke(kInvokeVirtual,
4773 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004774 number_of_arguments,
4775 0u,
4776 return_type,
4777 dex_pc,
4778 dex_method_index,
4779 resolved_method,
4780 kVirtual),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304781 vtable_index_(vtable_index) {
4782 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004783
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004784 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004785
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004786 bool CanBeNull() const override {
Aart Bik71bf7b42016-11-16 10:17:46 -08004787 switch (GetIntrinsic()) {
4788 case Intrinsics::kThreadCurrentThread:
4789 case Intrinsics::kStringBufferAppend:
4790 case Intrinsics::kStringBufferToString:
Vladimir Markod4561172017-10-30 17:48:25 +00004791 case Intrinsics::kStringBuilderAppendObject:
4792 case Intrinsics::kStringBuilderAppendString:
4793 case Intrinsics::kStringBuilderAppendCharSequence:
4794 case Intrinsics::kStringBuilderAppendCharArray:
4795 case Intrinsics::kStringBuilderAppendBoolean:
4796 case Intrinsics::kStringBuilderAppendChar:
4797 case Intrinsics::kStringBuilderAppendInt:
4798 case Intrinsics::kStringBuilderAppendLong:
4799 case Intrinsics::kStringBuilderAppendFloat:
4800 case Intrinsics::kStringBuilderAppendDouble:
Aart Bik71bf7b42016-11-16 10:17:46 -08004801 case Intrinsics::kStringBuilderToString:
4802 return false;
4803 default:
4804 return HInvoke::CanBeNull();
4805 }
4806 }
4807
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004808 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004809 // TODO: Add implicit null checks in intrinsics.
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01004810 return (obj == InputAt(0)) && !IsIntrinsic();
Calin Juravle77520bc2015-01-12 18:45:46 +00004811 }
4812
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004813 uint32_t GetVTableIndex() const { return vtable_index_; }
4814
4815 DECLARE_INSTRUCTION(InvokeVirtual);
4816
Artem Serovcced8ba2017-07-19 18:18:09 +01004817 protected:
4818 DEFAULT_COPY_CONSTRUCTOR(InvokeVirtual);
4819
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004820 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004821 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004822 const uint32_t vtable_index_;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004823};
4824
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004825class HInvokeInterface final : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004826 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004827 HInvokeInterface(ArenaAllocator* allocator,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004828 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004829 DataType::Type return_type,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004830 uint32_t dex_pc,
4831 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004832 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004833 uint32_t imt_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304834 : HInvoke(kInvokeInterface,
4835 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004836 number_of_arguments,
4837 0u,
4838 return_type,
4839 dex_pc,
4840 dex_method_index,
4841 resolved_method,
4842 kInterface),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304843 imt_index_(imt_index) {
4844 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004845
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004846 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004847
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004848 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004849 // TODO: Add implicit null checks in intrinsics.
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01004850 return (obj == InputAt(0)) && !IsIntrinsic();
Calin Juravle77520bc2015-01-12 18:45:46 +00004851 }
4852
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004853 bool NeedsDexCacheOfDeclaringClass() const override {
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004854 // The assembly stub currently needs it.
4855 return true;
4856 }
4857
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004858 uint32_t GetImtIndex() const { return imt_index_; }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004859
4860 DECLARE_INSTRUCTION(InvokeInterface);
4861
Artem Serovcced8ba2017-07-19 18:18:09 +01004862 protected:
4863 DEFAULT_COPY_CONSTRUCTOR(InvokeInterface);
4864
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004865 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004866 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004867 const uint32_t imt_index_;
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004868};
4869
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004870class HNeg final : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004871 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004872 HNeg(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304873 : HUnaryOperation(kNeg, result_type, input, dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004874 DCHECK_EQ(result_type, DataType::Kind(input->GetType()));
Roland Levillain937e6cd2016-03-22 11:54:37 +00004875 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004876
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004877 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004878
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004879 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004880 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004881 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004882 HConstant* Evaluate(HLongConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004883 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004884 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004885 HConstant* Evaluate(HFloatConstant* x) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004886 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4887 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004888 HConstant* Evaluate(HDoubleConstant* x) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004889 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4890 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004891
Roland Levillain88cb1752014-10-20 16:36:47 +01004892 DECLARE_INSTRUCTION(Neg);
4893
Artem Serovcced8ba2017-07-19 18:18:09 +01004894 protected:
4895 DEFAULT_COPY_CONSTRUCTOR(Neg);
Roland Levillain88cb1752014-10-20 16:36:47 +01004896};
4897
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004898class HNewArray final : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004899 public:
Vladimir Markob5461632018-10-15 14:24:21 +01004900 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc, size_t component_size_shift)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304901 : HExpression(kNewArray, DataType::Type::kReference, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004902 SetRawInputAt(0, cls);
4903 SetRawInputAt(1, length);
Vladimir Markob5461632018-10-15 14:24:21 +01004904 SetPackedField<ComponentSizeShiftField>(component_size_shift);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004905 }
4906
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004907 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004908
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004909 // Calls runtime so needs an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004910 bool NeedsEnvironment() const override { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004911
Mingyao Yang0c365e62015-03-31 15:09:29 -07004912 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004913 bool CanThrow() const override { return true; }
Mingyao Yang0c365e62015-03-31 15:09:29 -07004914
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004915 bool CanBeNull() const override { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004916
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004917 HLoadClass* GetLoadClass() const {
4918 DCHECK(InputAt(0)->IsLoadClass());
4919 return InputAt(0)->AsLoadClass();
4920 }
4921
4922 HInstruction* GetLength() const {
4923 return InputAt(1);
4924 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004925
Vladimir Markob5461632018-10-15 14:24:21 +01004926 size_t GetComponentSizeShift() {
4927 return GetPackedField<ComponentSizeShiftField>();
4928 }
4929
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004930 DECLARE_INSTRUCTION(NewArray);
4931
Artem Serovcced8ba2017-07-19 18:18:09 +01004932 protected:
4933 DEFAULT_COPY_CONSTRUCTOR(NewArray);
Vladimir Markob5461632018-10-15 14:24:21 +01004934
4935 private:
4936 static constexpr size_t kFieldComponentSizeShift = kNumberOfGenericPackedBits;
4937 static constexpr size_t kFieldComponentSizeShiftSize = MinimumBitsToStore(3u);
4938 static constexpr size_t kNumberOfNewArrayPackedBits =
4939 kFieldComponentSizeShift + kFieldComponentSizeShiftSize;
4940 static_assert(kNumberOfNewArrayPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4941 using ComponentSizeShiftField =
4942 BitField<size_t, kFieldComponentSizeShift, kFieldComponentSizeShift>;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004943};
4944
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004945class HAdd final : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004946 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004947 HAdd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004948 HInstruction* left,
4949 HInstruction* right,
4950 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304951 : HBinaryOperation(kAdd, result_type, left, right, SideEffects::None(), dex_pc) {
4952 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004953
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004954 bool IsCommutative() const override { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004955
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004956 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004957
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004958 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004959 return GetBlock()->GetGraph()->GetIntConstant(
4960 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004961 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004962 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004963 return GetBlock()->GetGraph()->GetLongConstant(
4964 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004965 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004966 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004967 return GetBlock()->GetGraph()->GetFloatConstant(
4968 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4969 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004970 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004971 return GetBlock()->GetGraph()->GetDoubleConstant(
4972 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4973 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004974
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004975 DECLARE_INSTRUCTION(Add);
4976
Artem Serovcced8ba2017-07-19 18:18:09 +01004977 protected:
4978 DEFAULT_COPY_CONSTRUCTOR(Add);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004979};
4980
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004981class HSub final : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004982 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004983 HSub(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004984 HInstruction* left,
4985 HInstruction* right,
4986 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304987 : HBinaryOperation(kSub, result_type, left, right, SideEffects::None(), dex_pc) {
4988 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004989
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004990 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004991
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004992 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004993 return GetBlock()->GetGraph()->GetIntConstant(
4994 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004995 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004996 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004997 return GetBlock()->GetGraph()->GetLongConstant(
4998 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004999 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005000 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005001 return GetBlock()->GetGraph()->GetFloatConstant(
5002 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5003 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005004 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005005 return GetBlock()->GetGraph()->GetDoubleConstant(
5006 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5007 }
Roland Levillain556c3d12014-09-18 15:25:07 +01005008
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005009 DECLARE_INSTRUCTION(Sub);
5010
Artem Serovcced8ba2017-07-19 18:18:09 +01005011 protected:
5012 DEFAULT_COPY_CONSTRUCTOR(Sub);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005013};
5014
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005015class HMul final : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01005016 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005017 HMul(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005018 HInstruction* left,
5019 HInstruction* right,
5020 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305021 : HBinaryOperation(kMul, result_type, left, right, SideEffects::None(), dex_pc) {
5022 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005023
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005024 bool IsCommutative() const override { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005025
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005026 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005027
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005028 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005029 return GetBlock()->GetGraph()->GetIntConstant(
5030 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005031 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005032 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005033 return GetBlock()->GetGraph()->GetLongConstant(
5034 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005035 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005036 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005037 return GetBlock()->GetGraph()->GetFloatConstant(
5038 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5039 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005040 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005041 return GetBlock()->GetGraph()->GetDoubleConstant(
5042 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5043 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005044
5045 DECLARE_INSTRUCTION(Mul);
5046
Artem Serovcced8ba2017-07-19 18:18:09 +01005047 protected:
5048 DEFAULT_COPY_CONSTRUCTOR(Mul);
Calin Juravle34bacdf2014-10-07 20:23:36 +01005049};
5050
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005051class HDiv final : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00005052 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005053 HDiv(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005054 HInstruction* left,
5055 HInstruction* right,
5056 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305057 : HBinaryOperation(kDiv, result_type, left, right, SideEffects::None(), dex_pc) {
5058 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00005059
Roland Levillain9867bc72015-08-05 10:21:34 +01005060 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00005061 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005062 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01005063 // Our graph structure ensures we never have 0 for `y` during
5064 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00005065 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00005066 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00005067 return (y == -1) ? -x : x / y;
5068 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005069
Roland Levillain31dd3d62016-02-16 12:21:02 +00005070 template <typename T>
5071 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005072 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00005073 return x / y;
5074 }
5075
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005076 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005077 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005078 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005079 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005080 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005081 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005082 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5083 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005084 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005085 return GetBlock()->GetGraph()->GetFloatConstant(
5086 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5087 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005088 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005089 return GetBlock()->GetGraph()->GetDoubleConstant(
5090 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005091 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00005092
5093 DECLARE_INSTRUCTION(Div);
5094
Artem Serovcced8ba2017-07-19 18:18:09 +01005095 protected:
5096 DEFAULT_COPY_CONSTRUCTOR(Div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00005097};
5098
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005099class HRem final : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00005100 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005101 HRem(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005102 HInstruction* left,
5103 HInstruction* right,
5104 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305105 : HBinaryOperation(kRem, result_type, left, right, SideEffects::None(), dex_pc) {
5106 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005107
Roland Levillain9867bc72015-08-05 10:21:34 +01005108 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00005109 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005110 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01005111 // Our graph structure ensures we never have 0 for `y` during
5112 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00005113 DCHECK_NE(y, 0);
5114 // Special case -1 to avoid getting a SIGFPE on x86(_64).
5115 return (y == -1) ? 0 : x % y;
5116 }
5117
Roland Levillain31dd3d62016-02-16 12:21:02 +00005118 template <typename T>
5119 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005120 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00005121 return std::fmod(x, y);
5122 }
5123
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005124 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005125 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005126 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005127 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005128 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005129 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005130 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005131 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005132 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005133 return GetBlock()->GetGraph()->GetFloatConstant(
5134 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5135 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005136 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005137 return GetBlock()->GetGraph()->GetDoubleConstant(
5138 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5139 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005140
5141 DECLARE_INSTRUCTION(Rem);
5142
Artem Serovcced8ba2017-07-19 18:18:09 +01005143 protected:
5144 DEFAULT_COPY_CONSTRUCTOR(Rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00005145};
5146
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005147class HMin final : public HBinaryOperation {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005148 public:
5149 HMin(DataType::Type result_type,
5150 HInstruction* left,
5151 HInstruction* right,
5152 uint32_t dex_pc)
5153 : HBinaryOperation(kMin, result_type, left, right, SideEffects::None(), dex_pc) {}
5154
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005155 bool IsCommutative() const override { return true; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005156
5157 // Evaluation for integral values.
5158 template <typename T> static T ComputeIntegral(T x, T y) {
5159 return (x <= y) ? x : y;
5160 }
5161
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005162 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005163 return GetBlock()->GetGraph()->GetIntConstant(
5164 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5165 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005166 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005167 return GetBlock()->GetGraph()->GetLongConstant(
5168 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5169 }
5170 // TODO: Evaluation for floating-point values.
5171 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005172 HFloatConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005173 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005174 HDoubleConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005175
5176 DECLARE_INSTRUCTION(Min);
5177
5178 protected:
5179 DEFAULT_COPY_CONSTRUCTOR(Min);
5180};
5181
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005182class HMax final : public HBinaryOperation {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005183 public:
5184 HMax(DataType::Type result_type,
5185 HInstruction* left,
5186 HInstruction* right,
5187 uint32_t dex_pc)
5188 : HBinaryOperation(kMax, result_type, left, right, SideEffects::None(), dex_pc) {}
5189
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005190 bool IsCommutative() const override { return true; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005191
5192 // Evaluation for integral values.
5193 template <typename T> static T ComputeIntegral(T x, T y) {
5194 return (x >= y) ? x : y;
5195 }
5196
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005197 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005198 return GetBlock()->GetGraph()->GetIntConstant(
5199 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5200 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005201 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005202 return GetBlock()->GetGraph()->GetLongConstant(
5203 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5204 }
5205 // TODO: Evaluation for floating-point values.
5206 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005207 HFloatConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005208 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005209 HDoubleConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005210
5211 DECLARE_INSTRUCTION(Max);
5212
5213 protected:
5214 DEFAULT_COPY_CONSTRUCTOR(Max);
5215};
5216
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005217class HAbs final : public HUnaryOperation {
Aart Bik3dad3412018-02-28 12:01:46 -08005218 public:
5219 HAbs(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
5220 : HUnaryOperation(kAbs, result_type, input, dex_pc) {}
5221
5222 // Evaluation for integral values.
5223 template <typename T> static T ComputeIntegral(T x) {
5224 return x < 0 ? -x : x;
5225 }
5226
5227 // Evaluation for floating-point values.
5228 // Note, as a "quality of implementation", rather than pure "spec compliance",
5229 // we require that Math.abs() clears the sign bit (but changes nothing else)
5230 // for all floating-point numbers, including NaN (signaling NaN may become quiet though).
5231 // http://b/30758343
5232 template <typename T, typename S> static T ComputeFP(T x) {
5233 S bits = bit_cast<S, T>(x);
5234 return bit_cast<T, S>(bits & std::numeric_limits<S>::max());
5235 }
5236
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005237 HConstant* Evaluate(HIntConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005238 return GetBlock()->GetGraph()->GetIntConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5239 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005240 HConstant* Evaluate(HLongConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005241 return GetBlock()->GetGraph()->GetLongConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5242 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005243 HConstant* Evaluate(HFloatConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005244 return GetBlock()->GetGraph()->GetFloatConstant(
5245 ComputeFP<float, int32_t>(x->GetValue()), GetDexPc());
5246 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005247 HConstant* Evaluate(HDoubleConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005248 return GetBlock()->GetGraph()->GetDoubleConstant(
5249 ComputeFP<double, int64_t>(x->GetValue()), GetDexPc());
5250 }
5251
5252 DECLARE_INSTRUCTION(Abs);
5253
5254 protected:
5255 DEFAULT_COPY_CONSTRUCTOR(Abs);
5256};
5257
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005258class HDivZeroCheck final : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00005259 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00005260 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01005261 // constructor. However it can only do it on a fatal slow path so execution never returns to the
5262 // instruction following the current one; thus 'SideEffects::None()' is used.
Calin Juravled0d48522014-11-04 16:40:20 +00005263 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01005264 : HExpression(kDivZeroCheck, value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00005265 SetRawInputAt(0, value);
5266 }
5267
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005268 bool IsClonable() const override { return true; }
5269 bool CanBeMoved() const override { return true; }
Calin Juravled0d48522014-11-04 16:40:20 +00005270
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005271 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Calin Juravled0d48522014-11-04 16:40:20 +00005272 return true;
5273 }
5274
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005275 bool NeedsEnvironment() const override { return true; }
5276 bool CanThrow() const override { return true; }
Calin Juravled0d48522014-11-04 16:40:20 +00005277
Calin Juravled0d48522014-11-04 16:40:20 +00005278 DECLARE_INSTRUCTION(DivZeroCheck);
5279
Artem Serovcced8ba2017-07-19 18:18:09 +01005280 protected:
5281 DEFAULT_COPY_CONSTRUCTOR(DivZeroCheck);
Calin Juravled0d48522014-11-04 16:40:20 +00005282};
5283
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005284class HShl final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005285 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005286 HShl(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005287 HInstruction* value,
5288 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005289 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305290 : HBinaryOperation(kShl, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005291 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5292 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005293 }
5294
Roland Levillain5b5b9312016-03-22 14:57:31 +00005295 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005296 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005297 return value << (distance & max_shift_distance);
5298 }
5299
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005300 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005301 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005302 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005303 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005304 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005305 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005306 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005307 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005308 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005309 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005310 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5311 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005312 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005313 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005314 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005315 LOG(FATAL) << DebugName() << " is not defined for float values";
5316 UNREACHABLE();
5317 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005318 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005319 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005320 LOG(FATAL) << DebugName() << " is not defined for double values";
5321 UNREACHABLE();
5322 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005323
5324 DECLARE_INSTRUCTION(Shl);
5325
Artem Serovcced8ba2017-07-19 18:18:09 +01005326 protected:
5327 DEFAULT_COPY_CONSTRUCTOR(Shl);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005328};
5329
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005330class HShr final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005331 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005332 HShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005333 HInstruction* value,
5334 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005335 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305336 : HBinaryOperation(kShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005337 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5338 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005339 }
5340
Roland Levillain5b5b9312016-03-22 14:57:31 +00005341 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005342 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005343 return value >> (distance & max_shift_distance);
5344 }
5345
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005346 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005347 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005348 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005349 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005350 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005351 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005352 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005353 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005354 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005355 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005356 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5357 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005358 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005359 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005360 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005361 LOG(FATAL) << DebugName() << " is not defined for float values";
5362 UNREACHABLE();
5363 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005364 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005365 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005366 LOG(FATAL) << DebugName() << " is not defined for double values";
5367 UNREACHABLE();
5368 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005369
5370 DECLARE_INSTRUCTION(Shr);
5371
Artem Serovcced8ba2017-07-19 18:18:09 +01005372 protected:
5373 DEFAULT_COPY_CONSTRUCTOR(Shr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005374};
5375
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005376class HUShr final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005377 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005378 HUShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005379 HInstruction* value,
5380 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005381 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305382 : HBinaryOperation(kUShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005383 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5384 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00005385 }
5386
Roland Levillain5b5b9312016-03-22 14:57:31 +00005387 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005388 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005389 typedef typename std::make_unsigned<T>::type V;
5390 V ux = static_cast<V>(value);
5391 return static_cast<T>(ux >> (distance & max_shift_distance));
5392 }
5393
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005394 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005395 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005396 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005397 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005398 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005399 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005400 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005401 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005402 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005403 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005404 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5405 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00005406 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005407 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005408 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005409 LOG(FATAL) << DebugName() << " is not defined for float values";
5410 UNREACHABLE();
5411 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005412 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005413 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005414 LOG(FATAL) << DebugName() << " is not defined for double values";
5415 UNREACHABLE();
5416 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005417
5418 DECLARE_INSTRUCTION(UShr);
5419
Artem Serovcced8ba2017-07-19 18:18:09 +01005420 protected:
5421 DEFAULT_COPY_CONSTRUCTOR(UShr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005422};
5423
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005424class HAnd final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005425 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005426 HAnd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005427 HInstruction* left,
5428 HInstruction* right,
5429 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305430 : HBinaryOperation(kAnd, result_type, left, right, SideEffects::None(), dex_pc) {
5431 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005432
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005433 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005434
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005435 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005436
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005437 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005438 return GetBlock()->GetGraph()->GetIntConstant(
5439 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005440 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005441 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005442 return GetBlock()->GetGraph()->GetLongConstant(
5443 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005444 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005445 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005446 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005447 LOG(FATAL) << DebugName() << " is not defined for float values";
5448 UNREACHABLE();
5449 }
5450 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005451 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005452 LOG(FATAL) << DebugName() << " is not defined for double values";
5453 UNREACHABLE();
5454 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005455
5456 DECLARE_INSTRUCTION(And);
5457
Artem Serovcced8ba2017-07-19 18:18:09 +01005458 protected:
5459 DEFAULT_COPY_CONSTRUCTOR(And);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005460};
5461
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005462class HOr final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005463 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005464 HOr(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005465 HInstruction* left,
5466 HInstruction* right,
5467 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305468 : HBinaryOperation(kOr, result_type, left, right, SideEffects::None(), dex_pc) {
5469 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005470
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005471 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005472
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005473 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005474
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005475 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005476 return GetBlock()->GetGraph()->GetIntConstant(
5477 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005478 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005479 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005480 return GetBlock()->GetGraph()->GetLongConstant(
5481 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005482 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005483 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005484 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005485 LOG(FATAL) << DebugName() << " is not defined for float values";
5486 UNREACHABLE();
5487 }
5488 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005489 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005490 LOG(FATAL) << DebugName() << " is not defined for double values";
5491 UNREACHABLE();
5492 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005493
5494 DECLARE_INSTRUCTION(Or);
5495
Artem Serovcced8ba2017-07-19 18:18:09 +01005496 protected:
5497 DEFAULT_COPY_CONSTRUCTOR(Or);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005498};
5499
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005500class HXor final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005501 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005502 HXor(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005503 HInstruction* left,
5504 HInstruction* right,
5505 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305506 : HBinaryOperation(kXor, result_type, left, right, SideEffects::None(), dex_pc) {
5507 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005508
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005509 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005510
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005511 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005512
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005513 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005514 return GetBlock()->GetGraph()->GetIntConstant(
5515 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005516 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005517 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005518 return GetBlock()->GetGraph()->GetLongConstant(
5519 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005520 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005521 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005522 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005523 LOG(FATAL) << DebugName() << " is not defined for float values";
5524 UNREACHABLE();
5525 }
5526 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005527 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005528 LOG(FATAL) << DebugName() << " is not defined for double values";
5529 UNREACHABLE();
5530 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005531
5532 DECLARE_INSTRUCTION(Xor);
5533
Artem Serovcced8ba2017-07-19 18:18:09 +01005534 protected:
5535 DEFAULT_COPY_CONSTRUCTOR(Xor);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005536};
5537
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005538class HRor final : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005539 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005540 HRor(DataType::Type result_type, HInstruction* value, HInstruction* distance)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305541 : HBinaryOperation(kRor, result_type, value, distance) {
Roland Levillain22c49222016-03-18 14:04:28 +00005542 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005543
Roland Levillain5b5b9312016-03-22 14:57:31 +00005544 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005545 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005546 typedef typename std::make_unsigned<T>::type V;
5547 V ux = static_cast<V>(value);
5548 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005549 return static_cast<T>(ux);
5550 } else {
5551 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005552 return static_cast<T>(ux >> (distance & max_shift_value)) |
5553 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005554 }
5555 }
5556
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005557 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005558 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005559 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005560 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005561 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005562 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005563 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005564 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005565 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005566 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005567 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5568 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005569 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005570 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005571 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005572 LOG(FATAL) << DebugName() << " is not defined for float values";
5573 UNREACHABLE();
5574 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005575 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005576 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005577 LOG(FATAL) << DebugName() << " is not defined for double values";
5578 UNREACHABLE();
5579 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005580
5581 DECLARE_INSTRUCTION(Ror);
5582
Artem Serovcced8ba2017-07-19 18:18:09 +01005583 protected:
5584 DEFAULT_COPY_CONSTRUCTOR(Ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005585};
5586
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005587// The value of a parameter in this method. Its location depends on
5588// the calling convention.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005589class HParameterValue final : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005590 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005591 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005592 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005593 uint8_t index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005594 DataType::Type parameter_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005595 bool is_this = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305596 : HExpression(kParameterValue, parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005597 dex_file_(dex_file),
5598 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005599 index_(index) {
5600 SetPackedFlag<kFlagIsThis>(is_this);
5601 SetPackedFlag<kFlagCanBeNull>(!is_this);
5602 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005603
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005604 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005605 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005606 uint8_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07005607 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005608
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005609 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005610 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005611
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005612 DECLARE_INSTRUCTION(ParameterValue);
5613
Artem Serovcced8ba2017-07-19 18:18:09 +01005614 protected:
5615 DEFAULT_COPY_CONSTRUCTOR(ParameterValue);
5616
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005617 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005618 // Whether or not the parameter value corresponds to 'this' argument.
Vladimir Markobd785672018-05-03 17:09:09 +01005619 static constexpr size_t kFlagIsThis = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005620 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5621 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5622 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5623 "Too many packed fields.");
5624
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005625 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005626 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005627 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005628 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005629 const uint8_t index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005630};
5631
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005632class HNot final : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005633 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005634 HNot(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305635 : HUnaryOperation(kNot, result_type, input, dex_pc) {
5636 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005637
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005638 bool CanBeMoved() const override { return true; }
5639 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005640 return true;
5641 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005642
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005643 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005644
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005645 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005646 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005647 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005648 HConstant* Evaluate(HLongConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005649 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005650 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005651 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005652 LOG(FATAL) << DebugName() << " is not defined for float values";
5653 UNREACHABLE();
5654 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005655 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005656 LOG(FATAL) << DebugName() << " is not defined for double values";
5657 UNREACHABLE();
5658 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005659
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005660 DECLARE_INSTRUCTION(Not);
5661
Artem Serovcced8ba2017-07-19 18:18:09 +01005662 protected:
5663 DEFAULT_COPY_CONSTRUCTOR(Not);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005664};
5665
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005666class HBooleanNot final : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005667 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005668 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305669 : HUnaryOperation(kBooleanNot, DataType::Type::kBool, input, dex_pc) {
5670 }
David Brazdil66d126e2015-04-03 16:02:44 +01005671
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005672 bool CanBeMoved() const override { return true; }
5673 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
David Brazdil66d126e2015-04-03 16:02:44 +01005674 return true;
5675 }
5676
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005677 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005678 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005679 return !x;
5680 }
5681
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005682 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005683 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005684 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005685 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005686 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005687 UNREACHABLE();
5688 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005689 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005690 LOG(FATAL) << DebugName() << " is not defined for float values";
5691 UNREACHABLE();
5692 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005693 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005694 LOG(FATAL) << DebugName() << " is not defined for double values";
5695 UNREACHABLE();
5696 }
David Brazdil66d126e2015-04-03 16:02:44 +01005697
5698 DECLARE_INSTRUCTION(BooleanNot);
5699
Artem Serovcced8ba2017-07-19 18:18:09 +01005700 protected:
5701 DEFAULT_COPY_CONSTRUCTOR(BooleanNot);
David Brazdil66d126e2015-04-03 16:02:44 +01005702};
5703
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005704class HTypeConversion final : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005705 public:
5706 // Instantiate a type conversion of `input` to `result_type`.
Vladimir Markoc8fb2112017-10-03 11:37:52 +01005707 HTypeConversion(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305708 : HExpression(kTypeConversion, result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005709 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005710 // Invariant: We should never generate a conversion to a Boolean value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005711 DCHECK_NE(DataType::Type::kBool, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005712 }
5713
5714 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005715 DataType::Type GetInputType() const { return GetInput()->GetType(); }
5716 DataType::Type GetResultType() const { return GetType(); }
Roland Levillaindff1f282014-11-05 14:15:05 +00005717
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005718 bool IsClonable() const override { return true; }
5719 bool CanBeMoved() const override { return true; }
5720 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01005721 return true;
5722 }
Nicolas Geoffrayacc56ac2018-10-09 08:45:24 +01005723 // Return whether the conversion is implicit. This includes conversion to the same type.
5724 bool IsImplicitConversion() const {
5725 return DataType::IsTypeConversionImplicit(GetInputType(), GetResultType());
5726 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005727
Mark Mendelle82549b2015-05-06 10:55:34 -04005728 // Try to statically evaluate the conversion and return a HConstant
5729 // containing the result. If the input cannot be converted, return nullptr.
5730 HConstant* TryStaticEvaluation() const;
5731
Roland Levillaindff1f282014-11-05 14:15:05 +00005732 DECLARE_INSTRUCTION(TypeConversion);
5733
Artem Serovcced8ba2017-07-19 18:18:09 +01005734 protected:
5735 DEFAULT_COPY_CONSTRUCTOR(TypeConversion);
Roland Levillaindff1f282014-11-05 14:15:05 +00005736};
5737
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005738static constexpr uint32_t kNoRegNumber = -1;
5739
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005740class HNullCheck final : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005741 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005742 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01005743 // constructor. However it can only do it on a fatal slow path so execution never returns to the
5744 // instruction following the current one; thus 'SideEffects::None()' is used.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005745 HNullCheck(HInstruction* value, uint32_t dex_pc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01005746 : HExpression(kNullCheck, value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005747 SetRawInputAt(0, value);
5748 }
5749
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005750 bool IsClonable() const override { return true; }
5751 bool CanBeMoved() const override { return true; }
5752 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005753 return true;
5754 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005755
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005756 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005757
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005758 bool CanThrow() const override { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00005759
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005760 bool CanBeNull() const override { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005761
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005762 DECLARE_INSTRUCTION(NullCheck);
5763
Artem Serovcced8ba2017-07-19 18:18:09 +01005764 protected:
5765 DEFAULT_COPY_CONSTRUCTOR(NullCheck);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005766};
5767
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005768// Embeds an ArtField and all the information required by the compiler. We cache
5769// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005770class FieldInfo : public ValueObject {
5771 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005772 FieldInfo(ArtField* field,
5773 MemberOffset field_offset,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005774 DataType::Type field_type,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005775 bool is_volatile,
5776 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005777 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005778 const DexFile& dex_file)
5779 : field_(field),
5780 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005781 field_type_(field_type),
5782 is_volatile_(is_volatile),
5783 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005784 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005785 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005786
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005787 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005788 MemberOffset GetFieldOffset() const { return field_offset_; }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005789 DataType::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005790 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005791 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005792 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005793 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005794
5795 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005796 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005797 const MemberOffset field_offset_;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005798 const DataType::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005799 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005800 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005801 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005802 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005803};
5804
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005805class HInstanceFieldGet final : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005806 public:
5807 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005808 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005809 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005810 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005811 bool is_volatile,
5812 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005813 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005814 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005815 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305816 : HExpression(kInstanceFieldGet,
5817 field_type,
5818 SideEffects::FieldReadOfType(field_type, is_volatile),
5819 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005820 field_info_(field,
5821 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005822 field_type,
5823 is_volatile,
5824 field_idx,
5825 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005826 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005827 SetRawInputAt(0, value);
5828 }
5829
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005830 bool IsClonable() const override { return true; }
5831 bool CanBeMoved() const override { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005832
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005833 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01005834 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005835 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005836 }
5837
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005838 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005839 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005840 }
5841
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005842 size_t ComputeHashCode() const override {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005843 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5844 }
5845
Calin Juravle52c48962014-12-16 17:02:57 +00005846 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005847 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005848 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005849 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005850
Vladimir Marko61b92282017-10-11 13:23:17 +01005851 void SetType(DataType::Type new_type) {
5852 DCHECK(DataType::IsIntegralType(GetType()));
5853 DCHECK(DataType::IsIntegralType(new_type));
5854 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5855 SetPackedField<TypeField>(new_type);
5856 }
5857
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005858 DECLARE_INSTRUCTION(InstanceFieldGet);
5859
Artem Serovcced8ba2017-07-19 18:18:09 +01005860 protected:
5861 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldGet);
5862
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005863 private:
5864 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005865};
5866
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005867class HInstanceFieldSet final : public HExpression<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005868 public:
5869 HInstanceFieldSet(HInstruction* object,
5870 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005871 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005872 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005873 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005874 bool is_volatile,
5875 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005876 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005877 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005878 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01005879 : HExpression(kInstanceFieldSet,
5880 SideEffects::FieldWriteOfType(field_type, is_volatile),
5881 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005882 field_info_(field,
5883 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005884 field_type,
5885 is_volatile,
5886 field_idx,
5887 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005888 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005889 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005890 SetRawInputAt(0, object);
5891 SetRawInputAt(1, value);
5892 }
5893
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005894 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01005895
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005896 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005897 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005898 }
5899
Calin Juravle52c48962014-12-16 17:02:57 +00005900 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005901 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005902 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005903 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005904 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005905 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5906 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005907
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005908 DECLARE_INSTRUCTION(InstanceFieldSet);
5909
Artem Serovcced8ba2017-07-19 18:18:09 +01005910 protected:
5911 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldSet);
5912
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005913 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005914 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5915 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5916 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5917 "Too many packed fields.");
5918
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005919 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005920};
5921
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005922class HArrayGet final : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005923 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005924 HArrayGet(HInstruction* array,
5925 HInstruction* index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005926 DataType::Type type,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005927 uint32_t dex_pc)
5928 : HArrayGet(array,
5929 index,
5930 type,
5931 SideEffects::ArrayReadOfType(type),
5932 dex_pc,
Andreas Gampe3db70682018-12-26 15:12:03 -08005933 /* is_string_char_at= */ false) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305934 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005935
5936 HArrayGet(HInstruction* array,
5937 HInstruction* index,
5938 DataType::Type type,
5939 SideEffects side_effects,
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005940 uint32_t dex_pc,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005941 bool is_string_char_at)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305942 : HExpression(kArrayGet, type, side_effects, dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005943 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005944 SetRawInputAt(0, array);
5945 SetRawInputAt(1, index);
5946 }
5947
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005948 bool IsClonable() const override { return true; }
5949 bool CanBeMoved() const override { return true; }
5950 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005951 return true;
5952 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005953 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00005954 // TODO: We can be smarter here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01005955 // Currently, unless the array is the result of NewArray, the array access is always
5956 // preceded by some form of null NullCheck necessary for the bounds check, usually
5957 // implicit null check on the ArrayLength input to BoundsCheck or Deoptimize for
5958 // dynamic BCE. There are cases when these could be removed to produce better code.
5959 // If we ever add optimizations to do so we should allow an implicit check here
5960 // (as long as the address falls in the first page).
5961 //
5962 // As an example of such fancy optimization, we could eliminate BoundsCheck for
5963 // a = cond ? new int[1] : null;
5964 // a[0]; // The Phi does not need bounds check for either input.
Calin Juravle77520bc2015-01-12 18:45:46 +00005965 return false;
5966 }
5967
David Brazdil4833f5a2015-12-16 10:37:39 +00005968 bool IsEquivalentOf(HArrayGet* other) const {
5969 bool result = (GetDexPc() == other->GetDexPc());
5970 if (kIsDebugBuild && result) {
5971 DCHECK_EQ(GetBlock(), other->GetBlock());
5972 DCHECK_EQ(GetArray(), other->GetArray());
5973 DCHECK_EQ(GetIndex(), other->GetIndex());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005974 if (DataType::IsIntOrLongType(GetType())) {
5975 DCHECK(DataType::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005976 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005977 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
5978 DCHECK(DataType::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005979 }
5980 }
5981 return result;
5982 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005983
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005984 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5985
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005986 HInstruction* GetArray() const { return InputAt(0); }
5987 HInstruction* GetIndex() const { return InputAt(1); }
5988
Vladimir Marko61b92282017-10-11 13:23:17 +01005989 void SetType(DataType::Type new_type) {
5990 DCHECK(DataType::IsIntegralType(GetType()));
5991 DCHECK(DataType::IsIntegralType(new_type));
5992 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5993 SetPackedField<TypeField>(new_type);
5994 }
5995
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005996 DECLARE_INSTRUCTION(ArrayGet);
5997
Artem Serovcced8ba2017-07-19 18:18:09 +01005998 protected:
5999 DEFAULT_COPY_CONSTRUCTOR(ArrayGet);
6000
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006001 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006002 // We treat a String as an array, creating the HArrayGet from String.charAt()
6003 // intrinsic in the instruction simplifier. We can always determine whether
6004 // a particular HArrayGet is actually a String.charAt() by looking at the type
6005 // of the input but that requires holding the mutator lock, so we prefer to use
6006 // a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01006007 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006008 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
6009 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6010 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006011};
6012
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006013class HArraySet final : public HExpression<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006014 public:
6015 HArraySet(HInstruction* array,
6016 HInstruction* index,
6017 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006018 DataType::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07006019 uint32_t dex_pc)
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006020 : HArraySet(array,
6021 index,
6022 value,
6023 expected_component_type,
6024 // Make a best guess for side effects now, may be refined during SSA building.
6025 ComputeSideEffects(GetComponentType(value->GetType(), expected_component_type)),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306026 dex_pc) {
6027 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006028
6029 HArraySet(HInstruction* array,
6030 HInstruction* index,
6031 HInstruction* value,
6032 DataType::Type expected_component_type,
6033 SideEffects side_effects,
6034 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006035 : HExpression(kArraySet, side_effects, dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006036 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006037 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006038 SetPackedFlag<kFlagValueCanBeNull>(true);
6039 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006040 SetRawInputAt(0, array);
6041 SetRawInputAt(1, index);
6042 SetRawInputAt(2, value);
6043 }
6044
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006045 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006046
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006047 bool NeedsEnvironment() const override {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006048 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006049 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006050 }
6051
Mingyao Yang81014cb2015-06-02 03:16:27 -07006052 // Can throw ArrayStoreException.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006053 bool CanThrow() const override { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07006054
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006055 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00006056 // TODO: Same as for ArrayGet.
6057 return false;
6058 }
6059
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006060 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006061 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006062 }
6063
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006064 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006065 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006066 }
6067
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006068 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006069 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006070 }
6071
Vladimir Markoa1de9182016-02-25 11:37:38 +00006072 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6073 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
6074 bool StaticTypeOfArrayIsObjectArray() const {
6075 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
6076 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006077
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006078 HInstruction* GetArray() const { return InputAt(0); }
6079 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006080 HInstruction* GetValue() const { return InputAt(2); }
6081
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006082 DataType::Type GetComponentType() const {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006083 return GetComponentType(GetValue()->GetType(), GetRawExpectedComponentType());
6084 }
6085
6086 static DataType::Type GetComponentType(DataType::Type value_type,
6087 DataType::Type expected_component_type) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006088 // The Dex format does not type floating point index operations. Since the
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006089 // `expected_component_type` comes from SSA building and can therefore not
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006090 // be correct, we also check what is the value type. If it is a floating
6091 // point type, we must use that type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006092 return ((value_type == DataType::Type::kFloat32) || (value_type == DataType::Type::kFloat64))
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006093 ? value_type
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006094 : expected_component_type;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006095 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01006096
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006097 DataType::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006098 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006099 }
6100
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006101 static SideEffects ComputeSideEffects(DataType::Type type) {
6102 return SideEffects::ArrayWriteOfType(type).Union(SideEffectsForArchRuntimeCalls(type));
Aart Bik18b36ab2016-04-13 16:41:35 -07006103 }
6104
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006105 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type value_type) {
6106 return (value_type == DataType::Type::kReference) ? SideEffects::CanTriggerGC()
6107 : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006108 }
6109
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006110 DECLARE_INSTRUCTION(ArraySet);
6111
Artem Serovcced8ba2017-07-19 18:18:09 +01006112 protected:
6113 DEFAULT_COPY_CONSTRUCTOR(ArraySet);
6114
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006115 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006116 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
6117 static constexpr size_t kFieldExpectedComponentTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006118 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006119 static constexpr size_t kFlagNeedsTypeCheck =
6120 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
6121 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006122 // Cached information for the reference_type_info_ so that codegen
6123 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006124 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
6125 static constexpr size_t kNumberOfArraySetPackedBits =
6126 kFlagStaticTypeOfArrayIsObjectArray + 1;
6127 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6128 using ExpectedComponentTypeField =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006129 BitField<DataType::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006130};
6131
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006132class HArrayLength final : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006133 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006134 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306135 : HExpression(kArrayLength, DataType::Type::kInt32, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006136 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006137 // Note that arrays do not change length, so the instruction does not
6138 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006139 SetRawInputAt(0, array);
6140 }
6141
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006142 bool IsClonable() const override { return true; }
6143 bool CanBeMoved() const override { return true; }
6144 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006145 return true;
6146 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006147 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle641547a2015-04-21 22:08:51 +01006148 return obj == InputAt(0);
6149 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006150
Vladimir Markodce016e2016-04-28 13:10:02 +01006151 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
6152
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006153 DECLARE_INSTRUCTION(ArrayLength);
6154
Artem Serovcced8ba2017-07-19 18:18:09 +01006155 protected:
6156 DEFAULT_COPY_CONSTRUCTOR(ArrayLength);
6157
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006158 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01006159 // We treat a String as an array, creating the HArrayLength from String.length()
6160 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
6161 // determine whether a particular HArrayLength is actually a String.length() by
6162 // looking at the type of the input but that requires holding the mutator lock, so
6163 // we prefer to use a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01006164 static constexpr size_t kFlagIsStringLength = kNumberOfGenericPackedBits;
Vladimir Markodce016e2016-04-28 13:10:02 +01006165 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
6166 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6167 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006168};
6169
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006170class HBoundsCheck final : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006171 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00006172 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01006173 // constructor. However it can only do it on a fatal slow path so execution never returns to the
6174 // instruction following the current one; thus 'SideEffects::None()' is used.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006175 HBoundsCheck(HInstruction* index,
6176 HInstruction* length,
6177 uint32_t dex_pc,
Vladimir Marko0259c242017-12-04 11:27:47 +00006178 bool is_string_char_at = false)
Artem Serovd1aa7d02018-06-22 11:35:46 +01006179 : HExpression(kBoundsCheck, index->GetType(), SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006180 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(index->GetType()));
Vladimir Marko0259c242017-12-04 11:27:47 +00006181 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006182 SetRawInputAt(0, index);
6183 SetRawInputAt(1, length);
6184 }
6185
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006186 bool IsClonable() const override { return true; }
6187 bool CanBeMoved() const override { return true; }
6188 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006189 return true;
6190 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006191
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006192 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006193
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006194 bool CanThrow() const override { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01006195
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00006196 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006197
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006198 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006199
6200 DECLARE_INSTRUCTION(BoundsCheck);
6201
Artem Serovcced8ba2017-07-19 18:18:09 +01006202 protected:
6203 DEFAULT_COPY_CONSTRUCTOR(BoundsCheck);
6204
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006205 private:
Vladimir Markobd785672018-05-03 17:09:09 +01006206 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Markof02046e2018-10-02 15:31:32 +01006207 static constexpr size_t kNumberOfBoundsCheckPackedBits = kFlagIsStringCharAt + 1;
6208 static_assert(kNumberOfBoundsCheckPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6209 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006210};
6211
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006212class HSuspendCheck final : public HExpression<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006213 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00006214 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01006215 : HExpression(kSuspendCheck, SideEffects::CanTriggerGC(), dex_pc),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306216 slow_path_(nullptr) {
6217 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006218
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006219 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006220
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006221 bool NeedsEnvironment() const override {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006222 return true;
6223 }
6224
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006225 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
6226 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006227
6228 DECLARE_INSTRUCTION(SuspendCheck);
6229
Artem Serovcced8ba2017-07-19 18:18:09 +01006230 protected:
6231 DEFAULT_COPY_CONSTRUCTOR(SuspendCheck);
6232
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006233 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006234 // Only used for code generation, in order to share the same slow path between back edges
6235 // of a same loop.
6236 SlowPathCode* slow_path_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006237};
6238
David Srbecky0cf44932015-12-09 14:09:59 +00006239// Pseudo-instruction which provides the native debugger with mapping information.
6240// It ensures that we can generate line number and local variables at this point.
Vladimir Markobd785672018-05-03 17:09:09 +01006241class HNativeDebugInfo : public HExpression<0> {
David Srbecky0cf44932015-12-09 14:09:59 +00006242 public:
6243 explicit HNativeDebugInfo(uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006244 : HExpression<0>(kNativeDebugInfo, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306245 }
David Srbecky0cf44932015-12-09 14:09:59 +00006246
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006247 bool NeedsEnvironment() const override {
David Srbecky0cf44932015-12-09 14:09:59 +00006248 return true;
6249 }
6250
6251 DECLARE_INSTRUCTION(NativeDebugInfo);
6252
Artem Serovcced8ba2017-07-19 18:18:09 +01006253 protected:
6254 DEFAULT_COPY_CONSTRUCTOR(NativeDebugInfo);
David Srbecky0cf44932015-12-09 14:09:59 +00006255};
6256
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006257/**
6258 * Instruction to load a Class object.
6259 */
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006260class HLoadClass final : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006261 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006262 // Determines how to load the Class.
6263 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006264 // We cannot load this class. See HSharpening::SharpenLoadClass.
6265 kInvalid = -1,
6266
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006267 // Use the Class* from the method's own ArtMethod*.
6268 kReferrersClass,
6269
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006270 // Use PC-relative boot image Class* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006271 // Used for boot image classes referenced by boot image code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006272 kBootImageLinkTimePcRelative,
6273
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006274 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006275 // Used for boot image classes referenced by apps in AOT-compiled code.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006276 kBootImageRelRo,
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006277
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006278 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006279 // Used for classes outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006280 kBssEntry,
6281
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006282 // Use a known boot image Class* address, embedded in the code by the codegen.
6283 // Used for boot image classes referenced by apps in JIT-compiled code.
6284 kJitBootImageAddress,
6285
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006286 // Load from the root table associated with the JIT compiled method.
6287 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006288
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006289 // Load using a simple runtime call. This is the fall-back load kind when
6290 // the codegen is unable to use another appropriate kind.
6291 kRuntimeCall,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006292
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006293 kLast = kRuntimeCall
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006294 };
6295
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006296 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08006297 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006298 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006299 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006300 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01006301 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00006302 bool needs_access_check)
Vladimir Markobd785672018-05-03 17:09:09 +01006303 : HInstruction(kLoadClass,
6304 DataType::Type::kReference,
6305 SideEffectsForArchRuntimeCalls(),
6306 dex_pc),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006307 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006308 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006309 dex_file_(dex_file),
Vladimir Marko175e7862018-03-27 09:03:13 +00006310 klass_(klass) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01006311 // Referrers class should not need access check. We never inline unverified
6312 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006313 DCHECK(!is_referrers_class || !needs_access_check);
6314
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006315 SetPackedField<LoadKindField>(
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006316 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kRuntimeCall);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006317 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00006318 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006319 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Marko175e7862018-03-27 09:03:13 +00006320 SetPackedFlag<kFlagValidLoadedClassRTI>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006321 }
6322
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006323 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006324
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006325 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006326
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006327 LoadKind GetLoadKind() const {
6328 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006329 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006330
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006331 bool HasPcRelativeLoadKind() const {
6332 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6333 GetLoadKind() == LoadKind::kBootImageRelRo ||
6334 GetLoadKind() == LoadKind::kBssEntry;
6335 }
6336
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006337 bool CanBeMoved() const override { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006338
Yi Kong39402542019-03-24 02:47:16 -07006339 bool InstructionDataEquals(const HInstruction* other) const override;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006340
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006341 size_t ComputeHashCode() const override { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006342
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006343 bool CanBeNull() const override { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006344
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006345 bool NeedsEnvironment() const override {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006346 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006347 }
6348
Calin Juravle0ba218d2015-05-19 18:46:01 +01006349 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00006350 // The entrypoint the code generator is going to call does not do
6351 // clinit of the class.
6352 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00006353 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006354 }
6355
6356 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00006357 return NeedsAccessCheck() ||
6358 MustGenerateClinitCheck() ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006359 GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006360 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01006361 }
6362
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006363 bool CanThrow() const override {
Vladimir Marko41559982017-01-06 14:04:23 +00006364 return NeedsAccessCheck() ||
6365 MustGenerateClinitCheck() ||
6366 // If the class is in the boot image, the lookup in the runtime call cannot throw.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006367 ((GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006368 GetLoadKind() == LoadKind::kBssEntry) &&
6369 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006370 }
6371
Calin Juravleacf735c2015-02-12 15:25:22 +00006372 ReferenceTypeInfo GetLoadedClassRTI() {
Vladimir Marko175e7862018-03-27 09:03:13 +00006373 if (GetPackedFlag<kFlagValidLoadedClassRTI>()) {
6374 // Note: The is_exact flag from the return value should not be used.
Andreas Gampe3db70682018-12-26 15:12:03 -08006375 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact= */ true);
Vladimir Marko175e7862018-03-27 09:03:13 +00006376 } else {
6377 return ReferenceTypeInfo::CreateInvalid();
6378 }
Calin Juravleacf735c2015-02-12 15:25:22 +00006379 }
6380
Vladimir Marko175e7862018-03-27 09:03:13 +00006381 // Loaded class RTI is marked as valid by RTP if the klass_ is admissible.
6382 void SetValidLoadedClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
6383 DCHECK(klass_ != nullptr);
6384 SetPackedFlag<kFlagValidLoadedClassRTI>(true);
Calin Juravleacf735c2015-02-12 15:25:22 +00006385 }
6386
Andreas Gampea5b09a62016-11-17 15:21:22 -08006387 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006388 const DexFile& GetDexFile() const { return dex_file_; }
6389
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006390 bool NeedsDexCacheOfDeclaringClass() const override {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006391 return GetLoadKind() == LoadKind::kRuntimeCall;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006392 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00006393
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006394 static SideEffects SideEffectsForArchRuntimeCalls() {
6395 return SideEffects::CanTriggerGC();
6396 }
6397
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006398 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006399 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006400 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006401 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006402
Vladimir Markoa9f303c2018-07-20 16:43:56 +01006403 bool MustResolveTypeOnSlowPath() const {
6404 // Check that this instruction has a slow path.
6405 DCHECK(GetLoadKind() != LoadKind::kRuntimeCall); // kRuntimeCall calls on main path.
6406 DCHECK(GetLoadKind() == LoadKind::kBssEntry || MustGenerateClinitCheck());
6407 return GetLoadKind() == LoadKind::kBssEntry;
6408 }
6409
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006410 void MarkInBootImage() {
6411 SetPackedFlag<kFlagIsInBootImage>(true);
6412 }
6413
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006414 void AddSpecialInput(HInstruction* special_input);
6415
6416 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006417 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006418 return ArrayRef<HUserRecord<HInstruction*>>(
6419 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6420 }
6421
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006422 Handle<mirror::Class> GetClass() const {
6423 return klass_;
6424 }
6425
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006426 DECLARE_INSTRUCTION(LoadClass);
6427
Artem Serovcced8ba2017-07-19 18:18:09 +01006428 protected:
6429 DEFAULT_COPY_CONSTRUCTOR(LoadClass);
6430
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006431 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006432 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00006433 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006434 // Whether this instruction must generate the initialization check.
6435 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006436 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006437 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
6438 static constexpr size_t kFieldLoadKindSize =
6439 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
Vladimir Marko175e7862018-03-27 09:03:13 +00006440 static constexpr size_t kFlagValidLoadedClassRTI = kFieldLoadKind + kFieldLoadKindSize;
6441 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagValidLoadedClassRTI + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006442 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006443 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
6444
6445 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006446 return load_kind == LoadKind::kReferrersClass ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006447 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006448 load_kind == LoadKind::kBssEntry ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006449 load_kind == LoadKind::kRuntimeCall;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006450 }
6451
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006452 void SetLoadKindInternal(LoadKind load_kind);
6453
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006454 // The special input is the HCurrentMethod for kRuntimeCall or kReferrersClass.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006455 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006456 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006457 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006458
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006459 // A type index and dex file where the class can be accessed. The dex file can be:
6460 // - The compiling method's dex file if the class is defined there too.
6461 // - The compiling method's dex file if the class is referenced there.
6462 // - The dex file where the class is defined. When the load kind can only be
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006463 // kBssEntry or kRuntimeCall, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08006464 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006465 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006466
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006467 Handle<mirror::Class> klass_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006468};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006469std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
6470
6471// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006472inline void HLoadClass::SetLoadKind(LoadKind load_kind) {
6473 // The load kind should be determined before inserting the instruction to the graph.
6474 DCHECK(GetBlock() == nullptr);
6475 DCHECK(GetEnvironment() == nullptr);
6476 SetPackedField<LoadKindField>(load_kind);
6477 if (load_kind != LoadKind::kRuntimeCall && load_kind != LoadKind::kReferrersClass) {
6478 special_input_ = HUserRecord<HInstruction*>(nullptr);
6479 }
6480 if (!NeedsEnvironment()) {
6481 SetSideEffects(SideEffects::None());
6482 }
6483}
6484
6485// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006486inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006487 // The special input is used for PC-relative loads on some architectures,
6488 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006489 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006490 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006491 GetLoadKind() == LoadKind::kBssEntry ||
6492 GetLoadKind() == LoadKind::kJitBootImageAddress) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006493 DCHECK(special_input_.GetInstruction() == nullptr);
6494 special_input_ = HUserRecord<HInstruction*>(special_input);
6495 special_input->AddUseAt(this, 0);
6496}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006497
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006498class HLoadString final : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006499 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006500 // Determines how to load the String.
6501 enum class LoadKind {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006502 // Use PC-relative boot image String* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006503 // Used for boot image strings referenced by boot image code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006504 kBootImageLinkTimePcRelative,
6505
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006506 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006507 // Used for boot image strings referenced by apps in AOT-compiled code.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006508 kBootImageRelRo,
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006509
Vladimir Markoaad75c62016-10-03 08:46:48 +00006510 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006511 // Used for strings outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Markoaad75c62016-10-03 08:46:48 +00006512 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006513
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006514 // Use a known boot image String* address, embedded in the code by the codegen.
6515 // Used for boot image strings referenced by apps in JIT-compiled code.
6516 kJitBootImageAddress,
6517
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006518 // Load from the root table associated with the JIT compiled method.
6519 kJitTableAddress,
6520
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006521 // Load using a simple runtime call. This is the fall-back load kind when
6522 // the codegen is unable to use another appropriate kind.
6523 kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006524
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006525 kLast = kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006526 };
6527
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006528 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006529 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006530 const DexFile& dex_file,
6531 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006532 : HInstruction(kLoadString,
6533 DataType::Type::kReference,
6534 SideEffectsForArchRuntimeCalls(),
6535 dex_pc),
Vladimir Marko372f10e2016-05-17 16:30:10 +01006536 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006537 string_index_(string_index),
6538 dex_file_(dex_file) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006539 SetPackedField<LoadKindField>(LoadKind::kRuntimeCall);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006540 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006541
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006542 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006543
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006544 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006545
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006546 LoadKind GetLoadKind() const {
6547 return GetPackedField<LoadKindField>();
6548 }
6549
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006550 bool HasPcRelativeLoadKind() const {
6551 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6552 GetLoadKind() == LoadKind::kBootImageRelRo ||
6553 GetLoadKind() == LoadKind::kBssEntry;
6554 }
6555
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006556 const DexFile& GetDexFile() const {
6557 return dex_file_;
6558 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006559
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006560 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006561 return string_index_;
6562 }
6563
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006564 Handle<mirror::String> GetString() const {
6565 return string_;
6566 }
6567
6568 void SetString(Handle<mirror::String> str) {
6569 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006570 }
6571
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006572 bool CanBeMoved() const override { return true; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006573
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006574 bool InstructionDataEquals(const HInstruction* other) const override;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006575
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006576 size_t ComputeHashCode() const override { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006577
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006578 // Will call the runtime if we need to load the string through
6579 // the dex cache and the string is not guaranteed to be there yet.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006580 bool NeedsEnvironment() const override {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006581 LoadKind load_kind = GetLoadKind();
Vladimir Marko764d4542017-05-16 10:31:41 +01006582 if (load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006583 load_kind == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006584 load_kind == LoadKind::kJitBootImageAddress ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006585 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006586 return false;
6587 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006588 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006589 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006590
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006591 bool NeedsDexCacheOfDeclaringClass() const override {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006592 return GetLoadKind() == LoadKind::kRuntimeCall;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006593 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006594
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006595 bool CanBeNull() const override { return false; }
6596 bool CanThrow() const override { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006597
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006598 static SideEffects SideEffectsForArchRuntimeCalls() {
6599 return SideEffects::CanTriggerGC();
6600 }
6601
Vladimir Marko372f10e2016-05-17 16:30:10 +01006602 void AddSpecialInput(HInstruction* special_input);
6603
6604 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006605 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01006606 return ArrayRef<HUserRecord<HInstruction*>>(
6607 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006608 }
6609
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006610 DECLARE_INSTRUCTION(LoadString);
6611
Artem Serovcced8ba2017-07-19 18:18:09 +01006612 protected:
6613 DEFAULT_COPY_CONSTRUCTOR(LoadString);
6614
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006615 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006616 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006617 static constexpr size_t kFieldLoadKindSize =
6618 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
6619 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006620 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006621 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006622
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006623 void SetLoadKindInternal(LoadKind load_kind);
6624
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006625 // The special input is the HCurrentMethod for kRuntimeCall.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006626 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00006627 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01006628 HUserRecord<HInstruction*> special_input_;
6629
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006630 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006631 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006632
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006633 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006634};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006635std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
6636
6637// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006638inline void HLoadString::SetLoadKind(LoadKind load_kind) {
6639 // The load kind should be determined before inserting the instruction to the graph.
6640 DCHECK(GetBlock() == nullptr);
6641 DCHECK(GetEnvironment() == nullptr);
6642 DCHECK_EQ(GetLoadKind(), LoadKind::kRuntimeCall);
6643 SetPackedField<LoadKindField>(load_kind);
6644 if (load_kind != LoadKind::kRuntimeCall) {
6645 special_input_ = HUserRecord<HInstruction*>(nullptr);
6646 }
6647 if (!NeedsEnvironment()) {
6648 SetSideEffects(SideEffects::None());
6649 }
6650}
6651
6652// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006653inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006654 // The special input is used for PC-relative loads on some architectures,
6655 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006656 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006657 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006658 GetLoadKind() == LoadKind::kBssEntry ||
6659 GetLoadKind() == LoadKind::kJitBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01006660 // HLoadString::GetInputRecords() returns an empty array at this point,
6661 // so use the GetInputRecords() from the base class to set the input record.
6662 DCHECK(special_input_.GetInstruction() == nullptr);
6663 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006664 special_input->AddUseAt(this, 0);
6665}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006666
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006667class HLoadMethodHandle final : public HInstruction {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006668 public:
6669 HLoadMethodHandle(HCurrentMethod* current_method,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01006670 uint16_t method_handle_idx,
6671 const DexFile& dex_file,
6672 uint32_t dex_pc)
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006673 : HInstruction(kLoadMethodHandle,
6674 DataType::Type::kReference,
6675 SideEffectsForArchRuntimeCalls(),
6676 dex_pc),
6677 special_input_(HUserRecord<HInstruction*>(current_method)),
6678 method_handle_idx_(method_handle_idx),
6679 dex_file_(dex_file) {
6680 }
6681
6682 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006683 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006684 return ArrayRef<HUserRecord<HInstruction*>>(
6685 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6686 }
6687
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006688 bool IsClonable() const override { return true; }
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006689
6690 uint16_t GetMethodHandleIndex() const { return method_handle_idx_; }
6691
6692 const DexFile& GetDexFile() const { return dex_file_; }
6693
6694 static SideEffects SideEffectsForArchRuntimeCalls() {
6695 return SideEffects::CanTriggerGC();
6696 }
6697
6698 DECLARE_INSTRUCTION(LoadMethodHandle);
6699
6700 protected:
6701 DEFAULT_COPY_CONSTRUCTOR(LoadMethodHandle);
6702
6703 private:
6704 // The special input is the HCurrentMethod for kRuntimeCall.
6705 HUserRecord<HInstruction*> special_input_;
6706
6707 const uint16_t method_handle_idx_;
6708 const DexFile& dex_file_;
6709};
6710
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006711class HLoadMethodType final : public HInstruction {
Orion Hodson18259d72018-04-12 11:18:23 +01006712 public:
6713 HLoadMethodType(HCurrentMethod* current_method,
Orion Hodson06d10a72018-05-14 08:53:38 +01006714 dex::ProtoIndex proto_index,
Orion Hodson18259d72018-04-12 11:18:23 +01006715 const DexFile& dex_file,
6716 uint32_t dex_pc)
6717 : HInstruction(kLoadMethodType,
6718 DataType::Type::kReference,
6719 SideEffectsForArchRuntimeCalls(),
6720 dex_pc),
6721 special_input_(HUserRecord<HInstruction*>(current_method)),
Orion Hodson06d10a72018-05-14 08:53:38 +01006722 proto_index_(proto_index),
Orion Hodson18259d72018-04-12 11:18:23 +01006723 dex_file_(dex_file) {
6724 }
6725
6726 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006727 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Orion Hodson18259d72018-04-12 11:18:23 +01006728 return ArrayRef<HUserRecord<HInstruction*>>(
6729 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6730 }
6731
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006732 bool IsClonable() const override { return true; }
Orion Hodson18259d72018-04-12 11:18:23 +01006733
Orion Hodson06d10a72018-05-14 08:53:38 +01006734 dex::ProtoIndex GetProtoIndex() const { return proto_index_; }
Orion Hodson18259d72018-04-12 11:18:23 +01006735
6736 const DexFile& GetDexFile() const { return dex_file_; }
6737
6738 static SideEffects SideEffectsForArchRuntimeCalls() {
6739 return SideEffects::CanTriggerGC();
6740 }
6741
6742 DECLARE_INSTRUCTION(LoadMethodType);
6743
6744 protected:
6745 DEFAULT_COPY_CONSTRUCTOR(LoadMethodType);
6746
6747 private:
6748 // The special input is the HCurrentMethod for kRuntimeCall.
6749 HUserRecord<HInstruction*> special_input_;
6750
Orion Hodson06d10a72018-05-14 08:53:38 +01006751 const dex::ProtoIndex proto_index_;
Orion Hodson18259d72018-04-12 11:18:23 +01006752 const DexFile& dex_file_;
6753};
6754
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006755/**
6756 * Performs an initialization check on its Class object input.
6757 */
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006758class HClinitCheck final : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006759 public:
Roland Levillain3887c462015-08-12 18:15:42 +01006760 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07006761 : HExpression(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306762 kClinitCheck,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006763 DataType::Type::kReference,
Mingyao Yang217eb062017-12-11 15:20:07 -08006764 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006765 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006766 SetRawInputAt(0, constant);
6767 }
Artem Serova6e26142018-06-19 14:55:17 +01006768 // TODO: Make ClinitCheck clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006769 bool CanBeMoved() const override { return true; }
6770 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006771 return true;
6772 }
6773
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006774 bool NeedsEnvironment() const override {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006775 // May call runtime to initialize the class.
6776 return true;
6777 }
6778
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006779 bool CanThrow() const override { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006780
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006781 HLoadClass* GetLoadClass() const {
6782 DCHECK(InputAt(0)->IsLoadClass());
6783 return InputAt(0)->AsLoadClass();
6784 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006785
6786 DECLARE_INSTRUCTION(ClinitCheck);
6787
Artem Serovcced8ba2017-07-19 18:18:09 +01006788
6789 protected:
6790 DEFAULT_COPY_CONSTRUCTOR(ClinitCheck);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006791};
6792
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006793class HStaticFieldGet final : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006794 public:
6795 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006796 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006797 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006798 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006799 bool is_volatile,
6800 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006801 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006802 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006803 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306804 : HExpression(kStaticFieldGet,
6805 field_type,
6806 SideEffects::FieldReadOfType(field_type, is_volatile),
6807 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006808 field_info_(field,
6809 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006810 field_type,
6811 is_volatile,
6812 field_idx,
6813 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006814 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006815 SetRawInputAt(0, cls);
6816 }
6817
Calin Juravle52c48962014-12-16 17:02:57 +00006818
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006819 bool IsClonable() const override { return true; }
6820 bool CanBeMoved() const override { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006821
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006822 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01006823 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006824 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006825 }
6826
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006827 size_t ComputeHashCode() const override {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006828 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6829 }
6830
Calin Juravle52c48962014-12-16 17:02:57 +00006831 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006832 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006833 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006834 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006835
Vladimir Marko61b92282017-10-11 13:23:17 +01006836 void SetType(DataType::Type new_type) {
6837 DCHECK(DataType::IsIntegralType(GetType()));
6838 DCHECK(DataType::IsIntegralType(new_type));
6839 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6840 SetPackedField<TypeField>(new_type);
6841 }
6842
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006843 DECLARE_INSTRUCTION(StaticFieldGet);
6844
Artem Serovcced8ba2017-07-19 18:18:09 +01006845 protected:
6846 DEFAULT_COPY_CONSTRUCTOR(StaticFieldGet);
6847
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006848 private:
6849 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006850};
6851
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006852class HStaticFieldSet final : public HExpression<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006853 public:
6854 HStaticFieldSet(HInstruction* cls,
6855 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006856 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006857 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006858 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006859 bool is_volatile,
6860 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006861 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006862 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006863 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006864 : HExpression(kStaticFieldSet,
6865 SideEffects::FieldWriteOfType(field_type, is_volatile),
6866 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006867 field_info_(field,
6868 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006869 field_type,
6870 is_volatile,
6871 field_idx,
6872 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006873 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006874 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006875 SetRawInputAt(0, cls);
6876 SetRawInputAt(1, value);
6877 }
6878
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006879 bool IsClonable() const override { return true; }
Calin Juravle52c48962014-12-16 17:02:57 +00006880 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006881 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006882 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006883 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006884
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006885 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006886 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6887 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006888
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006889 DECLARE_INSTRUCTION(StaticFieldSet);
6890
Artem Serovcced8ba2017-07-19 18:18:09 +01006891 protected:
6892 DEFAULT_COPY_CONSTRUCTOR(StaticFieldSet);
6893
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006894 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006895 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6896 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6897 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6898 "Too many packed fields.");
6899
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006900 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006901};
6902
Vladimir Marko552a1342017-10-31 10:56:47 +00006903class HStringBuilderAppend final : public HVariableInputSizeInstruction {
6904 public:
6905 HStringBuilderAppend(HIntConstant* format,
6906 uint32_t number_of_arguments,
6907 ArenaAllocator* allocator,
6908 uint32_t dex_pc)
6909 : HVariableInputSizeInstruction(
6910 kStringBuilderAppend,
6911 DataType::Type::kReference,
6912 // The runtime call may read memory from inputs. It never writes outside
6913 // of the newly allocated result object (or newly allocated helper objects).
6914 SideEffects::AllReads().Union(SideEffects::CanTriggerGC()),
6915 dex_pc,
6916 allocator,
6917 number_of_arguments + /* format */ 1u,
6918 kArenaAllocInvokeInputs) {
6919 DCHECK_GE(number_of_arguments, 1u); // There must be something to append.
6920 SetRawInputAt(FormatIndex(), format);
6921 }
6922
6923 void SetArgumentAt(size_t index, HInstruction* argument) {
6924 DCHECK_LE(index, GetNumberOfArguments());
6925 SetRawInputAt(index, argument);
6926 }
6927
6928 // Return the number of arguments, excluding the format.
6929 size_t GetNumberOfArguments() const {
6930 DCHECK_GE(InputCount(), 1u);
6931 return InputCount() - 1u;
6932 }
6933
6934 size_t FormatIndex() const {
6935 return GetNumberOfArguments();
6936 }
6937
6938 HIntConstant* GetFormat() {
6939 return InputAt(FormatIndex())->AsIntConstant();
6940 }
6941
6942 bool NeedsEnvironment() const override { return true; }
6943
6944 bool CanThrow() const override { return true; }
6945
Vladimir Markob1fe5e12020-03-10 14:30:49 +00006946 bool CanBeNull() const override { return false; }
6947
Vladimir Marko552a1342017-10-31 10:56:47 +00006948 DECLARE_INSTRUCTION(StringBuilderAppend);
6949
6950 protected:
6951 DEFAULT_COPY_CONSTRUCTOR(StringBuilderAppend);
6952};
6953
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006954class HUnresolvedInstanceFieldGet final : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006955 public:
6956 HUnresolvedInstanceFieldGet(HInstruction* obj,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006957 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006958 uint32_t field_index,
6959 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306960 : HExpression(kUnresolvedInstanceFieldGet,
6961 field_type,
6962 SideEffects::AllExceptGCDependency(),
6963 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006964 field_index_(field_index) {
6965 SetRawInputAt(0, obj);
6966 }
6967
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006968 bool IsClonable() const override { return true; }
6969 bool NeedsEnvironment() const override { return true; }
6970 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006971
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006972 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006973 uint32_t GetFieldIndex() const { return field_index_; }
6974
6975 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6976
Artem Serovcced8ba2017-07-19 18:18:09 +01006977 protected:
6978 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldGet);
6979
Calin Juravlee460d1d2015-09-29 04:52:17 +01006980 private:
6981 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006982};
6983
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006984class HUnresolvedInstanceFieldSet final : public HExpression<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006985 public:
6986 HUnresolvedInstanceFieldSet(HInstruction* obj,
6987 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006988 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006989 uint32_t field_index,
6990 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006991 : HExpression(kUnresolvedInstanceFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006992 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006993 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006994 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006995 SetRawInputAt(0, obj);
6996 SetRawInputAt(1, value);
6997 }
6998
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006999 bool IsClonable() const override { return true; }
7000 bool NeedsEnvironment() const override { return true; }
7001 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007002
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007003 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007004 uint32_t GetFieldIndex() const { return field_index_; }
7005
7006 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
7007
Artem Serovcced8ba2017-07-19 18:18:09 +01007008 protected:
7009 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldSet);
7010
Calin Juravlee460d1d2015-09-29 04:52:17 +01007011 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007012 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
7013 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007014 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00007015 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
7016 kFieldFieldType + kFieldFieldTypeSize;
7017 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7018 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007019 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007020
Calin Juravlee460d1d2015-09-29 04:52:17 +01007021 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007022};
7023
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007024class HUnresolvedStaticFieldGet final : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01007025 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007026 HUnresolvedStaticFieldGet(DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01007027 uint32_t field_index,
7028 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307029 : HExpression(kUnresolvedStaticFieldGet,
7030 field_type,
7031 SideEffects::AllExceptGCDependency(),
7032 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01007033 field_index_(field_index) {
7034 }
7035
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007036 bool IsClonable() const override { return true; }
7037 bool NeedsEnvironment() const override { return true; }
7038 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007039
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007040 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007041 uint32_t GetFieldIndex() const { return field_index_; }
7042
7043 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
7044
Artem Serovcced8ba2017-07-19 18:18:09 +01007045 protected:
7046 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldGet);
7047
Calin Juravlee460d1d2015-09-29 04:52:17 +01007048 private:
7049 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007050};
7051
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007052class HUnresolvedStaticFieldSet final : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01007053 public:
7054 HUnresolvedStaticFieldSet(HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007055 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01007056 uint32_t field_index,
7057 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007058 : HExpression(kUnresolvedStaticFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01007059 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007060 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007061 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01007062 SetRawInputAt(0, value);
7063 }
7064
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007065 bool IsClonable() const override { return true; }
7066 bool NeedsEnvironment() const override { return true; }
7067 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007068
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007069 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007070 uint32_t GetFieldIndex() const { return field_index_; }
7071
7072 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
7073
Artem Serovcced8ba2017-07-19 18:18:09 +01007074 protected:
7075 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldSet);
7076
Calin Juravlee460d1d2015-09-29 04:52:17 +01007077 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007078 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
7079 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007080 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00007081 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
7082 kFieldFieldType + kFieldFieldTypeSize;
7083 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7084 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007085 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007086
Calin Juravlee460d1d2015-09-29 04:52:17 +01007087 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007088};
7089
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007090// Implement the move-exception DEX instruction.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007091class HLoadException final : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007092 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007093 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307094 : HExpression(kLoadException, DataType::Type::kReference, SideEffects::None(), dex_pc) {
7095 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007096
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007097 bool CanBeNull() const override { return false; }
David Brazdilbbd733e2015-08-18 17:48:17 +01007098
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007099 DECLARE_INSTRUCTION(LoadException);
7100
Artem Serovcced8ba2017-07-19 18:18:09 +01007101 protected:
7102 DEFAULT_COPY_CONSTRUCTOR(LoadException);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007103};
7104
David Brazdilcb1c0552015-08-04 16:22:25 +01007105// Implicit part of move-exception which clears thread-local exception storage.
7106// Must not be removed because the runtime expects the TLS to get cleared.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007107class HClearException final : public HExpression<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01007108 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007109 explicit HClearException(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007110 : HExpression(kClearException, SideEffects::AllWrites(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307111 }
David Brazdilcb1c0552015-08-04 16:22:25 +01007112
7113 DECLARE_INSTRUCTION(ClearException);
7114
Artem Serovcced8ba2017-07-19 18:18:09 +01007115 protected:
7116 DEFAULT_COPY_CONSTRUCTOR(ClearException);
David Brazdilcb1c0552015-08-04 16:22:25 +01007117};
7118
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007119class HThrow final : public HExpression<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007120 public:
7121 HThrow(HInstruction* exception, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007122 : HExpression(kThrow, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007123 SetRawInputAt(0, exception);
7124 }
7125
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007126 bool IsControlFlow() const override { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007127
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007128 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007129
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007130 bool CanThrow() const override { return true; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007131
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007132 bool AlwaysThrows() const override { return true; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08007133
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007134 DECLARE_INSTRUCTION(Throw);
7135
Artem Serovcced8ba2017-07-19 18:18:09 +01007136 protected:
7137 DEFAULT_COPY_CONSTRUCTOR(Throw);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007138};
7139
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007140/**
7141 * Implementation strategies for the code generator of a HInstanceOf
7142 * or `HCheckCast`.
7143 */
7144enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01007145 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007146 kExactCheck, // Can do a single class compare.
7147 kClassHierarchyCheck, // Can just walk the super class chain.
7148 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
7149 kInterfaceCheck, // No optimization yet when checking against an interface.
7150 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00007151 kArrayCheck, // No optimization yet when checking against a generic array.
Vladimir Marko175e7862018-03-27 09:03:13 +00007152 kBitstringCheck, // Compare the type check bitstring.
Vladimir Markoa1de9182016-02-25 11:37:38 +00007153 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007154};
7155
Roland Levillain86503782016-02-11 19:07:30 +00007156std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
7157
Vladimir Marko175e7862018-03-27 09:03:13 +00007158// Note: HTypeCheckInstruction is just a helper class, not an abstract instruction with an
7159// `IsTypeCheckInstruction()`. (New virtual methods in the HInstruction class have a high cost.)
7160class HTypeCheckInstruction : public HVariableInputSizeInstruction {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007161 public:
Vladimir Marko175e7862018-03-27 09:03:13 +00007162 HTypeCheckInstruction(InstructionKind kind,
Vladimir Markobd785672018-05-03 17:09:09 +01007163 DataType::Type type,
Vladimir Marko175e7862018-03-27 09:03:13 +00007164 HInstruction* object,
7165 HInstruction* target_class_or_null,
7166 TypeCheckKind check_kind,
7167 Handle<mirror::Class> klass,
7168 uint32_t dex_pc,
7169 ArenaAllocator* allocator,
7170 HIntConstant* bitstring_path_to_root,
7171 HIntConstant* bitstring_mask,
7172 SideEffects side_effects)
7173 : HVariableInputSizeInstruction(
7174 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01007175 type,
Vladimir Marko175e7862018-03-27 09:03:13 +00007176 side_effects,
7177 dex_pc,
7178 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08007179 /* number_of_inputs= */ check_kind == TypeCheckKind::kBitstringCheck ? 4u : 2u,
Vladimir Marko175e7862018-03-27 09:03:13 +00007180 kArenaAllocTypeCheckInputs),
7181 klass_(klass) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007182 SetPackedField<TypeCheckKindField>(check_kind);
7183 SetPackedFlag<kFlagMustDoNullCheck>(true);
Vladimir Marko175e7862018-03-27 09:03:13 +00007184 SetPackedFlag<kFlagValidTargetClassRTI>(false);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007185 SetRawInputAt(0, object);
Vladimir Marko175e7862018-03-27 09:03:13 +00007186 SetRawInputAt(1, target_class_or_null);
7187 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_path_to_root != nullptr);
7188 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_mask != nullptr);
7189 if (check_kind == TypeCheckKind::kBitstringCheck) {
7190 DCHECK(target_class_or_null->IsNullConstant());
7191 SetRawInputAt(2, bitstring_path_to_root);
7192 SetRawInputAt(3, bitstring_mask);
7193 } else {
7194 DCHECK(target_class_or_null->IsLoadClass());
7195 }
Vladimir Marko87584542017-12-12 17:47:52 +00007196 }
7197
7198 HLoadClass* GetTargetClass() const {
Vladimir Marko175e7862018-03-27 09:03:13 +00007199 DCHECK_NE(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
Vladimir Marko87584542017-12-12 17:47:52 +00007200 HInstruction* load_class = InputAt(1);
7201 DCHECK(load_class->IsLoadClass());
7202 return load_class->AsLoadClass();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007203 }
7204
Vladimir Marko175e7862018-03-27 09:03:13 +00007205 uint32_t GetBitstringPathToRoot() const {
7206 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7207 HInstruction* path_to_root = InputAt(2);
7208 DCHECK(path_to_root->IsIntConstant());
7209 return static_cast<uint32_t>(path_to_root->AsIntConstant()->GetValue());
7210 }
7211
7212 uint32_t GetBitstringMask() const {
7213 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7214 HInstruction* mask = InputAt(3);
7215 DCHECK(mask->IsIntConstant());
7216 return static_cast<uint32_t>(mask->AsIntConstant()->GetValue());
7217 }
7218
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007219 bool IsClonable() const override { return true; }
7220 bool CanBeMoved() const override { return true; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007221
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007222 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko175e7862018-03-27 09:03:13 +00007223 DCHECK(other->IsInstanceOf() || other->IsCheckCast()) << other->DebugName();
7224 return GetPackedFields() == down_cast<const HTypeCheckInstruction*>(other)->GetPackedFields();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007225 }
7226
Andreas Gampe3fbd3ad2018-03-26 21:14:46 +00007227 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
7228 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
7229 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
7230 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
7231
Vladimir Marko175e7862018-03-27 09:03:13 +00007232 ReferenceTypeInfo GetTargetClassRTI() {
7233 if (GetPackedFlag<kFlagValidTargetClassRTI>()) {
7234 // Note: The is_exact flag from the return value should not be used.
Andreas Gampe3db70682018-12-26 15:12:03 -08007235 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact= */ true);
Vladimir Marko175e7862018-03-27 09:03:13 +00007236 } else {
7237 return ReferenceTypeInfo::CreateInvalid();
7238 }
7239 }
7240
7241 // Target class RTI is marked as valid by RTP if the klass_ is admissible.
7242 void SetValidTargetClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
7243 DCHECK(klass_ != nullptr);
7244 SetPackedFlag<kFlagValidTargetClassRTI>(true);
7245 }
7246
7247 Handle<mirror::Class> GetClass() const {
7248 return klass_;
7249 }
7250
7251 protected:
7252 DEFAULT_COPY_CONSTRUCTOR(TypeCheckInstruction);
7253
7254 private:
7255 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
7256 static constexpr size_t kFieldTypeCheckKindSize =
7257 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
7258 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
7259 static constexpr size_t kFlagValidTargetClassRTI = kFlagMustDoNullCheck + 1;
7260 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagValidTargetClassRTI + 1;
7261 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7262 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
7263
7264 Handle<mirror::Class> klass_;
7265};
7266
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007267class HInstanceOf final : public HTypeCheckInstruction {
Vladimir Marko175e7862018-03-27 09:03:13 +00007268 public:
7269 HInstanceOf(HInstruction* object,
7270 HInstruction* target_class_or_null,
7271 TypeCheckKind check_kind,
7272 Handle<mirror::Class> klass,
7273 uint32_t dex_pc,
7274 ArenaAllocator* allocator,
7275 HIntConstant* bitstring_path_to_root,
7276 HIntConstant* bitstring_mask)
7277 : HTypeCheckInstruction(kInstanceOf,
Vladimir Markobd785672018-05-03 17:09:09 +01007278 DataType::Type::kBool,
Vladimir Marko175e7862018-03-27 09:03:13 +00007279 object,
7280 target_class_or_null,
7281 check_kind,
7282 klass,
7283 dex_pc,
7284 allocator,
7285 bitstring_path_to_root,
7286 bitstring_mask,
7287 SideEffectsForArchRuntimeCalls(check_kind)) {}
7288
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007289 bool IsClonable() const override { return true; }
Artem Serova6e26142018-06-19 14:55:17 +01007290
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007291 bool NeedsEnvironment() const override {
Vladimir Marko175e7862018-03-27 09:03:13 +00007292 return CanCallRuntime(GetTypeCheckKind());
7293 }
7294
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007295 static bool CanCallRuntime(TypeCheckKind check_kind) {
Vladimir Marko54f4fbd2020-02-12 10:52:22 +00007296 // TODO: Re-evaluate now that mips codegen has been removed.
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007297 return check_kind != TypeCheckKind::kExactCheck;
7298 }
7299
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007300 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007301 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01007302 }
7303
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007304 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007305
Artem Serovcced8ba2017-07-19 18:18:09 +01007306 protected:
7307 DEFAULT_COPY_CONSTRUCTOR(InstanceOf);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007308};
7309
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007310class HBoundType final : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00007311 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07007312 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307313 : HExpression(kBoundType, DataType::Type::kReference, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00007314 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
7315 SetPackedFlag<kFlagUpperCanBeNull>(true);
7316 SetPackedFlag<kFlagCanBeNull>(true);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007317 DCHECK_EQ(input->GetType(), DataType::Type::kReference);
Calin Juravleb1498f62015-02-16 13:13:29 +00007318 SetRawInputAt(0, input);
7319 }
7320
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007321 bool InstructionDataEquals(const HInstruction* other) const override;
7322 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007323
David Brazdilf5552582015-12-27 13:36:12 +00007324 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007325 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00007326 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00007327 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007328
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007329 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007330 DCHECK(GetUpperCanBeNull() || !can_be_null);
7331 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007332 }
7333
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007334 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007335
Calin Juravleb1498f62015-02-16 13:13:29 +00007336 DECLARE_INSTRUCTION(BoundType);
7337
Artem Serovcced8ba2017-07-19 18:18:09 +01007338 protected:
7339 DEFAULT_COPY_CONSTRUCTOR(BoundType);
7340
Calin Juravleb1498f62015-02-16 13:13:29 +00007341 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007342 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
7343 // is false then CanBeNull() cannot be true).
Vladimir Markobd785672018-05-03 17:09:09 +01007344 static constexpr size_t kFlagUpperCanBeNull = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007345 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
7346 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
7347 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7348
Calin Juravleb1498f62015-02-16 13:13:29 +00007349 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007350 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
7351 // It is used to bound the type in cases like:
7352 // if (x instanceof ClassX) {
7353 // // uper_bound_ will be ClassX
7354 // }
David Brazdilf5552582015-12-27 13:36:12 +00007355 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00007356};
7357
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007358class HCheckCast final : public HTypeCheckInstruction {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007359 public:
7360 HCheckCast(HInstruction* object,
Vladimir Marko175e7862018-03-27 09:03:13 +00007361 HInstruction* target_class_or_null,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007362 TypeCheckKind check_kind,
Vladimir Marko175e7862018-03-27 09:03:13 +00007363 Handle<mirror::Class> klass,
7364 uint32_t dex_pc,
7365 ArenaAllocator* allocator,
7366 HIntConstant* bitstring_path_to_root,
7367 HIntConstant* bitstring_mask)
7368 : HTypeCheckInstruction(kCheckCast,
Vladimir Markobd785672018-05-03 17:09:09 +01007369 DataType::Type::kVoid,
Vladimir Marko175e7862018-03-27 09:03:13 +00007370 object,
7371 target_class_or_null,
7372 check_kind,
7373 klass,
7374 dex_pc,
7375 allocator,
7376 bitstring_path_to_root,
7377 bitstring_mask,
7378 SideEffects::CanTriggerGC()) {}
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007379
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007380 bool IsClonable() const override { return true; }
7381 bool NeedsEnvironment() const override {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007382 // Instruction may throw a CheckCastError.
7383 return true;
7384 }
7385
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007386 bool CanThrow() const override { return true; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007387
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007388 DECLARE_INSTRUCTION(CheckCast);
7389
Artem Serovcced8ba2017-07-19 18:18:09 +01007390 protected:
7391 DEFAULT_COPY_CONSTRUCTOR(CheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007392};
7393
Andreas Gampe26de38b2016-07-27 17:53:11 -07007394/**
7395 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
7396 * @details We define the combined barrier types that are actually required
7397 * by the Java Memory Model, rather than using exactly the terminology from
7398 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
7399 * primitives. Note that the JSR-133 cookbook generally does not deal with
7400 * store atomicity issues, and the recipes there are not always entirely sufficient.
7401 * The current recipe is as follows:
7402 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
7403 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
7404 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
7405 * -# Use StoreStore barrier after all stores but before return from any constructor whose
7406 * class has final fields.
7407 * -# Use NTStoreStore to order non-temporal stores with respect to all later
7408 * store-to-memory instructions. Only generated together with non-temporal stores.
7409 */
7410enum MemBarrierKind {
7411 kAnyStore,
7412 kLoadAny,
7413 kStoreStore,
7414 kAnyAny,
7415 kNTStoreStore,
7416 kLastBarrierKind = kNTStoreStore
7417};
7418std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
7419
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007420class HMemoryBarrier final : public HExpression<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01007421 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007422 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007423 : HExpression(kMemoryBarrier,
7424 SideEffects::AllWritesAndReads(), // Assume write/read on all fields/arrays.
7425 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007426 SetPackedField<BarrierKindField>(barrier_kind);
7427 }
Calin Juravle27df7582015-04-17 19:12:31 +01007428
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007429 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007430
Vladimir Markoa1de9182016-02-25 11:37:38 +00007431 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01007432
7433 DECLARE_INSTRUCTION(MemoryBarrier);
7434
Artem Serovcced8ba2017-07-19 18:18:09 +01007435 protected:
7436 DEFAULT_COPY_CONSTRUCTOR(MemoryBarrier);
7437
Calin Juravle27df7582015-04-17 19:12:31 +01007438 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007439 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
7440 static constexpr size_t kFieldBarrierKindSize =
7441 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
7442 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
7443 kFieldBarrierKind + kFieldBarrierKindSize;
7444 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
7445 "Too many packed fields.");
7446 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01007447};
7448
Igor Murashkind01745e2017-04-05 16:40:31 -07007449// A constructor fence orders all prior stores to fields that could be accessed via a final field of
7450// the specified object(s), with respect to any subsequent store that might "publish"
7451// (i.e. make visible) the specified object to another thread.
7452//
7453// JLS 17.5.1 "Semantics of final fields" states that a freeze action happens
7454// for all final fields (that were set) at the end of the invoked constructor.
7455//
7456// The constructor fence models the freeze actions for the final fields of an object
7457// being constructed (semantically at the end of the constructor). Constructor fences
7458// have a per-object affinity; two separate objects being constructed get two separate
7459// constructor fences.
7460//
7461// (Note: that if calling a super-constructor or forwarding to another constructor,
7462// the freezes would happen at the end of *that* constructor being invoked).
7463//
7464// The memory model guarantees that when the object being constructed is "published" after
7465// constructor completion (i.e. escapes the current thread via a store), then any final field
7466// writes must be observable on other threads (once they observe that publication).
7467//
7468// Further, anything written before the freeze, and read by dereferencing through the final field,
7469// must also be visible (so final object field could itself have an object with non-final fields;
7470// yet the freeze must also extend to them).
7471//
7472// Constructor example:
7473//
7474// class HasFinal {
7475// final int field; Optimizing IR for <init>()V:
7476// HasFinal() {
7477// field = 123; HInstanceFieldSet(this, HasFinal.field, 123)
7478// // freeze(this.field); HConstructorFence(this)
7479// } HReturn
7480// }
7481//
7482// HConstructorFence can serve double duty as a fence for new-instance/new-array allocations of
7483// already-initialized classes; in that case the allocation must act as a "default-initializer"
7484// of the object which effectively writes the class pointer "final field".
7485//
7486// For example, we can model default-initialiation as roughly the equivalent of the following:
7487//
7488// class Object {
7489// private final Class header;
7490// }
7491//
7492// Java code: Optimizing IR:
7493//
7494// T new_instance<T>() {
7495// Object obj = allocate_memory(T.class.size); obj = HInvoke(art_quick_alloc_object, T)
7496// obj.header = T.class; // header write is done by above call.
7497// // freeze(obj.header) HConstructorFence(obj)
7498// return (T)obj;
7499// }
7500//
7501// See also:
Vladimir Markoc1c34522018-10-31 13:56:49 +00007502// * DexCompilationUnit::RequiresConstructorBarrier
Igor Murashkind01745e2017-04-05 16:40:31 -07007503// * QuasiAtomic::ThreadFenceForConstructor
7504//
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007505class HConstructorFence final : public HVariableInputSizeInstruction {
Igor Murashkind01745e2017-04-05 16:40:31 -07007506 // A fence has variable inputs because the inputs can be removed
7507 // after prepare_for_register_allocation phase.
7508 // (TODO: In the future a fence could freeze multiple objects
7509 // after merging two fences together.)
7510 public:
7511 // `fence_object` is the reference that needs to be protected for correct publication.
7512 //
7513 // It makes sense in the following situations:
7514 // * <init> constructors, it's the "this" parameter (i.e. HParameterValue, s.t. IsThis() == true).
7515 // * new-instance-like instructions, it's the return value (i.e. HNewInstance).
7516 //
7517 // After construction the `fence_object` becomes the 0th input.
7518 // This is not an input in a real sense, but just a convenient place to stash the information
7519 // about the associated object.
7520 HConstructorFence(HInstruction* fence_object,
7521 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007522 ArenaAllocator* allocator)
Igor Murashkind01745e2017-04-05 16:40:31 -07007523 // We strongly suspect there is not a more accurate way to describe the fine-grained reordering
7524 // constraints described in the class header. We claim that these SideEffects constraints
7525 // enforce a superset of the real constraints.
7526 //
7527 // The ordering described above is conservatively modeled with SideEffects as follows:
7528 //
7529 // * To prevent reordering of the publication stores:
7530 // ----> "Reads of objects" is the initial SideEffect.
7531 // * For every primitive final field store in the constructor:
7532 // ----> Union that field's type as a read (e.g. "Read of T") into the SideEffect.
7533 // * If there are any stores to reference final fields in the constructor:
7534 // ----> Use a more conservative "AllReads" SideEffect because any stores to any references
7535 // that are reachable from `fence_object` also need to be prevented for reordering
7536 // (and we do not want to do alias analysis to figure out what those stores are).
7537 //
7538 // In the implementation, this initially starts out as an "all reads" side effect; this is an
7539 // even more conservative approach than the one described above, and prevents all of the
7540 // above reordering without analyzing any of the instructions in the constructor.
7541 //
7542 // If in a later phase we discover that there are no writes to reference final fields,
7543 // we can refine the side effect to a smaller set of type reads (see above constraints).
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307544 : HVariableInputSizeInstruction(kConstructorFence,
7545 SideEffects::AllReads(),
Igor Murashkind01745e2017-04-05 16:40:31 -07007546 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007547 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08007548 /* number_of_inputs= */ 1,
Igor Murashkind01745e2017-04-05 16:40:31 -07007549 kArenaAllocConstructorFenceInputs) {
7550 DCHECK(fence_object != nullptr);
7551 SetRawInputAt(0, fence_object);
7552 }
7553
7554 // The object associated with this constructor fence.
7555 //
7556 // (Note: This will be null after the prepare_for_register_allocation phase,
7557 // as all constructor fence inputs are removed there).
7558 HInstruction* GetFenceObject() const {
7559 return InputAt(0);
7560 }
7561
7562 // Find all the HConstructorFence uses (`fence_use`) for `this` and:
7563 // - Delete `fence_use` from `this`'s use list.
7564 // - Delete `this` from `fence_use`'s inputs list.
7565 // - If the `fence_use` is dead, remove it from the graph.
7566 //
7567 // A fence is considered dead once it no longer has any uses
7568 // and all of the inputs are dead.
7569 //
7570 // This must *not* be called during/after prepare_for_register_allocation,
7571 // because that removes all the inputs to the fences but the fence is actually
7572 // still considered live.
Igor Murashkin6ef45672017-08-08 13:59:55 -07007573 //
7574 // Returns how many HConstructorFence instructions were removed from graph.
7575 static size_t RemoveConstructorFences(HInstruction* instruction);
Igor Murashkind01745e2017-04-05 16:40:31 -07007576
Igor Murashkindd018df2017-08-09 10:38:31 -07007577 // Combine all inputs of `this` and `other` instruction and remove
7578 // `other` from the graph.
7579 //
7580 // Inputs are unique after the merge.
7581 //
7582 // Requirement: `this` must not be the same as `other.
7583 void Merge(HConstructorFence* other);
7584
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007585 // Check if this constructor fence is protecting
7586 // an HNewInstance or HNewArray that is also the immediate
7587 // predecessor of `this`.
7588 //
Igor Murashkindd018df2017-08-09 10:38:31 -07007589 // If `ignore_inputs` is true, then the immediate predecessor doesn't need
7590 // to be one of the inputs of `this`.
7591 //
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007592 // Returns the associated HNewArray or HNewInstance,
7593 // or null otherwise.
Igor Murashkindd018df2017-08-09 10:38:31 -07007594 HInstruction* GetAssociatedAllocation(bool ignore_inputs = false);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007595
Igor Murashkind01745e2017-04-05 16:40:31 -07007596 DECLARE_INSTRUCTION(ConstructorFence);
7597
Artem Serovcced8ba2017-07-19 18:18:09 +01007598 protected:
7599 DEFAULT_COPY_CONSTRUCTOR(ConstructorFence);
Igor Murashkind01745e2017-04-05 16:40:31 -07007600};
7601
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007602class HMonitorOperation final : public HExpression<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007603 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007604 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007605 kEnter,
7606 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00007607 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007608 };
7609
7610 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007611 : HExpression(kMonitorOperation,
7612 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
7613 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007614 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007615 SetRawInputAt(0, object);
7616 }
7617
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007618 // Instruction may go into runtime, so we need an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007619 bool NeedsEnvironment() const override { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00007620
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007621 bool CanThrow() const override {
David Brazdilbff75032015-07-08 17:26:51 +00007622 // Verifier guarantees that monitor-exit cannot throw.
7623 // This is important because it allows the HGraphBuilder to remove
7624 // a dead throw-catch loop generated for `synchronized` blocks/methods.
7625 return IsEnter();
7626 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007627
Vladimir Markoa1de9182016-02-25 11:37:38 +00007628 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
7629 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007630
7631 DECLARE_INSTRUCTION(MonitorOperation);
7632
Artem Serovcced8ba2017-07-19 18:18:09 +01007633 protected:
7634 DEFAULT_COPY_CONSTRUCTOR(MonitorOperation);
7635
Calin Juravle52c48962014-12-16 17:02:57 +00007636 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007637 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
7638 static constexpr size_t kFieldOperationKindSize =
7639 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
7640 static constexpr size_t kNumberOfMonitorOperationPackedBits =
7641 kFieldOperationKind + kFieldOperationKindSize;
7642 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7643 "Too many packed fields.");
7644 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007645};
7646
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007647class HSelect final : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00007648 public:
7649 HSelect(HInstruction* condition,
7650 HInstruction* true_value,
7651 HInstruction* false_value,
7652 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307653 : HExpression(kSelect, HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
David Brazdil74eb1b22015-12-14 11:44:01 +00007654 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
7655
7656 // First input must be `true_value` or `false_value` to allow codegens to
7657 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
7658 // that architectures which implement HSelect as a conditional move also
7659 // will not need to invert the condition.
7660 SetRawInputAt(0, false_value);
7661 SetRawInputAt(1, true_value);
7662 SetRawInputAt(2, condition);
7663 }
7664
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007665 bool IsClonable() const override { return true; }
David Brazdil74eb1b22015-12-14 11:44:01 +00007666 HInstruction* GetFalseValue() const { return InputAt(0); }
7667 HInstruction* GetTrueValue() const { return InputAt(1); }
7668 HInstruction* GetCondition() const { return InputAt(2); }
7669
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007670 bool CanBeMoved() const override { return true; }
7671 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01007672 return true;
7673 }
David Brazdil74eb1b22015-12-14 11:44:01 +00007674
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007675 bool CanBeNull() const override {
David Brazdil74eb1b22015-12-14 11:44:01 +00007676 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
7677 }
7678
7679 DECLARE_INSTRUCTION(Select);
7680
Artem Serovcced8ba2017-07-19 18:18:09 +01007681 protected:
7682 DEFAULT_COPY_CONSTRUCTOR(Select);
David Brazdil74eb1b22015-12-14 11:44:01 +00007683};
7684
Vladimir Markof9f64412015-09-02 14:05:49 +01007685class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007686 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01007687 MoveOperands(Location source,
7688 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007689 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007690 HInstruction* instruction)
7691 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007692
7693 Location GetSource() const { return source_; }
7694 Location GetDestination() const { return destination_; }
7695
7696 void SetSource(Location value) { source_ = value; }
7697 void SetDestination(Location value) { destination_ = value; }
7698
7699 // The parallel move resolver marks moves as "in-progress" by clearing the
7700 // destination (but not the source).
7701 Location MarkPending() {
7702 DCHECK(!IsPending());
7703 Location dest = destination_;
7704 destination_ = Location::NoLocation();
7705 return dest;
7706 }
7707
7708 void ClearPending(Location dest) {
7709 DCHECK(IsPending());
7710 destination_ = dest;
7711 }
7712
7713 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00007714 DCHECK(source_.IsValid() || destination_.IsInvalid());
7715 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007716 }
7717
7718 // True if this blocks a move from the given location.
7719 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08007720 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007721 }
7722
7723 // A move is redundant if it's been eliminated, if its source and
7724 // destination are the same, or if its destination is unneeded.
7725 bool IsRedundant() const {
7726 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
7727 }
7728
7729 // We clear both operands to indicate move that's been eliminated.
7730 void Eliminate() {
7731 source_ = destination_ = Location::NoLocation();
7732 }
7733
7734 bool IsEliminated() const {
7735 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
7736 return source_.IsInvalid();
7737 }
7738
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007739 DataType::Type GetType() const { return type_; }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007740
Nicolas Geoffray90218252015-04-15 11:56:51 +01007741 bool Is64BitMove() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007742 return DataType::Is64BitType(type_);
Nicolas Geoffray90218252015-04-15 11:56:51 +01007743 }
7744
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007745 HInstruction* GetInstruction() const { return instruction_; }
7746
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007747 private:
7748 Location source_;
7749 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01007750 // The type this move is for.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007751 DataType::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007752 // The instruction this move is assocatied with. Null when this move is
7753 // for moving an input in the expected locations of user (including a phi user).
7754 // This is only used in debug mode, to ensure we do not connect interval siblings
7755 // in the same parallel move.
7756 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007757};
7758
Roland Levillainc9285912015-12-18 10:38:42 +00007759std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
7760
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007761static constexpr size_t kDefaultNumberOfMoves = 4;
7762
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007763class HParallelMove final : public HExpression<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007764 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007765 explicit HParallelMove(ArenaAllocator* allocator, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007766 : HExpression(kParallelMove, SideEffects::None(), dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007767 moves_(allocator->Adapter(kArenaAllocMoveOperands)) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007768 moves_.reserve(kDefaultNumberOfMoves);
7769 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007770
Nicolas Geoffray90218252015-04-15 11:56:51 +01007771 void AddMove(Location source,
7772 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007773 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007774 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00007775 DCHECK(source.IsValid());
7776 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007777 if (kIsDebugBuild) {
7778 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007779 for (const MoveOperands& move : moves_) {
7780 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007781 // Special case the situation where the move is for the spill slot
7782 // of the instruction.
7783 if ((GetPrevious() == instruction)
7784 || ((GetPrevious() == nullptr)
7785 && instruction->IsPhi()
7786 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007787 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007788 << "Doing parallel moves for the same instruction.";
7789 } else {
7790 DCHECK(false) << "Doing parallel moves for the same instruction.";
7791 }
7792 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00007793 }
7794 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007795 for (const MoveOperands& move : moves_) {
7796 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007797 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01007798 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007799 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007800 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007801 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007802 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007803 }
7804
Vladimir Marko225b6462015-09-28 12:17:40 +01007805 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007806 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007807 }
7808
Vladimir Marko225b6462015-09-28 12:17:40 +01007809 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007810
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01007811 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007812
Artem Serovcced8ba2017-07-19 18:18:09 +01007813 protected:
7814 DEFAULT_COPY_CONSTRUCTOR(ParallelMove);
7815
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007816 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01007817 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007818};
7819
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007820// This instruction computes an intermediate address pointing in the 'middle' of an object. The
7821// result pointer cannot be handled by GC, so extra care is taken to make sure that this value is
7822// never used across anything that can trigger GC.
7823// The result of this instruction is not a pointer in the sense of `DataType::Type::kreference`.
7824// So we represent it by the type `DataType::Type::kInt`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007825class HIntermediateAddress final : public HExpression<2> {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007826 public:
7827 HIntermediateAddress(HInstruction* base_address, HInstruction* offset, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307828 : HExpression(kIntermediateAddress,
7829 DataType::Type::kInt32,
7830 SideEffects::DependsOnGC(),
7831 dex_pc) {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007832 DCHECK_EQ(DataType::Size(DataType::Type::kInt32),
7833 DataType::Size(DataType::Type::kReference))
7834 << "kPrimInt and kPrimNot have different sizes.";
7835 SetRawInputAt(0, base_address);
7836 SetRawInputAt(1, offset);
7837 }
7838
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007839 bool IsClonable() const override { return true; }
7840 bool CanBeMoved() const override { return true; }
7841 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007842 return true;
7843 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007844 bool IsActualObject() const override { return false; }
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007845
7846 HInstruction* GetBaseAddress() const { return InputAt(0); }
7847 HInstruction* GetOffset() const { return InputAt(1); }
7848
7849 DECLARE_INSTRUCTION(IntermediateAddress);
7850
Artem Serovcced8ba2017-07-19 18:18:09 +01007851 protected:
7852 DEFAULT_COPY_CONSTRUCTOR(IntermediateAddress);
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007853};
7854
7855
Mark Mendell0616ae02015-04-17 12:49:27 -04007856} // namespace art
7857
Aart Bikf8f5a162017-02-06 15:35:29 -08007858#include "nodes_vector.h"
7859
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007860#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
7861#include "nodes_shared.h"
7862#endif
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05307863#if defined(ART_ENABLE_CODEGEN_x86) || defined(ART_ENABLE_CODEGEN_x86_64)
Mark Mendell0616ae02015-04-17 12:49:27 -04007864#include "nodes_x86.h"
7865#endif
7866
Vladimir Marko0a516052019-10-14 13:00:44 +00007867namespace art {
Mark Mendell0616ae02015-04-17 12:49:27 -04007868
Igor Murashkin6ef45672017-08-08 13:59:55 -07007869class OptimizingCompilerStats;
7870
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007871class HGraphVisitor : public ValueObject {
7872 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007873 explicit HGraphVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7874 : stats_(stats),
7875 graph_(graph) {}
Dave Allison20dfc792014-06-16 20:44:29 -07007876 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007877
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007878 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007879 virtual void VisitBasicBlock(HBasicBlock* block);
7880
Roland Levillain633021e2014-10-01 14:12:25 +01007881 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007882 void VisitInsertionOrder();
7883
Roland Levillain633021e2014-10-01 14:12:25 +01007884 // Visit the graph following dominator tree reverse post-order.
7885 void VisitReversePostOrder();
7886
Nicolas Geoffray787c3072014-03-17 10:20:19 +00007887 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007888
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007889 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007890#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007891 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
7892
7893 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7894
7895#undef DECLARE_VISIT_INSTRUCTION
7896
Igor Murashkin6ef45672017-08-08 13:59:55 -07007897 protected:
7898 OptimizingCompilerStats* stats_;
7899
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007900 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07007901 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007902
7903 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
7904};
7905
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007906class HGraphDelegateVisitor : public HGraphVisitor {
7907 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007908 explicit HGraphDelegateVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7909 : HGraphVisitor(graph, stats) {}
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007910 virtual ~HGraphDelegateVisitor() {}
7911
7912 // Visit functions that delegate to to super class.
7913#define DECLARE_VISIT_INSTRUCTION(name, super) \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007914 void Visit##name(H##name* instr) override { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007915
7916 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7917
7918#undef DECLARE_VISIT_INSTRUCTION
7919
7920 private:
7921 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
7922};
7923
Artem Serovcced8ba2017-07-19 18:18:09 +01007924// Create a clone of the instruction, insert it into the graph; replace the old one with a new
7925// and remove the old instruction.
7926HInstruction* ReplaceInstrOrPhiByClone(HInstruction* instr);
7927
7928// Create a clone for each clonable instructions/phis and replace the original with the clone.
7929//
7930// Used for testing individual instruction cloner.
7931class CloneAndReplaceInstructionVisitor : public HGraphDelegateVisitor {
7932 public:
7933 explicit CloneAndReplaceInstructionVisitor(HGraph* graph)
Artem Serov7f4aff62017-06-21 17:02:18 +01007934 : HGraphDelegateVisitor(graph), instr_replaced_by_clones_count_(0) {}
Artem Serovcced8ba2017-07-19 18:18:09 +01007935
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007936 void VisitInstruction(HInstruction* instruction) override {
Artem Serovcced8ba2017-07-19 18:18:09 +01007937 if (instruction->IsClonable()) {
7938 ReplaceInstrOrPhiByClone(instruction);
Artem Serov7f4aff62017-06-21 17:02:18 +01007939 instr_replaced_by_clones_count_++;
Artem Serovcced8ba2017-07-19 18:18:09 +01007940 }
7941 }
7942
Artem Serov7f4aff62017-06-21 17:02:18 +01007943 size_t GetInstrReplacedByClonesCount() const { return instr_replaced_by_clones_count_; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007944
7945 private:
Artem Serov7f4aff62017-06-21 17:02:18 +01007946 size_t instr_replaced_by_clones_count_;
Artem Serovcced8ba2017-07-19 18:18:09 +01007947
7948 DISALLOW_COPY_AND_ASSIGN(CloneAndReplaceInstructionVisitor);
7949};
7950
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007951// Iterator over the blocks that art part of the loop. Includes blocks part
7952// of an inner loop. The order in which the blocks are iterated is on their
7953// block id.
7954class HBlocksInLoopIterator : public ValueObject {
7955 public:
7956 explicit HBlocksInLoopIterator(const HLoopInformation& info)
7957 : blocks_in_loop_(info.GetBlocks()),
7958 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
7959 index_(0) {
7960 if (!blocks_in_loop_.IsBitSet(index_)) {
7961 Advance();
7962 }
7963 }
7964
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007965 bool Done() const { return index_ == blocks_.size(); }
7966 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007967 void Advance() {
7968 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007969 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007970 if (blocks_in_loop_.IsBitSet(index_)) {
7971 break;
7972 }
7973 }
7974 }
7975
7976 private:
7977 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007978 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007979 size_t index_;
7980
7981 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
7982};
7983
Mingyao Yang3584bce2015-05-19 16:01:59 -07007984// Iterator over the blocks that art part of the loop. Includes blocks part
7985// of an inner loop. The order in which the blocks are iterated is reverse
7986// post order.
7987class HBlocksInLoopReversePostOrderIterator : public ValueObject {
7988 public:
7989 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
7990 : blocks_in_loop_(info.GetBlocks()),
7991 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
7992 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007993 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007994 Advance();
7995 }
7996 }
7997
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007998 bool Done() const { return index_ == blocks_.size(); }
7999 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07008000 void Advance() {
8001 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008002 for (size_t e = blocks_.size(); index_ < e; ++index_) {
8003 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07008004 break;
8005 }
8006 }
8007 }
8008
8009 private:
8010 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008011 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07008012 size_t index_;
8013
8014 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
8015};
8016
Aart Bikf3e61ee2017-04-12 17:09:20 -07008017// Returns int64_t value of a properly typed constant.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00008018inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00008019 if (constant->IsIntConstant()) {
8020 return constant->AsIntConstant()->GetValue();
8021 } else if (constant->IsLongConstant()) {
8022 return constant->AsLongConstant()->GetValue();
8023 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00008024 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00008025 return 0;
8026 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00008027}
8028
Aart Bikf3e61ee2017-04-12 17:09:20 -07008029// Returns true iff instruction is an integral constant (and sets value on success).
8030inline bool IsInt64AndGet(HInstruction* instruction, /*out*/ int64_t* value) {
8031 if (instruction->IsIntConstant()) {
8032 *value = instruction->AsIntConstant()->GetValue();
8033 return true;
8034 } else if (instruction->IsLongConstant()) {
8035 *value = instruction->AsLongConstant()->GetValue();
8036 return true;
8037 } else if (instruction->IsNullConstant()) {
8038 *value = 0;
8039 return true;
8040 }
8041 return false;
8042}
8043
Aart Bik0148de42017-09-05 09:25:01 -07008044// Returns true iff instruction is the given integral constant.
8045inline bool IsInt64Value(HInstruction* instruction, int64_t value) {
8046 int64_t val = 0;
8047 return IsInt64AndGet(instruction, &val) && val == value;
8048}
8049
8050// Returns true iff instruction is a zero bit pattern.
8051inline bool IsZeroBitPattern(HInstruction* instruction) {
8052 return instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern();
8053}
8054
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01008055// Implement HInstruction::Is##type() for concrete instructions.
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008056#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01008057 inline bool HInstruction::Is##type() const { return GetKind() == k##type; }
8058 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
8059#undef INSTRUCTION_TYPE_CHECK
8060
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01008061// Implement HInstruction::Is##type() for abstract instructions.
Vladimir Markoa90dd512018-05-04 15:04:45 +01008062#define INSTRUCTION_TYPE_CHECK_RESULT(type, super) \
8063 std::is_base_of<BaseType, H##type>::value,
8064#define INSTRUCTION_TYPE_CHECK(type, super) \
8065 inline bool HInstruction::Is##type() const { \
8066 DCHECK_LT(GetKind(), kLastInstructionKind); \
8067 using BaseType = H##type; \
8068 static constexpr bool results[] = { \
8069 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK_RESULT) \
8070 }; \
8071 return results[static_cast<size_t>(GetKind())]; \
8072 }
8073
8074 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
8075#undef INSTRUCTION_TYPE_CHECK
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01008076#undef INSTRUCTION_TYPE_CHECK_RESULT
Vladimir Markoa90dd512018-05-04 15:04:45 +01008077
8078#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008079 inline const H##type* HInstruction::As##type() const { \
8080 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
8081 } \
8082 inline H##type* HInstruction::As##type() { \
8083 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
8084 }
8085
Vladimir Markoa90dd512018-05-04 15:04:45 +01008086 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
8087#undef INSTRUCTION_TYPE_CAST
8088
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008089
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00008090// Create space in `blocks` for adding `number_of_new_blocks` entries
8091// starting at location `at`. Blocks after `at` are moved accordingly.
8092inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
8093 size_t number_of_new_blocks,
8094 size_t after) {
8095 DCHECK_LT(after, blocks->size());
8096 size_t old_size = blocks->size();
8097 size_t new_size = old_size + number_of_new_blocks;
8098 blocks->resize(new_size);
8099 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
8100}
8101
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008102/*
8103 * Hunt "under the hood" of array lengths (leading to array references),
8104 * null checks (also leading to array references), and new arrays
8105 * (leading to the actual length). This makes it more likely related
8106 * instructions become actually comparable.
8107 */
8108inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
8109 while (instruction->IsArrayLength() ||
8110 instruction->IsNullCheck() ||
8111 instruction->IsNewArray()) {
8112 instruction = instruction->IsNewArray()
8113 ? instruction->AsNewArray()->GetLength()
8114 : instruction->InputAt(0);
8115 }
8116 return instruction;
8117}
8118
Artem Serovb47b9782019-12-04 21:02:09 +00008119inline bool IsAddOrSub(const HInstruction* instruction) {
8120 return instruction->IsAdd() || instruction->IsSub();
8121}
8122
Artem Serov21c7e6f2017-07-27 16:04:42 +01008123void RemoveEnvironmentUses(HInstruction* instruction);
8124bool HasEnvironmentUsedByOthers(HInstruction* instruction);
8125void ResetEnvironmentInputRecords(HInstruction* instruction);
8126
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008127} // namespace art
8128
8129#endif // ART_COMPILER_OPTIMIZING_NODES_H_